Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize columns and values refactoring #447

Merged
merged 3 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/QueryBuilder/DMLQueryBuilder.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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