Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
- Chg #1001: Add public properties `$type` and `$value` to `Param` class instead of `getType()` and `getValue()` methods that were removed (@vjik)
- Chg #1002: Remove specific condition interfaces (@vjik)
- Chg #1003, #1006: Refactor namespace of condition objects and use promoted properties instead of getters (@vjik)
- Enh #1010: Improve `Quoter::getTableNameParts()` method (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
14 changes: 12 additions & 2 deletions src/Schema/Quoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,21 @@ public function getRawTableName(string $name): string
return $name;
}

/** @psalm-return array{schemaName?: string, name: string} */
public function getTableNameParts(string $name): array
{
$parts = array_slice(explode('.', $name), -2, 2);
$parts = array_reverse(array_slice(explode('.', $name), -2, 2));
/** @var string[] */
$parts = array_map($this->unquoteSimpleTableName(...), $parts);

return array_map($this->unquoteSimpleTableName(...), $parts);
if (!isset($parts[1])) {
return ['name' => $parts[0]];
}

return [
'schemaName' => $parts[1],
'name' => $parts[0],
];
}

public function ensureNameQuoted(string $name): string
Expand Down
4 changes: 3 additions & 1 deletion src/Schema/QuoterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public function cleanUpTableNames(array $tableNames): array;
public function getRawTableName(string $name): string;

/**
* Splits full table name into parts.
* Splits a full table name into parts in the appropriate order, using the part name as the key.
*
* For example, "dbname.tblname" will be split into an array `['schemaName' => 'dbname', 'name' => 'tblname']`
*
* @param string $name The full name of the table.
*
Expand Down
34 changes: 11 additions & 23 deletions tests/AbstractQuoterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,32 @@

namespace Yiisoft\Db\Tests;

use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Tests\Provider\QuoterProvider;
use Yiisoft\Db\Tests\Support\TestTrait;

abstract class AbstractQuoterTest extends TestCase
{
use TestTrait;

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\QuoterProvider::ensureColumnName
*/
#[DataProviderExternal(QuoterProvider::class, 'ensureColumnName')]
public function testEnsureColumnName(string $columnName, string $expected): void
{
$db = $this->getConnection();

$this->assertSame($expected, $db->getQuoter()->ensureColumnName($columnName));
}

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\QuoterProvider::ensureNameQuoted
*/
#[DataProviderExternal(QuoterProvider::class, 'ensureNameQuoted')]
public function testEnsureNameQuoted(string $name, string $expected): void
{
$db = $this->getConnection();

$this->assertSame($expected, $db->getQuoter()->ensureNameQuoted($name));
}

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\QuoterProvider::rawTableNames
*/
#[DataProviderExternal(QuoterProvider::class, 'rawTableNames')]
public function testGetRawTableName(string $tableName, string $expected, string $tablePrefix = ''): void
{
$db = $this->getConnection();
Expand All @@ -43,29 +39,23 @@ public function testGetRawTableName(string $tableName, string $expected, string
$this->assertSame($expected, $db->getQuoter()->getRawTableName($tableName));
}

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\QuoterProvider::tableNameParts
*/
public function testGetTableNameParts(string $tableName, string ...$expected): void
#[DataProviderExternal(QuoterProvider::class, 'tableNameParts')]
public function testGetTableNameParts(string $tableName, array $expected): void
{
$db = $this->getConnection();

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

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\QuoterProvider::columnNames
*/
#[DataProviderExternal(QuoterProvider::class, 'columnNames')]
public function testQuoteColumnName(string $columnName, string $expected): void
{
$db = $this->getConnection();

$this->assertSame($expected, $db->getQuoter()->quoteColumnName($columnName));
}

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\QuoterProvider::simpleColumnNames
*/
#[DataProviderExternal(QuoterProvider::class, 'simpleColumnNames')]
public function testQuoteSimpleColumnName(
string $columnName,
string $expectedQuotedColumnName,
Expand All @@ -83,9 +73,7 @@ public function testQuoteSimpleColumnName(
$this->assertSame($expectedUnQuotedColumnName, $unQuoted);
}

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\QuoterProvider::simpleTableNames
*/
#[DataProviderExternal(QuoterProvider::class, 'simpleTableNames')]
public function testQuoteTableName(string $tableName, string $expected): void
{
$db = $this->getConnection();
Expand Down
2 changes: 1 addition & 1 deletion tests/Db/Schema/QuoterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function testGetTableNamePartsWithDifferentQuotes(): void
{
$quoter = new Quoter('`', '"');

$this->assertSame(['schema', 'table'], $quoter->getTableNameParts('"schema"."table"'));
$this->assertSame(['schemaName' => 'schema', 'name' => 'table'], $quoter->getTableNameParts('"schema"."table"'));
}

public function testQuoteSqlWithTablePrefix(): void
Expand Down
17 changes: 8 additions & 9 deletions tests/Provider/QuoterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,17 @@ public static function rawTableNames(): array
];
}

/**
* @return string[][]
*/
public static function tableNameParts(): array
{
return [
['animal', 'animal',],
['dbo.animal', 'animal', 'dbo'],
['[dbo].[animal]', 'animal', 'dbo'],
['[other].[animal2]', 'animal2', 'other'],
['other.[animal2]', 'animal2', 'other'],
['other.animal2', 'animal2', 'other'],
['', ['name' => '']],
['[]', ['name' => '']],
['animal', ['name' => 'animal']],
['[animal]', ['name' => 'animal']],
['dbo.animal', ['schemaName' => 'dbo', 'name' => 'animal']],
['[dbo].[animal]', ['schemaName' => 'dbo', 'name' => 'animal']],
['[dbo].animal', ['schemaName' => 'dbo', 'name' => 'animal']],
['dbo.[animal]', ['schemaName' => 'dbo', 'name' => 'animal']],
];
}

Expand Down