Skip to content

Commit 8f9ac17

Browse files
author
Robin de Graaf
committed
Squashed commit of the following:
commit 5e7fcfb Author: Robin de Graaf <hello@devvoh.com> Date: Tue Mar 10 09:22:06 2020 +0000 Rename QueryPartsTest to StringBuilderTest commit 5af55fc Author: Robin de Graaf <hello@devvoh.com> Date: Mon Feb 24 09:33:40 2020 +0000 Small fixes commit 2fdbb04 Author: Robin de Graaf <hello@devvoh.com> Date: Mon Feb 24 09:19:38 2020 +0000 Add StringBuilder and implement commit 3a55660 Author: Robin de Graaf <hello@devvoh.com> Date: Sat Feb 22 14:12:47 2020 +0000 Code improvements and test fixes commit 0eccd0a Author: Robin de Graaf <hello@devvoh.com> Date: Thu Nov 7 11:48:20 2019 +0000 Add force index to query
1 parent 7f5e9b4 commit 8f9ac17

32 files changed

Lines changed: 651 additions & 357 deletions

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Parable PHP Query
22

3+
## 0.3.0
4+
5+
_Changes_
6+
7+
- The `SupportsForceIndexTrait` trait `Query::forceIndex(string $key)` and `Query::getForceIndex(): ?string` have been added, allowing you to force a specific index. Use `PRIMARY_KEY_INDEX_VALUE` in place of the actual primary key to force the primary key as an index.
8+
- `Query::delete()` and `Query::update()` no longer accept aliases since they caused issues.
9+
- `AbstractCondition::VALUE_TYPE_VALUE` and `AbstractCondition::VALUE_TYPE_KEY` have been removed.
10+
- `Query::ORDER_ASC` and `Query::ORDER_DESC` have been removed.
11+
- `Query::VALID_TYPES` has been removed, as it was unused.
12+
- All string values of `AND` and `OR` have been replaced with `AbstractCondition::TYPE_AND` and `AbstractCondition::TYPE_OR`.
13+
- `ORDER_ASC` and `ORDER_DESC` have been changed from `int` values to their corresponding `string` values `ASC` and `DESC`.
14+
- `OrderBy::getDirectionAsString()` has turned into `OrderBy::getDirection()`.
15+
- The `BuilderTest` now also attempts to _run_ the queries. Adding this step made some issues clear, which are now all fixed.
16+
317
## 0.2.1
418

519
_Changes_

src/Condition/AbstractCondition.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ abstract class AbstractCondition
99
public const TYPE_AND = 'AND';
1010
public const TYPE_OR = 'OR';
1111

12-
public const VALUE_TYPE_VALUE = 1;
13-
public const VALUE_TYPE_KEY = 2;
14-
1512
/**
1613
* @var string
1714
*/

src/Join.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function on(string $joinKey, string $comparator, string $value): self
5555
$joinKey,
5656
$comparator,
5757
$value,
58-
'AND',
58+
AbstractCondition::TYPE_AND,
5959
false
6060
);
6161

@@ -69,7 +69,7 @@ public function orOn(string $joinKey, string $comparator, string $value): self
6969
$joinKey,
7070
$comparator,
7171
$value,
72-
'OR',
72+
AbstractCondition::TYPE_OR,
7373
false
7474
);
7575

@@ -83,7 +83,7 @@ public function onNull(string $key): self
8383
$key,
8484
'IS NULL',
8585
null,
86-
'AND',
86+
AbstractCondition::TYPE_AND,
8787
false
8888
);
8989

@@ -97,7 +97,7 @@ public function onNotNull(string $key): self
9797
$key,
9898
'IS NOT NULL',
9999
null,
100-
'AND',
100+
AbstractCondition::TYPE_AND,
101101
false
102102
);
103103

@@ -111,7 +111,7 @@ public function onKey(string $key, string $comparator, string $queryKey): self
111111
$key,
112112
$comparator,
113113
$queryKey,
114-
'AND',
114+
AbstractCondition::TYPE_AND,
115115
true
116116
);
117117

@@ -125,7 +125,7 @@ public function orOnKey(string $key, string $comparator, string $queryKey): self
125125
$key,
126126
$comparator,
127127
$queryKey,
128-
'OR',
128+
AbstractCondition::TYPE_OR,
129129
true
130130
);
131131

@@ -134,14 +134,14 @@ public function orOnKey(string $key, string $comparator, string $queryKey): self
134134

135135
public function onCallable(callable $callable): self
136136
{
137-
$this->onConditions[] = new CallableCondition($callable, 'AND');
137+
$this->onConditions[] = new CallableCondition($callable, AbstractCondition::TYPE_AND);
138138

139139
return $this;
140140
}
141141

142142
public function orOnCallable(callable $callable): self
143143
{
144-
$this->onConditions[] = new CallableCondition($callable, 'OR');
144+
$this->onConditions[] = new CallableCondition($callable, AbstractCondition::TYPE_OR);
145145

146146
return $this;
147147
}

src/OrderBy.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
class OrderBy
66
{
7-
protected const ORDER_ASC = 1;
8-
protected const ORDER_DESC = 2;
7+
protected const ORDER_ASC = 'ASC';
8+
protected const ORDER_DESC = 'DESC';
99

1010
/**
1111
* @var int
@@ -17,7 +17,7 @@ class OrderBy
1717
*/
1818
protected $keys = [];
1919

20-
protected function __construct(int $direction, array $keys)
20+
protected function __construct(string $direction, array $keys)
2121
{
2222
$this->direction = $direction;
2323

@@ -28,9 +28,9 @@ protected function __construct(int $direction, array $keys)
2828
$this->keys = $keys;
2929
}
3030

31-
public function getDirectionAsString(): string
31+
public function getDirection(): string
3232
{
33-
return $this->isAscending() ? 'ASC' : 'DESC';
33+
return $this->direction;
3434
}
3535

3636
public function getKeys(): array

src/Query.php

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,7 @@ class Query
1616
public const JOIN_TYPE_INNER = 'INNER';
1717
public const JOIN_TYPE_LEFT = 'LEFT';
1818

19-
public const ORDER_ASC = 'ASC';
20-
public const ORDER_DESC = 'DESC';
21-
22-
protected const VALID_TYPES = [
23-
self::TYPE_DELETE,
24-
self::TYPE_INSERT,
25-
self::TYPE_UPDATE,
26-
self::TYPE_SELECT,
27-
];
19+
public const PRIMARY_KEY_INDEX = 'PRIMARY_KEY_INDEX_VALUE';
2820

2921
/**
3022
* @var string
@@ -66,6 +58,11 @@ class Query
6658
*/
6759
protected $offset;
6860

61+
/**
62+
* @var string|null
63+
*/
64+
protected $forceIndex;
65+
6966
/**
7067
* @var string[]
7168
*/
@@ -130,7 +127,7 @@ public function where(string $key, string $comparator, $value): self
130127
$key,
131128
$comparator,
132129
$value,
133-
'AND'
130+
AbstractCondition::TYPE_AND
134131
);
135132

136133
return $this;
@@ -143,7 +140,7 @@ public function whereNull(string $key): self
143140
$key,
144141
'IS NULL',
145142
null,
146-
'AND'
143+
AbstractCondition::TYPE_AND
147144
);
148145

149146
return $this;
@@ -156,7 +153,7 @@ public function whereNotNull(string $key): self
156153
$key,
157154
'IS NOT NULL',
158155
null,
159-
'AND'
156+
AbstractCondition::TYPE_AND
160157
);
161158

162159
return $this;
@@ -169,7 +166,7 @@ public function orWhere(string $key, string $comparator, $value): self
169166
$key,
170167
$comparator,
171168
(string)$value,
172-
'OR'
169+
AbstractCondition::TYPE_OR
173170
);
174171

175172
return $this;
@@ -182,7 +179,7 @@ public function orWhereNull(string $key): self
182179
$key,
183180
'IS NULL',
184181
null,
185-
'OR'
182+
AbstractCondition::TYPE_OR
186183
);
187184

188185
return $this;
@@ -195,22 +192,22 @@ public function orWhereNotNull(string $key): self
195192
$key,
196193
'IS NOT NULL',
197194
null,
198-
'OR'
195+
AbstractCondition::TYPE_OR
199196
);
200197

201198
return $this;
202199
}
203200

204201
public function whereCallable(callable $callable): self
205202
{
206-
$this->whereConditions[] = new CallableCondition($callable, 'AND');
203+
$this->whereConditions[] = new CallableCondition($callable, AbstractCondition::TYPE_AND);
207204

208205
return $this;
209206
}
210207

211208
public function orWhereCallable(callable $callable): self
212209
{
213-
$this->whereConditions[] = new CallableCondition($callable, 'OR');
210+
$this->whereConditions[] = new CallableCondition($callable, AbstractCondition::TYPE_OR);
214211

215212
return $this;
216213
}
@@ -261,16 +258,8 @@ public function getJoinsByType(string $type): array
261258

262259
public function limit(int $limit, int $offset = null): self
263260
{
264-
if ($limit === 0) {
265-
$limit = null;
266-
}
267-
268-
if ($offset === 0) {
269-
$offset = null;
270-
}
271-
272-
$this->limit = $limit;
273-
$this->offset = $offset;
261+
$this->limit = $limit > 0 ? $limit : null;
262+
$this->offset = $offset > 0 ? $offset : null;
274263

275264
return $this;
276265
}
@@ -285,9 +274,18 @@ public function getOffset(): ?int
285274
return $this->offset;
286275
}
287276

288-
/**
289-
* @param string[] $keys
290-
*/
277+
public function forceIndex(string $key): self
278+
{
279+
$this->forceIndex = $key;
280+
281+
return $this;
282+
}
283+
284+
public function getForceIndex(): ?string
285+
{
286+
return $this->forceIndex;
287+
}
288+
291289
public function groupBy(string ...$keys): self
292290
{
293291
$this->groupBy = $keys;
@@ -374,19 +372,19 @@ protected function createValueCondition(
374372
);
375373
}
376374

377-
public static function delete(string $tableName, ?string $tableAlias = null): self
375+
public static function delete(string $tableName): self
378376
{
379-
return new self(self::TYPE_DELETE, $tableName, $tableAlias);
377+
return new self(self::TYPE_DELETE, $tableName);
380378
}
381379

382380
public static function insert(string $tableName): self
383381
{
384382
return new self(self::TYPE_INSERT, $tableName);
385383
}
386384

387-
public static function update(string $tableName, ?string $tableAlias = null): self
385+
public static function update(string $tableName): self
388386
{
389-
return new self(self::TYPE_UPDATE, $tableName, $tableAlias);
387+
return new self(self::TYPE_UPDATE, $tableName);
390388
}
391389

392390
public static function select(string $tableName, ?string $tableAlias = null): self

src/StringBuilder.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Parable\Query;
4+
5+
class StringBuilder
6+
{
7+
protected const DEFAULT_GLUE = ' ';
8+
9+
/**
10+
* @var string
11+
*/
12+
protected $glue;
13+
14+
/**
15+
* @var string[]
16+
*/
17+
protected $parts = [];
18+
19+
public function __construct(string $glue = self::DEFAULT_GLUE)
20+
{
21+
$this->glue = $glue;
22+
}
23+
24+
public function prepend(...$parts): void
25+
{
26+
array_unshift($this->parts, ...$parts);
27+
}
28+
29+
public function add(...$parts): void
30+
{
31+
array_push($this->parts, ...$parts);
32+
}
33+
34+
public function getGlue(): string
35+
{
36+
return $this->glue;
37+
}
38+
39+
public function getParts(): array
40+
{
41+
return $this->parts;
42+
}
43+
44+
public function isEmpty(): bool
45+
{
46+
return $this->parts === [];
47+
}
48+
49+
public function merge(self $queryParts): self
50+
{
51+
if ($this->glue !== $queryParts->getGlue()) {
52+
throw new Exception('Cannot merge StringBuilder with different glues.');
53+
}
54+
55+
if ($queryParts->isEmpty()) {
56+
return $this;
57+
}
58+
59+
$this->add(...$queryParts->getParts());
60+
61+
return $this;
62+
}
63+
64+
public function toString(): string
65+
{
66+
return trim(implode($this->glue, array_filter($this->parts)));
67+
}
68+
69+
public static function fromArray(array $parts, string $glue = self::DEFAULT_GLUE): self
70+
{
71+
$queryParts = new self($glue);
72+
73+
$queryParts->add(...array_values($parts));
74+
75+
return $queryParts;
76+
}
77+
}

0 commit comments

Comments
 (0)