From 31eecc273d04aa813e6baffdeb326b6474a86453 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Fri, 26 Jan 2024 00:40:14 +0700 Subject: [PATCH 01/13] Move and deprecate `Schema` methods --- src/Helper/DbStringHelper.php | 17 ++++++++++++++++- src/Schema/AbstractSchema.php | 3 +++ src/Schema/Quoter.php | 11 +++++++++++ src/Schema/QuoterInterface.php | 12 ++++++++++++ src/Schema/SchemaInterface.php | 4 ++++ tests/AbstractSchemaTest.php | 13 +++++-------- 6 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/Helper/DbStringHelper.php b/src/Helper/DbStringHelper.php index 2fee97efc..58da98279 100644 --- a/src/Helper/DbStringHelper.php +++ b/src/Helper/DbStringHelper.php @@ -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; @@ -38,7 +39,7 @@ final class DbStringHelper */ public static function baseName(string $path): string { - $path = rtrim(str_replace('\\', '/', $path), '/\\'); + $path = rtrim(str_replace('\\', '/', $path), '/'); $position = mb_strrpos($path, '/'); if ($position !== false) { @@ -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. * diff --git a/src/Schema/AbstractSchema.php b/src/Schema/AbstractSchema.php index f9f39e400..9b396c286 100644 --- a/src/Schema/AbstractSchema.php +++ b/src/Schema/AbstractSchema.php @@ -532,6 +532,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 { diff --git a/src/Schema/Quoter.php b/src/Schema/Quoter.php index 1f8aef970..d0e56ee50 100644 --- a/src/Schema/Quoter.php +++ b/src/Schema/Quoter.php @@ -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); diff --git a/src/Schema/QuoterInterface.php b/src/Schema/QuoterInterface.php index f51e446cc..9435c434c 100644 --- a/src/Schema/QuoterInterface.php +++ b/src/Schema/QuoterInterface.php @@ -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. * diff --git a/src/Schema/SchemaInterface.php b/src/Schema/SchemaInterface.php index 5bfa909a0..744ebc555 100644 --- a/src/Schema/SchemaInterface.php +++ b/src/Schema/SchemaInterface.php @@ -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 Quoter::getRawTableName()}. Will be removed in version 2.0.0. */ public function getRawTableName(string $name): string; @@ -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. */ public function isReadQuery(string $sql): bool; diff --git a/tests/AbstractSchemaTest.php b/tests/AbstractSchemaTest.php index e868a1a60..2f0520644 100644 --- a/tests/AbstractSchemaTest.php +++ b/tests/AbstractSchemaTest.php @@ -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; @@ -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 From 1a3d48e87d942338ffda9c1e366a2b9dca7fd117 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Fri, 26 Jan 2024 01:22:51 +0700 Subject: [PATCH 02/13] Add tests --- tests/AbstractQuoterTest.php | 12 ++++++++++++ tests/Provider/QuoterProvider.php | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/tests/AbstractQuoterTest.php b/tests/AbstractQuoterTest.php index a5bd9c921..e98a3ab07 100644 --- a/tests/AbstractQuoterTest.php +++ b/tests/AbstractQuoterTest.php @@ -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 */ diff --git a/tests/Provider/QuoterProvider.php b/tests/Provider/QuoterProvider.php index 74808f2fa..db6181d74 100644 --- a/tests/Provider/QuoterProvider.php +++ b/tests/Provider/QuoterProvider.php @@ -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[][] */ From 6c06f6be76efa6b60806f9aa649c57e9bcd2ed1d Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Fri, 26 Jan 2024 14:53:39 +0700 Subject: [PATCH 03/13] Add lines to CHANGELOG.md [skip ci] --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22389beb9..3281310b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ - 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) ## 1.2.0 November 12, 2023 From 7e3799137ebbab822a41d5126d05f38f925e259e Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Fri, 26 Jan 2024 19:59:07 +0700 Subject: [PATCH 04/13] Apply review suggestions --- CHANGELOG.md | 1 + src/Schema/AbstractSchema.php | 8 +++++--- src/Schema/SchemaInterface.php | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3281310b3..d5b56f413 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - 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) ## 1.2.0 November 12, 2023 diff --git a/src/Schema/AbstractSchema.php b/src/Schema/AbstractSchema.php index 9b396c286..0e0ba1de2 100644 --- a/src/Schema/AbstractSchema.php +++ b/src/Schema/AbstractSchema.php @@ -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, '{{')) { @@ -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'; @@ -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]); @@ -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); @@ -571,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; } /** diff --git a/src/Schema/SchemaInterface.php b/src/Schema/SchemaInterface.php index 744ebc555..9582da1c1 100644 --- a/src/Schema/SchemaInterface.php +++ b/src/Schema/SchemaInterface.php @@ -285,7 +285,7 @@ public function getDataType(mixed $data): int; * * @return string The real name of the given table name. * - * @deprecated Use {@see Quoter::getRawTableName()}. Will be removed in version 2.0.0. + * @deprecated Use {@see QuoterInterface::getRawTableName()}. Will be removed in version 2.0.0. */ public function getRawTableName(string $name): string; From 277c97ed577765033f449741c9dc91bc979df685 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Fri, 26 Jan 2024 21:49:51 +0700 Subject: [PATCH 05/13] Fix bug with `Quoter` --- CHANGELOG.md | 1 + src/Connection/AbstractConnection.php | 3 +++ src/Driver/Pdo/AbstractPdoConnection.php | 2 -- src/Schema/Quoter.php | 5 +++++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5b56f413..c0926baea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ - 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) +- Bug #801: Fix bug with `Quoter::$tablePrefix` when change `AbstractConnection::$tablePrefix` property (@Tigrov) ## 1.2.0 November 12, 2023 diff --git a/src/Connection/AbstractConnection.php b/src/Connection/AbstractConnection.php index 0f53bee6e..4e9425fe1 100644 --- a/src/Connection/AbstractConnection.php +++ b/src/Connection/AbstractConnection.php @@ -9,6 +9,7 @@ use Yiisoft\Db\Query\BatchQueryResult; use Yiisoft\Db\Query\BatchQueryResultInterface; use Yiisoft\Db\Query\QueryInterface; +use Yiisoft\Db\Schema\QuoterInterface; use Yiisoft\Db\Schema\TableSchemaInterface; use Yiisoft\Db\Transaction\TransactionInterface; @@ -21,6 +22,7 @@ abstract class AbstractConnection implements ConnectionInterface { protected TransactionInterface|null $transaction = null; + protected QuoterInterface|null $quoter = null; private bool $enableSavepoint = true; private string $tablePrefix = ''; @@ -71,6 +73,7 @@ public function setEnableSavepoint(bool $value): void public function setTablePrefix(string $value): void { $this->tablePrefix = $value; + $this->quoter?->setTablePrefix($value); } public function transaction(Closure $closure, string $isolationLevel = null): mixed diff --git a/src/Driver/Pdo/AbstractPdoConnection.php b/src/Driver/Pdo/AbstractPdoConnection.php index 477c4b5dc..8462939a7 100644 --- a/src/Driver/Pdo/AbstractPdoConnection.php +++ b/src/Driver/Pdo/AbstractPdoConnection.php @@ -19,7 +19,6 @@ use Yiisoft\Db\Profiler\ProfilerAwareInterface; use Yiisoft\Db\Profiler\ProfilerAwareTrait; use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; -use Yiisoft\Db\Schema\QuoterInterface; use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Transaction\TransactionInterface; @@ -45,7 +44,6 @@ abstract class AbstractPdoConnection extends AbstractConnection implements PdoCo protected string $serverVersion = ''; protected bool|null $emulatePrepare = null; protected QueryBuilderInterface|null $queryBuilder = null; - protected QuoterInterface|null $quoter = null; protected SchemaInterface|null $schema = null; public function __construct(protected PdoDriverInterface $driver, protected SchemaCache $schemaCache) diff --git a/src/Schema/Quoter.php b/src/Schema/Quoter.php index d0e56ee50..18f78aa2e 100644 --- a/src/Schema/Quoter.php +++ b/src/Schema/Quoter.php @@ -215,6 +215,11 @@ public function quoteValue(mixed $value): mixed return "'" . str_replace("'", "''", addcslashes($value, "\000\032")) . "'"; } + public function setTablePrefix(string $value): void + { + $this->tablePrefix = $value; + } + public function unquoteSimpleColumnName(string $name): string { if (is_string($this->columnQuoteCharacter)) { From 7042f7154414b5e16bc822e6b9a58bff7b31e15e Mon Sep 17 00:00:00 2001 From: Tigrov Date: Tue, 30 Jan 2024 09:50:25 +0700 Subject: [PATCH 06/13] Move `quoter->setTablePrefix()` to `AbstractPdoConnection` --- src/Connection/AbstractConnection.php | 3 --- src/Driver/Pdo/AbstractPdoConnection.php | 8 ++++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Connection/AbstractConnection.php b/src/Connection/AbstractConnection.php index 4e9425fe1..0f53bee6e 100644 --- a/src/Connection/AbstractConnection.php +++ b/src/Connection/AbstractConnection.php @@ -9,7 +9,6 @@ use Yiisoft\Db\Query\BatchQueryResult; use Yiisoft\Db\Query\BatchQueryResultInterface; use Yiisoft\Db\Query\QueryInterface; -use Yiisoft\Db\Schema\QuoterInterface; use Yiisoft\Db\Schema\TableSchemaInterface; use Yiisoft\Db\Transaction\TransactionInterface; @@ -22,7 +21,6 @@ abstract class AbstractConnection implements ConnectionInterface { protected TransactionInterface|null $transaction = null; - protected QuoterInterface|null $quoter = null; private bool $enableSavepoint = true; private string $tablePrefix = ''; @@ -73,7 +71,6 @@ public function setEnableSavepoint(bool $value): void public function setTablePrefix(string $value): void { $this->tablePrefix = $value; - $this->quoter?->setTablePrefix($value); } public function transaction(Closure $closure, string $isolationLevel = null): mixed diff --git a/src/Driver/Pdo/AbstractPdoConnection.php b/src/Driver/Pdo/AbstractPdoConnection.php index 8462939a7..2046c00a2 100644 --- a/src/Driver/Pdo/AbstractPdoConnection.php +++ b/src/Driver/Pdo/AbstractPdoConnection.php @@ -19,6 +19,7 @@ use Yiisoft\Db\Profiler\ProfilerAwareInterface; use Yiisoft\Db\Profiler\ProfilerAwareTrait; use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; +use Yiisoft\Db\Schema\QuoterInterface; use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Transaction\TransactionInterface; @@ -44,6 +45,7 @@ abstract class AbstractPdoConnection extends AbstractConnection implements PdoCo protected string $serverVersion = ''; protected bool|null $emulatePrepare = null; protected QueryBuilderInterface|null $queryBuilder = null; + protected QuoterInterface|null $quoter = null; protected SchemaInterface|null $schema = null; public function __construct(protected PdoDriverInterface $driver, protected SchemaCache $schemaCache) @@ -196,6 +198,12 @@ public function setEmulatePrepare(bool $value): void $this->emulatePrepare = $value; } + public function setTablePrefix(string $value): void + { + $this->tablePrefix = $value; + $this->quoter?->setTablePrefix($value); + } + /** * Initializes the DB connection. * From 24d55706f28d60e014a58a5e57eb385cc1130c72 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Tue, 30 Jan 2024 11:09:59 +0700 Subject: [PATCH 07/13] Fix setTablePrefix() --- src/Driver/Pdo/AbstractPdoConnection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Driver/Pdo/AbstractPdoConnection.php b/src/Driver/Pdo/AbstractPdoConnection.php index 2046c00a2..f2705f30f 100644 --- a/src/Driver/Pdo/AbstractPdoConnection.php +++ b/src/Driver/Pdo/AbstractPdoConnection.php @@ -200,7 +200,7 @@ public function setEmulatePrepare(bool $value): void public function setTablePrefix(string $value): void { - $this->tablePrefix = $value; + parent::setTablePrefix($value); $this->quoter?->setTablePrefix($value); } From 2684eba0276741966b25c13dad6f23084aa06cc0 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 1 Feb 2024 17:50:54 +0700 Subject: [PATCH 08/13] Remove `QuoterInterface::getRawTableName()` due to BC issue --- src/Schema/QuoterInterface.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Schema/QuoterInterface.php b/src/Schema/QuoterInterface.php index 9435c434c..f51e446cc 100644 --- a/src/Schema/QuoterInterface.php +++ b/src/Schema/QuoterInterface.php @@ -26,18 +26,6 @@ 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. * From d442aaee117c9da074500e098fdc04efe0cb86d5 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 2 Feb 2024 08:37:39 +0700 Subject: [PATCH 09/13] Update doc [skip ci] --- src/Schema/AbstractSchema.php | 2 +- src/Schema/Quoter.php | 10 ++++++++++ src/Schema/SchemaInterface.php | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Schema/AbstractSchema.php b/src/Schema/AbstractSchema.php index 0e0ba1de2..facbe2698 100644 --- a/src/Schema/AbstractSchema.php +++ b/src/Schema/AbstractSchema.php @@ -144,7 +144,7 @@ public function getDataType(mixed $data): int }; } - /** @deprecated Use {@see QuoterInterface::getRawTableName()}. Will be removed in version 2.0.0. */ + /** @deprecated Use {@see Quoter::getRawTableName()}. Will be removed in version 2.0.0. */ public function getRawTableName(string $name): string { if (str_contains($name, '{{')) { diff --git a/src/Schema/Quoter.php b/src/Schema/Quoter.php index 18f78aa2e..8314c1499 100644 --- a/src/Schema/Quoter.php +++ b/src/Schema/Quoter.php @@ -83,6 +83,16 @@ public function cleanUpTableNames(array $tableNames): array return $cleanedUpTableNames; } + /** + * 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 { if (str_contains($name, '{{')) { diff --git a/src/Schema/SchemaInterface.php b/src/Schema/SchemaInterface.php index 9582da1c1..744ebc555 100644 --- a/src/Schema/SchemaInterface.php +++ b/src/Schema/SchemaInterface.php @@ -285,7 +285,7 @@ public function getDataType(mixed $data): int; * * @return string The real name of the given table name. * - * @deprecated Use {@see QuoterInterface::getRawTableName()}. Will be removed in version 2.0.0. + * @deprecated Use {@see Quoter::getRawTableName()}. Will be removed in version 2.0.0. */ public function getRawTableName(string $name): string; From 440fab9ce94d42516be7d0b0d95b6a54351a0ba1 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Fri, 2 Feb 2024 21:20:46 +0700 Subject: [PATCH 10/13] Rearrange tests --- tests/AbstractSchemaTest.php | 8 -------- tests/Db/Helper/DbStringHelperTest.php | 8 ++++++++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/AbstractSchemaTest.php b/tests/AbstractSchemaTest.php index 2f0520644..87ba8b0c0 100644 --- a/tests/AbstractSchemaTest.php +++ b/tests/AbstractSchemaTest.php @@ -75,14 +75,6 @@ public function testGetDataType(): void fclose($fp); } - public function testIsReadQuery(): void - { - $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 { $db = $this->getConnection(); diff --git a/tests/Db/Helper/DbStringHelperTest.php b/tests/Db/Helper/DbStringHelperTest.php index bdb69235b..fb522edcb 100644 --- a/tests/Db/Helper/DbStringHelperTest.php +++ b/tests/Db/Helper/DbStringHelperTest.php @@ -18,6 +18,14 @@ public function testBaseName(): void $this->assertSame('TestCase', DbStringHelper::baseName('TestCase')); } + public function testIsReadQuery(): void + { + $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 static function pascalCaseToIdProvider(): array { return [ From 721b36c125569617f5a42622acd3b3821ca28d73 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 2 Feb 2024 14:21:08 +0000 Subject: [PATCH 11/13] Apply fixes from StyleCI --- tests/AbstractSchemaTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/AbstractSchemaTest.php b/tests/AbstractSchemaTest.php index 87ba8b0c0..3059e606f 100644 --- a/tests/AbstractSchemaTest.php +++ b/tests/AbstractSchemaTest.php @@ -6,7 +6,6 @@ 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; From e94a5fd7a254ac5d97f376ce5e67e062e75087b0 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Mon, 5 Feb 2024 01:30:50 +0700 Subject: [PATCH 12/13] Fix psalm issues --- src/Driver/Pdo/AbstractPdoConnection.php | 5 ++++- src/Schema/AbstractSchema.php | 13 +++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Driver/Pdo/AbstractPdoConnection.php b/src/Driver/Pdo/AbstractPdoConnection.php index f2705f30f..f6895545d 100644 --- a/src/Driver/Pdo/AbstractPdoConnection.php +++ b/src/Driver/Pdo/AbstractPdoConnection.php @@ -25,6 +25,7 @@ use function array_keys; use function is_string; +use function method_exists; /** * Represents a connection to a database using the PDO (PHP Data Objects) extension. @@ -201,7 +202,9 @@ public function setEmulatePrepare(bool $value): void public function setTablePrefix(string $value): void { parent::setTablePrefix($value); - $this->quoter?->setTablePrefix($value); + if ($this->quoter !== null && method_exists($this->quoter, 'setTablePrefix')) { + $this->quoter->setTablePrefix($value); + } } /** diff --git a/src/Schema/AbstractSchema.php b/src/Schema/AbstractSchema.php index facbe2698..7e13c88f0 100644 --- a/src/Schema/AbstractSchema.php +++ b/src/Schema/AbstractSchema.php @@ -342,7 +342,8 @@ public function refresh(): void */ public function refreshTableSchema(string $name): void { - $rawName = $this->db->getQuoter()->getRawTableName($name); + /** @psalm-suppress DeprecatedMethod */ + $rawName = $this->getRawTableName($name); unset($this->tableMetadata[$rawName]); @@ -472,7 +473,8 @@ protected function getSchemaMetadata(string $schema, string $type, bool $refresh */ protected function getTableMetadata(string $name, string $type, bool $refresh = false): mixed { - $rawName = $this->db->getQuoter()->getRawTableName($name); + /** @psalm-suppress DeprecatedMethod */ + $rawName = $this->getRawTableName($name); if (!isset($this->tableMetadata[$rawName])) { $this->loadTableMetadataFromCache($rawName); @@ -572,8 +574,11 @@ protected function resolveTableName(string $name): TableSchemaInterface */ protected function setTableMetadata(string $name, string $type, mixed $data): void { - /** @psalm-suppress MixedArrayAssignment */ - $this->tableMetadata[$this->db->getQuoter()->getRawTableName($name)][$type] = $data; + /** + * @psalm-suppress MixedArrayAssignment + * @psalm-suppress DeprecatedMethod + */ + $this->tableMetadata[$this->getRawTableName($name)][$type] = $data; } /** From 7473f66fd02646baaf5ab36acbe3aa664fd787a1 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sat, 10 Feb 2024 17:52:46 +0700 Subject: [PATCH 13/13] Update CHANGELOG.md [skip ci] --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef0b1d36e..6a395afc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ - Enh #794: Add message type to log context (@darkdef) - Enh #802: Minor refactoring of `SchemaCache`, `AbstractPdoCommand` and `AbstractDDLQueryBuilder` (@Tigrov) - Enh #801: Deprecate `AbstractSchema::normalizeRowKeyCase()` method (@Tigrov) -- Enh #801: Deprecate `SchemaInterface::getRawTableName()` and add `QuoterInterface::getRawTableName()` method (@Tigrov) +- Enh #801: Deprecate `SchemaInterface::getRawTableName()` and add `Quoter::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) - Bug #801: Fix bug with `Quoter::$tablePrefix` when change `AbstractConnection::$tablePrefix` property (@Tigrov)