Skip to content

Commit 5fd5f63

Browse files
authored
Merge pull request #8451 from kenjis/fix-postgre-deleteBatch
fix: [Postgre] QueryBuilder::deleteBatch() does not work
2 parents c7cfba4 + 4be3bd8 commit 5fd5f63

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

system/Database/Postgre/Builder.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -562,18 +562,25 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri
562562

563563
$sql .= ') ' . $alias . "\n";
564564

565+
$that = $this;
565566
$sql .= 'WHERE ' . implode(
566567
' AND ',
567568
array_map(
568-
static fn ($key, $value) => (
569-
$value instanceof RawSql ?
570-
$value :
571-
(
572-
is_string($key) ?
573-
$table . '.' . $key . ' = ' . $alias . '.' . $value :
574-
$table . '.' . $value . ' = ' . $alias . '.' . $value
575-
)
576-
),
569+
static function ($key, $value) use ($table, $alias, $that) {
570+
if ($value instanceof RawSql) {
571+
return $value;
572+
}
573+
574+
if (is_string($key)) {
575+
return $table . '.' . $key . ' = '
576+
. $that->cast(
577+
$alias . '.' . $value,
578+
$that->getFieldType($table, $key)
579+
);
580+
}
581+
582+
return $table . '.' . $value . ' = ' . $alias . '.' . $value;
583+
},
577584
array_keys($constraints),
578585
$constraints
579586
)

tests/system/Database/Live/DeleteTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,52 @@ public function testDeleteBatch(): void
9898
$this->dontSeeInDatabase('user', ['email' => 'ahmadinejad@world.com', 'name' => 'Ahmadinejad']);
9999
}
100100

101+
public function testDeleteBatchConstraintsDate(): void
102+
{
103+
$table = 'type_test';
104+
105+
// Prepares test data.
106+
$builder = $this->db->table($table);
107+
$builder->truncate();
108+
109+
for ($i = 1; $i < 4; $i++) {
110+
$builder->insert([
111+
'type_varchar' => 'test' . $i,
112+
'type_char' => 'char',
113+
'type_text' => 'text',
114+
'type_smallint' => 32767,
115+
'type_integer' => 2_147_483_647,
116+
'type_bigint' => 9_223_372_036_854_775_807,
117+
'type_float' => 10.1,
118+
'type_numeric' => 123.23,
119+
'type_date' => '2023-12-0' . $i,
120+
'type_datetime' => '2023-12-21 12:00:00',
121+
]);
122+
}
123+
124+
$data = [
125+
['date' => '2023-12-01', 'unused' => 'You can have fields you dont use'],
126+
['date' => '2023-12-02', 'unused' => 'You can have fields you dont use'],
127+
];
128+
$builder = $this->db->table($table)
129+
->setData($data, null, 'data')
130+
->onConstraint(['type_date' => 'date']);
131+
$builder->deleteBatch();
132+
133+
$this->dontSeeInDatabase(
134+
$table,
135+
['type_date' => '2023-12-01', 'type_varchar' => 'test1']
136+
);
137+
$this->dontSeeInDatabase(
138+
$table,
139+
['type_date' => '2023-12-02', 'type_varchar' => 'test2']
140+
);
141+
$this->seeInDatabase(
142+
$table,
143+
['type_date' => '2023-12-03', 'type_varchar' => 'test3']
144+
);
145+
}
146+
101147
public function testDeleteBatchWithQuery(): void
102148
{
103149
$this->forge = Database::forge($this->DBGroup);

0 commit comments

Comments
 (0)