@@ -24,14 +24,15 @@ |
||
| 24 | 24 | if (!isset($this->resultCache[$cacheId])) { |
| 25 | 25 | $query = $this->whereBuilder->createSql($fields); |
| 26 | 26 | |
| 27 | - if (!isset($options['order'])) $options['order'] = $this->defaultSort; |
|
| 27 | + if (!isset($options['order'])) { |
|
| 28 | + $options['order'] = $this->defaultSort; |
|
| 29 | + } |
|
| 28 | 30 | |
| 29 | 31 | try { |
| 30 | 32 | $this->resultCache[$cacheId] = $this->selectQuery($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options)); |
| 31 | 33 | $this->databaseModify->addIndex(array_keys($query['args'])); |
| 32 | 34 | $this->databaseModify->addIndex(explode(',', $options['order'])); |
| 33 | - } |
|
| 34 | - catch (\Exception $e) { |
|
| 35 | + } catch (\Exception $e) { |
|
| 35 | 36 | $this->errors[] = $e; |
| 36 | 37 | $this->resultCache[$cacheId] = []; |
| 37 | 38 | } |
@@ -51,19 +51,23 @@ discard block |
||
| 51 | 51 | if (!isset($this->cache[$id])) { |
| 52 | 52 | try { |
| 53 | 53 | $result = $this->selectQuery($this->selectBuilder->select($this->table, $this->getPrimaryKey()[0] . ' = :id', [':id' => $id], ['limit' => 1])); |
| 54 | - } |
|
| 55 | - catch (\Exception $e) { |
|
| 54 | + } catch (\Exception $e) { |
|
| 56 | 55 | } |
| 57 | 56 | |
| 58 | - if (isset($result[0])) $this->cache[$id] = $result[0]; |
|
| 59 | - else return null; |
|
| 57 | + if (isset($result[0])) { |
|
| 58 | + $this->cache[$id] = $result[0]; |
|
| 59 | + } else { |
|
| 60 | + return null; |
|
| 61 | + } |
|
| 60 | 62 | } |
| 61 | 63 | return $this->cache[$id]; |
| 62 | 64 | } |
| 63 | 65 | |
| 64 | 66 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
| 65 | 67 | //Cannot count/sum/max multiple fields, pick the first one. This should only come into play when trying to count() a mapper with multiple primary keys |
| 66 | - if (is_array($field)) $field = $field[0]; |
|
| 68 | + if (is_array($field)) { |
|
| 69 | + $field = $field[0]; |
|
| 70 | + } |
|
| 67 | 71 | $query = $this->whereBuilder->createSql($criteria); |
| 68 | 72 | |
| 69 | 73 | try { |
@@ -72,8 +76,7 @@ discard block |
||
| 72 | 76 | $result = $this->selectQuery($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group)); |
| 73 | 77 | |
| 74 | 78 | return $this->determineAggregateResult($result, $group, $field); |
| 75 | - } |
|
| 76 | - catch (\Exception $e) { |
|
| 79 | + } catch (\Exception $e) { |
|
| 77 | 80 | return $group ? [] : 0; |
| 78 | 81 | } |
| 79 | 82 | } |
@@ -81,11 +84,15 @@ discard block |
||
| 81 | 84 | private function determineAggregateResult($result, $group, $field) { |
| 82 | 85 | if ($group != null) { |
| 83 | 86 | $ret = []; |
| 84 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 87 | + foreach ($result as $res) { |
|
| 88 | + $ret[$res->$field] = $res->val; |
|
| 89 | + } |
|
| 85 | 90 | return $ret; |
| 91 | + } else if (isset($result[0])) { |
|
| 92 | + return $result[0]->val; |
|
| 93 | + } else { |
|
| 94 | + return 0; |
|
| 86 | 95 | } |
| 87 | - else if (isset($result[0])) return $result[0]->val; |
|
| 88 | - else return 0; |
|
| 89 | 96 | } |
| 90 | 97 | |
| 91 | 98 | public function findByField(array $fields, $options = []) { |
@@ -120,10 +127,13 @@ discard block |
||
| 120 | 127 | $result = $this->insert($this->table, $this->primaryKey, $data); |
| 121 | 128 | |
| 122 | 129 | //If there was an error but PDO is silent, trigger the catch block anyway |
| 123 | - if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
|
| 124 | - } |
|
| 125 | - catch (\Exception $e) { |
|
| 126 | - if (!$this->getTryAgain($tryagain)) throw $e; |
|
| 130 | + if ($result->errorCode() !== '00000') { |
|
| 131 | + throw new \Exception('Could not insert into ' . $this->table); |
|
| 132 | + } |
|
| 133 | + } catch (\Exception $e) { |
|
| 134 | + if (!$this->getTryAgain($tryagain)) { |
|
| 135 | + throw $e; |
|
| 136 | + } |
|
| 127 | 137 | |
| 128 | 138 | $this->adapter->alterDatabase($this->table, $this->primaryKey, $data); |
| 129 | 139 | $this->save($data, false); |
@@ -140,27 +150,33 @@ discard block |
||
| 140 | 150 | } |
| 141 | 151 | |
| 142 | 152 | private function updatePK($data, $new) { |
| 143 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 153 | + if ($new && count($this->primaryKey) == 1) { |
|
| 154 | + $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 155 | + } |
|
| 144 | 156 | } |
| 145 | 157 | |
| 146 | 158 | private function checkIfUpdateWorked($data) { |
| 147 | 159 | $updateWhere = $this->whereBuilder->createSql($data); |
| 148 | 160 | $matched = $this->findByField($updateWhere['args']); |
| 149 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 161 | + if (count($matched) == 0) { |
|
| 162 | + throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 163 | + } |
|
| 150 | 164 | } |
| 151 | 165 | |
| 152 | 166 | private function updateCache($data) { |
| 153 | 167 | $pkValue = $data->{$this->primaryKey[0]}; |
| 154 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 155 | - else $this->cache[$pkValue] = $data; |
|
| 168 | + if (isset($this->cache[$pkValue])) { |
|
| 169 | + $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 170 | + } else { |
|
| 171 | + $this->cache[$pkValue] = $data; |
|
| 172 | + } |
|
| 156 | 173 | } |
| 157 | 174 | |
| 158 | 175 | private function insert($table, array $primaryKey, $data) { |
| 159 | 176 | $error = 0; |
| 160 | 177 | try { |
| 161 | 178 | $result = $this->adapter->query($this->crudBuilder->insert($table, $data)); |
| 162 | - } |
|
| 163 | - catch (\Exception $e) { |
|
| 179 | + } catch (\Exception $e) { |
|
| 164 | 180 | $error = 1; |
| 165 | 181 | } |
| 166 | 182 | |
@@ -173,7 +189,9 @@ discard block |
||
| 173 | 189 | |
| 174 | 190 | private function tryUpdate($table, array $primaryKey, $data) { |
| 175 | 191 | $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
| 176 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 192 | + if ($result->rowCount() === 0) { |
|
| 193 | + $this->checkIfUpdateWorked($data); |
|
| 194 | + } |
|
| 177 | 195 | |
| 178 | 196 | return $result; |
| 179 | 197 | } |
@@ -13,10 +13,14 @@ |
||
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | public function addIndex($args) { |
| 16 | - if (Database::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
|
| 16 | + if (Database::EDIT_INDEX & $this->alterDb) { |
|
| 17 | + $this->adapter->addIndex($this->table, $args); |
|
| 18 | + } |
|
| 17 | 19 | } |
| 18 | 20 | |
| 19 | 21 | public function optimizeColumns() { |
| 20 | - if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
| 22 | + if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) { |
|
| 23 | + $this->adapter->optimiseColumns($this->table); |
|
| 24 | + } |
|
| 21 | 25 | } |
| 22 | 26 | } |