@@ -2,18 +2,18 @@ discard block |
||
| 2 | 2 | namespace Maphper\Lib\Sql; |
| 3 | 3 | |
| 4 | 4 | class WhereBuilder { |
| 5 | - //Needs to be broken up into better methods |
|
| 5 | + //Needs to be broken up into better methods |
|
| 6 | 6 | public function createSql($fields, $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND){ |
| 7 | 7 | $args = []; |
| 8 | 8 | $sql = []; |
| 9 | 9 | |
| 10 | 10 | foreach ($fields as $key => $value) { |
| 11 | - if ($value instanceof \DateTime) { |
|
| 12 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
| 13 | - else $value = $value->format('Y-m-d H:i:s'); |
|
| 14 | - } |
|
| 11 | + if ($value instanceof \DateTime) { |
|
| 12 | + if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
| 13 | + else $value = $value->format('Y-m-d H:i:s'); |
|
| 14 | + } |
|
| 15 | 15 | |
| 16 | - if (is_object($value)) continue; |
|
| 16 | + if (is_object($value)) continue; |
|
| 17 | 17 | if (is_numeric($key) && is_array($value)) { |
| 18 | 18 | $result = $this->createSql($value, $key); |
| 19 | 19 | foreach ($result['args'] as $arg_key => $arg) $args[$arg_key] = $arg; |
@@ -64,17 +64,17 @@ discard block |
||
| 64 | 64 | return ['args' => $args, 'sql' => $query]; |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - private function getOperator($mode) { |
|
| 68 | - $operator = ""; |
|
| 67 | + private function getOperator($mode) { |
|
| 68 | + $operator = ""; |
|
| 69 | 69 | |
| 70 | - if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE'; |
|
| 71 | - else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&'; |
|
| 72 | - else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
| 73 | - else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
| 74 | - else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!='; |
|
| 70 | + if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE'; |
|
| 71 | + else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&'; |
|
| 72 | + else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
| 73 | + else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
| 74 | + else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!='; |
|
| 75 | 75 | |
| 76 | - if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
| 76 | + if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
| 77 | 77 | |
| 78 | - return $operator; |
|
| 79 | - } |
|
| 78 | + return $operator; |
|
| 79 | + } |
|
| 80 | 80 | } |
@@ -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,8 +15,8 @@ discard block |
||
| 15 | 15 | private $alterDb = false; |
| 16 | 16 | private $adapter; |
| 17 | 17 | private $crudBuilder; |
| 18 | - private $selectBuilder; |
|
| 19 | - private $whereBuilder; |
|
| 18 | + private $selectBuilder; |
|
| 19 | + private $whereBuilder; |
|
| 20 | 20 | |
| 21 | 21 | public function __construct($db, $table, $primaryKey = 'id', array $options = []) { |
| 22 | 22 | $this->options = new DatabaseOptions($db, $options); |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | |
| 28 | 28 | $this->crudBuilder = new \Maphper\Lib\CrudBuilder(); |
| 29 | 29 | $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
| 30 | - $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
| 30 | + $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
| 31 | 31 | |
| 32 | 32 | $this->fields = implode(',', array_map([$this->adapter, 'quote'], (array) $this->options->read('fields'))); |
| 33 | 33 | |
@@ -38,9 +38,9 @@ discard block |
||
| 38 | 38 | $this->optimizeColumns(); |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - private function optimizeColumns() { |
|
| 42 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
| 43 | - } |
|
| 41 | + private function optimizeColumns() { |
|
| 42 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | 45 | public function getPrimaryKey() { |
| 46 | 46 | return $this->primaryKey; |
@@ -82,15 +82,15 @@ discard block |
||
| 82 | 82 | } |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - private function determineAggregateResult($result, $group, $field) { |
|
| 86 | - if ($group != null) { |
|
| 87 | - $ret = []; |
|
| 88 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 89 | - return $ret; |
|
| 90 | - } |
|
| 91 | - else if (isset($result[0])) return $result[0]->val; |
|
| 92 | - else return 0; |
|
| 93 | - } |
|
| 85 | + private function determineAggregateResult($result, $group, $field) { |
|
| 86 | + if ($group != null) { |
|
| 87 | + $ret = []; |
|
| 88 | + foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 89 | + return $ret; |
|
| 90 | + } |
|
| 91 | + else if (isset($result[0])) return $result[0]->val; |
|
| 92 | + else return 0; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | 95 | private function addIndex($args) { |
| 96 | 96 | if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
@@ -126,22 +126,22 @@ discard block |
||
| 126 | 126 | $this->resultCache = []; |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | - private function getIfNew($data) { |
|
| 130 | - $new = false; |
|
| 131 | - foreach ($this->primaryKey as $k) { |
|
| 132 | - if (empty($data->$k)) { |
|
| 133 | - $data->$k = null; |
|
| 134 | - $new = true; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - return $new; |
|
| 138 | - } |
|
| 129 | + private function getIfNew($data) { |
|
| 130 | + $new = false; |
|
| 131 | + foreach ($this->primaryKey as $k) { |
|
| 132 | + if (empty($data->$k)) { |
|
| 133 | + $data->$k = null; |
|
| 134 | + $new = true; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + return $new; |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | 140 | public function save($data, $tryagain = true) { |
| 141 | - $new = $this->getIfNew($data); |
|
| 141 | + $new = $this->getIfNew($data); |
|
| 142 | 142 | |
| 143 | 143 | try { |
| 144 | - $result = $this->insert($this->table, $this->primaryKey, $data); |
|
| 144 | + $result = $this->insert($this->table, $this->primaryKey, $data); |
|
| 145 | 145 | |
| 146 | 146 | //If there was an error but PDO is silent, trigger the catch block anyway |
| 147 | 147 | if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
@@ -159,26 +159,26 @@ discard block |
||
| 159 | 159 | $this->updateCache($data); |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | - private function getTryAgain($tryagain) { |
|
| 163 | - return $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
|
| 164 | - } |
|
| 162 | + private function getTryAgain($tryagain) { |
|
| 163 | + return $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - private function updatePK($data, $new) { |
|
| 167 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 168 | - } |
|
| 166 | + private function updatePK($data, $new) { |
|
| 167 | + if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - private function checkIfUpdateWorked($data) { |
|
| 171 | - $updateWhere = $this->whereBuilder->createSql($data); |
|
| 172 | - $matched = $this->findByField($updateWhere['args']); |
|
| 170 | + private function checkIfUpdateWorked($data) { |
|
| 171 | + $updateWhere = $this->whereBuilder->createSql($data); |
|
| 172 | + $matched = $this->findByField($updateWhere['args']); |
|
| 173 | 173 | |
| 174 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 175 | - } |
|
| 174 | + if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - private function updateCache($data) { |
|
| 178 | - $pkValue = $data->{$this->primaryKey[0]}; |
|
| 177 | + private function updateCache($data) { |
|
| 178 | + $pkValue = $data->{$this->primaryKey[0]}; |
|
| 179 | 179 | if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
| 180 | 180 | else $this->cache[$pkValue] = $data; |
| 181 | - } |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | 183 | private function insert($table, array $primaryKey, $data) { |
| 184 | 184 | $error = 0; |
@@ -190,20 +190,20 @@ discard block |
||
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | if ($error || $result->errorCode() !== '00000') { |
| 193 | - $result = $this->tryUpdate($table, $primaryKey, $data); |
|
| 194 | - } |
|
| 193 | + $result = $this->tryUpdate($table, $primaryKey, $data); |
|
| 194 | + } |
|
| 195 | 195 | |
| 196 | 196 | return $result; |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | - private function tryUpdate($table, array $primaryKey, $data) { |
|
| 200 | - $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
| 201 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 199 | + private function tryUpdate($table, array $primaryKey, $data) { |
|
| 200 | + $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
| 201 | + if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 202 | 202 | |
| 203 | - return $result; |
|
| 204 | - } |
|
| 203 | + return $result; |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - private function selectQuery(\Maphper\Lib\Query $query) { |
|
| 207 | - return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
| 208 | - } |
|
| 206 | + private function selectQuery(\Maphper\Lib\Query $query) { |
|
| 207 | + return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
| 208 | + } |
|
| 209 | 209 | } |