Skip to content

Commit

Permalink
Merge pull request #8451 from kenjis/fix-postgre-deleteBatch
Browse files Browse the repository at this point in the history
fix: [Postgre] QueryBuilder::deleteBatch() does not work
  • Loading branch information
kenjis authored Jan 25, 2024
2 parents c7cfba4 + 4be3bd8 commit 5fd5f63
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
25 changes: 16 additions & 9 deletions system/Database/Postgre/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,18 +562,25 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri

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

$that = $this;
$sql .= 'WHERE ' . implode(
' AND ',
array_map(
static fn ($key, $value) => (
$value instanceof RawSql ?
$value :
(
is_string($key) ?
$table . '.' . $key . ' = ' . $alias . '.' . $value :
$table . '.' . $value . ' = ' . $alias . '.' . $value
)
),
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 . ' = ' . $alias . '.' . $value;
},
array_keys($constraints),
$constraints
)
Expand Down
46 changes: 46 additions & 0 deletions tests/system/Database/Live/DeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,52 @@ public function testDeleteBatch(): void
$this->dontSeeInDatabase('user', ['email' => '[email protected]', '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);
Expand Down

0 comments on commit 5fd5f63

Please sign in to comment.