@@ -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); |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | else $limit = ''; |
| 123 | 123 | |
| 124 | 124 | $query = $this->selectBuilder->createSql($fields, $mode); |
| 125 | - $query['sql'] = array_filter($query['sql']); |
|
| 125 | + $query['sql'] = array_filter($query['sql']); |
|
| 126 | 126 | $this->adapter->query($this->crudBuilder->delete($this->table, $query['sql'], $query['args'], $limit)); |
| 127 | 127 | $this->addIndex(array_keys($query['args'])); |
| 128 | 128 | |
@@ -131,22 +131,22 @@ discard block |
||
| 131 | 131 | $this->resultCache = []; |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - private function getIfNew($data) { |
|
| 135 | - $new = false; |
|
| 136 | - foreach ($this->primaryKey as $k) { |
|
| 137 | - if (empty($data->$k)) { |
|
| 138 | - $data->$k = null; |
|
| 139 | - $new = true; |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - return $new; |
|
| 143 | - } |
|
| 134 | + private function getIfNew($data) { |
|
| 135 | + $new = false; |
|
| 136 | + foreach ($this->primaryKey as $k) { |
|
| 137 | + if (empty($data->$k)) { |
|
| 138 | + $data->$k = null; |
|
| 139 | + $new = true; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + return $new; |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | 145 | public function save($data, $tryagain = true) { |
| 146 | - $new = $this->getIfNew($data); |
|
| 146 | + $new = $this->getIfNew($data); |
|
| 147 | 147 | |
| 148 | 148 | try { |
| 149 | - $result = $this->insert($this->table, $this->primaryKey, $data); |
|
| 149 | + $result = $this->insert($this->table, $this->primaryKey, $data); |
|
| 150 | 150 | |
| 151 | 151 | //If there was an error but PDO is silent, trigger the catch block anyway |
| 152 | 152 | if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
@@ -164,26 +164,26 @@ discard block |
||
| 164 | 164 | $this->updateCache($data); |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - private function getTryAgain($tryagain) { |
|
| 168 | - return $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
|
| 169 | - } |
|
| 167 | + private function getTryAgain($tryagain) { |
|
| 168 | + return $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - private function updatePK($data, $new) { |
|
| 172 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 173 | - } |
|
| 171 | + private function updatePK($data, $new) { |
|
| 172 | + if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | - private function checkIfUpdateWorked($data) { |
|
| 176 | - $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
|
| 177 | - $matched = $this->findByField($updateWhere->getArgs()); |
|
| 175 | + private function checkIfUpdateWorked($data) { |
|
| 176 | + $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
|
| 177 | + $matched = $this->findByField($updateWhere->getArgs()); |
|
| 178 | 178 | |
| 179 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 180 | - } |
|
| 179 | + if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | - private function updateCache($data) { |
|
| 183 | - $pkValue = $data->{$this->primaryKey[0]}; |
|
| 182 | + private function updateCache($data) { |
|
| 183 | + $pkValue = $data->{$this->primaryKey[0]}; |
|
| 184 | 184 | if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
| 185 | 185 | else $this->cache[$pkValue] = $data; |
| 186 | - } |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | 188 | private function insert($table, array $primaryKey, $data) { |
| 189 | 189 | $error = 0; |
@@ -195,16 +195,16 @@ discard block |
||
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | if ($error || $result->errorCode() !== '00000') { |
| 198 | - $result = $this->tryUpdate($table, $primaryKey, $data); |
|
| 199 | - } |
|
| 198 | + $result = $this->tryUpdate($table, $primaryKey, $data); |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | 201 | return $result; |
| 202 | 202 | } |
| 203 | 203 | |
| 204 | - private function tryUpdate($table, array $primaryKey, $data) { |
|
| 205 | - $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
| 206 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 204 | + private function tryUpdate($table, array $primaryKey, $data) { |
|
| 205 | + $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
| 206 | + if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 207 | 207 | |
| 208 | - return $result; |
|
| 209 | - } |
|
| 208 | + return $result; |
|
| 209 | + } |
|
| 210 | 210 | } |