@@ -10,7 +10,9 @@ discard block |
||
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | public function getAdapter() { |
| 13 | - if (!($this->db instanceof \PDO)) return $this->db; |
|
| 13 | + if (!($this->db instanceof \PDO)) { |
|
| 14 | + return $this->db; |
|
| 15 | + } |
|
| 14 | 16 | |
| 15 | 17 | $adapter = '\\Maphper\\DataSource\\' . ucfirst($this->db->getAttribute(\PDO::ATTR_DRIVER_NAME)) . 'Adapter'; |
| 16 | 18 | |
@@ -18,7 +20,9 @@ discard block |
||
| 18 | 20 | } |
| 19 | 21 | |
| 20 | 22 | public function getEditMode() { |
| 21 | - if (!isset($this->options['editmode'])) return false; |
|
| 23 | + if (!isset($this->options['editmode'])) { |
|
| 24 | + return false; |
|
| 25 | + } |
|
| 22 | 26 | |
| 23 | 27 | return $this->options['editmode'] === true ? Database::EDIT_STRUCTURE | Database::EDIT_INDEX | Database::EDIT_OPTIMISE : $this->options['editmode']; |
| 24 | 28 | } |
@@ -114,7 +114,7 @@ |
||
| 114 | 114 | else $limit = ''; |
| 115 | 115 | |
| 116 | 116 | $query = $this->selectBuilder->createSql($fields, $mode); |
| 117 | - $query['sql'] = array_filter($query['sql']); |
|
| 117 | + $query['sql'] = array_filter($query['sql']); |
|
| 118 | 118 | $this->adapter->query($this->crudBuilder->delete($this->table, $query['sql'], $query['args'], $limit)); |
| 119 | 119 | $this->addIndex(array_keys($query['args'])); |
| 120 | 120 | |
@@ -25,13 +25,13 @@ discard block |
||
| 25 | 25 | $this->crudBuilder = new \Maphper\Lib\CrudBuilder(); |
| 26 | 26 | $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
| 27 | 27 | |
| 28 | - $this->fields = implode(',', array_map([$this->adapter, 'quote'], (array) $this->options->read('fields'))); |
|
| 28 | + $this->fields = implode(',', array_map([$this->adapter, 'quote'], (array)$this->options->read('fields'))); |
|
| 29 | 29 | |
| 30 | - $this->defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort') : implode(', ', $this->primaryKey); |
|
| 30 | + $this->defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort') : implode(', ', $this->primaryKey); |
|
| 31 | 31 | |
| 32 | 32 | $this->alterDb = $this->options->getEditMode(); |
| 33 | 33 | |
| 34 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($table); |
|
| 34 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0, 500) == 1) $this->adapter->optimiseColumns($table); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | public function getPrimaryKey() { |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | //Something has changed, clear any cached results as they may now be incorrect |
| 162 | 162 | $this->resultCache = []; |
| 163 | 163 | $pkValue = $data->{$this->primaryKey[0]}; |
| 164 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 164 | + if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object)array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 165 | 165 | else $this->cache[$pkValue] = $data; |
| 166 | 166 | } |
| 167 | 167 | |
@@ -31,7 +31,9 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | $this->alterDb = $this->options->getEditMode(); |
| 33 | 33 | |
| 34 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($table); |
|
| 34 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) { |
|
| 35 | + $this->adapter->optimiseColumns($table); |
|
| 36 | + } |
|
| 35 | 37 | } |
| 36 | 38 | |
| 37 | 39 | public function getPrimaryKey() { |
@@ -47,20 +49,24 @@ discard block |
||
| 47 | 49 | if (!isset($this->cache[$id])) { |
| 48 | 50 | try { |
| 49 | 51 | $result = $this->adapter->query($this->selectBuilder->select($this->table, [$this->getPrimaryKey()[0] . ' = :id'], [':id' => $id], ['limit' => 1])); |
| 50 | - } |
|
| 51 | - catch (\Exception $e) { |
|
| 52 | + } catch (\Exception $e) { |
|
| 52 | 53 | $this->errors[] = $e; |
| 53 | 54 | } |
| 54 | 55 | |
| 55 | - if (isset($result[0])) $this->cache[$id] = $result[0]; |
|
| 56 | - else return null; |
|
| 56 | + if (isset($result[0])) { |
|
| 57 | + $this->cache[$id] = $result[0]; |
|
| 58 | + } else { |
|
| 59 | + return null; |
|
| 60 | + } |
|
| 57 | 61 | } |
| 58 | 62 | return $this->cache[$id]; |
| 59 | 63 | } |
| 60 | 64 | |
| 61 | 65 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
| 62 | 66 | //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 |
| 63 | - if (is_array($field)) $field = $field[0]; |
|
| 67 | + if (is_array($field)) { |
|
| 68 | + $field = $field[0]; |
|
| 69 | + } |
|
| 64 | 70 | $query = $this->selectBuilder->createSql($criteria, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
| 65 | 71 | |
| 66 | 72 | try { |
@@ -68,22 +74,27 @@ discard block |
||
| 68 | 74 | $this->addIndex(explode(',', $group)); |
| 69 | 75 | $result = $this->adapter->query($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group)); |
| 70 | 76 | |
| 71 | - if (isset($result[0]) && $group == null) return $result[0]->val; |
|
| 72 | - else if ($group != null) { |
|
| 77 | + if (isset($result[0]) && $group == null) { |
|
| 78 | + return $result[0]->val; |
|
| 79 | + } else if ($group != null) { |
|
| 73 | 80 | $ret = []; |
| 74 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 81 | + foreach ($result as $res) { |
|
| 82 | + $ret[$res->$field] = $res->val; |
|
| 83 | + } |
|
| 75 | 84 | return $ret; |
| 85 | + } else { |
|
| 86 | + return 0; |
|
| 76 | 87 | } |
| 77 | - else return 0; |
|
| 78 | - } |
|
| 79 | - catch (\Exception $e) { |
|
| 88 | + } catch (\Exception $e) { |
|
| 80 | 89 | $this->errors[] = $e; |
| 81 | 90 | return $group ? [] : 0; |
| 82 | 91 | } |
| 83 | 92 | } |
| 84 | 93 | |
| 85 | 94 | private function addIndex($args) { |
| 86 | - if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
|
| 95 | + if (self::EDIT_INDEX & $this->alterDb) { |
|
| 96 | + $this->adapter->addIndex($this->table, $args); |
|
| 97 | + } |
|
| 87 | 98 | } |
| 88 | 99 | |
| 89 | 100 | public function findByField(array $fields, $options = []) { |
@@ -91,7 +102,9 @@ discard block |
||
| 91 | 102 | if (!isset($this->resultCache[$cacheId])) { |
| 92 | 103 | $query = $this->selectBuilder->createSql($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
| 93 | 104 | |
| 94 | - if (!isset($options['order'])) $options['order'] = $this->defaultSort; |
|
| 105 | + if (!isset($options['order'])) { |
|
| 106 | + $options['order'] = $this->defaultSort; |
|
| 107 | + } |
|
| 95 | 108 | |
| 96 | 109 | $query['sql'] = array_filter($query['sql']); |
| 97 | 110 | |
@@ -99,8 +112,7 @@ discard block |
||
| 99 | 112 | $this->resultCache[$cacheId] = $this->adapter->query($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options)); |
| 100 | 113 | $this->addIndex(array_keys($query['args'])); |
| 101 | 114 | $this->addIndex(explode(',', $options['order'])); |
| 102 | - } |
|
| 103 | - catch (\Exception $e) { |
|
| 115 | + } catch (\Exception $e) { |
|
| 104 | 116 | $this->errors[] = $e; |
| 105 | 117 | $this->resultCache[$cacheId] = []; |
| 106 | 118 | } |
@@ -109,9 +121,14 @@ discard block |
||
| 109 | 121 | } |
| 110 | 122 | |
| 111 | 123 | public function deleteByField(array $fields, array $options = [], $mode = null) { |
| 112 | - if ($mode == null) $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND; |
|
| 113 | - if (isset($options['limit']) != null) $limit = ' LIMIT ' . $options['limit']; |
|
| 114 | - else $limit = ''; |
|
| 124 | + if ($mode == null) { |
|
| 125 | + $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND; |
|
| 126 | + } |
|
| 127 | + if (isset($options['limit']) != null) { |
|
| 128 | + $limit = ' LIMIT ' . $options['limit']; |
|
| 129 | + } else { |
|
| 130 | + $limit = ''; |
|
| 131 | + } |
|
| 115 | 132 | |
| 116 | 133 | $query = $this->selectBuilder->createSql($fields, $mode); |
| 117 | 134 | $query['sql'] = array_filter($query['sql']); |
@@ -137,7 +154,9 @@ discard block |
||
| 137 | 154 | $result = $this->insert($this->table, $this->primaryKey, $data); |
| 138 | 155 | |
| 139 | 156 | //If there was an error but PDO is silent, trigger the catch block anyway |
| 140 | - if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
|
| 157 | + if ($result->errorCode() !== '00000') { |
|
| 158 | + throw new \Exception('Could not insert into ' . $this->table); |
|
| 159 | + } |
|
| 141 | 160 | |
| 142 | 161 | if ($tryagain === false && $result->rowCount() === 0) { |
| 143 | 162 | |
@@ -145,32 +164,38 @@ discard block |
||
| 145 | 164 | |
| 146 | 165 | $matched = $this->findByField($updateWhere->getArgs()); |
| 147 | 166 | |
| 148 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 167 | + if (count($matched) == 0) { |
|
| 168 | + throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 169 | + } |
|
| 149 | 170 | } |
| 150 | 171 | |
| 151 | - } |
|
| 152 | - catch (\Exception $e) { |
|
| 172 | + } catch (\Exception $e) { |
|
| 153 | 173 | if ($tryagain) { |
| 154 | 174 | $this->adapter->alterDatabase($this->table, $this->primaryKey, $data); |
| 155 | 175 | $this->save($data, false); |
| 176 | + } else { |
|
| 177 | + throw $e; |
|
| 156 | 178 | } |
| 157 | - else throw $e; |
|
| 158 | 179 | } |
| 159 | 180 | //TODO: This will error if the primary key is a private field |
| 160 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 181 | + if ($new && count($this->primaryKey) == 1) { |
|
| 182 | + $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 183 | + } |
|
| 161 | 184 | //Something has changed, clear any cached results as they may now be incorrect |
| 162 | 185 | $this->resultCache = []; |
| 163 | 186 | $pkValue = $data->{$this->primaryKey[0]}; |
| 164 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 165 | - else $this->cache[$pkValue] = $data; |
|
| 187 | + if (isset($this->cache[$pkValue])) { |
|
| 188 | + $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 189 | + } else { |
|
| 190 | + $this->cache[$pkValue] = $data; |
|
| 191 | + } |
|
| 166 | 192 | } |
| 167 | 193 | |
| 168 | 194 | private function insert($table, array $primaryKey, $data) { |
| 169 | 195 | $error = 0; |
| 170 | 196 | try { |
| 171 | 197 | $result = $this->adapter->query($this->crudBuilder->insert($table, $data)); |
| 172 | - } |
|
| 173 | - catch (\Exception $e) { |
|
| 198 | + } catch (\Exception $e) { |
|
| 174 | 199 | $error = 1; |
| 175 | 200 | } |
| 176 | 201 | |