brunobmorais /
php-database
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace BMorais\Database; |
||||
| 4 | |||||
| 5 | use PDOException; |
||||
| 6 | |||||
| 7 | /** |
||||
| 8 | * CLASS ABSTRACT CRUD |
||||
| 9 | * Basic class to make connection between the database and the application |
||||
| 10 | * |
||||
| 11 | * @author Bruno Morais <[email protected]> |
||||
| 12 | * @copyright MIT, bmorais.com |
||||
| 13 | * @package bmorais\database |
||||
| 14 | * @subpackage class |
||||
| 15 | * @access private |
||||
| 16 | */ |
||||
| 17 | |||||
| 18 | abstract class Crud |
||||
| 19 | {
|
||||
| 20 | use DatalayerTrait; |
||||
| 21 | |||||
| 22 | /** |
||||
| 23 | * @param string $fields |
||||
| 24 | * @param string $add |
||||
| 25 | * @param array|null $values |
||||
| 26 | * @param bool $returnModel |
||||
| 27 | * @param bool $debug |
||||
| 28 | * @return array|false|void|null |
||||
| 29 | * @throws DatabaseException |
||||
| 30 | */ |
||||
| 31 | public function select(string $fields = '*', string $add = '', ?array $values = null, bool $returnModel = false, bool $debug = false) |
||||
| 32 | {
|
||||
| 33 | try {
|
||||
| 34 | if (strlen($add) > 0) {
|
||||
| 35 | $add = ' ' . $add; |
||||
| 36 | } |
||||
| 37 | |||||
| 38 | $sql = "SELECT {$fields} FROM {$this->getTableName()}";
|
||||
| 39 | |||||
| 40 | if (!empty($this->getTableAlias())) |
||||
| 41 | $sql .= " AS {$this->getTableAlias()}";
|
||||
| 42 | |||||
| 43 | $sql .= $add; |
||||
| 44 | |||||
| 45 | if ($debug) {
|
||||
| 46 | echo $sql; |
||||
| 47 | return; |
||||
| 48 | } |
||||
| 49 | $this->executeSQL($sql, $values); |
||||
| 50 | if ($returnModel) {
|
||||
| 51 | return $this->fetchArrayClass(); |
||||
| 52 | } else {
|
||||
| 53 | return $this->fetchArrayObj(); |
||||
| 54 | } |
||||
| 55 | } catch (PDOException $e) {
|
||||
| 56 | throw new DatabaseException( |
||||
| 57 | "Query failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}] COLUMNS: [{$fields}]",
|
||||
| 58 | $e->getCode(), |
||||
| 59 | $e, |
||||
| 60 | $sql ?? '', |
||||
| 61 | $values |
||||
| 62 | ); |
||||
| 63 | } |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | /** |
||||
| 67 | * @param string $fields |
||||
| 68 | * @param array|null $values |
||||
| 69 | * @param bool $debug |
||||
| 70 | * @return bool|void |
||||
| 71 | * @throws DatabaseException |
||||
| 72 | */ |
||||
| 73 | public function insert(string $fields, ?array $values = null, bool $debug = false) |
||||
| 74 | {
|
||||
| 75 | try {
|
||||
| 76 | $numparams = ''; |
||||
| 77 | foreach ($values as $item) {
|
||||
| 78 | $numparams .= ',?'; |
||||
| 79 | } |
||||
| 80 | $numparams = substr($numparams, 1); |
||||
| 81 | $sql = "INSERT INTO {$this->getTableName()} ({$fields}) VALUES ({$numparams})";
|
||||
| 82 | if ($debug) {
|
||||
| 83 | echo $sql . '<pre>' . print_r($values) . '</pre>'; |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 84 | return; |
||||
| 85 | } |
||||
| 86 | $result = $this->executeSQL($sql, $values); |
||||
| 87 | return !empty($result); |
||||
| 88 | } catch (PDOException $e) {
|
||||
| 89 | throw new DatabaseException( |
||||
| 90 | "Insert failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}] COLUMNS: [{$fields}]",
|
||||
| 91 | $e->getCode(), |
||||
| 92 | $e, |
||||
| 93 | $sql ?? '', |
||||
| 94 | $values |
||||
| 95 | ); |
||||
| 96 | } |
||||
| 97 | } |
||||
| 98 | |||||
| 99 | /** |
||||
| 100 | * @param object $object |
||||
| 101 | * @return bool|null |
||||
| 102 | * @throws DatabaseException |
||||
| 103 | */ |
||||
| 104 | public function insertObject(object $object) |
||||
| 105 | {
|
||||
| 106 | try {
|
||||
| 107 | $args = []; |
||||
| 108 | $params = []; |
||||
| 109 | |||||
| 110 | // Filtrar propriedades do objeto que não são null |
||||
| 111 | foreach ($object as $chave => $valor) {
|
||||
| 112 | if ($valor !== null) { // Verifica explicitamente se o valor não é null
|
||||
| 113 | $args[] = $chave; |
||||
| 114 | $params[] = $valor; |
||||
| 115 | } |
||||
| 116 | } |
||||
| 117 | |||||
| 118 | // Se houver colunas a serem inseridas |
||||
| 119 | if (!empty($args)) {
|
||||
| 120 | $columns = implode(',', $args);
|
||||
| 121 | return $this->insert($columns, $params); |
||||
| 122 | } |
||||
| 123 | |||||
| 124 | // Retorna falso se todos os valores forem null |
||||
| 125 | return false; |
||||
| 126 | } catch (PDOException $e) {
|
||||
| 127 | throw new DatabaseException( |
||||
| 128 | "Insert object failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}] COLUMNS: [{$columns}]",
|
||||
| 129 | $e->getCode(), |
||||
| 130 | $e |
||||
| 131 | ); |
||||
| 132 | } |
||||
| 133 | } |
||||
| 134 | |||||
| 135 | /** |
||||
| 136 | * @param array $object |
||||
| 137 | * @return bool |
||||
| 138 | * @throws DatabaseException |
||||
| 139 | */ |
||||
| 140 | public function insertArray(array $object) |
||||
| 141 | {
|
||||
| 142 | try {
|
||||
| 143 | $args = []; |
||||
| 144 | $params = []; |
||||
| 145 | foreach ($object as $chave => $valor) {
|
||||
| 146 | if ($valor !== null) {
|
||||
| 147 | $args[] = $chave; |
||||
| 148 | $params[] = $valor; |
||||
| 149 | } |
||||
| 150 | } |
||||
| 151 | |||||
| 152 | if (!empty($args)) {
|
||||
| 153 | $args = implode(',', $args);
|
||||
| 154 | return $this->insert($args, $params); |
||||
| 155 | } |
||||
| 156 | |||||
| 157 | return false; |
||||
| 158 | } catch (PDOException $e) {
|
||||
| 159 | throw new DatabaseException( |
||||
| 160 | "Insert array failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}] COLUMNS: [{$args}]",
|
||||
| 161 | $e->getCode(), |
||||
| 162 | $e |
||||
| 163 | ); |
||||
| 164 | } |
||||
| 165 | } |
||||
| 166 | |||||
| 167 | /** |
||||
| 168 | * @param string $fields |
||||
| 169 | * @param array|null $values |
||||
| 170 | * @param string|null $where |
||||
| 171 | * @param bool $debug |
||||
| 172 | * @return bool|void |
||||
| 173 | * @throws DatabaseException |
||||
| 174 | */ |
||||
| 175 | public function update(string $fields, ?array $values = null, ?string $where = null, bool $debug = false) |
||||
| 176 | {
|
||||
| 177 | try {
|
||||
| 178 | $fields_T = ''; |
||||
| 179 | $atributos = explode(',', $fields);
|
||||
| 180 | |||||
| 181 | foreach ($atributos as $item) {
|
||||
| 182 | $fields_T .= ", {$item} = ?";
|
||||
| 183 | } |
||||
| 184 | $fields_T = substr($fields_T, 2); |
||||
| 185 | $sql = "UPDATE {$this->getTableName()} SET {$fields_T}";
|
||||
| 186 | if (isset($where)) {
|
||||
| 187 | $sql .= " WHERE $where"; |
||||
| 188 | } |
||||
| 189 | if ($debug) {
|
||||
| 190 | echo $sql . '<pre>' . print_r($values) . '</pre>'; |
||||
|
0 ignored issues
–
show
Are you sure
print_r($values) of type string|true can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 191 | return; |
||||
| 192 | } |
||||
| 193 | $result = $this->executeSQL($sql, $values); |
||||
| 194 | return !empty($result); |
||||
| 195 | } catch (PDOException $e) {
|
||||
| 196 | throw new DatabaseException( |
||||
| 197 | "Update failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}] COLUMNS: {$fields}",
|
||||
| 198 | $e->getCode(), |
||||
| 199 | $e, |
||||
| 200 | $sql ?? '', |
||||
| 201 | $values |
||||
| 202 | ); |
||||
| 203 | } |
||||
| 204 | } |
||||
| 205 | |||||
| 206 | /** |
||||
| 207 | * @param object $object |
||||
| 208 | * @param string $where |
||||
| 209 | * @param array $whereValues |
||||
| 210 | * @return bool|null |
||||
| 211 | * @throws DatabaseException |
||||
| 212 | */ |
||||
| 213 | public function updateObject(object $object, string $where, array $whereValues = []) |
||||
| 214 | {
|
||||
| 215 | try {
|
||||
| 216 | $args = []; |
||||
| 217 | $params = []; |
||||
| 218 | foreach ($object as $chave => $valor) {
|
||||
| 219 | if ($valor !== null) {
|
||||
| 220 | $args[] = $chave; |
||||
| 221 | $params[] = $valor; |
||||
| 222 | } |
||||
| 223 | } |
||||
| 224 | |||||
| 225 | // Adiciona os valores do WHERE no final |
||||
| 226 | $params = array_merge($params, $whereValues); |
||||
| 227 | |||||
| 228 | if (!empty($args)) {
|
||||
| 229 | $args = implode(',', $args);
|
||||
| 230 | return $this->update($args, $params, $where); |
||||
| 231 | } |
||||
| 232 | |||||
| 233 | return false; |
||||
| 234 | } catch (PDOException $e) {
|
||||
| 235 | throw new DatabaseException( |
||||
| 236 | "Update object failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}] COLUMNS: [{$args}]",
|
||||
| 237 | $e->getCode(), |
||||
| 238 | $e |
||||
| 239 | ); |
||||
| 240 | } |
||||
| 241 | } |
||||
| 242 | |||||
| 243 | /** |
||||
| 244 | * @param array $object |
||||
| 245 | * @param string $where |
||||
| 246 | * @param array $whereValues |
||||
| 247 | * * @return bool |
||||
| 248 | * @throws DatabaseException |
||||
| 249 | */ |
||||
| 250 | public function updateArray(array $object, string $where, array $whereValues = []) |
||||
| 251 | {
|
||||
| 252 | try {
|
||||
| 253 | $args = []; |
||||
| 254 | $params = []; |
||||
| 255 | |||||
| 256 | foreach ($object as $chave => $valor) {
|
||||
| 257 | if ($valor !== null) {
|
||||
| 258 | $args[] = $chave; |
||||
| 259 | $params[] = $valor; |
||||
| 260 | } |
||||
| 261 | } |
||||
| 262 | |||||
| 263 | // Adiciona os valores do WHERE no final |
||||
| 264 | $params = array_merge($params, $whereValues); |
||||
| 265 | |||||
| 266 | if (!empty($args)) {
|
||||
| 267 | $args = implode(',', $args);
|
||||
| 268 | return $this->update($args, $params, $where); |
||||
| 269 | } |
||||
| 270 | |||||
| 271 | return false; |
||||
| 272 | } catch (PDOException $e) {
|
||||
| 273 | throw new DatabaseException( |
||||
| 274 | "Update array failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}] COLUMNS: [{$args}]",
|
||||
| 275 | $e->getCode(), |
||||
| 276 | $e |
||||
| 277 | ); |
||||
| 278 | } |
||||
| 279 | } |
||||
| 280 | |||||
| 281 | /** |
||||
| 282 | * @param array|null $values |
||||
| 283 | * @param string|null $where |
||||
| 284 | * @param bool $debug |
||||
| 285 | * @return bool|void |
||||
| 286 | * @throws DatabaseException |
||||
| 287 | */ |
||||
| 288 | public function delete(?array $values = null, ?string $where = null, bool $debug = false) |
||||
| 289 | {
|
||||
| 290 | try {
|
||||
| 291 | $sql = "DELETE FROM {$this->getTableName()}";
|
||||
| 292 | if (!empty($where)) {
|
||||
| 293 | $sql .= " WHERE $where"; |
||||
| 294 | } |
||||
| 295 | if ($debug) {
|
||||
| 296 | echo $sql . '<pre>' . print_r($values) . '</pre>'; |
||||
|
0 ignored issues
–
show
Are you sure
print_r($values) of type string|true can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 297 | return; |
||||
| 298 | } |
||||
| 299 | $result = $this->executeSQL($sql, $values); |
||||
| 300 | return !empty($result); |
||||
| 301 | } catch (PDOException $e) {
|
||||
| 302 | throw new DatabaseException( |
||||
| 303 | "Delete failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]",
|
||||
| 304 | $e->getCode(), |
||||
| 305 | $e, |
||||
| 306 | $sql ?? '', |
||||
| 307 | $values |
||||
| 308 | ); |
||||
| 309 | } |
||||
| 310 | } |
||||
| 311 | |||||
| 312 | /** |
||||
| 313 | * @return false|string |
||||
| 314 | */ |
||||
| 315 | public function lastInsertId(): ?string |
||||
| 316 | {
|
||||
| 317 | return $this->lastId(); |
||||
| 318 | } |
||||
| 319 | |||||
| 320 | /** |
||||
| 321 | * @return string|null |
||||
| 322 | */ |
||||
| 323 | public function getLogSQL(): ?string |
||||
| 324 | {
|
||||
| 325 | return $this->logSQL ?? ""; |
||||
| 326 | } |
||||
| 327 | } |