-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL.php
More file actions
180 lines (151 loc) · 7.31 KB
/
SQL.php
File metadata and controls
180 lines (151 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace Utopia\Query\Builder;
use Utopia\Query\Builder as BaseBuilder;
use Utopia\Query\Builder\Feature\BitwiseAggregates;
use Utopia\Query\Builder\Feature\Locking;
use Utopia\Query\Builder\Feature\StatisticalAggregates;
use Utopia\Query\Builder\Feature\Transactions;
use Utopia\Query\Method;
use Utopia\Query\Query;
use Utopia\Query\QuotesIdentifiers;
use Utopia\Query\Schema\ColumnType;
abstract class SQL extends BaseBuilder implements Locking, Transactions, StatisticalAggregates, BitwiseAggregates
{
use QuotesIdentifiers;
use Trait\BitwiseAggregates;
use Trait\Json;
use Trait\Locking;
use Trait\StatisticalAggregates;
use Trait\Transactions;
/** @var array<string, Condition> */
protected array $jsonSets = [];
abstract protected function compileConflictHeader(): string;
abstract protected function compileConflictAssignment(string $wrapped): string;
protected function compileConflictClause(): string
{
$updates = [];
foreach ($this->conflictUpdateColumns as $col) {
$wrapped = $this->resolveAndWrap($col);
if (isset($this->conflictRawSets[$col])) {
$updates[] = $wrapped . ' = ' . $this->conflictRawSets[$col];
foreach ($this->conflictRawSetBindings[$col] ?? [] as $binding) {
$this->addBinding($binding);
}
} else {
$updates[] = $wrapped . ' = ' . $this->compileConflictAssignment($wrapped);
}
}
return $this->compileConflictHeader() . ' ' . \implode(', ', $updates);
}
#[\Override]
public function compileFilter(Query $query): string
{
$method = $query->getMethod();
$attribute = $this->resolveAndWrap($query->getAttribute());
if ($method === Method::Search) {
return $this->compileSearchExpr($attribute, $query->getValues(), false);
}
if ($method === Method::NotSearch) {
return $this->compileSearchExpr($attribute, $query->getValues(), true);
}
if ($method->isSpatial()) {
return $this->compileSpatialFilter($method, $attribute, $query);
}
$attrType = $query->getAttributeType();
$isSpatialAttr = \in_array($attrType, [ColumnType::Point->value, ColumnType::Linestring->value, ColumnType::Polygon->value], true);
if ($isSpatialAttr) {
$spatialMethod = match ($method) {
Method::Equal => Method::SpatialEquals,
Method::NotEqual => Method::NotSpatialEquals,
Method::Contains => Method::Covers,
Method::NotContains => Method::NotCovers,
default => null,
};
if ($spatialMethod !== null) {
return $this->compileSpatialFilter($spatialMethod, $attribute, $query);
}
}
if ($method->isJson()) {
return $this->compileJsonFilter($method, $attribute, $query);
}
if ($query->onArray() && \in_array($method, [Method::Contains, Method::ContainsAny, Method::NotContains, Method::ContainsAll], true)) {
return $this->compileArrayFilter($method, $attribute, $query);
}
return parent::compileFilter($query);
}
protected function compileArrayFilter(Method $method, string $attribute, Query $query): string
{
$values = $query->getValues();
return match ($method) {
Method::Contains,
Method::ContainsAny => $this->compileJsonOverlapsExpr($attribute, [$values]),
Method::NotContains => 'NOT ' . $this->compileJsonOverlapsExpr($attribute, [$values]),
Method::ContainsAll => $this->compileJsonContainsExpr($attribute, [$values], false),
default => parent::compileFilter($query),
};
}
protected function compileSpatialFilter(Method $method, string $attribute, Query $query): string
{
$values = $query->getValues();
return match ($method) {
Method::DistanceLessThan,
Method::DistanceGreaterThan,
Method::DistanceEqual,
Method::DistanceNotEqual => $this->compileSpatialDistance($method, $attribute, $values),
Method::Intersects => $this->compileSpatialPredicate('ST_Intersects', $attribute, $values, false),
Method::NotIntersects => $this->compileSpatialPredicate('ST_Intersects', $attribute, $values, true),
Method::Crosses => $this->compileSpatialPredicate('ST_Crosses', $attribute, $values, false),
Method::NotCrosses => $this->compileSpatialPredicate('ST_Crosses', $attribute, $values, true),
Method::Overlaps => $this->compileSpatialPredicate('ST_Overlaps', $attribute, $values, false),
Method::NotOverlaps => $this->compileSpatialPredicate('ST_Overlaps', $attribute, $values, true),
Method::Touches => $this->compileSpatialPredicate('ST_Touches', $attribute, $values, false),
Method::NotTouches => $this->compileSpatialPredicate('ST_Touches', $attribute, $values, true),
Method::Covers => $this->compileSpatialCoversPredicate($attribute, $values, false),
Method::NotCovers => $this->compileSpatialCoversPredicate($attribute, $values, true),
Method::SpatialEquals => $this->compileSpatialPredicate('ST_Equals', $attribute, $values, false),
Method::NotSpatialEquals => $this->compileSpatialPredicate('ST_Equals', $attribute, $values, true),
default => parent::compileFilter($query),
};
}
/**
* @param array<mixed> $values
*/
abstract protected function compileSpatialDistance(Method $method, string $attribute, array $values): string;
/**
* @param array<mixed> $values
*/
abstract protected function compileSpatialPredicate(string $function, string $attribute, array $values, bool $not): string;
/**
* Compile covers/not-covers spatial predicate. MySQL uses ST_Contains, PostgreSQL uses ST_Covers.
*
* @param array<mixed> $values
*/
abstract protected function compileSpatialCoversPredicate(string $attribute, array $values, bool $not): string;
/**
* @param array<mixed> $values
*/
abstract protected function compileSearchExpr(string $attribute, array $values, bool $not): string;
protected function compileJsonFilter(Method $method, string $attribute, Query $query): string
{
$values = $query->getValues();
return match ($method) {
Method::JsonContains => $this->compileJsonContainsExpr($attribute, $values, false),
Method::JsonNotContains => $this->compileJsonContainsExpr($attribute, $values, true),
Method::JsonOverlaps => $this->compileJsonOverlapsExpr($attribute, $values),
Method::JsonPath => $this->compileJsonPathExpr($attribute, $values),
default => parent::compileFilter($query),
};
}
/**
* @param array<mixed> $values
*/
abstract protected function compileJsonContainsExpr(string $attribute, array $values, bool $not): string;
/**
* @param array<mixed> $values
*/
abstract protected function compileJsonOverlapsExpr(string $attribute, array $values): string;
/**
* @param array<mixed> $values
*/
abstract protected function compileJsonPathExpr(string $attribute, array $values): string;
}