@@ -1,14 +1,14 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace Maphper\DataSource; |
| 3 | 3 | class StmtCache { |
| 4 | - private $pdo; |
|
| 5 | - private $queryCache = []; |
|
| 4 | + private $pdo; |
|
| 5 | + private $queryCache = []; |
|
| 6 | 6 | |
| 7 | - public function __construct(\PDO $pdo) { |
|
| 8 | - $this->pdo = $pdo; |
|
| 9 | - } |
|
| 7 | + public function __construct(\PDO $pdo) { |
|
| 8 | + $this->pdo = $pdo; |
|
| 9 | + } |
|
| 10 | 10 | |
| 11 | - public function getCachedStmt($sql) { |
|
| 11 | + public function getCachedStmt($sql) { |
|
| 12 | 12 | $queryId = $this->getQueryId($sql); |
| 13 | 13 | if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId]; |
| 14 | 14 | else { |
@@ -18,11 +18,11 @@ discard block |
||
| 18 | 18 | return $stmt; |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | - private function getQueryId($sql) { |
|
| 22 | - return md5($sql); |
|
| 23 | - } |
|
| 21 | + private function getQueryId($sql) { |
|
| 22 | + return md5($sql); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - public function deleteQueryFromCache($sql) { |
|
| 26 | - unset($this->queryCache[$this->getQueryId($sql)]); |
|
| 27 | - } |
|
| 25 | + public function deleteQueryFromCache($sql) { |
|
| 26 | + unset($this->queryCache[$this->getQueryId($sql)]); |
|
| 27 | + } |
|
| 28 | 28 | } |
@@ -8,7 +8,7 @@ discard block |
||
| 8 | 8 | $this->pdo = $pdo; |
| 9 | 9 | //Set to strict mode to detect 'out of range' errors, action at a distance but it needs to be set for all INSERT queries |
| 10 | 10 | $this->pdo->query('SET sql_mode = STRICT_ALL_TABLES'); |
| 11 | - $this->stmtCache = new StmtCache($pdo); |
|
| 11 | + $this->stmtCache = new StmtCache($pdo); |
|
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | public function quote($str) { |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | public function query(\Maphper\Lib\Query $query) { |
| 19 | 19 | $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
| 20 | 20 | $args = $query->getArgs(); |
| 21 | - $stmt->execute($args); |
|
| 21 | + $stmt->execute($args); |
|
| 22 | 22 | |
| 23 | 23 | return $stmt; |
| 24 | 24 | } |
@@ -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,20 +188,20 @@ 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 | |
| 204 | - private function selectQuery(\Maphper\Lib\Query $query) { |
|
| 205 | - return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
| 206 | - } |
|
| 204 | + private function selectQuery(\Maphper\Lib\Query $query) { |
|
| 205 | + return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
| 206 | + } |
|
| 207 | 207 | } |
@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | |
| 8 | 8 | public function __construct(\PDO $pdo) { |
| 9 | 9 | $this->pdo = $pdo; |
| 10 | - $this->stmtCache = new StmtCache($pdo); |
|
| 10 | + $this->stmtCache = new StmtCache($pdo); |
|
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | public function quote($str) { |
@@ -15,19 +15,19 @@ discard block |
||
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | public function query(\Maphper\Lib\Query $query) { |
| 18 | - $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
|
| 18 | + $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
|
| 19 | 19 | $args = $query->getArgs(); |
| 20 | 20 | |
| 21 | - //Handle SQLite when PDO_ERRMODE is set to SILENT |
|
| 22 | - if ($stmt === false) throw new \Exception('Invalid query'); |
|
| 21 | + //Handle SQLite when PDO_ERRMODE is set to SILENT |
|
| 22 | + if ($stmt === false) throw new \Exception('Invalid query'); |
|
| 23 | 23 | |
| 24 | - $stmt->execute($args); |
|
| 25 | - if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') { |
|
| 24 | + $stmt->execute($args); |
|
| 25 | + if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') { |
|
| 26 | 26 | $this->stmtCache->deleteQueryFromCache($query->getSql()); |
| 27 | 27 | return $this->query($query); |
| 28 | - } |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - return $stmt; |
|
| 30 | + return $stmt; |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | public function lastInsertId() { |