| 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: {$e->getMessage()}",
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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>';
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 return;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $result = $this->executeSQL($sql, $values);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             return !empty($result);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } catch (PDOException $e) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             throw new DatabaseException(
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 "Insert failed: {$e->getMessage()}",
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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: {$e->getMessage()}",
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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: {$e->getMessage()}",
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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>';
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 191 |  |  |                 return;
 | 
            
                                                                        
                            
            
                                    
            
            
                | 192 |  |  |             }
 | 
            
                                                                        
                            
            
                                    
            
            
                | 193 |  |  |             $result = $this->executeSQL($sql, $values);
 | 
            
                                                                        
                            
            
                                    
            
            
                | 194 |  |  |             return !empty($result);
 | 
            
                                                                        
                            
            
                                    
            
            
                | 195 |  |  |         } catch (PDOException $e) {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 196 |  |  |             throw new DatabaseException(
 | 
            
                                                                        
                            
            
                                    
            
            
                | 197 |  |  |                 "Update failed: {$e->getMessage()}",
 | 
            
                                                                        
                            
            
                                    
            
            
                | 198 |  |  |                 $e->getCode(),
 | 
            
                                                                        
                            
            
                                    
            
            
                | 199 |  |  |                 $e,
 | 
            
                                                                        
                            
            
                                    
            
            
                | 200 |  |  |                 $sql ?? '',
 | 
            
                                                                        
                            
            
                                    
            
            
                | 201 |  |  |                 $values
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |             );
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |         }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |      * @param object $object
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |      * @param string $where
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |      * @return bool|null
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |      * @throws DatabaseException
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |     public function updateObject(object $object, string $where)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |         try {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |             $args = [];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |             $params = [];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |             foreach ($object as $chave => $valor) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |                 if ($valor !== null) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |                     $args[] = $chave;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |                     $params[] = $valor;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |                 }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |             }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |             if (!empty($args)) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |                 $args = implode(',', $args);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |                 return $this->update($args, $params, $where);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |             }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |             return false;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |         } catch (PDOException $e) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |             throw new DatabaseException(
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |                 "Update object failed: {$e->getMessage()}",
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |                 $e->getCode(),
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |                 $e
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |             );
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |         }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |      * @param array $object
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |      * @param string $where
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |      * @return bool
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |      * @throws DatabaseException
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |     public function updateArray(array $object, string $where)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |         try {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |             $args = [];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |             $params = [];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |             foreach ($object as $chave => $valor) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |                 if ($valor !== null) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |                     $args[] = $chave;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |                     $params[] = $valor;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |                 }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |             }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |             if (!empty($args)) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |                 $args = implode(',', $args);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |                 return $this->update($args, $params, $where);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |             }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |             return false;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |         } catch (PDOException $e) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |             throw new DatabaseException(
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |                 "Update array failed: {$e->getMessage()}",
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |                 $e->getCode(),
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |                 $e
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |             );
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |         }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |      * @param array|null $values
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |      * @param string|null $where
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |      * @param bool $debug
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |      * @return bool|void
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |      * @throws DatabaseException
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |     public function delete(?array $values = null, ?string $where = null, bool $debug = false)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |         try {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |             $sql = "DELETE FROM {$this->getTableName()}";
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |             if (!empty($where)) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |                 $sql .= " WHERE $where";
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |             }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |             if ($debug) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |                 echo $sql . '<pre>' . print_r($values) . '</pre>';
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |                 return;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |             }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |             $result = $this->executeSQL($sql, $values);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |             return !empty($result);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |         } catch (PDOException $e) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |             throw new DatabaseException(
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |                 "Delete failed: {$e->getMessage()}",
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |                 $e->getCode(),
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |                 $e,
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |                 $sql ?? '',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |                 $values
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |             );
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |         }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |      * @return false|string
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |     public function lastInsertId(): ?string
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |         return $this->lastId();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |      * @return string|null
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |     public function getLogSQL(): ?string
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |         return $this->logSQL ?? "";
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 318 |  |  |     }
 | 
            
                                                        
            
                                    
            
            
                | 319 |  |  | } |