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\StructuredQuery; |
21
|
|
|
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\JoinTrait; |
22
|
|
|
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\NumberPartTrait; |
23
|
|
|
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\QuoteNameTrait; |
24
|
|
|
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\SubQueryTrait; |
25
|
|
|
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\WhereTrait; |
26
|
|
|
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\WrapValueTrait; |
27
|
|
|
use ArekX\PQL\Drivers\MySql\Builder\MySqlQueryBuilderState; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Represents a query builder for building UPDATE query. |
31
|
|
|
*/ |
32
|
|
|
class UpdateBuilder extends QueryPartBuilder |
33
|
|
|
{ |
34
|
|
|
use QuoteNameTrait; |
35
|
|
|
use WrapValueTrait; |
36
|
|
|
use SubQueryTrait; |
37
|
|
|
use JoinTrait; |
38
|
|
|
use WhereTrait; |
39
|
|
|
use NumberPartTrait; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
44
|
7 |
|
protected function getInitialParts(): array |
45
|
|
|
{ |
46
|
7 |
|
return ['UPDATE']; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritDoc |
51
|
|
|
*/ |
52
|
7 |
|
protected function getRequiredParts(): array |
53
|
|
|
{ |
54
|
7 |
|
return ['to', 'set']; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritDoc |
59
|
|
|
*/ |
60
|
7 |
|
protected function getPartBuilders(): array |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
7 |
|
'to' => fn($parts, $state) => $this->buildTo($parts, $state), |
64
|
7 |
|
'join' => fn($parts, $state) => $this->buildJoin($parts, $state), |
65
|
7 |
|
'set' => fn($parts, $state) => $this->buildSet($parts, $state), |
66
|
7 |
|
'where' => fn($parts, $state) => $this->buildWhere($parts, $state), |
67
|
7 |
|
'limit' => fn($parts, $state) => $this->buildLimit($parts), |
|
|
|
|
68
|
7 |
|
'offset' => fn($parts, $state) => $this->buildOffset($parts), |
|
|
|
|
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Builds table which will be updated. |
74
|
|
|
* |
75
|
|
|
* @param array|StructuredQuery $parts Part which will be updated. |
76
|
|
|
* @param MySqlQueryBuilderState $state Query builder state. |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
7 |
|
protected function buildTo($parts, MySqlQueryBuilderState $state) |
80
|
|
|
{ |
81
|
7 |
|
if ($parts instanceof StructuredQuery) { |
82
|
1 |
|
return $this->buildQuery($parts, $state); |
83
|
|
|
} |
84
|
|
|
|
85
|
7 |
|
return $this->quoteName($parts); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Build SET part. |
90
|
|
|
* |
91
|
|
|
* @param array|StructuredQuery $parts Part to be built |
92
|
|
|
* @param MySqlQueryBuilderState $state Query builder state |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
7 |
|
protected function buildSet($parts, MySqlQueryBuilderState $state) |
96
|
|
|
{ |
97
|
7 |
|
if ($parts instanceof StructuredQuery) { |
98
|
1 |
|
return 'SET ' . $this->buildQuery($parts, $state); |
99
|
|
|
} |
100
|
|
|
|
101
|
7 |
|
$results = []; |
102
|
|
|
|
103
|
7 |
|
foreach ($parts as $key => $value) { |
104
|
7 |
|
$results[] = $this->quoteName($key) . ' = ' . $this->buildWrapValue($value, $state); |
105
|
|
|
} |
106
|
|
|
|
107
|
7 |
|
return 'SET ' . implode(', ', $results); |
108
|
|
|
} |
109
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.