Skip to content

Commit

Permalink
Move and deprecate Schema methods (#801)
Browse files Browse the repository at this point in the history
* Move and deprecate `Schema` methods

* Add tests

* Add lines to CHANGELOG.md [skip ci]

* Apply review suggestions

* Fix bug with `Quoter`

* Move `quoter->setTablePrefix()` to `AbstractPdoConnection`

* Fix setTablePrefix()

* Remove `QuoterInterface::getRawTableName()` due to BC issue

* Update doc [skip ci]

* Rearrange tests

* Apply fixes from StyleCI

* Fix psalm issues

* Update CHANGELOG.md [skip ci]

---------

Co-authored-by: StyleCI Bot <[email protected]>
Co-authored-by: Sergei Predvoditelev <[email protected]>
  • Loading branch information
3 people authored Feb 10, 2024
1 parent 89e1003 commit e44b52f
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
- Enh #795: Allow to use `DMLQueryBuilderInterface::batchInsert()` method with empty columns (@Tigrov)
- 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 `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)

## 1.2.0 November 12, 2023

Expand Down
9 changes: 9 additions & 0 deletions src/Driver/Pdo/AbstractPdoConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -198,6 +199,14 @@ public function setEmulatePrepare(bool $value): void
$this->emulatePrepare = $value;
}

public function setTablePrefix(string $value): void
{
parent::setTablePrefix($value);
if ($this->quoter !== null && method_exists($this->quoter, 'setTablePrefix')) {
$this->quoter->setTablePrefix($value);
}
}

/**
* 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), '/');
$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
12 changes: 11 additions & 1 deletion 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 Quoter::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,6 +342,7 @@ public function refresh(): void
*/
public function refreshTableSchema(string $name): void
{
/** @psalm-suppress DeprecatedMethod */
$rawName = $this->getRawTableName($name);

unset($this->tableMetadata[$rawName]);
Expand Down Expand Up @@ -470,6 +473,7 @@ protected function getSchemaMetadata(string $schema, string $type, bool $refresh
*/
protected function getTableMetadata(string $name, string $type, bool $refresh = false): mixed
{
/** @psalm-suppress DeprecatedMethod */
$rawName = $this->getRawTableName($name);

if (!isset($this->tableMetadata[$rawName])) {
Expand Down Expand Up @@ -532,6 +536,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 @@ -567,7 +574,10 @@ protected function resolveTableName(string $name): TableSchemaInterface
*/
protected function setTableMetadata(string $name, string $type, mixed $data): void
{
/** @psalm-suppress MixedArrayAssignment */
/**
* @psalm-suppress MixedArrayAssignment
* @psalm-suppress DeprecatedMethod
*/
$this->tableMetadata[$this->getRawTableName($name)][$type] = $data;
}

Expand Down
26 changes: 26 additions & 0 deletions src/Schema/Quoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@ 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, '{{')) {
$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 +225,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)) {
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 Quoter::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.
*/
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
12 changes: 0 additions & 12 deletions tests/AbstractSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,6 @@ public function testGetDataType(): void
fclose($fp);
}

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'));
}

public function testRefresh(): void
{
$db = $this->getConnection();
Expand Down
8 changes: 8 additions & 0 deletions tests/Db/Helper/DbStringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
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

0 comments on commit e44b52f

Please sign in to comment.