From f04e07b364ec958edf551ee3e02ad84d62e69554 Mon Sep 17 00:00:00 2001 From: Jan Skrasek Date: Tue, 19 Mar 2024 13:16:46 +0100 Subject: [PATCH 1/2] swap schema & name property ordering in Fqn class --- src/Platforms/Data/Fqn.php | 2 +- src/Platforms/MySqlPlatform.php | 6 +++--- src/Platforms/PostgreSqlPlatform.php | 6 +++--- src/Platforms/SqlServerPlatform.php | 6 +++--- .../cases/integration/connection.postgres.php | 2 +- tests/cases/integration/platform.mysql.php | 20 +++++++++---------- tests/cases/integration/platform.postgres.php | 20 +++++++++---------- .../cases/integration/platform.sqlserver.php | 20 +++++++++---------- .../unit/SqlProcessorTest.identifiers.php | 2 +- 9 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/Platforms/Data/Fqn.php b/src/Platforms/Data/Fqn.php index b114cc7..9a52b42 100644 --- a/src/Platforms/Data/Fqn.php +++ b/src/Platforms/Data/Fqn.php @@ -15,8 +15,8 @@ class Fqn public function __construct( - public readonly string $name, public readonly string $schema, + public readonly string $name, ) { } diff --git a/src/Platforms/MySqlPlatform.php b/src/Platforms/MySqlPlatform.php index 277adc6..46e11a9 100644 --- a/src/Platforms/MySqlPlatform.php +++ b/src/Platforms/MySqlPlatform.php @@ -62,7 +62,7 @@ public function getTables(?string $schema = null): array $tables = []; foreach ($result as $row) { $table = new Table( - fqnName: new Fqn((string) $row->TABLE_NAME, (string) $row->TABLE_SCHEMA), + fqnName: new Fqn((string) $row->TABLE_SCHEMA, (string) $row->TABLE_NAME), isView: $row->TABLE_TYPE === 'VIEW', ); $tables[$table->fqnName->getUnescaped()] = $table; @@ -125,9 +125,9 @@ public function getForeignKeys(string $table, ?string $schema = null): array $keys = []; foreach ($result as $row) { $foreignKey = new ForeignKey( - fqnName: new Fqn((string) $row->CONSTRAINT_NAME, (string) $row->CONSTRAINT_SCHEMA), + fqnName: new Fqn((string) $row->CONSTRAINT_SCHEMA, (string) $row->CONSTRAINT_NAME), column: (string) $row->COLUMN_NAME, - refTable: new Fqn((string) $row->REFERENCED_TABLE_NAME, (string) $row->REFERENCED_TABLE_SCHEMA), + refTable: new Fqn((string) $row->REFERENCED_TABLE_SCHEMA, (string) $row->REFERENCED_TABLE_NAME), refColumn: (string) $row->REFERENCED_COLUMN_NAME, ); $keys[$foreignKey->column] = $foreignKey; diff --git a/src/Platforms/PostgreSqlPlatform.php b/src/Platforms/PostgreSqlPlatform.php index 7a16dbe..951736d 100644 --- a/src/Platforms/PostgreSqlPlatform.php +++ b/src/Platforms/PostgreSqlPlatform.php @@ -68,7 +68,7 @@ public function getTables(?string $schema = null): array $tables = []; foreach ($result as $row) { $table = new Table( - fqnName: new Fqn((string) $row->name, (string) $row->schema), + fqnName: new Fqn((string) $row->schema, (string) $row->name), isView: (bool) $row->is_view, ); $tables[$table->fqnName->getUnescaped()] = $table; @@ -160,9 +160,9 @@ public function getForeignKeys(string $table, ?string $schema = null): array $keys = []; foreach ($result as $row) { $foreignKey = new ForeignKey( - fqnName: new Fqn((string) $row->name, (string) $row->schema), + fqnName: new Fqn((string) $row->schema, (string) $row->name), column: (string) $row->column, - refTable: new Fqn((string) $row->ref_table, (string) $row->ref_table_schema), + refTable: new Fqn((string) $row->ref_table_schema, (string) $row->ref_table), refColumn: (string) $row->ref_column, ); $keys[$foreignKey->column] = $foreignKey; diff --git a/src/Platforms/SqlServerPlatform.php b/src/Platforms/SqlServerPlatform.php index 7389c59..f48318c 100644 --- a/src/Platforms/SqlServerPlatform.php +++ b/src/Platforms/SqlServerPlatform.php @@ -53,7 +53,7 @@ public function getTables(?string $schema = null): array $tables = []; foreach ($result as $row) { $table = new Table( - fqnName: new Fqn((string) $row->TABLE_NAME, (string) $row->TABLE_SCHEMA), + fqnName: new Fqn((string) $row->TABLE_SCHEMA, (string) $row->TABLE_NAME), isView: $row->TABLE_TYPE === 'VIEW', ); $tables[$table->fqnName->getUnescaped()] = $table; @@ -157,9 +157,9 @@ public function getForeignKeys(string $table, ?string $schema = null): array $keys = []; foreach ($result as $row) { $foreignKey = new ForeignKey( - fqnName: new Fqn((string) $row->name, (string) $row->schema), + fqnName: new Fqn((string) $row->schema, (string) $row->name), column: (string) $row->column, - refTable: new Fqn((string) $row->ref_table, (string) $row->ref_table_schema), + refTable: new Fqn((string) $row->ref_table_schema, (string) $row->ref_table), refColumn: (string) $row->ref_column, ); $keys[$foreignKey->column] = $foreignKey; diff --git a/tests/cases/integration/connection.postgres.php b/tests/cases/integration/connection.postgres.php index 8704f75..010e2e3 100644 --- a/tests/cases/integration/connection.postgres.php +++ b/tests/cases/integration/connection.postgres.php @@ -33,7 +33,7 @@ public function testLastInsertId() $this->connection->query('INSERT INTO publishers %values', ['name' => 'FOO']); Assert::same(2, $this->connection->getLastInsertedId('publishers_id_seq')); - Assert::same(2, $this->connection->getLastInsertedId(new Fqn(name: 'publishers_id_seq', schema: 'public'))); + Assert::same(2, $this->connection->getLastInsertedId(new Fqn(schema: 'public', name: 'publishers_id_seq'))); Assert::exception(function() { $this->connection->getLastInsertedId(); diff --git a/tests/cases/integration/platform.mysql.php b/tests/cases/integration/platform.mysql.php index 68a0222..8cec5bc 100644 --- a/tests/cases/integration/platform.mysql.php +++ b/tests/cases/integration/platform.mysql.php @@ -199,27 +199,27 @@ public function testForeignKeys() Assert::equal([ 'author_id' => [ - 'fqnName' => new Fqn('books_authors', $dbName), + 'fqnName' => new Fqn($dbName, 'books_authors'), 'column' => 'author_id', - 'refTable' => new Fqn('authors', $dbName . '2'), + 'refTable' => new Fqn($dbName . '2', 'authors'), 'refColumn' => 'id', ], 'ean_id' => [ - 'fqnName' => new Fqn('books_ean', $dbName), + 'fqnName' => new Fqn($dbName, 'books_ean'), 'column' => 'ean_id', - 'refTable' => new Fqn('eans', $dbName), + 'refTable' => new Fqn($dbName, 'eans'), 'refColumn' => 'id', ], 'publisher_id' => [ - 'fqnName' => new Fqn('books_publisher', $dbName), + 'fqnName' => new Fqn($dbName, 'books_publisher'), 'column' => 'publisher_id', - 'refTable' => new Fqn('publishers', $dbName), + 'refTable' => new Fqn($dbName, 'publishers'), 'refColumn' => 'id', ], 'translator_id' => [ - 'fqnName' => new Fqn('books_translator', $dbName), + 'fqnName' => new Fqn($dbName, 'books_translator'), 'column' => 'translator_id', - 'refTable' => new Fqn('authors', $dbName . '2'), + 'refTable' => new Fqn($dbName . '2', 'authors'), 'refColumn' => 'id', ], ], $keys); @@ -239,9 +239,9 @@ public function testForeignKeys() }, $schemaKeys); Assert::equal([ 'book_id' => [ - 'fqnName' => new Fqn('book_id', $dbName2), + 'fqnName' => new Fqn($dbName2, 'book_id'), 'column' => 'book_id', - 'refTable' => new Fqn('books', $dbName), + 'refTable' => new Fqn($dbName, 'books'), 'refColumn' => 'id', ], ], $schemaKeys); diff --git a/tests/cases/integration/platform.postgres.php b/tests/cases/integration/platform.postgres.php index 7a6aa51..e60b8a5 100644 --- a/tests/cases/integration/platform.postgres.php +++ b/tests/cases/integration/platform.postgres.php @@ -168,27 +168,27 @@ public function testForeignKeys() Assert::equal([ 'author_id' => [ - 'fqnName' => new Fqn('books_authors', 'public'), + 'fqnName' => new Fqn('public', 'books_authors'), 'column' => 'author_id', - 'refTable' => new Fqn('authors', 'second_schema'), + 'refTable' => new Fqn('second_schema', 'authors'), 'refColumn' => 'id', ], 'translator_id' => [ - 'fqnName' => new Fqn('books_translator', 'public'), + 'fqnName' => new Fqn('public', 'books_translator'), 'column' => 'translator_id', - 'refTable' => new Fqn('authors', 'second_schema'), + 'refTable' => new Fqn('second_schema', 'authors'), 'refColumn' => 'id', ], 'publisher_id' => [ - 'fqnName' => new Fqn('books_publisher', 'public'), + 'fqnName' => new Fqn('public', 'books_publisher'), 'column' => 'publisher_id', - 'refTable' => new Fqn('publishers', 'public'), + 'refTable' => new Fqn('public', 'publishers'), 'refColumn' => 'id', ], 'ean_id' => [ - 'fqnName' => new Fqn('books_ean', 'public'), + 'fqnName' => new Fqn('public', 'books_ean'), 'column' => 'ean_id', - 'refTable' => new Fqn('eans', 'public'), + 'refTable' => new Fqn('public', 'eans'), 'refColumn' => 'id', ], ], $keys); @@ -206,9 +206,9 @@ public function testForeignKeys() Assert::equal([ 'book_id' => [ - 'fqnName' => new Fqn('book_id', 'second_schema'), + 'fqnName' => new Fqn('second_schema', 'book_id'), 'column' => 'book_id', - 'refTable' => new Fqn('books', 'public'), + 'refTable' => new Fqn('public', 'books'), 'refColumn' => 'id', ], ], $schemaKeys); diff --git a/tests/cases/integration/platform.sqlserver.php b/tests/cases/integration/platform.sqlserver.php index 073077e..397f956 100644 --- a/tests/cases/integration/platform.sqlserver.php +++ b/tests/cases/integration/platform.sqlserver.php @@ -170,27 +170,27 @@ public function testForeignKeys() Assert::equal([ 'author_id' => [ - 'fqnName' => new Fqn('books_authors', 'dbo'), + 'fqnName' => new Fqn('dbo', 'books_authors'), 'column' => 'author_id', - 'refTable' => new Fqn('authors', 'second_schema'), + 'refTable' => new Fqn('second_schema', 'authors'), 'refColumn' => 'id', ], 'ean_id' => [ - 'fqnName' => new Fqn('books_ean', 'dbo'), + 'fqnName' => new Fqn('dbo', 'books_ean'), 'column' => 'ean_id', - 'refTable' => new Fqn('eans', 'dbo'), + 'refTable' => new Fqn('dbo', 'eans'), 'refColumn' => 'id', ], 'publisher_id' => [ - 'fqnName' => new Fqn('books_publisher', 'dbo'), + 'fqnName' => new Fqn('dbo', 'books_publisher'), 'column' => 'publisher_id', - 'refTable' => new Fqn('publishers', 'dbo'), + 'refTable' => new Fqn('dbo', 'publishers'), 'refColumn' => 'id', ], 'translator_id' => [ - 'fqnName' => new Fqn('books_translator', 'dbo'), + 'fqnName' => new Fqn('dbo', 'books_translator'), 'column' => 'translator_id', - 'refTable' => new Fqn('authors', 'second_schema'), + 'refTable' => new Fqn('second_schema', 'authors'), 'refColumn' => 'id', ], ], $keys); @@ -208,9 +208,9 @@ public function testForeignKeys() Assert::equal([ 'book_id' => [ - 'fqnName' => new Fqn('book_id', 'second_schema'), + 'fqnName' => new Fqn('second_schema', 'book_id'), 'column' => 'book_id', - 'refTable' => new Fqn('books', 'dbo'), + 'refTable' => new Fqn('dbo', 'books'), 'refColumn' => 'id', ], ], $schemaKeys); diff --git a/tests/cases/unit/SqlProcessorTest.identifiers.php b/tests/cases/unit/SqlProcessorTest.identifiers.php index 3fa51c9..7451f2c 100644 --- a/tests/cases/unit/SqlProcessorTest.identifiers.php +++ b/tests/cases/unit/SqlProcessorTest.identifiers.php @@ -53,7 +53,7 @@ public function testFqn() Assert::same( '`a`.`b`', - $this->parser->process(['%table', new Fqn('b', schema: 'a')]), + $this->parser->process(['%table', new Fqn(schema: 'a', name: 'b')]), ); } From 4f2d3a39cd6e719cf21c33f5afead7e86bb6603d Mon Sep 17 00:00:00 2001 From: Jan Skrasek Date: Tue, 19 Mar 2024 13:18:09 +0100 Subject: [PATCH 2/2] allow processing Fqn with %column modifier --- src/SqlProcessor.php | 1 + tests/cases/unit/SqlProcessorTest.identifiers.php | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/src/SqlProcessor.php b/src/SqlProcessor.php index 653eb42..71141f3 100644 --- a/src/SqlProcessor.php +++ b/src/SqlProcessor.php @@ -297,6 +297,7 @@ public function processModifier(string $type, mixed $value): string } elseif ($value instanceof Fqn) { switch ($type) { + case 'column': case 'table': $schema = $this->identifierToSql($value->schema); $table = $this->identifierToSql($value->name); diff --git a/tests/cases/unit/SqlProcessorTest.identifiers.php b/tests/cases/unit/SqlProcessorTest.identifiers.php index 7451f2c..9e66490 100644 --- a/tests/cases/unit/SqlProcessorTest.identifiers.php +++ b/tests/cases/unit/SqlProcessorTest.identifiers.php @@ -55,6 +55,11 @@ public function testFqn() '`a`.`b`', $this->parser->process(['%table', new Fqn(schema: 'a', name: 'b')]), ); + + Assert::same( + '`a`.`b`', + $this->parser->process(['%column', new Fqn(schema: 'a', name: 'b')]), + ); }