@@ -6,7 +6,7 @@ discard block |
||
| 6 | 6 | const EDIT_OPTIMISE = 4; |
| 7 | 7 | |
| 8 | 8 | private $table; |
| 9 | - private $options; |
|
| 9 | + private $options; |
|
| 10 | 10 | private $cache = []; |
| 11 | 11 | private $primaryKey; |
| 12 | 12 | private $fields = '*'; |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | private $alterDb = false; |
| 16 | 16 | private $adapter; |
| 17 | 17 | private $crudBuilder; |
| 18 | - private $selectBuilder; |
|
| 18 | + private $selectBuilder; |
|
| 19 | 19 | |
| 20 | 20 | public function __construct($db, $table, $primaryKey = 'id', array $options = []) { |
| 21 | 21 | $this->options = new DatabaseOptions($db, $options); |
@@ -36,9 +36,9 @@ discard block |
||
| 36 | 36 | $this->optimizeColumns(); |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - private function optimizeColumns() { |
|
| 40 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
| 41 | - } |
|
| 39 | + private function optimizeColumns() { |
|
| 40 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | 43 | public function getPrimaryKey() { |
| 44 | 44 | return $this->primaryKey; |
@@ -80,15 +80,15 @@ discard block |
||
| 80 | 80 | } |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | - private function determineAggregateResult($result, $group, $field) { |
|
| 84 | - if ($group != null) { |
|
| 85 | - $ret = []; |
|
| 86 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 87 | - return $ret; |
|
| 88 | - } |
|
| 89 | - else if (isset($result[0])) return $result[0]->val; |
|
| 90 | - else return 0; |
|
| 91 | - } |
|
| 83 | + private function determineAggregateResult($result, $group, $field) { |
|
| 84 | + if ($group != null) { |
|
| 85 | + $ret = []; |
|
| 86 | + foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 87 | + return $ret; |
|
| 88 | + } |
|
| 89 | + else if (isset($result[0])) return $result[0]->val; |
|
| 90 | + else return 0; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | 93 | private function addIndex($args) { |
| 94 | 94 | if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
@@ -124,22 +124,22 @@ discard block |
||
| 124 | 124 | $this->resultCache = []; |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | - private function getIfNew($data) { |
|
| 128 | - $new = false; |
|
| 129 | - foreach ($this->primaryKey as $k) { |
|
| 130 | - if (empty($data->$k)) { |
|
| 131 | - $data->$k = null; |
|
| 132 | - $new = true; |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - return $new; |
|
| 136 | - } |
|
| 127 | + private function getIfNew($data) { |
|
| 128 | + $new = false; |
|
| 129 | + foreach ($this->primaryKey as $k) { |
|
| 130 | + if (empty($data->$k)) { |
|
| 131 | + $data->$k = null; |
|
| 132 | + $new = true; |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + return $new; |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | 138 | public function save($data, $tryagain = true) { |
| 139 | - $new = $this->getIfNew($data); |
|
| 139 | + $new = $this->getIfNew($data); |
|
| 140 | 140 | |
| 141 | 141 | try { |
| 142 | - $result = $this->insert($this->table, $this->primaryKey, $data); |
|
| 142 | + $result = $this->insert($this->table, $this->primaryKey, $data); |
|
| 143 | 143 | |
| 144 | 144 | //If there was an error but PDO is silent, trigger the catch block anyway |
| 145 | 145 | if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
@@ -157,26 +157,26 @@ discard block |
||
| 157 | 157 | $this->updateCache($data); |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | - private function getTryAgain($tryagain) { |
|
| 161 | - return $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
|
| 162 | - } |
|
| 160 | + private function getTryAgain($tryagain) { |
|
| 161 | + return $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - private function updatePK($data, $new) { |
|
| 165 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 166 | - } |
|
| 164 | + private function updatePK($data, $new) { |
|
| 165 | + if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - private function checkIfUpdateWorked($data) { |
|
| 169 | - $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
|
| 170 | - $matched = $this->findByField($updateWhere->getArgs()); |
|
| 168 | + private function checkIfUpdateWorked($data) { |
|
| 169 | + $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
|
| 170 | + $matched = $this->findByField($updateWhere->getArgs()); |
|
| 171 | 171 | |
| 172 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 173 | - } |
|
| 172 | + if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | - private function updateCache($data) { |
|
| 176 | - $pkValue = $data->{$this->primaryKey[0]}; |
|
| 175 | + private function updateCache($data) { |
|
| 176 | + $pkValue = $data->{$this->primaryKey[0]}; |
|
| 177 | 177 | if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
| 178 | 178 | else $this->cache[$pkValue] = $data; |
| 179 | - } |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | 181 | private function insert($table, array $primaryKey, $data) { |
| 182 | 182 | $error = 0; |
@@ -188,16 +188,16 @@ discard block |
||
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | if ($error || $result->errorCode() !== '00000') { |
| 191 | - $result = $this->tryUpdate($table, $primaryKey, $data); |
|
| 192 | - } |
|
| 191 | + $result = $this->tryUpdate($table, $primaryKey, $data); |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | 194 | return $result; |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | - private function tryUpdate($table, array $primaryKey, $data) { |
|
| 198 | - $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
| 199 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 197 | + private function tryUpdate($table, array $primaryKey, $data) { |
|
| 198 | + $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
| 199 | + if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 200 | 200 | |
| 201 | - return $result; |
|
| 202 | - } |
|
| 201 | + return $result; |
|
| 202 | + } |
|
| 203 | 203 | } |
@@ -29,10 +29,10 @@ discard block |
||
| 29 | 29 | $sql = []; |
| 30 | 30 | |
| 31 | 31 | foreach ($fields as $key => $value) { |
| 32 | - if ($value instanceof \DateTime) { |
|
| 33 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
| 34 | - else $value = $value->format('Y-m-d H:i:s'); |
|
| 35 | - } |
|
| 32 | + if ($value instanceof \DateTime) { |
|
| 33 | + if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
| 34 | + else $value = $value->format('Y-m-d H:i:s'); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | 37 | if (is_numeric($key) && is_array($value)) { |
| 38 | 38 | $result = $this->createSql($value, $key); |
@@ -84,17 +84,17 @@ discard block |
||
| 84 | 84 | return ['args' => $args, 'sql' => $query]; |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - private function getOperator($mode) { |
|
| 88 | - $operator = ""; |
|
| 87 | + private function getOperator($mode) { |
|
| 88 | + $operator = ""; |
|
| 89 | 89 | |
| 90 | - if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE'; |
|
| 91 | - else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&'; |
|
| 92 | - else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
| 93 | - else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
| 94 | - else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!='; |
|
| 90 | + if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE'; |
|
| 91 | + else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&'; |
|
| 92 | + else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
| 93 | + else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
| 94 | + else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!='; |
|
| 95 | 95 | |
| 96 | - if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
| 96 | + if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
| 97 | 97 | |
| 98 | - return $operator; |
|
| 99 | - } |
|
| 98 | + return $operator; |
|
| 99 | + } |
|
| 100 | 100 | } |
@@ -8,7 +8,7 @@ |
||
| 8 | 8 | public function delete($table, $criteria, $args, $limit = null, $offset = null, $order = null) { |
| 9 | 9 | $limit = $limit ? ' LIMIT ' . $limit : ''; |
| 10 | 10 | $offset = $offset ? ' OFFSET ' . $offset : ''; |
| 11 | - $order = $order ? ' ORDER BY ' . $order : ''; |
|
| 11 | + $order = $order ? ' ORDER BY ' . $order : ''; |
|
| 12 | 12 | return new Query('DELETE FROM ' . $table . ' WHERE ' . ($criteria ?: '1 = 1 ') . $order . $limit . $offset, $args); |
| 13 | 13 | } |
| 14 | 14 | |