Skip to content

Commit

Permalink
Merge pull request #234 from nextras/fqn-changes
Browse files Browse the repository at this point in the history
Fqn changes
  • Loading branch information
hrach committed Mar 19, 2024
2 parents a5df924 + 4f2d3a3 commit a6e16ad
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/Platforms/Data/Fqn.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class Fqn


public function __construct(
public readonly string $name,
public readonly string $schema,
public readonly string $name,
)
{
}
Expand Down
6 changes: 3 additions & 3 deletions src/Platforms/MySqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Platforms/PostgreSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Platforms/SqlServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/SqlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/integration/connection.postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
20 changes: 10 additions & 10 deletions tests/cases/integration/platform.mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
20 changes: 10 additions & 10 deletions tests/cases/integration/platform.postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
20 changes: 10 additions & 10 deletions tests/cases/integration/platform.sqlserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion tests/cases/unit/SqlProcessorTest.identifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ 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')]),
);

Assert::same(
'`a`.`b`',
$this->parser->process(['%column', new Fqn(schema: 'a', name: 'b')]),
);
}

Expand Down

0 comments on commit a6e16ad

Please sign in to comment.