From fcf5384188459b480c3304c80f02db9c249ed124 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 25 Jan 2024 09:20:11 +0900 Subject: [PATCH 1/4] test: add test for deleteBatch() with DATE where key --- tests/system/Database/Live/DeleteTest.php | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/system/Database/Live/DeleteTest.php b/tests/system/Database/Live/DeleteTest.php index 4383d32b78a0..7b53c8b0b272 100644 --- a/tests/system/Database/Live/DeleteTest.php +++ b/tests/system/Database/Live/DeleteTest.php @@ -98,6 +98,52 @@ public function testDeleteBatch(): void $this->dontSeeInDatabase('user', ['email' => 'ahmadinejad@world.com', 'name' => 'Ahmadinejad']); } + public function testDeleteBatchConstraintsDate(): void + { + $table = 'type_test'; + + // Prepares test data. + $builder = $this->db->table($table); + $builder->truncate(); + + for ($i = 1; $i < 4; $i++) { + $builder->insert([ + 'type_varchar' => 'test' . $i, + 'type_char' => 'char', + 'type_text' => 'text', + 'type_smallint' => 32767, + 'type_integer' => 2_147_483_647, + 'type_bigint' => 9_223_372_036_854_775_807, + 'type_float' => 10.1, + 'type_numeric' => 123.23, + 'type_date' => '2023-12-0' . $i, + 'type_datetime' => '2023-12-21 12:00:00', + ]); + } + + $data = [ + ['date' => '2023-12-01', 'unused' => 'You can have fields you dont use'], + ['date' => '2023-12-02', 'unused' => 'You can have fields you dont use'], + ]; + $builder = $this->db->table($table) + ->setData($data, null, 'data') + ->onConstraint(['type_date' => 'date']); + $builder->deleteBatch(); + + $this->dontSeeInDatabase( + $table, + ['type_date' => '2023-12-01', 'type_varchar' => 'test1'] + ); + $this->dontSeeInDatabase( + $table, + ['type_date' => '2023-12-02', 'type_varchar' => 'test2'] + ); + $this->seeInDatabase( + $table, + ['type_date' => '2023-12-03', 'type_varchar' => 'test3'] + ); + } + public function testDeleteBatchWithQuery(): void { $this->forge = Database::forge($this->DBGroup); From bec64222b336bc0816857e0d3c655390458dc948 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 25 Jan 2024 09:21:31 +0900 Subject: [PATCH 2/4] fix: deleteBatch() Postgre type error --- system/Database/Postgre/Builder.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/system/Database/Postgre/Builder.php b/system/Database/Postgre/Builder.php index 3d566f6051a0..6da3d377b93b 100644 --- a/system/Database/Postgre/Builder.php +++ b/system/Database/Postgre/Builder.php @@ -562,6 +562,7 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri $sql .= ') ' . $alias . "\n"; + $that = $this; $sql .= 'WHERE ' . implode( ' AND ', array_map( @@ -570,8 +571,8 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri $value : ( is_string($key) ? - $table . '.' . $key . ' = ' . $alias . '.' . $value : - $table . '.' . $value . ' = ' . $alias . '.' . $value + $table . '.' . $key . ' = ' . $that->cast($alias . '.' . $value, $that->getFieldType($table, $key)) : + $table . '.' . $value . ' = ' . $that->cast($alias . '.' . $value, $that->getFieldType($table, $key)) ) ), array_keys($constraints), From b6b8c311af589df901243d63ed34756234dc3599 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 25 Jan 2024 09:27:30 +0900 Subject: [PATCH 3/4] refactor: use if statement --- system/Database/Postgre/Builder.php | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/system/Database/Postgre/Builder.php b/system/Database/Postgre/Builder.php index 6da3d377b93b..61562a7f8d65 100644 --- a/system/Database/Postgre/Builder.php +++ b/system/Database/Postgre/Builder.php @@ -566,15 +566,25 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri $sql .= 'WHERE ' . implode( ' AND ', array_map( - static fn ($key, $value) => ( - $value instanceof RawSql ? - $value : - ( - is_string($key) ? - $table . '.' . $key . ' = ' . $that->cast($alias . '.' . $value, $that->getFieldType($table, $key)) : - $table . '.' . $value . ' = ' . $that->cast($alias . '.' . $value, $that->getFieldType($table, $key)) - ) - ), + static function ($key, $value) use ($table, $alias, $that) { + if ($value instanceof RawSql) { + return $value; + } + + if (is_string($key)) { + return $table . '.' . $key . ' = ' + . $that->cast( + $alias . '.' . $value, + $that->getFieldType($table, $key) + ); + } + + return $table . '.' . $value . ' = ' + . $that->cast( + $alias . '.' . $value, + $that->getFieldType($table, $key) + ); + }, array_keys($constraints), $constraints ) From 4be3bd8dd50151e8634fd39f006cc136cc98c2ec Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 25 Jan 2024 09:46:16 +0900 Subject: [PATCH 4/4] fix: remove unneeded cast() --- system/Database/Postgre/Builder.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/system/Database/Postgre/Builder.php b/system/Database/Postgre/Builder.php index 61562a7f8d65..3749d8383237 100644 --- a/system/Database/Postgre/Builder.php +++ b/system/Database/Postgre/Builder.php @@ -579,11 +579,7 @@ static function ($key, $value) use ($table, $alias, $that) { ); } - return $table . '.' . $value . ' = ' - . $that->cast( - $alias . '.' . $value, - $that->getFieldType($table, $key) - ); + return $table . '.' . $value . ' = ' . $alias . '.' . $value; }, array_keys($constraints), $constraints