Skip to content

Commit a6e16ad

Browse files
authored
Merge pull request #234 from nextras/fqn-changes
Fqn changes
2 parents a5df924 + 4f2d3a3 commit a6e16ad

File tree

10 files changed

+48
-42
lines changed

10 files changed

+48
-42
lines changed

src/Platforms/Data/Fqn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class Fqn
1515

1616

1717
public function __construct(
18-
public readonly string $name,
1918
public readonly string $schema,
19+
public readonly string $name,
2020
)
2121
{
2222
}

src/Platforms/MySqlPlatform.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function getTables(?string $schema = null): array
6262
$tables = [];
6363
foreach ($result as $row) {
6464
$table = new Table(
65-
fqnName: new Fqn((string) $row->TABLE_NAME, (string) $row->TABLE_SCHEMA),
65+
fqnName: new Fqn((string) $row->TABLE_SCHEMA, (string) $row->TABLE_NAME),
6666
isView: $row->TABLE_TYPE === 'VIEW',
6767
);
6868
$tables[$table->fqnName->getUnescaped()] = $table;
@@ -125,9 +125,9 @@ public function getForeignKeys(string $table, ?string $schema = null): array
125125
$keys = [];
126126
foreach ($result as $row) {
127127
$foreignKey = new ForeignKey(
128-
fqnName: new Fqn((string) $row->CONSTRAINT_NAME, (string) $row->CONSTRAINT_SCHEMA),
128+
fqnName: new Fqn((string) $row->CONSTRAINT_SCHEMA, (string) $row->CONSTRAINT_NAME),
129129
column: (string) $row->COLUMN_NAME,
130-
refTable: new Fqn((string) $row->REFERENCED_TABLE_NAME, (string) $row->REFERENCED_TABLE_SCHEMA),
130+
refTable: new Fqn((string) $row->REFERENCED_TABLE_SCHEMA, (string) $row->REFERENCED_TABLE_NAME),
131131
refColumn: (string) $row->REFERENCED_COLUMN_NAME,
132132
);
133133
$keys[$foreignKey->column] = $foreignKey;

src/Platforms/PostgreSqlPlatform.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function getTables(?string $schema = null): array
6868
$tables = [];
6969
foreach ($result as $row) {
7070
$table = new Table(
71-
fqnName: new Fqn((string) $row->name, (string) $row->schema),
71+
fqnName: new Fqn((string) $row->schema, (string) $row->name),
7272
isView: (bool) $row->is_view,
7373
);
7474
$tables[$table->fqnName->getUnescaped()] = $table;
@@ -160,9 +160,9 @@ public function getForeignKeys(string $table, ?string $schema = null): array
160160
$keys = [];
161161
foreach ($result as $row) {
162162
$foreignKey = new ForeignKey(
163-
fqnName: new Fqn((string) $row->name, (string) $row->schema),
163+
fqnName: new Fqn((string) $row->schema, (string) $row->name),
164164
column: (string) $row->column,
165-
refTable: new Fqn((string) $row->ref_table, (string) $row->ref_table_schema),
165+
refTable: new Fqn((string) $row->ref_table_schema, (string) $row->ref_table),
166166
refColumn: (string) $row->ref_column,
167167
);
168168
$keys[$foreignKey->column] = $foreignKey;

src/Platforms/SqlServerPlatform.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function getTables(?string $schema = null): array
5353
$tables = [];
5454
foreach ($result as $row) {
5555
$table = new Table(
56-
fqnName: new Fqn((string) $row->TABLE_NAME, (string) $row->TABLE_SCHEMA),
56+
fqnName: new Fqn((string) $row->TABLE_SCHEMA, (string) $row->TABLE_NAME),
5757
isView: $row->TABLE_TYPE === 'VIEW',
5858
);
5959
$tables[$table->fqnName->getUnescaped()] = $table;
@@ -157,9 +157,9 @@ public function getForeignKeys(string $table, ?string $schema = null): array
157157
$keys = [];
158158
foreach ($result as $row) {
159159
$foreignKey = new ForeignKey(
160-
fqnName: new Fqn((string) $row->name, (string) $row->schema),
160+
fqnName: new Fqn((string) $row->schema, (string) $row->name),
161161
column: (string) $row->column,
162-
refTable: new Fqn((string) $row->ref_table, (string) $row->ref_table_schema),
162+
refTable: new Fqn((string) $row->ref_table_schema, (string) $row->ref_table),
163163
refColumn: (string) $row->ref_column,
164164
);
165165
$keys[$foreignKey->column] = $foreignKey;

src/SqlProcessor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ public function processModifier(string $type, mixed $value): string
297297

298298
} elseif ($value instanceof Fqn) {
299299
switch ($type) {
300+
case 'column':
300301
case 'table':
301302
$schema = $this->identifierToSql($value->schema);
302303
$table = $this->identifierToSql($value->name);

tests/cases/integration/connection.postgres.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testLastInsertId()
3333

3434
$this->connection->query('INSERT INTO publishers %values', ['name' => 'FOO']);
3535
Assert::same(2, $this->connection->getLastInsertedId('publishers_id_seq'));
36-
Assert::same(2, $this->connection->getLastInsertedId(new Fqn(name: 'publishers_id_seq', schema: 'public')));
36+
Assert::same(2, $this->connection->getLastInsertedId(new Fqn(schema: 'public', name: 'publishers_id_seq')));
3737

3838
Assert::exception(function() {
3939
$this->connection->getLastInsertedId();

tests/cases/integration/platform.mysql.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,27 +199,27 @@ public function testForeignKeys()
199199

200200
Assert::equal([
201201
'author_id' => [
202-
'fqnName' => new Fqn('books_authors', $dbName),
202+
'fqnName' => new Fqn($dbName, 'books_authors'),
203203
'column' => 'author_id',
204-
'refTable' => new Fqn('authors', $dbName . '2'),
204+
'refTable' => new Fqn($dbName . '2', 'authors'),
205205
'refColumn' => 'id',
206206
],
207207
'ean_id' => [
208-
'fqnName' => new Fqn('books_ean', $dbName),
208+
'fqnName' => new Fqn($dbName, 'books_ean'),
209209
'column' => 'ean_id',
210-
'refTable' => new Fqn('eans', $dbName),
210+
'refTable' => new Fqn($dbName, 'eans'),
211211
'refColumn' => 'id',
212212
],
213213
'publisher_id' => [
214-
'fqnName' => new Fqn('books_publisher', $dbName),
214+
'fqnName' => new Fqn($dbName, 'books_publisher'),
215215
'column' => 'publisher_id',
216-
'refTable' => new Fqn('publishers', $dbName),
216+
'refTable' => new Fqn($dbName, 'publishers'),
217217
'refColumn' => 'id',
218218
],
219219
'translator_id' => [
220-
'fqnName' => new Fqn('books_translator', $dbName),
220+
'fqnName' => new Fqn($dbName, 'books_translator'),
221221
'column' => 'translator_id',
222-
'refTable' => new Fqn('authors', $dbName . '2'),
222+
'refTable' => new Fqn($dbName . '2', 'authors'),
223223
'refColumn' => 'id',
224224
],
225225
], $keys);
@@ -239,9 +239,9 @@ public function testForeignKeys()
239239
}, $schemaKeys);
240240
Assert::equal([
241241
'book_id' => [
242-
'fqnName' => new Fqn('book_id', $dbName2),
242+
'fqnName' => new Fqn($dbName2, 'book_id'),
243243
'column' => 'book_id',
244-
'refTable' => new Fqn('books', $dbName),
244+
'refTable' => new Fqn($dbName, 'books'),
245245
'refColumn' => 'id',
246246
],
247247
], $schemaKeys);

tests/cases/integration/platform.postgres.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,27 +168,27 @@ public function testForeignKeys()
168168

169169
Assert::equal([
170170
'author_id' => [
171-
'fqnName' => new Fqn('books_authors', 'public'),
171+
'fqnName' => new Fqn('public', 'books_authors'),
172172
'column' => 'author_id',
173-
'refTable' => new Fqn('authors', 'second_schema'),
173+
'refTable' => new Fqn('second_schema', 'authors'),
174174
'refColumn' => 'id',
175175
],
176176
'translator_id' => [
177-
'fqnName' => new Fqn('books_translator', 'public'),
177+
'fqnName' => new Fqn('public', 'books_translator'),
178178
'column' => 'translator_id',
179-
'refTable' => new Fqn('authors', 'second_schema'),
179+
'refTable' => new Fqn('second_schema', 'authors'),
180180
'refColumn' => 'id',
181181
],
182182
'publisher_id' => [
183-
'fqnName' => new Fqn('books_publisher', 'public'),
183+
'fqnName' => new Fqn('public', 'books_publisher'),
184184
'column' => 'publisher_id',
185-
'refTable' => new Fqn('publishers', 'public'),
185+
'refTable' => new Fqn('public', 'publishers'),
186186
'refColumn' => 'id',
187187
],
188188
'ean_id' => [
189-
'fqnName' => new Fqn('books_ean', 'public'),
189+
'fqnName' => new Fqn('public', 'books_ean'),
190190
'column' => 'ean_id',
191-
'refTable' => new Fqn('eans', 'public'),
191+
'refTable' => new Fqn('public', 'eans'),
192192
'refColumn' => 'id',
193193
],
194194
], $keys);
@@ -206,9 +206,9 @@ public function testForeignKeys()
206206

207207
Assert::equal([
208208
'book_id' => [
209-
'fqnName' => new Fqn('book_id', 'second_schema'),
209+
'fqnName' => new Fqn('second_schema', 'book_id'),
210210
'column' => 'book_id',
211-
'refTable' => new Fqn('books', 'public'),
211+
'refTable' => new Fqn('public', 'books'),
212212
'refColumn' => 'id',
213213
],
214214
], $schemaKeys);

tests/cases/integration/platform.sqlserver.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,27 +170,27 @@ public function testForeignKeys()
170170

171171
Assert::equal([
172172
'author_id' => [
173-
'fqnName' => new Fqn('books_authors', 'dbo'),
173+
'fqnName' => new Fqn('dbo', 'books_authors'),
174174
'column' => 'author_id',
175-
'refTable' => new Fqn('authors', 'second_schema'),
175+
'refTable' => new Fqn('second_schema', 'authors'),
176176
'refColumn' => 'id',
177177
],
178178
'ean_id' => [
179-
'fqnName' => new Fqn('books_ean', 'dbo'),
179+
'fqnName' => new Fqn('dbo', 'books_ean'),
180180
'column' => 'ean_id',
181-
'refTable' => new Fqn('eans', 'dbo'),
181+
'refTable' => new Fqn('dbo', 'eans'),
182182
'refColumn' => 'id',
183183
],
184184
'publisher_id' => [
185-
'fqnName' => new Fqn('books_publisher', 'dbo'),
185+
'fqnName' => new Fqn('dbo', 'books_publisher'),
186186
'column' => 'publisher_id',
187-
'refTable' => new Fqn('publishers', 'dbo'),
187+
'refTable' => new Fqn('dbo', 'publishers'),
188188
'refColumn' => 'id',
189189
],
190190
'translator_id' => [
191-
'fqnName' => new Fqn('books_translator', 'dbo'),
191+
'fqnName' => new Fqn('dbo', 'books_translator'),
192192
'column' => 'translator_id',
193-
'refTable' => new Fqn('authors', 'second_schema'),
193+
'refTable' => new Fqn('second_schema', 'authors'),
194194
'refColumn' => 'id',
195195
],
196196
], $keys);
@@ -208,9 +208,9 @@ public function testForeignKeys()
208208

209209
Assert::equal([
210210
'book_id' => [
211-
'fqnName' => new Fqn('book_id', 'second_schema'),
211+
'fqnName' => new Fqn('second_schema', 'book_id'),
212212
'column' => 'book_id',
213-
'refTable' => new Fqn('books', 'dbo'),
213+
'refTable' => new Fqn('dbo', 'books'),
214214
'refColumn' => 'id',
215215
],
216216
], $schemaKeys);

tests/cases/unit/SqlProcessorTest.identifiers.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ public function testFqn()
5353

5454
Assert::same(
5555
'`a`.`b`',
56-
$this->parser->process(['%table', new Fqn('b', schema: 'a')]),
56+
$this->parser->process(['%table', new Fqn(schema: 'a', name: 'b')]),
57+
);
58+
59+
Assert::same(
60+
'`a`.`b`',
61+
$this->parser->process(['%column', new Fqn(schema: 'a', name: 'b')]),
5762
);
5863
}
5964

0 commit comments

Comments
 (0)