Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
58 / 58 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
| Sql | |
100.00% |
58 / 58 |
|
100.00% |
11 / 11 |
26 | |
100.00% |
1 / 1 |
| select | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
6 | |||
| insert | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| update | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| delete | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| hasSelect | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasInsert | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasUpdate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasDelete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| reset | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| render | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (https://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 8 | * @license https://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Db; |
| 15 | |
| 16 | use Pop\Db\Sql\AbstractSql; |
| 17 | |
| 18 | /** |
| 19 | * Sql class |
| 20 | * |
| 21 | * @category Pop |
| 22 | * @package Pop\Db |
| 23 | * @author Nick Sagona, III <dev@noladev.com> |
| 24 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 25 | * @license https://www.popphp.org/license New BSD License |
| 26 | * @version 6.7.0 |
| 27 | */ |
| 28 | class Sql extends AbstractSql |
| 29 | { |
| 30 | |
| 31 | /** |
| 32 | * Select object |
| 33 | * @var ?Sql\Select |
| 34 | */ |
| 35 | protected ?Sql\Select $select = null; |
| 36 | |
| 37 | /** |
| 38 | * Insert object |
| 39 | * @var ?Sql\Insert |
| 40 | */ |
| 41 | protected ?Sql\Insert $insert = null; |
| 42 | |
| 43 | /** |
| 44 | * Update object |
| 45 | * @var ?Sql\Update |
| 46 | */ |
| 47 | protected ?Sql\Update $update = null; |
| 48 | |
| 49 | /** |
| 50 | * Delete object |
| 51 | * @var ?Sql\Delete |
| 52 | */ |
| 53 | protected ?Sql\Delete $delete = null; |
| 54 | |
| 55 | /** |
| 56 | * Access the select object |
| 57 | * |
| 58 | * @param mixed $columns |
| 59 | * @return ?Sql\Select |
| 60 | */ |
| 61 | public function select(mixed $columns = null): ?Sql\Select |
| 62 | { |
| 63 | $this->insert = null; |
| 64 | $this->update = null; |
| 65 | $this->delete = null; |
| 66 | |
| 67 | if ($this->select === null) { |
| 68 | $this->select = new Sql\Select($this->db); |
| 69 | } |
| 70 | if ($columns !== null) { |
| 71 | if (!is_array($columns)) { |
| 72 | $columns = [$columns]; |
| 73 | } |
| 74 | foreach ($columns as $name => $value) { |
| 75 | if (!is_numeric($name)) { |
| 76 | $this->select->addValue($value, $name); |
| 77 | } else { |
| 78 | $this->select->addValue($value); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return $this->select; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Access the insert object |
| 88 | * |
| 89 | * @param ?string $table |
| 90 | * @return ?Sql\Insert |
| 91 | */ |
| 92 | public function insert(?string $table = null): ?Sql\Insert |
| 93 | { |
| 94 | $this->select = null; |
| 95 | $this->update = null; |
| 96 | $this->delete = null; |
| 97 | |
| 98 | if ($this->insert === null) { |
| 99 | $this->insert = new Sql\Insert($this->db); |
| 100 | } |
| 101 | if ($table !== null) { |
| 102 | $this->insert->setTable($table); |
| 103 | } |
| 104 | |
| 105 | return $this->insert; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Access the update object |
| 110 | * |
| 111 | * @param ?string $table |
| 112 | * @return ?Sql\Update |
| 113 | */ |
| 114 | public function update(?string $table = null): ?Sql\Update |
| 115 | { |
| 116 | $this->insert = null; |
| 117 | $this->select = null; |
| 118 | $this->delete = null; |
| 119 | |
| 120 | if ($this->update === null) { |
| 121 | $this->update = new Sql\Update($this->db); |
| 122 | } |
| 123 | if ($table !== null) { |
| 124 | $this->update->setTable($table); |
| 125 | } |
| 126 | |
| 127 | return $this->update; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Access the delete object |
| 132 | * |
| 133 | * @param ?string $table |
| 134 | * @return ?Sql\Delete |
| 135 | */ |
| 136 | public function delete(?string $table = null): ?Sql\Delete |
| 137 | { |
| 138 | $this->insert = null; |
| 139 | $this->update = null; |
| 140 | $this->select = null; |
| 141 | |
| 142 | if ($this->delete === null) { |
| 143 | $this->delete = new Sql\Delete($this->db); |
| 144 | } |
| 145 | if ($table !== null) { |
| 146 | $this->delete->setTable($table); |
| 147 | } |
| 148 | |
| 149 | return $this->delete; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Determine if SQL object has a select object |
| 154 | * |
| 155 | * @return bool |
| 156 | */ |
| 157 | public function hasSelect(): bool |
| 158 | { |
| 159 | return ($this->select !== null); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Determine if SQL object has a insert object |
| 164 | * |
| 165 | * @return bool |
| 166 | */ |
| 167 | public function hasInsert(): bool |
| 168 | { |
| 169 | return ($this->insert !== null); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Determine if SQL object has a update object |
| 174 | * |
| 175 | * @return bool |
| 176 | */ |
| 177 | public function hasUpdate(): bool |
| 178 | { |
| 179 | return ($this->update !== null); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Determine if SQL object has a delete object |
| 184 | * |
| 185 | * @return bool |
| 186 | */ |
| 187 | public function hasDelete(): bool |
| 188 | { |
| 189 | return ($this->delete !== null); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Reset and clear the SQL object |
| 194 | * |
| 195 | * @return Sql |
| 196 | */ |
| 197 | public function reset(): Sql |
| 198 | { |
| 199 | $this->select = null; |
| 200 | $this->insert = null; |
| 201 | $this->update = null; |
| 202 | $this->delete = null; |
| 203 | |
| 204 | return $this; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Render the SQL statement |
| 209 | * |
| 210 | * @return string |
| 211 | */ |
| 212 | public function render(): string |
| 213 | { |
| 214 | $sql = ''; |
| 215 | |
| 216 | if ($this->select !== null) { |
| 217 | $sql = $this->select->render(); |
| 218 | } else if ($this->insert !== null) { |
| 219 | $sql = $this->insert->render(); |
| 220 | } else if ($this->update !== null) { |
| 221 | $sql = $this->update->render(); |
| 222 | } else if ($this->delete !== null) { |
| 223 | $sql = $this->delete->render(); |
| 224 | } |
| 225 | |
| 226 | $this->reset(); |
| 227 | |
| 228 | return $sql; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Render the SQL statement |
| 233 | * |
| 234 | * @return string |
| 235 | */ |
| 236 | public function __toString(): string |
| 237 | { |
| 238 | return $this->render(); |
| 239 | } |
| 240 | |
| 241 | } |