Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [Postgre] QueryBuilder::deleteBatch() does not work #8451

Merged
merged 4 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 +98,53 @@
$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

Check warning on line 147 in tests/system/Database/Live/DeleteTest.php

View workflow job for this annotation

GitHub Actions / DatabaseLive (8.1, OCI8, 8.0) / tests

Took 1.09s from 0.50s limit to run CodeIgniter\\Database\\Live\\DeleteTest::testDeleteBatchWithQuery
{
$this->forge = Database::forge($this->DBGroup);

Expand Down
Loading