From 441b88960f15132ab00c0e1e0fb03a42b781e244 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Sun, 14 Apr 2024 23:07:05 +0800 Subject: [PATCH 01/10] fix: handle other behavior of sql server. --- system/Database/SQLSRV/Builder.php | 9 +++++++++ tests/system/Database/Builder/FromTest.php | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/system/Database/SQLSRV/Builder.php b/system/Database/SQLSRV/Builder.php index a624dced38d2..1ba2094ed720 100755 --- a/system/Database/SQLSRV/Builder.php +++ b/system/Database/SQLSRV/Builder.php @@ -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; } diff --git a/tests/system/Database/Builder/FromTest.php b/tests/system/Database/Builder/FromTest.php index 36edaa1dcf0c..6abff01ab241 100644 --- a/tests/system/Database/Builder/FromTest.php +++ b/tests/system/Database/Builder/FromTest.php @@ -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())); + } } From 8ce148f0c04f6061b7e231a91bc83911cf2a9777 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Sun, 14 Apr 2024 23:27:47 +0800 Subject: [PATCH 02/10] docs: change the comment. --- tests/system/Database/Builder/FromTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/system/Database/Builder/FromTest.php b/tests/system/Database/Builder/FromTest.php index 6abff01ab241..384509155542 100644 --- a/tests/system/Database/Builder/FromTest.php +++ b/tests/system/Database/Builder/FromTest.php @@ -155,9 +155,7 @@ public function testFromSubqueryWithSQLSRV(): void } /** - * See https://github.com/codeigniter4/CodeIgniter4/issues/8697 - * - * @return void + * @see https://github.com/codeigniter4/CodeIgniter4/issues/8697 */ public function testConstructorWithMultipleSegmentTableWithSQLSRV(): void { From 349f996cccf4de2fcafa2f2e4202592092846dfe Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Sun, 14 Apr 2024 23:32:27 +0800 Subject: [PATCH 03/10] style: fix the code style. --- system/Database/SQLSRV/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Database/SQLSRV/Builder.php b/system/Database/SQLSRV/Builder.php index 1ba2094ed720..982492610d06 100755 --- a/system/Database/SQLSRV/Builder.php +++ b/system/Database/SQLSRV/Builder.php @@ -298,7 +298,7 @@ private function getFullName(string $table): string return '"' . $database . '"."' . $schema . '"."' . str_replace('"', '', $tableName) . '"' . $alias; } - + return '"' . $this->db->getDatabase() . '"."' . $this->db->schema . '"."' . str_replace('"', '', $table) . '"' . $alias; } From 6a6a0f20020583d11978c0de30b416f62fdfaef9 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Tue, 16 Apr 2024 02:03:49 +0800 Subject: [PATCH 04/10] feat: add the validation. --- system/Database/SQLSRV/Builder.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/system/Database/SQLSRV/Builder.php b/system/Database/SQLSRV/Builder.php index 982492610d06..57a9434d6e98 100755 --- a/system/Database/SQLSRV/Builder.php +++ b/system/Database/SQLSRV/Builder.php @@ -291,10 +291,16 @@ 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]); + $dbInfo = explode('.', $table); + $database = $this->db->getDatabase(); + $schema = $this->db->schema; + $table = $dbInfo[0]; + + if (isset($dbInfo[1], $dbInfo[2])) { + $database = str_replace('"', '', $dbInfo[0]); + $schema = str_replace('"', '', $dbInfo[1]); + $tableName = str_replace('"', '', $dbInfo[2]); + } return '"' . $database . '"."' . $schema . '"."' . str_replace('"', '', $tableName) . '"' . $alias; } From 6a29ef3656b380a30e26777e15acf18ee37106c7 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Wed, 17 Apr 2024 01:28:50 +0800 Subject: [PATCH 05/10] feat: Add more validation. --- system/Database/SQLSRV/Builder.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/system/Database/SQLSRV/Builder.php b/system/Database/SQLSRV/Builder.php index 57a9434d6e98..9f54c68a4e5e 100755 --- a/system/Database/SQLSRV/Builder.php +++ b/system/Database/SQLSRV/Builder.php @@ -290,19 +290,19 @@ private function getFullName(string $table): string } if ($this->db->escapeChar === '"') { - if (str_contains($table, '.')) { + if (str_contains($table, '.') && !str_starts_with($table, '.') && !str_ends_with($table, '.')) { $dbInfo = explode('.', $table); $database = $this->db->getDatabase(); - $schema = $this->db->schema; $table = $dbInfo[0]; if (isset($dbInfo[1], $dbInfo[2])) { $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; From f7d4499d0d9e8fec06daebcb3b68cac8072ac310 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Wed, 17 Apr 2024 01:43:26 +0800 Subject: [PATCH 06/10] style: do cs fix. --- system/Database/SQLSRV/Builder.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/system/Database/SQLSRV/Builder.php b/system/Database/SQLSRV/Builder.php index 9f54c68a4e5e..b8f6d16b8466 100755 --- a/system/Database/SQLSRV/Builder.php +++ b/system/Database/SQLSRV/Builder.php @@ -290,7 +290,7 @@ private function getFullName(string $table): string } if ($this->db->escapeChar === '"') { - if (str_contains($table, '.') && !str_starts_with($table, '.') && !str_ends_with($table, '.')) { + if (str_contains($table, '.') && ! str_starts_with($table, '.') && ! str_ends_with($table, '.')) { $dbInfo = explode('.', $table); $database = $this->db->getDatabase(); $table = $dbInfo[0]; @@ -300,9 +300,8 @@ private function getFullName(string $table): string $schema = str_replace('"', '', $dbInfo[1]); $tableName = str_replace('"', '', $dbInfo[2]); - return '"' . $database . '"."' . $schema . '"."' . str_replace('"', '', $tableName) . '"' . $alias; + return '"' . $database . '"."' . $schema . '"."' . str_replace('"', '', $tableName) . '"' . $alias; } - } return '"' . $this->db->getDatabase() . '"."' . $this->db->schema . '"."' . str_replace('"', '', $table) . '"' . $alias; From 617f70cd354b13b2415f5f656c1cfac6aed0bbfc Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Wed, 17 Apr 2024 13:22:23 +0800 Subject: [PATCH 07/10] fix: decrease the verify amount. --- system/Database/SQLSRV/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Database/SQLSRV/Builder.php b/system/Database/SQLSRV/Builder.php index b8f6d16b8466..ab41c2f2b361 100755 --- a/system/Database/SQLSRV/Builder.php +++ b/system/Database/SQLSRV/Builder.php @@ -295,7 +295,7 @@ private function getFullName(string $table): string $database = $this->db->getDatabase(); $table = $dbInfo[0]; - if (isset($dbInfo[1], $dbInfo[2])) { + if (isset($dbInfo[2])) { $database = str_replace('"', '', $dbInfo[0]); $schema = str_replace('"', '', $dbInfo[1]); $tableName = str_replace('"', '', $dbInfo[2]); From 1434af4a6434b5ec050ffaaadec42494231b8895 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Wed, 17 Apr 2024 15:27:15 +0800 Subject: [PATCH 08/10] feat: extend the code to implement the scenario without database. --- system/Database/SQLSRV/Builder.php | 13 +++++++++---- tests/system/Database/Builder/FromTest.php | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/system/Database/SQLSRV/Builder.php b/system/Database/SQLSRV/Builder.php index ab41c2f2b361..f01e733f9119 100755 --- a/system/Database/SQLSRV/Builder.php +++ b/system/Database/SQLSRV/Builder.php @@ -295,10 +295,15 @@ private function getFullName(string $table): string $database = $this->db->getDatabase(); $table = $dbInfo[0]; - if (isset($dbInfo[2])) { - $database = str_replace('"', '', $dbInfo[0]); - $schema = str_replace('"', '', $dbInfo[1]); - $tableName = str_replace('"', '', $dbInfo[2]); + if (count($dbInfo) >= 2) { + if (count($dbInfo) === 3) { + $database = str_replace('"', '', $dbInfo[0]); + $schema = str_replace('"', '', $dbInfo[1]); + $tableName = str_replace('"', '', $dbInfo[2]); + } else { + $schema = str_replace('"', '', $dbInfo[0]); + $tableName = str_replace('"', '', $dbInfo[1]); + } return '"' . $database . '"."' . $schema . '"."' . str_replace('"', '', $tableName) . '"' . $alias; } diff --git a/tests/system/Database/Builder/FromTest.php b/tests/system/Database/Builder/FromTest.php index 384509155542..e003353c12f8 100644 --- a/tests/system/Database/Builder/FromTest.php +++ b/tests/system/Database/Builder/FromTest.php @@ -167,4 +167,18 @@ public function testConstructorWithMultipleSegmentTableWithSQLSRV(): void $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/8697 + */ + public function testConstructorWithMultipleSegmentTableWithoutDatabaseWithSQLSRV(): void + { + $this->db = new MockConnection(['DBDriver' => 'SQLSRV', 'database' => 'test', 'schema' => 'dbo']); + + $builder = new SQLSRVBuilder('dbo.table', $this->db); + + $expectedSQL = 'SELECT * FROM "test"."dbo"."table"'; + + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); + } } From d7dd6b4cdf0fa72aa4428287ad642847fab853ef Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Wed, 17 Apr 2024 15:29:28 +0800 Subject: [PATCH 09/10] feat: add the validation. --- system/Database/SQLSRV/Builder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/Database/SQLSRV/Builder.php b/system/Database/SQLSRV/Builder.php index f01e733f9119..baa4e0406c7f 100755 --- a/system/Database/SQLSRV/Builder.php +++ b/system/Database/SQLSRV/Builder.php @@ -295,8 +295,8 @@ private function getFullName(string $table): string $database = $this->db->getDatabase(); $table = $dbInfo[0]; - if (count($dbInfo) >= 2) { - if (count($dbInfo) === 3) { + if (count($dbInfo) >= 2 && isset($dbInfo[1])) { + if (count($dbInfo) === 3 && isset($dbInfo[2])) { $database = str_replace('"', '', $dbInfo[0]); $schema = str_replace('"', '', $dbInfo[1]); $tableName = str_replace('"', '', $dbInfo[2]); From 4ac35c07f35307fe64742ed0fd2904296c8565d8 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Thu, 18 Apr 2024 13:57:52 +0800 Subject: [PATCH 10/10] feat: change the condition. --- system/Database/SQLSRV/Builder.php | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/system/Database/SQLSRV/Builder.php b/system/Database/SQLSRV/Builder.php index baa4e0406c7f..c1b61947c90d 100755 --- a/system/Database/SQLSRV/Builder.php +++ b/system/Database/SQLSRV/Builder.php @@ -295,18 +295,16 @@ private function getFullName(string $table): string $database = $this->db->getDatabase(); $table = $dbInfo[0]; - if (count($dbInfo) >= 2 && isset($dbInfo[1])) { - if (count($dbInfo) === 3 && isset($dbInfo[2])) { - $database = str_replace('"', '', $dbInfo[0]); - $schema = str_replace('"', '', $dbInfo[1]); - $tableName = str_replace('"', '', $dbInfo[2]); - } else { - $schema = str_replace('"', '', $dbInfo[0]); - $tableName = str_replace('"', '', $dbInfo[1]); - } - - return '"' . $database . '"."' . $schema . '"."' . str_replace('"', '', $tableName) . '"' . $alias; + if (count($dbInfo) === 3) { + $database = str_replace('"', '', $dbInfo[0]); + $schema = str_replace('"', '', $dbInfo[1]); + $tableName = str_replace('"', '', $dbInfo[2]); + } else { + $schema = str_replace('"', '', $dbInfo[0]); + $tableName = str_replace('"', '', $dbInfo[1]); } + + return '"' . $database . '"."' . $schema . '"."' . str_replace('"', '', $tableName) . '"' . $alias; } return '"' . $this->db->getDatabase() . '"."' . $this->db->schema . '"."' . str_replace('"', '', $table) . '"' . $alias;