Skip to content

Commit

Permalink
fix: handle other behavior of sql server.
Browse files Browse the repository at this point in the history
  • Loading branch information
ping-yee committed Apr 14, 2024
1 parent 78dc4b4 commit 441b889
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions system/Database/SQLSRV/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,15 @@ private function getFullName(string $table): string
}

if ($this->db->escapeChar === '"') {
if (str_contains($table, '.')) {
$dbInfo = explode('.', $table);
$database = str_replace('"', '', $dbInfo[0]);
$schema = str_replace('"', '', $dbInfo[1]);
$tableName = str_replace('"', '', $dbInfo[2]);

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

return '"' . $this->db->getDatabase() . '"."' . $this->db->schema . '"."' . str_replace('"', '', $table) . '"' . $alias;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/system/Database/Builder/FromTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,20 @@ public function testFromSubqueryWithSQLSRV(): void

$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
}

/**
* See https://github.com/codeigniter4/CodeIgniter4/issues/8697
*
* @return void
*/
public function testConstructorWithMultipleSegmentTableWithSQLSRV(): void
{
$this->db = new MockConnection(['DBDriver' => 'SQLSRV', 'database' => 'test', 'schema' => 'dbo']);

$builder = new SQLSRVBuilder('database.dbo.table', $this->db);

$expectedSQL = 'SELECT * FROM "database"."dbo"."table"';

$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
}
}

0 comments on commit 441b889

Please sign in to comment.