Skip to content

Commit

Permalink
Fix of normalize for column names in insert, batchInsert, upsert, upd…
Browse files Browse the repository at this point in the history
…ate (#194)
  • Loading branch information
darkdef committed Dec 30, 2022
1 parent e8acbea commit 849330d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/DMLQueryBuilder.php
Expand Up @@ -112,6 +112,14 @@ public function upsert(
bool|array $updateColumns,
array &$params
): string {
if (!$insertColumns instanceof QueryInterface) {
$insertColumns = $this->normalizeColumnNames($table, $insertColumns);
}

if (!is_bool($updateColumns)) {
$updateColumns = $this->normalizeColumnNames($table, $updateColumns);
}

$insertSql = $this->insert($table, $insertColumns, $params);

/** @var array $uniqueNames */
Expand Down
12 changes: 6 additions & 6 deletions tests/Provider/QueryBuilderProvider.php
Expand Up @@ -131,19 +131,19 @@ public function upsert(): array
'WHERE `name`=:qp0 LIMIT 1',
],
'values and expressions' => [
3 => 'INSERT INTO {{%T_upsert}} ({{%T_upsert}}.[[email]], [[ts]]) VALUES (:qp0, CURRENT_TIMESTAMP) ' .
'ON DUPLICATE KEY UPDATE [[ts]]=VALUES([[ts]])',
3 => 'INSERT INTO {{%T_upsert}} (`email`, `ts`) VALUES (:qp0, CURRENT_TIMESTAMP) ' .
'ON DUPLICATE KEY UPDATE `ts`=VALUES(`ts`)',
],
'values and expressions with update part' => [
3 => 'INSERT INTO {{%T_upsert}} ({{%T_upsert}}.[[email]], [[ts]]) VALUES (:qp0, CURRENT_TIMESTAMP) ' .
'ON DUPLICATE KEY UPDATE [[orders]]=T_upsert.orders + 1',
3 => 'INSERT INTO {{%T_upsert}} (`email`, `ts`) VALUES (:qp0, CURRENT_TIMESTAMP) ' .
'ON DUPLICATE KEY UPDATE `orders`=T_upsert.orders + 1',
],
'values and expressions without update part' => [
3 => 'INSERT IGNORE INTO {{%T_upsert}} ({{%T_upsert}}.[[email]], [[ts]]) VALUES (:qp0, CURRENT_TIMESTAMP)',
3 => 'INSERT IGNORE INTO {{%T_upsert}} (`email`, `ts`) VALUES (:qp0, CURRENT_TIMESTAMP)',
],
'query, values and expressions with update part' => [
3 => 'INSERT INTO {{%T_upsert}} (`email`, [[ts]]) SELECT :phEmail AS `email`, CURRENT_TIMESTAMP AS [[ts]] ' .
'ON DUPLICATE KEY UPDATE `ts`=:qp1, [[orders]]=T_upsert.orders + 1',
'ON DUPLICATE KEY UPDATE `ts`=:qp1, `orders`=T_upsert.orders + 1',
],
'query, values and expressions without update part' => [
3 => 'INSERT IGNORE INTO {{%T_upsert}} (`email`, [[ts]]) SELECT :phEmail AS `email`, CURRENT_TIMESTAMP AS [[ts]]',
Expand Down

0 comments on commit 849330d

Please sign in to comment.