Skip to content

Commit

Permalink
Normalize columns and values refactoring (#447)
Browse files Browse the repository at this point in the history
* Normalize columns and values refactoring

* pgsql check

* revert
  • Loading branch information
darkdef committed Jan 3, 2023
1 parent 486b156 commit 684e74c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/QueryBuilder/DMLQueryBuilder.php
Expand Up @@ -104,10 +104,6 @@ public function delete(string $table, array|string $condition, array &$params):

public function insert(string $table, QueryInterface|array $columns, array &$params = []): string
{
if (!$columns instanceof QueryInterface) {
$columns = $this->normalizeColumnNames($table, $columns);
}

/**
* @psalm-var string[] $names
* @psalm-var string[] $placeholders
Expand Down Expand Up @@ -142,8 +138,6 @@ public function resetSequence(string $tableName, int|string|null $value = null):
*/
public function update(string $table, array $columns, array|string $condition, array &$params = []): string
{
$columns = $this->normalizeColumnNames($table, $columns);

/** @psalm-var string[] $lines */
[$lines, $params] = $this->prepareUpdateSets($table, $columns, $params);
$sql = 'UPDATE ' . $this->quoter->quoteTableName($table) . ' SET ' . implode(', ', $lines);
Expand Down Expand Up @@ -212,6 +206,7 @@ protected function prepareInsertValues(string $table, array|QueryInterface $colu
if ($columns instanceof QueryInterface) {
[$names, $values, $params] = $this->prepareInsertSelectSubQuery($columns, $params);
} else {
$columns = $this->normalizeColumnNames($table, $columns);
/**
* @var mixed $value
* @psalm-var array<string, mixed> $columns
Expand Down Expand Up @@ -240,6 +235,8 @@ protected function prepareUpdateSets(string $table, array $columns, array $param

$sets = [];

$columns = $this->normalizeColumnNames($table, $columns);

/**
* @psalm-var array<string, mixed> $columns
* @psalm-var mixed $value
Expand Down Expand Up @@ -272,6 +269,14 @@ protected function prepareUpsertColumns(
): array {
$insertNames = [];

if (!$insertColumns instanceof QueryInterface) {
$insertColumns = $this->normalizeColumnNames($table, $insertColumns);
}

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

if ($insertColumns instanceof QueryInterface) {
/** @psalm-var list<string> $insertNames */
[$insertNames] = $this->prepareInsertSelectSubQuery($insertColumns);
Expand Down
12 changes: 12 additions & 0 deletions tests/Provider/AbstractCommandProvider.php
Expand Up @@ -591,6 +591,18 @@ public function update(): array
$this->getDriverName(),
),
],
[
'{{table}}',
['{{table}}.name' => '{{test}}'],
['id' => 1],
['id' => 'boolean'],
DbHelper::replaceQuotes(
<<<SQL
UPDATE [[table]] SET [[name]]=:qp1 WHERE [[id]]=:qp2
SQL,
$this->getDriverName(),
),
],
[
'{{table}}',
['name' => '{{test}}'],
Expand Down

0 comments on commit 684e74c

Please sign in to comment.