Johnathan Douglas 2023-07-07 19:41:05 -03:00
parent 15d76cbde6
commit 333cd6057f
1 changed files with 10 additions and 46 deletions

View File

@ -9,27 +9,14 @@ class UpdateMassive2
/**
*
* update test as t set
* column_a = c.column_a,
* column_c = c.column_c
* from (values
* ('123', 1, '---'),
* ('345', 2, '+++')
* ) as c(column_b, column_a, column_c)
* where c.column_b = t.column_b;
*
*
* UPDATE transactions
* SET updated_at = CASE id
* WHEN 1 THEN '2019-01-15 10:00:00'::date
* WHEN 2 THEN '2019-01-16 11:00:00'::date
* WHEN 3 THEN '2019-01-17 12:00:00'::date
* END
* WHERE id IN (1, 2, 3);
*
* update transactions set updated_at = '2019-01-15 10:00:00' where id = 1
* update transactions set updated_at = '2019-01-16 10:00:00' where id = 2
* update transactions set updated_at = '2019-01-17 10:00:00' where id = 3
* update transactions as t
* set date = c.date,
* value = c.value
* from (
* values (1, '2023-01-05 00:00:00'::timestamp, 23.44),
* (2, '2023-01-03 00:00:00'::timestamp, 23.44)
* ) as c(id, date, value)
* where t.id = c.id;
*
* @param string $table
* @param string $primaryKeyName
@ -40,7 +27,7 @@ class UpdateMassive2
* [
* "primary_key" => 200576
* "columns" => array:1 [
* "transactions_banks.coupon_code" => "2001462387"
* "coupon_code" => "2001462387"
* ]
* ]
* ]
@ -49,36 +36,13 @@ class UpdateMassive2
*/
public function apply(string $table, string $primaryKeyName, array $config): void
{
/**
* update test as t set
* column_a = c.column_a,
* column_c = c.column_c
* from (values
* ('123', 1, '---'),
* ('345', 2, '+++')
* ) as c(column_b, column_a, column_c)
* where c.column_b = t.column_b;
*
*
* update transactions as t
* set date = c.date,
* value = c.value
* from (
* values (1, '2023-01-05 00:00:00'::timestamp, 23.44),
* (2, '2023-01-03 00:00:00'::timestamp, 23.44)
* ) as c(id, date, value)
* where t.id = c.id;
*
*/
$valuesSqls = [];
$columns = $config[0]['columns'];
foreach ($config as $configItem) {
$valuesSqls[] = vsprintf('(%s, %s)', [$configItem['primary_key'], implode(', ', $configItem['columns'])]);
}
$valuesSql = implode(', ', $valuesSqls);
$setSqls = [];
foreach ($columns as $columnName => $columnValue) {