Skip to content

Commit d925101

Browse files
authored
Merge pull request #9 from utopia-php/fix-builder-throws
refactor(schema/builder): no methods that throw — gate dialect features per type
2 parents 34048c2 + 3450ce5 commit d925101

74 files changed

Lines changed: 2153 additions & 1165 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ composer.phar
55
coverage
66
coverage.xml
77
.DS_Store
8-
.claude/worktrees/
8+
.claude/
99
.phpunit.cache/
1010
coverage/

src/Query/Builder.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -390,15 +390,6 @@ private function compileWhenCondition(WhenClause $when): string
390390
}
391391
}
392392

393-
/**
394-
* Build an INSERT ... ON CONFLICT/DUPLICATE KEY UPDATE statement.
395-
* Requires onConflict() to be called first to configure conflict keys and update columns.
396-
*/
397-
public function upsert(): Statement
398-
{
399-
throw new UnsupportedException('UPSERT is not supported by this dialect.');
400-
}
401-
402393
#[\Override]
403394
public function build(): Statement
404395
{
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Utopia\Query\Builder\Feature;
4+
5+
use Utopia\Query\Builder\Statement;
6+
7+
interface InsertOrIgnore
8+
{
9+
public function insertOrIgnore(): Statement;
10+
}

src/Query/Builder/Feature/Upsert.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,4 @@
77
interface Upsert
88
{
99
public function upsert(): Statement;
10-
11-
public function insertOrIgnore(): Statement;
12-
13-
public function upsertSelect(): Statement;
1410
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Utopia\Query\Builder\Feature;
4+
5+
use Utopia\Query\Builder\Statement;
6+
7+
interface UpsertSelect
8+
{
9+
public function upsertSelect(): Statement;
10+
}

src/Query/Builder/MongoDB.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use stdClass;
66
use Utopia\Query\Builder as BaseBuilder;
77
use Utopia\Query\Builder\Feature\FullTextSearch;
8+
use Utopia\Query\Builder\Feature\InsertOrIgnore;
89
use Utopia\Query\Builder\Feature\MongoDB\ArrayPushModifiers;
910
use Utopia\Query\Builder\Feature\MongoDB\AtlasSearch;
1011
use Utopia\Query\Builder\Feature\MongoDB\ConditionalArrayUpdates;
@@ -22,6 +23,7 @@
2223

2324
class MongoDB extends BaseBuilder implements
2425
Upsert,
26+
InsertOrIgnore,
2527
FullTextSearch,
2628
TableSampling,
2729
FieldUpdates,
@@ -358,7 +360,6 @@ public function delete(): Statement
358360
);
359361
}
360362

361-
#[\Override]
362363
public function upsert(): Statement
363364
{
364365
$this->bindings = [];
@@ -436,12 +437,6 @@ public function insertOrIgnore(): Statement
436437
);
437438
}
438439

439-
#[\Override]
440-
public function upsertSelect(): Statement
441-
{
442-
throw new UnsupportedException('upsertSelect() is not supported in MongoDB builder.');
443-
}
444-
445440
private function needsAggregation(ParsedQuery $grouped): bool
446441
{
447442
if (! empty(Query::getByType($this->pendingQueries, [Method::OrderRandom], false))) {

src/Query/Builder/MySQL.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,41 @@
33
namespace Utopia\Query\Builder;
44

55
use Utopia\Query\Builder\Feature\ConditionalAggregates;
6+
use Utopia\Query\Builder\Feature\FullTextSearch;
67
use Utopia\Query\Builder\Feature\GroupByModifiers;
78
use Utopia\Query\Builder\Feature\Hints;
9+
use Utopia\Query\Builder\Feature\InsertOrIgnore;
810
use Utopia\Query\Builder\Feature\Json;
911
use Utopia\Query\Builder\Feature\LateralJoins;
12+
use Utopia\Query\Builder\Feature\Spatial;
1013
use Utopia\Query\Builder\Feature\StringAggregates;
14+
use Utopia\Query\Builder\Feature\Upsert;
15+
use Utopia\Query\Builder\Feature\UpsertSelect;
1116
use Utopia\Query\Exception\ValidationException;
1217
use Utopia\Query\Method;
1318

14-
class MySQL extends SQL implements Json, Hints, ConditionalAggregates, LateralJoins, StringAggregates, GroupByModifiers
19+
class MySQL extends SQL implements
20+
Json,
21+
Hints,
22+
ConditionalAggregates,
23+
LateralJoins,
24+
StringAggregates,
25+
GroupByModifiers,
26+
Spatial,
27+
FullTextSearch,
28+
Upsert,
29+
UpsertSelect,
30+
InsertOrIgnore
1531
{
1632
use Trait\ConditionalAggregates;
33+
use Trait\FullTextSearch;
1734
use Trait\GroupByModifiers;
1835
use Trait\Hints;
1936
use Trait\LateralJoins;
37+
use Trait\Spatial;
2038
use Trait\StringAggregates;
39+
use Trait\Upsert;
40+
use Trait\UpsertSelect;
2141

2242
protected string $updateJoinTable = '';
2343

src/Query/Builder/PostgreSQL.php

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use Utopia\Query\AST\Serializer\PostgreSQL as PostgreSQLSerializer;
77
use Utopia\Query\Builder\Feature\ConditionalAggregates;
88
use Utopia\Query\Builder\Feature\FullOuterJoins;
9+
use Utopia\Query\Builder\Feature\FullTextSearch;
910
use Utopia\Query\Builder\Feature\GroupByModifiers;
11+
use Utopia\Query\Builder\Feature\InsertOrIgnore;
1012
use Utopia\Query\Builder\Feature\Json;
1113
use Utopia\Query\Builder\Feature\LateralJoins;
1214
use Utopia\Query\Builder\Feature\PostgreSQL\AggregateFilter;
@@ -17,8 +19,11 @@
1719
use Utopia\Query\Builder\Feature\PostgreSQL\Returning;
1820
use Utopia\Query\Builder\Feature\PostgreSQL\VectorSearch;
1921
use Utopia\Query\Builder\Feature\Sequences;
22+
use Utopia\Query\Builder\Feature\Spatial;
2023
use Utopia\Query\Builder\Feature\StringAggregates;
2124
use Utopia\Query\Builder\Feature\TableSampling;
25+
use Utopia\Query\Builder\Feature\Upsert;
26+
use Utopia\Query\Builder\Feature\UpsertSelect;
2227
use Utopia\Query\Builder\PostgreSQL\DeleteUsing;
2328
use Utopia\Query\Builder\PostgreSQL\MergeTarget;
2429
use Utopia\Query\Builder\PostgreSQL\UpdateFrom;
@@ -27,9 +32,30 @@
2732
use Utopia\Query\Query;
2833
use Utopia\Query\Schema\ColumnType;
2934

30-
class PostgreSQL extends SQL implements VectorSearch, Json, Returning, LockingOf, ConditionalAggregates, Merge, LateralJoins, TableSampling, FullOuterJoins, StringAggregates, OrderedSetAggregates, DistinctOn, AggregateFilter, GroupByModifiers, Sequences
35+
class PostgreSQL extends SQL implements
36+
VectorSearch,
37+
Json,
38+
Returning,
39+
LockingOf,
40+
ConditionalAggregates,
41+
Merge,
42+
LateralJoins,
43+
TableSampling,
44+
FullOuterJoins,
45+
StringAggregates,
46+
OrderedSetAggregates,
47+
DistinctOn,
48+
AggregateFilter,
49+
GroupByModifiers,
50+
Sequences,
51+
Spatial,
52+
FullTextSearch,
53+
Upsert,
54+
UpsertSelect,
55+
InsertOrIgnore
3156
{
3257
use Trait\FullOuterJoins;
58+
use Trait\FullTextSearch;
3359
use Trait\GroupByModifiers;
3460
use Trait\LateralJoins;
3561
use Trait\PostgreSQL\AggregateFilter;
@@ -40,7 +66,14 @@ class PostgreSQL extends SQL implements VectorSearch, Json, Returning, LockingOf
4066
use Trait\PostgreSQL\Sequences;
4167
use Trait\PostgreSQL\VectorSearch;
4268
use Trait\Returning;
69+
use Trait\Spatial;
4370
use Trait\StringAggregates;
71+
use Trait\Upsert {
72+
upsert as private baseUpsert;
73+
}
74+
use Trait\UpsertSelect {
75+
upsertSelect as private baseUpsertSelect;
76+
}
4477

4578
protected string $wrapChar = '"';
4679

@@ -337,20 +370,14 @@ private function mergeIntoWhereClause(array &$parts, array $extra): void
337370
$parts[] = 'WHERE ' . \implode(' AND ', $extra);
338371
}
339372

340-
#[\Override]
341373
public function upsert(): Statement
342374
{
343-
$result = parent::upsert();
344-
345-
return $this->appendReturning($result);
375+
return $this->appendReturning($this->baseUpsert());
346376
}
347377

348-
#[\Override]
349378
public function upsertSelect(): Statement
350379
{
351-
$result = parent::upsertSelect();
352-
353-
return $this->appendReturning($result);
380+
return $this->appendReturning($this->baseUpsertSelect());
354381
}
355382

356383
#[\Override]

src/Query/Builder/SQL.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,26 @@
44

55
use Utopia\Query\Builder as BaseBuilder;
66
use Utopia\Query\Builder\Feature\BitwiseAggregates;
7-
use Utopia\Query\Builder\Feature\FullTextSearch;
87
use Utopia\Query\Builder\Feature\Locking;
9-
use Utopia\Query\Builder\Feature\Spatial;
108
use Utopia\Query\Builder\Feature\StatisticalAggregates;
119
use Utopia\Query\Builder\Feature\Transactions;
12-
use Utopia\Query\Builder\Feature\Upsert;
1310
use Utopia\Query\Method;
1411
use Utopia\Query\Query;
1512
use Utopia\Query\QuotesIdentifiers;
1613
use Utopia\Query\Schema\ColumnType;
1714

18-
abstract class SQL extends BaseBuilder implements Locking, Transactions, Upsert, Spatial, FullTextSearch, StatisticalAggregates, BitwiseAggregates
15+
abstract class SQL extends BaseBuilder implements Locking, Transactions, StatisticalAggregates, BitwiseAggregates
1916
{
2017
use QuotesIdentifiers;
2118
use Trait\BitwiseAggregates;
22-
use Trait\FullTextSearch;
2319
use Trait\Json;
2420
use Trait\Locking;
25-
use Trait\Spatial;
2621
use Trait\StatisticalAggregates;
2722
use Trait\Transactions;
28-
use Trait\Upsert;
2923

3024
/** @var array<string, Condition> */
3125
protected array $jsonSets = [];
3226

33-
abstract public function insertOrIgnore(): Statement;
34-
3527
abstract protected function compileConflictHeader(): string;
3628

3729
abstract protected function compileConflictAssignment(string $wrapped): string;

src/Query/Builder/SQLite.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
55
use Utopia\Query\AST\Serializer;
66
use Utopia\Query\AST\Serializer\SQLite as SQLiteSerializer;
77
use Utopia\Query\Builder\Feature\ConditionalAggregates;
8+
use Utopia\Query\Builder\Feature\InsertOrIgnore;
89
use Utopia\Query\Builder\Feature\Json;
910
use Utopia\Query\Builder\Feature\StringAggregates;
11+
use Utopia\Query\Builder\Feature\Upsert;
12+
use Utopia\Query\Builder\Feature\UpsertSelect;
1013
use Utopia\Query\Exception\UnsupportedException;
1114
use Utopia\Query\Exception\ValidationException;
1215
use Utopia\Query\Method;
1316

14-
class SQLite extends SQL implements Json, ConditionalAggregates, StringAggregates
17+
class SQLite extends SQL implements Json, ConditionalAggregates, StringAggregates, InsertOrIgnore, Upsert, UpsertSelect
1518
{
1619
use Trait\ConditionalAggregates;
1720
use Trait\StringAggregates;
21+
use Trait\Upsert;
22+
use Trait\UpsertSelect;
1823

1924
/** @var array<string, Condition> */
2025
protected array $jsonSets = [];

0 commit comments

Comments
 (0)