Skip to content

Commit

Permalink
Normalize column names (#198)
Browse files Browse the repository at this point in the history
* Fix of normalize for column names in insert, batchInsert, upsert, update

* remove comments
  • Loading branch information
darkdef committed Dec 30, 2022
1 parent 78d6b04 commit 0eb9be0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 33 deletions.
25 changes: 6 additions & 19 deletions src/DMLQueryBuilder.php
Expand Up @@ -283,35 +283,22 @@ private function normalizeTableRowData(string $table, QueryInterface|array $colu
return $columns;
}

$normalizeColumns = [];
$rawTableName = $this->schema->getRawTableName($table);
$columnSchemas = $tableSchema->getColumns();
/**
* @var string $name
* @var mixed $value
*/
foreach ($columns as $name => $value) {
$parts = $this->quoter->getTableNameParts($name, true);
$columns = $this->normalizeColumnNames($table, $columns);

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

$name = $parts[count($parts) - 1];
/** @psalm-var mixed $value */
foreach ($columns as $name => $value) {
if (
isset($columnSchemas[$name]) &&
$columnSchemas[$name]->getType() === Schema::TYPE_BINARY &&
is_string($value)
) {
/** explicitly setup PDO param type for binary column */
$normalizeColumns[$name] = new Param($value, PDO::PARAM_LOB);
} else {
/** @psalm-suppress MixedAssignment */
$normalizeColumns[$name] = $value;
$columns[$name] = new Param($value, PDO::PARAM_LOB);
}
}

/** @psalm-var mixed[] $normalizeColumns */
return $normalizeColumns;
return $columns;
}
}
3 changes: 0 additions & 3 deletions tests/Provider/CommandProvider.php
Expand Up @@ -17,9 +17,6 @@ public function batchInsert(): array
{
$batchInsert = parent::batchInsert();

// @todo need fix with using normalizer as in upsert
unset($batchInsert['wrongBehavior']);

$batchInsert['batchInsert binds params from jsonExpression'] = [
'{{%type}}',
['json_col', 'int_col', 'float_col', 'char_col', 'bool_col'],
Expand Down
18 changes: 7 additions & 11 deletions tests/Provider/QueryBuilderProvider.php
Expand Up @@ -262,10 +262,6 @@ public function insert(): array
INSERT INTO "customer" DEFAULT VALUES
SQL;

$insert['params-and-expressions'][3] = <<<SQL
INSERT INTO {{%type}} ([[related_id]], [[time]]) VALUES (:qp0, now())
SQL;

return $insert;
}

Expand Down Expand Up @@ -300,7 +296,7 @@ public function insertEx(): array
['{{%type}}.[[related_id]]' => null, '[[time]]' => new Expression('now()')],
[],
<<<SQL
INSERT INTO {{%type}} ([[related_id]], [[time]]) VALUES (:qp0, now())
INSERT INTO {{%type}} ("related_id", "time") VALUES (:qp0, now())
SQL,
[':qp0' => null],
],
Expand Down Expand Up @@ -394,18 +390,18 @@ public function upsert(): array
],
'values and expressions' => [
1 => ['{{%T_upsert}}.[[email]]' => 'dynamic@example.com', '[[ts]]' => new Expression('extract(epoch from now()) * 1000')],
3 => 'INSERT INTO {{%T_upsert}} ([[email]], [[ts]]) VALUES (:qp0, extract(epoch from now()) * 1000) ' .
'ON CONFLICT ("email") DO UPDATE SET [[ts]]=EXCLUDED.[[ts]]',
3 => 'INSERT INTO {{%T_upsert}} ("email", "ts") VALUES (:qp0, extract(epoch from now()) * 1000) ' .
'ON CONFLICT ("email") DO UPDATE SET "ts"=EXCLUDED."ts"',
],
'values and expressions with update part' => [
1 => ['{{%T_upsert}}.[[email]]' => 'dynamic@example.com', '[[ts]]' => new Expression('extract(epoch from now()) * 1000')],
2 => ['[[orders]]' => new Expression('EXCLUDED.orders + 1')],
3 => 'INSERT INTO {{%T_upsert}} ([[email]], [[ts]]) VALUES (:qp0, extract(epoch from now()) * 1000) ' .
'ON CONFLICT ("email") DO UPDATE SET [[orders]]=EXCLUDED.orders + 1',
3 => 'INSERT INTO {{%T_upsert}} ("email", "ts") VALUES (:qp0, extract(epoch from now()) * 1000) ' .
'ON CONFLICT ("email") DO UPDATE SET "orders"=EXCLUDED.orders + 1',
],
'values and expressions without update part' => [
1 => ['{{%T_upsert}}.[[email]]' => 'dynamic@example.com', '[[ts]]' => new Expression('extract(epoch from now()) * 1000')],
3 => 'INSERT INTO {{%T_upsert}} ([[email]], [[ts]]) VALUES (:qp0, extract(epoch from now()) * 1000) ON CONFLICT DO NOTHING',
3 => 'INSERT INTO {{%T_upsert}} ("email", "ts") VALUES (:qp0, extract(epoch from now()) * 1000) ON CONFLICT DO NOTHING',
],
'query, values and expressions with update part' => [
1 => (new Query($db))
Expand All @@ -417,7 +413,7 @@ public function upsert(): array
),
2 => ['ts' => 0, '[[orders]]' => new Expression('EXCLUDED.orders + 1')],
3 => 'INSERT INTO {{%T_upsert}} ("email", [[ts]]) SELECT :phEmail AS "email", extract(epoch from now()) * 1000 AS [[ts]] ' .
'ON CONFLICT ("email") DO UPDATE SET "ts"=:qp1, [[orders]]=EXCLUDED.orders + 1',
'ON CONFLICT ("email") DO UPDATE SET "ts"=:qp1, "orders"=EXCLUDED.orders + 1',
],
'query, values and expressions without update part' => [
1 => (new Query($db))
Expand Down

0 comments on commit 0eb9be0

Please sign in to comment.