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

Move and deprecate Schema methods #801

Merged
merged 14 commits into from
Feb 10, 2024
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
- Enh #789: Remove unnecessary type casting to array in `AbstractDMLQueryBuilder::getTableUniqueColumnNames()` (@Tigrov)
- Enh #795: Allow to use `DMLQueryBuilderInterface::batchInsert()` method with empty columns (@Tigrov)
- Enh #794: Add message type to log context (@darkdef)
- Enh #801: Deprecate `AbstractSchema::normalizeRowKeyCase()` method (@Tigrov)
- Enh #801: Deprecate `SchemaInterface::getRawTableName()` and add `QuoterInterface::getRawTableName()` method (@Tigrov)
- Enh #801: Deprecate `SchemaInterface::isReadQuery()` and add `DbStringHelper::isReadQuery()` method (@Tigrov)
- Enh #801: Remove unnecessary symbol `\\` from `rtrim()` function inside `DbStringHelper::baseName()` method (@Tigrov)
vjik marked this conversation as resolved.
Show resolved Hide resolved
- Bug #801: Fix bug with `Quoter::$tablePrefix` when change `AbstractConnection::$tablePrefix` property (@Tigrov)

## 1.2.0 November 12, 2023

Expand Down
6 changes: 6 additions & 0 deletions src/Driver/Pdo/AbstractPdoConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@
$this->emulatePrepare = $value;
}

public function setTablePrefix(string $value): void
{
parent::setTablePrefix($value);
$this->quoter?->setTablePrefix($value);

Check failure on line 204 in src/Driver/Pdo/AbstractPdoConnection.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedInterfaceMethod

src/Driver/Pdo/AbstractPdoConnection.php:204:25: UndefinedInterfaceMethod: Method Yiisoft\Db\Schema\QuoterInterface::setTablePrefix does not exist (see https://psalm.dev/181)

Check failure on line 204 in src/Driver/Pdo/AbstractPdoConnection.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

UndefinedInterfaceMethod

src/Driver/Pdo/AbstractPdoConnection.php:204:25: UndefinedInterfaceMethod: Method Yiisoft\Db\Schema\QuoterInterface::setTablePrefix does not exist (see https://psalm.dev/181)

Check failure on line 204 in src/Driver/Pdo/AbstractPdoConnection.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedInterfaceMethod

src/Driver/Pdo/AbstractPdoConnection.php:204:25: UndefinedInterfaceMethod: Method Yiisoft\Db\Schema\QuoterInterface::setTablePrefix does not exist (see https://psalm.dev/181)
}

/**
* Initializes the DB connection.
*
Expand Down
17 changes: 16 additions & 1 deletion src/Helper/DbStringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use function mb_strrpos;
use function mb_strtolower;
use function mb_substr;
use function preg_match;
use function preg_replace;
use function rtrim;
use function str_replace;
Expand Down Expand Up @@ -38,7 +39,7 @@ final class DbStringHelper
*/
public static function baseName(string $path): string
{
$path = rtrim(str_replace('\\', '/', $path), '/\\');
$path = rtrim(str_replace('\\', '/', $path), '/');
Tigrov marked this conversation as resolved.
Show resolved Hide resolved
$position = mb_strrpos($path, '/');

if ($position !== false) {
Expand All @@ -48,6 +49,20 @@ public static function baseName(string $path): string
return $path;
}

/**
* Returns a value indicating whether an SQL statement is for read purpose.
*
* @param string $sql The SQL statement.
*
* @return bool Whether an SQL statement is for read purpose.
*/
public static function isReadQuery(string $sql): bool
{
$pattern = '/^\s*(SELECT|SHOW|DESCRIBE)\b/i';

return preg_match($pattern, $sql) === 1;
}

/**
* Returns string representation of a number value without a thousand separators and with dot as decimal separator.
*
Expand Down
11 changes: 8 additions & 3 deletions src/Schema/AbstractSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public function getDataType(mixed $data): int
};
}

/** @deprecated Use {@see QuoterInterface::getRawTableName()}. Will be removed in version 2.0.0. */
public function getRawTableName(string $name): string
{
if (str_contains($name, '{{')) {
Expand Down Expand Up @@ -315,6 +316,7 @@ public function getTableUniques(string $name, bool $refresh = false): array
return is_array($tableUniques) ? $tableUniques : [];
}

/** @deprecated Use {@see DbStringHelper::isReadQuery()}. Will be removed in version 2.0.0. */
public function isReadQuery(string $sql): bool
{
$pattern = '/^\s*(SELECT|SHOW|DESCRIBE)\b/i';
Expand All @@ -340,7 +342,7 @@ public function refresh(): void
*/
public function refreshTableSchema(string $name): void
{
$rawName = $this->getRawTableName($name);
$rawName = $this->db->getQuoter()->getRawTableName($name);

unset($this->tableMetadata[$rawName]);

Expand Down Expand Up @@ -470,7 +472,7 @@ protected function getSchemaMetadata(string $schema, string $type, bool $refresh
*/
protected function getTableMetadata(string $name, string $type, bool $refresh = false): mixed
{
$rawName = $this->getRawTableName($name);
$rawName = $this->db->getQuoter()->getRawTableName($name);

if (!isset($this->tableMetadata[$rawName])) {
$this->loadTableMetadataFromCache($rawName);
Expand Down Expand Up @@ -532,6 +534,9 @@ protected function getTableTypeMetadata(
* @param bool $multiple Whether many rows or a single row passed.
*
* @return array The normalized row or rows.
*
* @deprecated Use `array_change_key_case($row)` or `array_map('array_change_key_case', $row)`.
* Will be removed in version 2.0.0.
*/
protected function normalizeRowKeyCase(array $row, bool $multiple): array
{
Expand Down Expand Up @@ -568,7 +573,7 @@ protected function resolveTableName(string $name): TableSchemaInterface
protected function setTableMetadata(string $name, string $type, mixed $data): void
{
/** @psalm-suppress MixedArrayAssignment */
$this->tableMetadata[$this->getRawTableName($name)][$type] = $data;
$this->tableMetadata[$this->db->getQuoter()->getRawTableName($name)][$type] = $data;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/Schema/Quoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ public function cleanUpTableNames(array $tableNames): array
return $cleanedUpTableNames;
}

public function getRawTableName(string $name): string
{
if (str_contains($name, '{{')) {
$name = preg_replace('/{{(.*?)}}/', '\1', $name);

return str_replace('%', $this->tablePrefix, $name);
}

return $name;
}

public function getTableNameParts(string $name, bool $withColumn = false): array
{
$parts = array_slice(explode('.', $name), -2, 2);
Expand Down Expand Up @@ -204,6 +215,11 @@ public function quoteValue(mixed $value): mixed
return "'" . str_replace("'", "''", addcslashes($value, "\000\032")) . "'";
}

public function setTablePrefix(string $value): void
darkdef marked this conversation as resolved.
Show resolved Hide resolved
{
$this->tablePrefix = $value;
}

public function unquoteSimpleColumnName(string $name): string
{
if (is_string($this->columnQuoteCharacter)) {
Expand Down
12 changes: 12 additions & 0 deletions src/Schema/QuoterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ interface QuoterInterface
*/
public function cleanUpTableNames(array $tableNames): array;

/**
* Returns the actual name of a given table name.
*
* This method will strip off curly brackets from the given table name and replace the percentage character '%' with
* {@see ConnectionInterface::tablePrefix}.
*
* @param string $name The table name to convert.
*
* @return string The real name of the given table name.
*/
public function getRawTableName(string $name): string;

/**
* Splits full table name into parts.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Schema/SchemaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ public function getDataType(mixed $data): int;
* @param string $name The table name to convert.
*
* @return string The real name of the given table name.
*
* @deprecated Use {@see QuoterInterface::getRawTableName()}. Will be removed in version 2.0.0.
*/
public function getRawTableName(string $name): string;

Expand Down Expand Up @@ -367,6 +369,8 @@ public function getTableSchemas(string $schema = '', bool $refresh = false): arr
* @param string $sql The SQL statement.
*
* @return bool Whether an SQL statement is for read purpose.
*
* @deprecated Use {@see DbStringHelper::isReadQuery()}. Will be removed in version 2.0.0.
Tigrov marked this conversation as resolved.
Show resolved Hide resolved
*/
public function isReadQuery(string $sql): bool;

Expand Down
12 changes: 12 additions & 0 deletions tests/AbstractQuoterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ public function testEnsureNameQuoted(string $name, string $expected): void
$this->assertSame($expected, $db->getQuoter()->ensureNameQuoted($name));
}

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\QuoterProvider::rawTableNames
*/
public function testGetRawTableName(string $tableName, string $expected, string $tablePrefix = ''): void
{
$db = $this->getConnection();

$db->setTablePrefix($tablePrefix);

$this->assertSame($expected, $db->getQuoter()->getRawTableName($tableName));
}

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\QuoterProvider::tableNameParts
*/
Expand Down
13 changes: 5 additions & 8 deletions tests/AbstractSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Command\DataType;
use Yiisoft\Db\Helper\DbStringHelper;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Tests\Support\Assert;
Expand Down Expand Up @@ -76,14 +77,10 @@ public function testGetDataType(): void

public function testIsReadQuery(): void
{
$db = $this->getConnection();

$schema = $db->getSchema();

$this->assertTrue($schema->isReadQuery('SELECT * FROM tbl'));
$this->assertTrue($schema->isReadQuery('SELECT * FROM tbl WHERE id=1'));
$this->assertTrue($schema->isReadQuery('SELECT * FROM tbl WHERE id=1 LIMIT 1'));
$this->assertTrue($schema->isReadQuery('SELECT * FROM tbl WHERE id=1 LIMIT 1 OFFSET 1'));
$this->assertTrue(DbStringHelper::isReadQuery('SELECT * FROM tbl'));
$this->assertTrue(DbStringHelper::isReadQuery('SELECT * FROM tbl WHERE id=1'));
$this->assertTrue(DbStringHelper::isReadQuery('SELECT * FROM tbl WHERE id=1 LIMIT 1'));
$this->assertTrue(DbStringHelper::isReadQuery('SELECT * FROM tbl WHERE id=1 LIMIT 1 OFFSET 1'));
}

public function testRefresh(): void
Expand Down
16 changes: 16 additions & 0 deletions tests/Provider/QuoterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ public static function simpleTableNames(): array
];
}

/**
* @return string[][]
*/
public static function rawTableNames(): array
{
return [
['table', 'table'],
['"table"', '"table"'],
['public.table', 'public.table'],
['{{table}}', 'table'],
['{{public}}.{{table}}', 'public.table'],
['{{%table}}', 'yii_table', 'yii_'],
['{{public}}.{{%table}}', 'public.yii_table', 'yii_'],
];
}

/**
* @return string[][]
*/
Expand Down
Loading