Skip to content

Commit 1434af4

Browse files
committed
feat: extend the code to implement the scenario without database.
1 parent 617f70c commit 1434af4

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

system/Database/SQLSRV/Builder.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,15 @@ private function getFullName(string $table): string
295295
$database = $this->db->getDatabase();
296296
$table = $dbInfo[0];
297297

298-
if (isset($dbInfo[2])) {
299-
$database = str_replace('"', '', $dbInfo[0]);
300-
$schema = str_replace('"', '', $dbInfo[1]);
301-
$tableName = str_replace('"', '', $dbInfo[2]);
298+
if (count($dbInfo) >= 2) {
299+
if (count($dbInfo) === 3) {
300+
$database = str_replace('"', '', $dbInfo[0]);
301+
$schema = str_replace('"', '', $dbInfo[1]);
302+
$tableName = str_replace('"', '', $dbInfo[2]);
303+
} else {
304+
$schema = str_replace('"', '', $dbInfo[0]);
305+
$tableName = str_replace('"', '', $dbInfo[1]);
306+
}
302307

303308
return '"' . $database . '"."' . $schema . '"."' . str_replace('"', '', $tableName) . '"' . $alias;
304309
}

tests/system/Database/Builder/FromTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,18 @@ public function testConstructorWithMultipleSegmentTableWithSQLSRV(): void
167167

168168
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
169169
}
170+
171+
/**
172+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/8697
173+
*/
174+
public function testConstructorWithMultipleSegmentTableWithoutDatabaseWithSQLSRV(): void
175+
{
176+
$this->db = new MockConnection(['DBDriver' => 'SQLSRV', 'database' => 'test', 'schema' => 'dbo']);
177+
178+
$builder = new SQLSRVBuilder('dbo.table', $this->db);
179+
180+
$expectedSQL = 'SELECT * FROM "test"."dbo"."table"';
181+
182+
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
183+
}
170184
}

0 commit comments

Comments
 (0)