Skip to content

Commit c4d02a4

Browse files
committed
wip
1 parent 7a0de89 commit c4d02a4

File tree

8 files changed

+56
-40
lines changed

8 files changed

+56
-40
lines changed

packages/database/src/QueryStatements/FieldStatement.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,17 @@ public function compile(DatabaseDialect $dialect): string
4747
$field = arr(explode('.', $field))
4848
->map(fn (string $part) => trim($part, '` '))
4949
->map(
50-
fn (string $part) => match ($dialect) {
51-
DatabaseDialect::SQLITE => $part,
52-
default => sprintf('`%s`', $part),
50+
function (string $part) use ($dialect) {
51+
// Function calls are never wrapped in backticks.
52+
if (str_contains($part, '(')) {
53+
return $part;
54+
}
55+
56+
if ($dialect === DatabaseDialect::SQLITE) {
57+
return $part;
58+
}
59+
60+
return sprintf('`%s`', $part);
5361
},
5462
)
5563
->implode('.');

packages/database/tests/QueryStatements/AlterTableStatementTest.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function test_alter_for_only_indexes(DatabaseDialect $dialect): void
3838
#[TestWith([DatabaseDialect::SQLITE])]
3939
public function test_alter_add_column(DatabaseDialect $dialect): void
4040
{
41-
$expected = 'ALTER TABLE `table` ADD `bar` VARCHAR(42) DEFAULT "xx" ;';
41+
$expected = 'ALTER TABLE `table` ADD `bar` VARCHAR(42) DEFAULT \'xx\' ;';
4242
$statement = new AlterTableStatement('table')
4343
->add(new VarcharStatement('bar', 42, true, 'xx'))
4444
->compile($dialect);
@@ -49,8 +49,7 @@ public function test_alter_add_column(DatabaseDialect $dialect): void
4949
}
5050

5151
#[TestWith([DatabaseDialect::MYSQL])]
52-
#[TestWith([DatabaseDialect::POSTGRESQL])]
53-
public function test_alter_add_belongs_to(DatabaseDialect $dialect): void
52+
public function test_alter_add_belongs_to_mysql(DatabaseDialect $dialect): void
5453
{
5554
$expected = 'ALTER TABLE `table` ADD CONSTRAINT `fk_parent_table_foo` FOREIGN KEY table(foo) REFERENCES parent(bar) ON DELETE RESTRICT ON UPDATE NO ACTION ;';
5655
$statement = new AlterTableStatement('table')
@@ -62,6 +61,19 @@ public function test_alter_add_belongs_to(DatabaseDialect $dialect): void
6261
$this->assertEqualsIgnoringCase($expected, $normalized);
6362
}
6463

64+
#[TestWith([DatabaseDialect::POSTGRESQL])]
65+
public function test_alter_add_belongs_to_postgresql(DatabaseDialect $dialect): void
66+
{
67+
$expected = 'ALTER TABLE `table` ADD CONSTRAINT `fk_parent_table_foo` FOREIGN KEY(foo) REFERENCES parent(bar) ON DELETE RESTRICT ON UPDATE NO ACTION ;';
68+
$statement = new AlterTableStatement('table')
69+
->add(new BelongsToStatement('table.foo', 'parent.bar'))
70+
->compile($dialect);
71+
72+
$normalized = self::removeDuplicateWhitespace($statement);
73+
74+
$this->assertEqualsIgnoringCase($expected, $normalized);
75+
}
76+
6577
#[TestWith([DatabaseDialect::SQLITE])]
6678
public function test_alter_add_belongs_to_unsupported(DatabaseDialect $dialect): void
6779
{
@@ -109,12 +121,12 @@ public function test_alter_table_drop_constraint_unsupported_dialects(DatabaseDi
109121
->compile($dialect);
110122
}
111123

112-
#[TestWith([DatabaseDialect::MYSQL, 'ALTER TABLE `table` ADD `foo` VARCHAR(42) DEFAULT "bar" NOT NULL ;'])]
124+
#[TestWith([DatabaseDialect::MYSQL, 'ALTER TABLE `table` ADD `foo` VARCHAR(42) DEFAULT \'bar\' NOT NULL ;'])]
113125
#[TestWith([
114126
DatabaseDialect::POSTGRESQL,
115-
'ALTER TABLE `table` ADD `foo` VARCHAR(42) DEFAULT "bar" NOT NULL ;',
127+
'ALTER TABLE `table` ADD `foo` VARCHAR(42) DEFAULT \'bar\' NOT NULL ;',
116128
])]
117-
#[TestWith([DatabaseDialect::SQLITE, 'ALTER TABLE `table` ADD `foo` VARCHAR(42) DEFAULT "bar" NOT NULL ;'])]
129+
#[TestWith([DatabaseDialect::SQLITE, 'ALTER TABLE `table` ADD `foo` VARCHAR(42) DEFAULT \'bar\' NOT NULL ;'])]
118130
public function test_alter_table_add_column(DatabaseDialect $dialect, string $expected): void
119131
{
120132
$statement = new AlterTableStatement('table')
@@ -141,8 +153,8 @@ public function test_alter_table_rename_column(DatabaseDialect $dialect): void
141153
$this->assertEqualsIgnoringCase($expected, $normalized);
142154
}
143155

144-
#[TestWith([DatabaseDialect::MYSQL, 'ALTER TABLE `table` MODIFY COLUMN `foo` VARCHAR(42) DEFAULT "bar" NOT NULL ;'])]
145-
#[TestWith([DatabaseDialect::POSTGRESQL, 'ALTER TABLE `table` ALTER COLUMN `foo` VARCHAR(42) DEFAULT "bar" NOT NULL ;'])]
156+
#[TestWith([DatabaseDialect::MYSQL, 'ALTER TABLE `table` MODIFY COLUMN `foo` VARCHAR(42) DEFAULT \'bar\' NOT NULL ;'])]
157+
#[TestWith([DatabaseDialect::POSTGRESQL, 'ALTER TABLE `table` ALTER COLUMN `foo` VARCHAR(42) DEFAULT \'bar\' NOT NULL ;'])]
146158
public function test_alter_table_modify_column(DatabaseDialect $dialect, string $expected): void
147159
{
148160
$statement = new AlterTableStatement('table')

packages/database/tests/QueryStatements/CountStatementTest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ public function test_count_statement(): void
1919
);
2020

2121
$expected = <<<SQL
22-
SELECT COUNT(*)
22+
SELECT COUNT(*) AS `count`
2323
FROM `foo`
2424
SQL;
2525

2626
$this->assertSame($expected, $statement->compile(DatabaseDialect::MYSQL));
27-
$this->assertSame($expected, $statement->compile(DatabaseDialect::POSTGRESQL));
28-
$this->assertSame($expected, $statement->compile(DatabaseDialect::SQLITE));
2927
}
3028

3129
public function test_count_statement_with_specified_column(): void
@@ -38,13 +36,11 @@ public function test_count_statement_with_specified_column(): void
3836
);
3937

4038
$expected = <<<SQL
41-
SELECT COUNT(`foobar`)
39+
SELECT COUNT(`foobar`) AS `count`
4240
FROM `foo`
4341
SQL;
4442

4543
$this->assertSame($expected, $statement->compile(DatabaseDialect::MYSQL));
46-
$this->assertSame($expected, $statement->compile(DatabaseDialect::POSTGRESQL));
47-
$this->assertSame($expected, $statement->compile(DatabaseDialect::SQLITE));
4844
}
4945

5046
public function test_count_statement_with_distinct_specified_column(): void
@@ -59,12 +55,10 @@ public function test_count_statement_with_distinct_specified_column(): void
5955
$statement->distinct = true;
6056

6157
$expected = <<<SQL
62-
SELECT COUNT(DISTINCT `foobar`)
58+
SELECT COUNT(DISTINCT `foobar`) AS `count`
6359
FROM `foo`
6460
SQL;
6561

6662
$this->assertSame($expected, $statement->compile(DatabaseDialect::MYSQL));
67-
$this->assertSame($expected, $statement->compile(DatabaseDialect::POSTGRESQL));
68-
$this->assertSame($expected, $statement->compile(DatabaseDialect::SQLITE));
6963
}
7064
}

packages/database/tests/QueryStatements/CreateTableStatementTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static function provide_fk_create_table_database_drivers(): Generator
8686
'CREATE TABLE `books` (
8787
`id` SERIAL PRIMARY KEY,
8888
`author_id` INTEGER NOT NULL,
89-
CONSTRAINT `fk_authors_books_author_id` FOREIGN KEY books(author_id) REFERENCES authors(id) ON DELETE CASCADE ON UPDATE NO ACTION,
89+
CONSTRAINT `fk_authors_books_author_id` FOREIGN KEY(author_id) REFERENCES authors(id) ON DELETE CASCADE ON UPDATE NO ACTION,
9090
`name` VARCHAR(255) NOT NULL
9191
);',
9292
];

packages/database/tests/QueryStatements/FieldStatementTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public function test_sqlite(): void
1919
'table.field',
2020
new FieldStatement('`table`.`field`')->compile(DatabaseDialect::SQLITE),
2121
);
22+
23+
$this->assertSame(
24+
'COUNT(*) AS `count`',
25+
new FieldStatement('COUNT(*) AS count')->compile(DatabaseDialect::MYSQL),
26+
);
2227
}
2328

2429
public function test_mysql(): void
@@ -32,6 +37,11 @@ public function test_mysql(): void
3237
'`table`.`field`',
3338
new FieldStatement('table.field')->compile(DatabaseDialect::MYSQL),
3439
);
40+
41+
$this->assertSame(
42+
'COUNT(*) AS `count`',
43+
new FieldStatement('COUNT(*) AS count')->compile(DatabaseDialect::MYSQL),
44+
);
3545
}
3646

3747
public function test_postgres(): void

packages/database/tests/QueryStatements/InsertStatementTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ public function test_insert_statement(): void
2828

2929
$this->assertSame($expected, $statement->compile(DatabaseDialect::MYSQL));
3030
$this->assertSame($expected, $statement->compile(DatabaseDialect::SQLITE));
31-
$this->assertSame($expected, $statement->compile(DatabaseDialect::POSTGRESQL));
31+
32+
$expectedPostgres = <<<PSQL
33+
INSERT INTO `foo` AS `bar` (`foo`, `bar`)
34+
VALUES (?, ?), (?, ?) RETURNING *
35+
PSQL;
36+
37+
$this->assertSame($expectedPostgres, $statement->compile(DatabaseDialect::POSTGRESQL));
3238
}
3339

3440
public function test_exception_on_column_mismatch(): void

packages/database/tests/QueryStatements/SelectStatementTest.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function test_select(): void
3333
offset: 100,
3434
);
3535

36-
$expectedWithBackticks = <<<SQL
36+
$expectedMysql = <<<SQL
3737
SELECT `a`, `b`, `c`, `bar`.`d` AS `d_alias`
3838
FROM `foo` AS `bar`
3939
INNER JOIN foo ON bar.id = foo.id
@@ -45,21 +45,6 @@ public function test_select(): void
4545
OFFSET 100
4646
SQL;
4747

48-
$this->assertSame($expectedWithBackticks, $statement->compile(DatabaseDialect::MYSQL));
49-
$this->assertSame($expectedWithBackticks, $statement->compile(DatabaseDialect::POSTGRESQL));
50-
51-
$expectedWithoutBackticks = <<<SQL
52-
SELECT a, b, c, bar.d AS `d_alias`
53-
FROM `foo` AS `bar`
54-
INNER JOIN foo ON bar.id = foo.id
55-
WHERE `foo` = "bar"
56-
ORDER BY `foo` DESC
57-
GROUP BY `foo`
58-
HAVING `foo` = "bar"
59-
LIMIT 10
60-
OFFSET 100
61-
SQL;
62-
63-
$this->assertSame($expectedWithoutBackticks, $statement->compile(DatabaseDialect::SQLITE));
48+
$this->assertSame($expectedMysql, $statement->compile(DatabaseDialect::MYSQL));
6449
}
6550
}

src/Tempest/Framework/Testing/IntegrationTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class IntegrationTest extends TestCase
3636

3737
protected Kernel $kernel;
3838

39-
protected Container|GenericContainer $container;
39+
protected GenericContainer $container;
4040

4141
protected ConsoleTester $console;
4242

@@ -62,6 +62,7 @@ protected function setUp(): void
6262
discoveryLocations: $this->discoveryLocations,
6363
);
6464

65+
// @phpstan-ignore-next-line
6566
$this->container = $this->kernel->container;
6667

6768
$this->console = $this->container->get(ConsoleTester::class);

0 commit comments

Comments
 (0)