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

Refactor AbstractCommand::getRawSql() #785

Merged
merged 10 commits into from
Nov 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
`DbArrayHelper::populate()` methods to `array[]` (@vjik)
- Enh #779: Specify populate closure type in `BatchQueryResultInterface` (@vjik)
- Enh #778: Deprecate unnecessary argument `$rawSql` of `AbstractCommand::internalExecute()` (@Tigrov)
- Enh #785: Refactor `AbstractCommand::getRawSql()` (@Tigrov)
- Bug #785: Fix bug of `AbstractCommand::getRawSql()` when a param value is `Stringable` object (@Tigrov)
- Enh #786: Refactor `AbstractSchema::getDataType()` (@Tigrov)
- Enh #784: Specify result type of `ConstraintSchemaInterface::getTableIndexes()` method to `IndexConstraint[]` (@vjik)
- Enh #784: Remove unused code in `AbstractSchema::getTableIndexes()` (@vjik)
Expand Down
58 changes: 27 additions & 31 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
use function explode;
use function get_resource_type;
use function is_array;
use function is_bool;
use function is_int;
use function is_object;
use function is_resource;
use function is_scalar;
use function is_string;
use function preg_replace_callback;
use function str_starts_with;
use function stream_get_contents;
use function strncmp;

/**
* Represents an SQL statement to execute in a database.
Expand Down Expand Up @@ -110,12 +108,12 @@ abstract class AbstractCommand implements CommandInterface
* @var string|null Transaction isolation level.
*/
protected string|null $isolationLevel = null;

/**
* @var array Parameters to use.
*
* @psalm-var ParamInterface[]
* @var ParamInterface[] Parameters to use.
*/
protected array $params = [];

/**
* @var string|null Name of the table to refresh schema for. Null means not to refresh the schema.
*/
Expand Down Expand Up @@ -343,44 +341,42 @@ public function getRawSql(): string
}

$params = [];
$quoter = $this->getQueryBuilder()->quoter();

/** @psalm-var mixed $value */
foreach ($this->params as $name => $value) {
if (is_string($name) && strncmp(':', $name, 1)) {
foreach ($this->params as $name => $param) {
if (is_string($name) && !str_starts_with($name, ':')) {
$name = ':' . $name;
}

if ($value instanceof ParamInterface) {
/** @psalm-var mixed $value */
$value = $value->getValue();
}

if (is_string($value)) {
/** @psalm-var mixed */
$params[$name] = $this->getQueryBuilder()->quoter()->quoteValue($value);
} elseif (is_bool($value)) {
/** @psalm-var string */
$params[$name] = $value ? 'TRUE' : 'FALSE';
} elseif ($value === null) {
$params[$name] = 'NULL';
} elseif ((!is_object($value) && !is_resource($value)) || $value instanceof Expression) {
/** @psalm-var mixed */
$params[$name] = $value;
}
$value = $param->getValue();

$params[$name] = match ($param->getType()) {
DataType::INTEGER => (string)$value,
DataType::STRING, DataType::LOB => match (true) {
$value instanceof Expression => (string)$value,
is_resource($value) => $name,
default => $quoter->quoteValue((string)$value),
},
DataType::BOOLEAN => $value ? 'TRUE' : 'FALSE',
DataType::NULL => 'NULL',
default => $name,
};
}

/** @var string[] $params */
if (!isset($params[0])) {
return preg_replace_callback('#(:\w+)#', static function (array $matches) use ($params): string {
$m = $matches[1];
return (string)($params[$m] ?? $m);
}, $this->sql);
return preg_replace_callback(
'#(:\w+)#',
static fn (array $matches): string => $params[$matches[1]] ?? $matches[1],
$this->sql
);
}

// Support unnamed placeholders should be dropped
$sql = '';

foreach (explode('?', $this->sql) as $i => $part) {
$sql .= $part . (string)($params[$i] ?? '');
$sql .= $part . ($params[$i] ?? '');
}

return $sql;
Expand Down
13 changes: 13 additions & 0 deletions tests/Provider/CommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Tests\Support\DbHelper;
use Yiisoft\Db\Tests\Support\Stringable;
use Yiisoft\Db\Tests\Support\TestTrait;

class CommandProvider
Expand Down Expand Up @@ -637,6 +638,18 @@ public static function rawSql(): array
static::$driverName,
),
],
[
<<<SQL
SELECT * FROM [[customer]] WHERE [[name]] = :name
SQL,
['name' => new Stringable('Alfa')],
DbHelper::replaceQuotes(
<<<SQL
SELECT * FROM [[customer]] WHERE [[name]] = 'Alfa'
SQL,
static::$driverName,
),
],
];
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Support/Stringable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Tests\Support;

class Stringable implements \Stringable
{
public function __construct(private string $value)
{
}

public function __toString(): string
{
return $this->value;
}
}
Loading