Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
26 / 26 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
Update | |
100.00% |
26 / 26 |
|
100.00% |
5 / 5 |
9 | |
100.00% |
1 / 1 |
sql | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
valuesSQL | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
params | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
reset | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
allTablesReferenced | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb\Query\Builder; |
4 | |
5 | /** |
6 | * Update |
7 | * |
8 | * Generates an SQL query for an UPDATE operation. |
9 | * |
10 | * @package BO\Zmsdb\Query\Builder |
11 | * @author Alex Gisby<alex@solution10.com> |
12 | * @license MIT |
13 | */ |
14 | class Update extends Query |
15 | { |
16 | use TableName; |
17 | use Values; |
18 | use Where; |
19 | use Paginate; |
20 | |
21 | /** |
22 | * @var string The base part of the query |
23 | */ |
24 | protected $queryBase = 'UPDATE'; |
25 | |
26 | /** |
27 | * Generates the full SQL statement for this query with all the composite parts. |
28 | * |
29 | * @return string |
30 | */ |
31 | public function sql() |
32 | { |
33 | if ($this->table === null) { |
34 | return ''; |
35 | } |
36 | |
37 | $candidateParts = [ |
38 | $this->queryBase, |
39 | $this->dialect->quoteTable($this->table), |
40 | $this->valuesSQL(), |
41 | $this->buildWhereSQL($this->dialect), |
42 | $this->buildPaginateSQL() |
43 | ]; |
44 | |
45 | return implode(' ', $candidateParts); |
46 | } |
47 | |
48 | /** |
49 | * Returns the values part of a query for an UPDATE statement (so key = value, key = value) |
50 | * |
51 | * @return string |
52 | */ |
53 | protected function valuesSQL() |
54 | { |
55 | $sql = ''; |
56 | if (!empty($this->values)) { |
57 | $sql .= 'SET '; |
58 | $parts = []; |
59 | foreach (array_keys($this->values) as $field) { |
60 | $parts[] = $this->dialect->quoteField($field) . ' = ?'; |
61 | } |
62 | $sql .= implode(', ', $parts); |
63 | } |
64 | return $sql; |
65 | } |
66 | |
67 | /** |
68 | * Returns all the parameters, in the correct order, to pass into PDO. |
69 | * |
70 | * @return array |
71 | */ |
72 | public function params() |
73 | { |
74 | return array_merge(array_values($this->values), $this->getWhereParams()); |
75 | } |
76 | |
77 | /** |
78 | * Resets the entire query. |
79 | * |
80 | * @return $this |
81 | */ |
82 | public function reset() |
83 | { |
84 | $this->table = null; |
85 | $this->resetValues(); |
86 | $this->resetWhere(); |
87 | return $this; |
88 | } |
89 | |
90 | /* |
91 | * ------------------- All Tables ------------------------ |
92 | */ |
93 | |
94 | /** |
95 | * Returns all the tables that this query makes mention of, in FROMs and JOINs |
96 | * |
97 | * @return array |
98 | */ |
99 | public function allTablesReferenced() |
100 | { |
101 | if ($this->table()) { |
102 | return [$this->table()]; |
103 | } |
104 | return []; |
105 | } |
106 | } |