Skip to content

Commit 441b889

Browse files
committed
fix: handle other behavior of sql server.
1 parent 78dc4b4 commit 441b889

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

system/Database/SQLSRV/Builder.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,15 @@ private function getFullName(string $table): string
290290
}
291291

292292
if ($this->db->escapeChar === '"') {
293+
if (str_contains($table, '.')) {
294+
$dbInfo = explode('.', $table);
295+
$database = str_replace('"', '', $dbInfo[0]);
296+
$schema = str_replace('"', '', $dbInfo[1]);
297+
$tableName = str_replace('"', '', $dbInfo[2]);
298+
299+
return '"' . $database . '"."' . $schema . '"."' . str_replace('"', '', $tableName) . '"' . $alias;
300+
}
301+
293302
return '"' . $this->db->getDatabase() . '"."' . $this->db->schema . '"."' . str_replace('"', '', $table) . '"' . $alias;
294303
}
295304

tests/system/Database/Builder/FromTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,20 @@ public function testFromSubqueryWithSQLSRV(): void
153153

154154
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
155155
}
156+
157+
/**
158+
* See https://github.com/codeigniter4/CodeIgniter4/issues/8697
159+
*
160+
* @return void
161+
*/
162+
public function testConstructorWithMultipleSegmentTableWithSQLSRV(): void
163+
{
164+
$this->db = new MockConnection(['DBDriver' => 'SQLSRV', 'database' => 'test', 'schema' => 'dbo']);
165+
166+
$builder = new SQLSRVBuilder('database.dbo.table', $this->db);
167+
168+
$expectedSQL = 'SELECT * FROM "database"."dbo"."table"';
169+
170+
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
171+
}
156172
}

0 commit comments

Comments
 (0)