1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2021 Aleksandar Panic |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ArekX\PQL\Drivers\MySql\Builder\Builders; |
19
|
|
|
|
20
|
|
|
use ArekX\PQL\Contracts\QueryBuilder; |
21
|
|
|
use ArekX\PQL\Contracts\QueryBuilderState; |
22
|
|
|
use ArekX\PQL\Contracts\RawQuery; |
23
|
|
|
use ArekX\PQL\Contracts\StructuredQuery; |
24
|
|
|
use ArekX\PQL\Drivers\MySql\Builder\MySqlQueryBuilderState; |
25
|
|
|
use ArekX\PQL\RawQueryResult; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Represents a base builder for query parts. |
29
|
|
|
*/ |
30
|
|
|
abstract class QueryPartBuilder implements QueryBuilder |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @inheritDoc |
34
|
|
|
*/ |
35
|
18 |
|
public function build(StructuredQuery $query, QueryBuilderState $state = null): RawQuery |
36
|
|
|
{ |
37
|
|
|
/** @var MySqlQueryBuilderState $state */ |
38
|
|
|
|
39
|
18 |
|
if ($state === null) { |
40
|
1 |
|
throw new \Exception('Passed state cannot be null.'); |
41
|
|
|
} |
42
|
|
|
|
43
|
17 |
|
$input = $query->toArray(); |
44
|
17 |
|
$results = implode($state->getQueryPartGlue(), $this->buildQueryParts($input, $state)); |
45
|
|
|
|
46
|
16 |
|
return RawQueryResult::create( |
47
|
16 |
|
$results, |
48
|
16 |
|
$state->getParamsBuilder()->build(), |
49
|
16 |
|
$query->get('config') ?? null |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Build each part from the structured query |
55
|
|
|
* |
56
|
|
|
* @param array $parts Parts from the structured query |
57
|
|
|
* @param MySqlQueryBuilderState $state Builder state |
58
|
|
|
* @return array Resulting array for each built part. |
59
|
|
|
* @throws \Exception |
60
|
|
|
*/ |
61
|
17 |
|
protected function buildQueryParts(array $parts, MySqlQueryBuilderState $state): array |
62
|
|
|
{ |
63
|
17 |
|
$results = $this->getInitialParts(); |
64
|
17 |
|
$requiredParts = $this->getRequiredParts(); |
65
|
|
|
|
66
|
17 |
|
foreach ($this->getPartBuilders() as $partName => $buildPart) { |
67
|
16 |
|
if (empty($parts[$partName])) { |
68
|
|
|
|
69
|
7 |
|
if (in_array($partName, $requiredParts)) { |
70
|
1 |
|
throw new \Exception("Part '${partName}' is required."); |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
16 |
|
$result = $buildPart($parts[$partName], $state); |
77
|
|
|
|
78
|
16 |
|
if ($result !== null) { |
79
|
16 |
|
$results[] = $result; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
16 |
|
return $results; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return initial parts for the query |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
protected abstract function getInitialParts(): array; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return parts which must be set in the query in order to be built. |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
protected abstract function getRequiredParts(): array; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return part builders for resolving the query parts to string. |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
protected abstract function getPartBuilders(): array; |
105
|
|
|
} |