Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
24 / 24 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
Update | |
100.00% |
24 / 24 |
|
100.00% |
5 / 5 |
12 | |
100.00% |
1 / 1 |
set | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
values | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
render | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
5 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__get | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Db\Sql; |
15 | |
16 | /** |
17 | * Update class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Db |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 6.5.0 |
25 | */ |
26 | class Update extends AbstractPredicateClause |
27 | { |
28 | |
29 | /** |
30 | * Set a value |
31 | * |
32 | * @param string $name |
33 | * @param mixed $value |
34 | * @return Update |
35 | */ |
36 | public function set(string $name, mixed $value): Update |
37 | { |
38 | $this->addNamedValue($name, $value); |
39 | return $this; |
40 | } |
41 | |
42 | /** |
43 | * Set a value |
44 | * |
45 | * @param array $values |
46 | * @return Update |
47 | */ |
48 | public function values(array $values): Update |
49 | { |
50 | $this->setValues($values); |
51 | return $this; |
52 | } |
53 | |
54 | /** |
55 | * Render the UPDATE statement |
56 | * |
57 | * @return string |
58 | */ |
59 | public function render(): string |
60 | { |
61 | // Start building the UPDATE statement |
62 | $sql = 'UPDATE ' . $this->quoteId($this->table) . ' SET '; |
63 | $set = []; |
64 | |
65 | $paramCount = 1; |
66 | $dbType = $this->getDbType(); |
67 | |
68 | foreach ($this->values as $column => $value) { |
69 | $colValue = (str_contains($column, '.')) ? |
70 | substr($column, (strpos($column, '.') + 1)) : $column; |
71 | |
72 | $val = ($value === null) ? 'NULL' : $this->quote($value); |
73 | $set[] = $this->quoteId($column) .' = ' . $val; |
74 | } |
75 | |
76 | $sql .= implode(', ', $set); |
77 | |
78 | // Build any WHERE clauses |
79 | if ($this->where !== null) { |
80 | $sql .= ' WHERE ' . $this->where; |
81 | } |
82 | |
83 | return $sql; |
84 | } |
85 | |
86 | /** |
87 | * Render the UPDATE statement |
88 | * |
89 | * @return string |
90 | */ |
91 | public function __toString(): string |
92 | { |
93 | return $this->render(); |
94 | } |
95 | |
96 | /** |
97 | * Magic method to access $where property |
98 | * |
99 | * @param string $name |
100 | * @throws Exception |
101 | * @return mixed |
102 | */ |
103 | public function __get(string $name): mixed |
104 | { |
105 | switch (strtolower($name)) { |
106 | case 'where': |
107 | if ($this->where === null) { |
108 | $this->where = new Where($this); |
109 | } |
110 | return $this->where; |
111 | break; |
112 | default: |
113 | throw new Exception('Not a valid property for this object.'); |
114 | } |
115 | } |
116 | |
117 | } |