Skip to content

Commit 85a568b

Browse files
committed
Improve database compatibility.
Fix syntax incompatibilities found for PostgreSQL.
1 parent 554aae4 commit 85a568b

File tree

2 files changed

+72
-72
lines changed

2 files changed

+72
-72
lines changed

components/Flute/src/Adapters/ModelQueryBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ public function applySorts(string $tableOrAlias, iterable $sorts): self
560560
*/
561561
public function buildColumnName(string $table, string $column): string
562562
{
563-
return "`$table`.`$column`";
563+
return $this->quoteTableName($table) . '.' . $this->quoteColumnName($column);
564564
}
565565

566566
/**
@@ -702,7 +702,7 @@ private function createAlias(string $tableName): string
702702
*/
703703
private function quoteTableName(string $tableName): string
704704
{
705-
return "`$tableName`";
705+
return $this->getConnection()->quoteIdentifier($tableName);
706706
}
707707

708708
/**
@@ -712,7 +712,7 @@ private function quoteTableName(string $tableName): string
712712
*/
713713
private function quoteColumnName(string $columnName): string
714714
{
715-
return "`$columnName`";
715+
return $this->getConnection()->quoteIdentifier($columnName);
716716
}
717717

718718
/**

components/Flute/tests/Adapters/ModelQueryBuilderTest.php

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ public function testAddBelongsToRelationshipAndFilterWithSort(): void
7575
->addRelationshipFiltersAndSortsWithAnd(Post::REL_USER, $filters, $sorts);
7676

7777
$expected =
78-
'SELECT `posts1`.`id_post`, `posts1`.`id_board_fk`, `posts1`.`id_user_fk`, `posts1`.`id_editor_fk`, ' .
79-
'`posts1`.`title`, `posts1`.`text`, `posts1`.`created_at`, `posts1`.`updated_at`, `posts1`.`deleted_at` ' .
80-
'FROM `posts` `posts1` ' .
81-
'INNER JOIN `users` `users2` ON `posts1`.`id_user_fk`=`users2`.`id_user` ' .
82-
'WHERE `users2`.`id_user` = :dcValue1 ' .
83-
'ORDER BY `users2`.`first_name` DESC';
78+
'SELECT "posts1"."id_post", "posts1"."id_board_fk", "posts1"."id_user_fk", "posts1"."id_editor_fk", ' .
79+
'"posts1"."title", "posts1"."text", "posts1"."created_at", "posts1"."updated_at", "posts1"."deleted_at" ' .
80+
'FROM "posts" "posts1" ' .
81+
'INNER JOIN "users" "users2" ON "posts1"."id_user_fk"="users2"."id_user" ' .
82+
'WHERE "users2"."id_user" = :dcValue1 ' .
83+
'ORDER BY "users2"."first_name" DESC';
8484
$this->assertEquals($expected, $builder->getSQL());
8585

8686
$this->migrateDatabase($this->connection);
@@ -110,11 +110,11 @@ public function testAddBelongsToRelationshipFilterOnly(): void
110110
->addRelationshipFiltersAndSortsWithAnd(Post::REL_USER, $filters, $sorts);
111111

112112
$expected =
113-
'SELECT `posts1`.`id_post`, `posts1`.`id_board_fk`, `posts1`.`id_user_fk`, `posts1`.`id_editor_fk`, ' .
114-
'`posts1`.`title`, `posts1`.`text`, `posts1`.`created_at`, `posts1`.`updated_at`, `posts1`.`deleted_at` ' .
115-
'FROM `posts` `posts1` ' .
116-
'INNER JOIN `users` `users2` ON `posts1`.`id_user_fk`=`users2`.`id_user` ' .
117-
'WHERE `users2`.`id_user` = :dcValue1';
113+
'SELECT "posts1"."id_post", "posts1"."id_board_fk", "posts1"."id_user_fk", "posts1"."id_editor_fk", ' .
114+
'"posts1"."title", "posts1"."text", "posts1"."created_at", "posts1"."updated_at", "posts1"."deleted_at" ' .
115+
'FROM "posts" "posts1" ' .
116+
'INNER JOIN "users" "users2" ON "posts1"."id_user_fk"="users2"."id_user" ' .
117+
'WHERE "users2"."id_user" = :dcValue1';
118118
$this->assertEquals($expected, $builder->getSQL());
119119

120120
$this->migrateDatabase($this->connection);
@@ -143,11 +143,11 @@ public function testAddBelongsToRelationshipSortOnly(): void
143143
->addRelationshipFiltersAndSortsWithAnd(Post::REL_USER, $filters, $sorts);
144144

145145
$expected =
146-
'SELECT `posts1`.`id_post`, `posts1`.`id_board_fk`, `posts1`.`id_user_fk`, `posts1`.`id_editor_fk`, ' .
147-
'`posts1`.`title`, `posts1`.`text`, `posts1`.`created_at`, `posts1`.`updated_at`, `posts1`.`deleted_at` ' .
148-
'FROM `posts` `posts1` ' .
149-
'INNER JOIN `users` `users2` ON `posts1`.`id_user_fk`=`users2`.`id_user` ' .
150-
'ORDER BY `users2`.`first_name` DESC';
146+
'SELECT "posts1"."id_post", "posts1"."id_board_fk", "posts1"."id_user_fk", "posts1"."id_editor_fk", ' .
147+
'"posts1"."title", "posts1"."text", "posts1"."created_at", "posts1"."updated_at", "posts1"."deleted_at" ' .
148+
'FROM "posts" "posts1" ' .
149+
'INNER JOIN "users" "users2" ON "posts1"."id_user_fk"="users2"."id_user" ' .
150+
'ORDER BY "users2"."first_name" DESC';
151151
$this->assertEquals($expected, $builder->getSQL());
152152

153153
$this->migrateDatabase($this->connection);
@@ -181,12 +181,12 @@ public function testAddHasManyRelationshipAndFilter(): void
181181
->addRelationshipFiltersAndSortsWithAnd(Post::REL_COMMENTS, $filters, $sorts);
182182

183183
$expected =
184-
'SELECT `posts1`.`id_post`, `posts1`.`id_board_fk`, `posts1`.`id_user_fk`, `posts1`.`id_editor_fk`, ' .
185-
'`posts1`.`title`, `posts1`.`text`, `posts1`.`created_at`, `posts1`.`updated_at`, `posts1`.`deleted_at` ' .
186-
'FROM `posts` `posts1` ' .
187-
'INNER JOIN `comments` `comments2` ON `posts1`.`id_post`=`comments2`.`id_post_fk` ' .
188-
'WHERE (`comments2`.`id_comment` >= :dcValue1) AND (`comments2`.`id_comment` <= :dcValue2) ' .
189-
'ORDER BY `comments2`.`text` ASC';
184+
'SELECT "posts1"."id_post", "posts1"."id_board_fk", "posts1"."id_user_fk", "posts1"."id_editor_fk", ' .
185+
'"posts1"."title", "posts1"."text", "posts1"."created_at", "posts1"."updated_at", "posts1"."deleted_at" ' .
186+
'FROM "posts" "posts1" ' .
187+
'INNER JOIN "comments" "comments2" ON "posts1"."id_post"="comments2"."id_post_fk" ' .
188+
'WHERE ("comments2"."id_comment" >= :dcValue1) AND ("comments2"."id_comment" <= :dcValue2) ' .
189+
'ORDER BY "comments2"."text" ASC';
190190
$this->assertEquals($expected, $builder->getSQL());
191191

192192
$this->migrateDatabase($this->connection);
@@ -220,12 +220,12 @@ public function testAddHasManyRelationshipOrFilter(): void
220220
->addRelationshipFiltersAndSortsWithOr(Post::REL_COMMENTS, $filters, $sorts);
221221

222222
$expected =
223-
'SELECT `posts1`.`id_post`, `posts1`.`id_board_fk`, `posts1`.`id_user_fk`, `posts1`.`id_editor_fk`, ' .
224-
'`posts1`.`title`, `posts1`.`text`, `posts1`.`created_at`, `posts1`.`updated_at`, `posts1`.`deleted_at` ' .
225-
'FROM `posts` `posts1` ' .
226-
'INNER JOIN `comments` `comments2` ON `posts1`.`id_post`=`comments2`.`id_post_fk` ' .
227-
'WHERE (`comments2`.`id_comment` >= :dcValue1) OR (`comments2`.`id_comment` <= :dcValue2) ' .
228-
'ORDER BY `comments2`.`text` ASC';
223+
'SELECT "posts1"."id_post", "posts1"."id_board_fk", "posts1"."id_user_fk", "posts1"."id_editor_fk", ' .
224+
'"posts1"."title", "posts1"."text", "posts1"."created_at", "posts1"."updated_at", "posts1"."deleted_at" ' .
225+
'FROM "posts" "posts1" ' .
226+
'INNER JOIN "comments" "comments2" ON "posts1"."id_post"="comments2"."id_post_fk" ' .
227+
'WHERE ("comments2"."id_comment" >= :dcValue1) OR ("comments2"."id_comment" <= :dcValue2) ' .
228+
'ORDER BY "comments2"."text" ASC';
229229
$this->assertEquals($expected, $builder->getSQL());
230230

231231
$this->migrateDatabase($this->connection);
@@ -259,16 +259,16 @@ public function testAddBelongsToManyRelationshipFilter(): void
259259
->addRelationshipFiltersAndSortsWithAnd(Comment::REL_EMOTIONS, $filters, $sorts);
260260

261261
$expected =
262-
'SELECT `comments1`.`id_comment`, `comments1`.`id_post_fk`, `comments1`.`id_user_fk`, ' .
263-
'`comments1`.`text`, `comments1`.`int_value`, `comments1`.`float_value`, `comments1`.`bool_value`, ' .
264-
'`comments1`.`datetime_value`, `comments1`.`created_at`, `comments1`.`updated_at`, ' .
265-
'`comments1`.`deleted_at` ' .
266-
'FROM `comments` `comments1` ' .
267-
'INNER JOIN `comments_emotions` `comments_emotions2` ON ' .
268-
'`comments1`.`id_comment`=`comments_emotions2`.`id_comment_fk` ' .
269-
'INNER JOIN `emotions` `emotions3` ON `comments_emotions2`.`id_emotion_fk`=`emotions3`.`id_emotion` ' .
270-
'WHERE `emotions3`.`id_emotion` = :dcValue1 ' .
271-
'ORDER BY `emotions3`.`name` ASC, `emotions3`.`id_emotion` DESC';
262+
'SELECT "comments1"."id_comment", "comments1"."id_post_fk", "comments1"."id_user_fk", ' .
263+
'"comments1"."text", "comments1"."int_value", "comments1"."float_value", "comments1"."bool_value", ' .
264+
'"comments1"."datetime_value", "comments1"."created_at", "comments1"."updated_at", ' .
265+
'"comments1"."deleted_at" ' .
266+
'FROM "comments" "comments1" ' .
267+
'INNER JOIN "comments_emotions" "comments_emotions2" ON ' .
268+
'"comments1"."id_comment"="comments_emotions2"."id_comment_fk" ' .
269+
'INNER JOIN "emotions" "emotions3" ON "comments_emotions2"."id_emotion_fk"="emotions3"."id_emotion" ' .
270+
'WHERE "emotions3"."id_emotion" = :dcValue1 ' .
271+
'ORDER BY "emotions3"."name" ASC, "emotions3"."id_emotion" DESC';
272272
$this->assertEquals($expected, $builder->getSQL());
273273

274274
$this->migrateDatabase($this->connection);
@@ -300,11 +300,11 @@ public function testRead(): void
300300
->addFiltersWithAndToAlias($filters)
301301
->addSorts($sorts);
302302
$expected =
303-
'SELECT `boards1`.`id_board`, `boards1`.`title`, `boards1`.`created_at`, ' .
304-
'`boards1`.`updated_at`, `boards1`.`deleted_at` ' .
305-
'FROM `boards` `boards1` ' .
306-
'WHERE `boards1`.`created_at` = :dcValue1 ' .
307-
'ORDER BY `boards1`.`title` ASC';
303+
'SELECT "boards1"."id_board", "boards1"."title", "boards1"."created_at", ' .
304+
'"boards1"."updated_at", "boards1"."deleted_at" ' .
305+
'FROM "boards" "boards1" ' .
306+
'WHERE "boards1"."created_at" = :dcValue1 ' .
307+
'ORDER BY "boards1"."title" ASC';
308308

309309
$this->assertEquals($expected, $builder->getSQL());
310310
}
@@ -322,9 +322,9 @@ public function testIndex(): void
322322
->selectModelColumns()
323323
->fromModelTable();
324324
$expected =
325-
'SELECT `boards1`.`id_board`, `boards1`.`title`, `boards1`.`created_at`, ' .
326-
'`boards1`.`updated_at`, `boards1`.`deleted_at` ' .
327-
'FROM `boards` `boards1`';
325+
'SELECT "boards1"."id_board", "boards1"."title", "boards1"."created_at", ' .
326+
'"boards1"."updated_at", "boards1"."deleted_at" ' .
327+
'FROM "boards" "boards1"';
328328

329329
$this->assertEquals($expected, $builder->getSQL());
330330
}
@@ -361,22 +361,22 @@ public function testIndexWithFilters(): void
361361
->addFiltersWithAndToAlias($filters);
362362

363363
$expected =
364-
'SELECT `boards1`.`id_board`, `boards1`.`title`, `boards1`.`created_at`, ' .
365-
'`boards1`.`updated_at`, `boards1`.`deleted_at` ' .
366-
'FROM `boards` `boards1` ' .
364+
'SELECT "boards1"."id_board", "boards1"."title", "boards1"."created_at", ' .
365+
'"boards1"."updated_at", "boards1"."deleted_at" ' .
366+
'FROM "boards" "boards1" ' .
367367
'WHERE ' .
368-
'(`boards1`.`title` = :dcValue1) AND ' .
369-
'(`boards1`.`title` <> :dcValue2) AND ' .
370-
'(`boards1`.`title` < :dcValue3) AND ' .
371-
'(`boards1`.`title` <= :dcValue4) AND ' .
372-
'(`boards1`.`title` > :dcValue5) AND ' .
373-
'(`boards1`.`title` >= :dcValue6) AND ' .
374-
'(`boards1`.`title` LIKE :dcValue7) AND ' .
375-
'(`boards1`.`title` NOT LIKE :dcValue8) AND ' .
376-
'(`boards1`.`title` IN (:dcValue9)) AND ' .
377-
'(`boards1`.`title` NOT IN (:dcValue10, :dcValue11)) AND ' .
378-
'(`boards1`.`title` IS NULL) AND ' .
379-
'(`boards1`.`title` IS NOT NULL)';
368+
'("boards1"."title" = :dcValue1) AND ' .
369+
'("boards1"."title" <> :dcValue2) AND ' .
370+
'("boards1"."title" < :dcValue3) AND ' .
371+
'("boards1"."title" <= :dcValue4) AND ' .
372+
'("boards1"."title" > :dcValue5) AND ' .
373+
'("boards1"."title" >= :dcValue6) AND ' .
374+
'("boards1"."title" LIKE :dcValue7) AND ' .
375+
'("boards1"."title" NOT LIKE :dcValue8) AND ' .
376+
'("boards1"."title" IN (:dcValue9)) AND ' .
377+
'("boards1"."title" NOT IN (:dcValue10, :dcValue11)) AND ' .
378+
'("boards1"."title" IS NULL) AND ' .
379+
'("boards1"."title" IS NOT NULL)';
380380

381381
$this->assertEquals($expected, $builder->getSQL());
382382
$this->assertEquals([
@@ -416,10 +416,10 @@ public function testIndexWithFiltersJoinedWithOR(): void
416416
->addFiltersWithOrToAlias($filters);
417417

418418
$expected =
419-
'SELECT `boards1`.`id_board`, `boards1`.`title`, `boards1`.`created_at`, ' .
420-
'`boards1`.`updated_at`, `boards1`.`deleted_at` ' .
421-
'FROM `boards` `boards1` ' .
422-
'WHERE (`boards1`.`title` = :dcValue1) OR (`boards1`.`title` <= :dcValue2)';
419+
'SELECT "boards1"."id_board", "boards1"."title", "boards1"."created_at", ' .
420+
'"boards1"."updated_at", "boards1"."deleted_at" ' .
421+
'FROM "boards" "boards1" ' .
422+
'WHERE ("boards1"."title" = :dcValue1) OR ("boards1"."title" <= :dcValue2)';
423423

424424
$this->assertEquals($expected, $builder->getSQL());
425425
$this->assertEquals([
@@ -474,8 +474,8 @@ public function testUpdateWithOrCondition(): void
474474
]);
475475

476476
$expected =
477-
'UPDATE `boards` SET `title` = :dcValue3 ' .
478-
'WHERE (`boards`.`id_board` >= :dcValue1) OR (`boards`.`id_board` <= :dcValue2)';
477+
'UPDATE "boards" SET "title" = :dcValue3 ' .
478+
'WHERE ("boards"."id_board" >= :dcValue1) OR ("boards"."id_board" <= :dcValue2)';
479479

480480
$this->assertEquals($expected, $builder->getSQL());
481481
$this->assertEquals([
@@ -494,8 +494,8 @@ public function testBuildingQuotedColumns(): void
494494
{
495495
$builder = $this->createModelQueryBuilder(Board::class);
496496

497-
$this->assertEquals('`boards`.`title`', $builder->getQuotedMainTableColumn(Board::FIELD_TITLE));
498-
$this->assertEquals('`boards1`.`title`', $builder->getQuotedMainAliasColumn(Board::FIELD_TITLE));
497+
$this->assertEquals('"boards"."title"', $builder->getQuotedMainTableColumn(Board::FIELD_TITLE));
498+
$this->assertEquals('"boards1"."title"', $builder->getQuotedMainAliasColumn(Board::FIELD_TITLE));
499499
}
500500

501501
/**

0 commit comments

Comments
 (0)