Skip to content

Commit 0eb9be0

Browse files
authored
Normalize column names (#198)
* Fix of normalize for column names in insert, batchInsert, upsert, update * remove comments
1 parent 78d6b04 commit 0eb9be0

3 files changed

Lines changed: 13 additions & 33 deletions

File tree

src/DMLQueryBuilder.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -283,35 +283,22 @@ private function normalizeTableRowData(string $table, QueryInterface|array $colu
283283
return $columns;
284284
}
285285

286-
$normalizeColumns = [];
287-
$rawTableName = $this->schema->getRawTableName($table);
288-
$columnSchemas = $tableSchema->getColumns();
289-
/**
290-
* @var string $name
291-
* @var mixed $value
292-
*/
293-
foreach ($columns as $name => $value) {
294-
$parts = $this->quoter->getTableNameParts($name, true);
286+
$columns = $this->normalizeColumnNames($table, $columns);
295287

296-
if (count($parts) === 2 && $this->schema->getRawTableName($parts[0]) !== $rawTableName) {
297-
continue;
298-
}
288+
$columnSchemas = $tableSchema->getColumns();
299289

300-
$name = $parts[count($parts) - 1];
290+
/** @psalm-var mixed $value */
291+
foreach ($columns as $name => $value) {
301292
if (
302293
isset($columnSchemas[$name]) &&
303294
$columnSchemas[$name]->getType() === Schema::TYPE_BINARY &&
304295
is_string($value)
305296
) {
306297
/** explicitly setup PDO param type for binary column */
307-
$normalizeColumns[$name] = new Param($value, PDO::PARAM_LOB);
308-
} else {
309-
/** @psalm-suppress MixedAssignment */
310-
$normalizeColumns[$name] = $value;
298+
$columns[$name] = new Param($value, PDO::PARAM_LOB);
311299
}
312300
}
313301

314-
/** @psalm-var mixed[] $normalizeColumns */
315-
return $normalizeColumns;
302+
return $columns;
316303
}
317304
}

tests/Provider/CommandProvider.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ public function batchInsert(): array
1717
{
1818
$batchInsert = parent::batchInsert();
1919

20-
// @todo need fix with using normalizer as in upsert
21-
unset($batchInsert['wrongBehavior']);
22-
2320
$batchInsert['batchInsert binds params from jsonExpression'] = [
2421
'{{%type}}',
2522
['json_col', 'int_col', 'float_col', 'char_col', 'bool_col'],

tests/Provider/QueryBuilderProvider.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,6 @@ public function insert(): array
262262
INSERT INTO "customer" DEFAULT VALUES
263263
SQL;
264264

265-
$insert['params-and-expressions'][3] = <<<SQL
266-
INSERT INTO {{%type}} ([[related_id]], [[time]]) VALUES (:qp0, now())
267-
SQL;
268-
269265
return $insert;
270266
}
271267

@@ -300,7 +296,7 @@ public function insertEx(): array
300296
['{{%type}}.[[related_id]]' => null, '[[time]]' => new Expression('now()')],
301297
[],
302298
<<<SQL
303-
INSERT INTO {{%type}} ([[related_id]], [[time]]) VALUES (:qp0, now())
299+
INSERT INTO {{%type}} ("related_id", "time") VALUES (:qp0, now())
304300
SQL,
305301
[':qp0' => null],
306302
],
@@ -394,18 +390,18 @@ public function upsert(): array
394390
],
395391
'values and expressions' => [
396392
1 => ['{{%T_upsert}}.[[email]]' => 'dynamic@example.com', '[[ts]]' => new Expression('extract(epoch from now()) * 1000')],
397-
3 => 'INSERT INTO {{%T_upsert}} ([[email]], [[ts]]) VALUES (:qp0, extract(epoch from now()) * 1000) ' .
398-
'ON CONFLICT ("email") DO UPDATE SET [[ts]]=EXCLUDED.[[ts]]',
393+
3 => 'INSERT INTO {{%T_upsert}} ("email", "ts") VALUES (:qp0, extract(epoch from now()) * 1000) ' .
394+
'ON CONFLICT ("email") DO UPDATE SET "ts"=EXCLUDED."ts"',
399395
],
400396
'values and expressions with update part' => [
401397
1 => ['{{%T_upsert}}.[[email]]' => 'dynamic@example.com', '[[ts]]' => new Expression('extract(epoch from now()) * 1000')],
402398
2 => ['[[orders]]' => new Expression('EXCLUDED.orders + 1')],
403-
3 => 'INSERT INTO {{%T_upsert}} ([[email]], [[ts]]) VALUES (:qp0, extract(epoch from now()) * 1000) ' .
404-
'ON CONFLICT ("email") DO UPDATE SET [[orders]]=EXCLUDED.orders + 1',
399+
3 => 'INSERT INTO {{%T_upsert}} ("email", "ts") VALUES (:qp0, extract(epoch from now()) * 1000) ' .
400+
'ON CONFLICT ("email") DO UPDATE SET "orders"=EXCLUDED.orders + 1',
405401
],
406402
'values and expressions without update part' => [
407403
1 => ['{{%T_upsert}}.[[email]]' => 'dynamic@example.com', '[[ts]]' => new Expression('extract(epoch from now()) * 1000')],
408-
3 => 'INSERT INTO {{%T_upsert}} ([[email]], [[ts]]) VALUES (:qp0, extract(epoch from now()) * 1000) ON CONFLICT DO NOTHING',
404+
3 => 'INSERT INTO {{%T_upsert}} ("email", "ts") VALUES (:qp0, extract(epoch from now()) * 1000) ON CONFLICT DO NOTHING',
409405
],
410406
'query, values and expressions with update part' => [
411407
1 => (new Query($db))
@@ -417,7 +413,7 @@ public function upsert(): array
417413
),
418414
2 => ['ts' => 0, '[[orders]]' => new Expression('EXCLUDED.orders + 1')],
419415
3 => 'INSERT INTO {{%T_upsert}} ("email", [[ts]]) SELECT :phEmail AS "email", extract(epoch from now()) * 1000 AS [[ts]] ' .
420-
'ON CONFLICT ("email") DO UPDATE SET "ts"=:qp1, [[orders]]=EXCLUDED.orders + 1',
416+
'ON CONFLICT ("email") DO UPDATE SET "ts"=:qp1, "orders"=EXCLUDED.orders + 1',
421417
],
422418
'query, values and expressions without update part' => [
423419
1 => (new Query($db))

0 commit comments

Comments
 (0)