Skip to content

Commit ca47bf4

Browse files
authored
Resolve table names. Parts 2 (#88)
* Resolve table names. Parts 2 * styleci fixes
1 parent cc0fbc0 commit ca47bf4

2 files changed

Lines changed: 84 additions & 38 deletions

File tree

src/Schema.php

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@
2222
use function array_change_key_case;
2323
use function array_map;
2424
use function array_merge;
25-
use function explode;
2625
use function is_array;
2726
use function md5;
28-
use function preg_replace;
2927
use function serialize;
3028
use function str_contains;
31-
use function str_replace;
3229
use function stripos;
3330
use function strlen;
3431
use function substr;
@@ -57,30 +54,27 @@
5754
*/
5855
final class Schema extends AbstractSchema
5956
{
60-
public function __construct(private ConnectionInterface $db, SchemaCache $schemaCache, string $defaultSchema)
57+
public function __construct(protected ConnectionInterface $db, SchemaCache $schemaCache, string $defaultSchema)
6158
{
6259
$this->defaultSchema = $defaultSchema;
63-
parent::__construct($schemaCache);
60+
parent::__construct($db, $schemaCache);
6461
}
6562

6663
protected function resolveTableName(string $name): TableSchemaInterface
6764
{
6865
$resolvedName = new TableSchema();
6966

70-
$parts = explode('.', str_replace('"', '', $name));
71-
72-
if (isset($parts[1])) {
73-
$resolvedName->schemaName($parts[0]);
74-
$resolvedName->name($parts[1]);
75-
} else {
76-
$resolvedName->schemaName($this->defaultSchema);
77-
$resolvedName->name($name);
78-
}
67+
$parts = array_reverse(
68+
$this->db->getQuoter()->getTableNameParts($name)
69+
);
7970

80-
$fullName = ($resolvedName->getSchemaName() !== $this->defaultSchema
81-
? (string) $resolvedName->getSchemaName() . '.' : '') . $resolvedName->getName();
71+
$resolvedName->name($parts[0] ?? '');
72+
$resolvedName->schemaName($parts[1] ?? $this->defaultSchema);
8273

83-
$resolvedName->fullName($fullName);
74+
$resolvedName->fullName(
75+
$resolvedName->getSchemaName() !== $this->defaultSchema ?
76+
implode('.', array_reverse($parts)) : $resolvedName->getName()
77+
);
8478

8579
return $resolvedName;
8680
}
@@ -788,27 +782,6 @@ protected function createColumnSchema(): ColumnSchema
788782
return new ColumnSchema();
789783
}
790784

791-
/**
792-
* Returns the actual name of a given table name.
793-
*
794-
* This method will strip off curly brackets from the given table name and replace the percentage character '%' with
795-
* {@see ConnectionInterface::tablePrefix}.
796-
*
797-
* @param string $name the table name to be converted.
798-
*
799-
* @return string the real name of the given table name.
800-
*/
801-
public function getRawTableName(string $name): string
802-
{
803-
if (str_contains($name, '{{')) {
804-
$name = preg_replace('/{{(.*?)}}/', '\1', $name);
805-
806-
return str_replace('%', $this->db->getTablePrefix(), $name);
807-
}
808-
809-
return $name;
810-
}
811-
812785
/**
813786
* Returns the cache key for the specified table name.
814787
*

tests/SchemaTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Yiisoft\Db\Oracle\Tests;
66

77
use PDO;
8+
use Yiisoft\Db\Command\CommandInterface;
9+
use Yiisoft\Db\Connection\ConnectionInterface;
810
use Yiisoft\Db\Constraint\CheckConstraint;
911
use Yiisoft\Db\Exception\Exception;
1012
use Yiisoft\Db\Exception\InvalidConfigException;
@@ -519,4 +521,75 @@ public function testGetSchemaDefaultValues(): void
519521
{
520522
$this->markTestSkipped('Oracle does not support default value constraints.');
521523
}
524+
525+
/**
526+
* @dataProvider tableSchemaWithDbSchemesDataProvider
527+
*/
528+
public function testTableSchemaWithDbSchemes(string $tableName, string $expectedTableName, string $expectedSchemaName = ''): void
529+
{
530+
$db = $this->getConnection();
531+
532+
$commandMock = $this->createMock(CommandInterface::class);
533+
$commandMock
534+
->method('queryAll')
535+
->willReturn([]);
536+
537+
$mockDb = $this->createMock(ConnectionInterface::class);
538+
539+
$mockDb->method('getQuoter')
540+
->willReturn($db->getQuoter());
541+
542+
$mockDb
543+
->method('createCommand')
544+
->with(self::callback(function ($sql) {
545+
return true;
546+
}), self::callback(function ($params) use ($expectedTableName, $expectedSchemaName) {
547+
$this->assertEquals($expectedTableName, $params[':tableName']);
548+
$this->assertEquals($expectedSchemaName, $params[':schemaName']);
549+
return true;
550+
}))
551+
->willReturn($commandMock);
552+
553+
$schema = new Schema($mockDb, $this->createSchemaCache(), 'dbo');
554+
555+
$schema->getTablePrimaryKey($tableName);
556+
}
557+
558+
public function tableSchemaWithDbSchemesDataProvider(): array
559+
{
560+
return [
561+
['animal', 'animal', 'dbo'],
562+
['dbo.animal', 'animal', 'dbo'],
563+
['"dbo"."animal"', 'animal', 'dbo'],
564+
['"other"."animal2"', 'animal2', 'other',],
565+
['other."animal2"', 'animal2', 'other',],
566+
['other.animal2', 'animal2', 'other',],
567+
['catalog.other.animal2', 'animal2', 'other'],
568+
];
569+
}
570+
571+
/**
572+
* @dataProvider quoterTablePartsDataProvider
573+
*/
574+
public function testQuoterTableParts(string $tableName, ...$expectedParts): void
575+
{
576+
$quoter = $this->getConnection()->getQuoter();
577+
578+
$parts = $quoter->getTableNameParts($tableName);
579+
580+
$this->assertEquals($expectedParts, array_reverse($parts));
581+
}
582+
583+
public function quoterTablePartsDataProvider(): array
584+
{
585+
return [
586+
['animal', 'animal',],
587+
['dbo.animal', 'animal', 'dbo'],
588+
['"dbo"."animal"', 'animal', 'dbo'],
589+
['"other"."animal2"', 'animal2', 'other'],
590+
['other."animal2"', 'animal2', 'other'],
591+
['other.animal2', 'animal2', 'other'],
592+
['catalog.other.animal2', 'animal2', 'other'],
593+
];
594+
}
522595
}

0 commit comments

Comments
 (0)