@@ -2,24 +2,24 @@ discard block |
||
| 2 | 2 | namespace Maphper\DataSource; |
| 3 | 3 | |
| 4 | 4 | class DatabaseSelect { |
| 5 | - private $resultCache = []; |
|
| 6 | - private $selectBuilder; |
|
| 7 | - private $whereBuilder; |
|
| 8 | - private $adapter; |
|
| 9 | - private $databaseModify; |
|
| 10 | - private $defaultSort; |
|
| 11 | - private $table; |
|
| 5 | + private $resultCache = []; |
|
| 6 | + private $selectBuilder; |
|
| 7 | + private $whereBuilder; |
|
| 8 | + private $adapter; |
|
| 9 | + private $databaseModify; |
|
| 10 | + private $defaultSort; |
|
| 11 | + private $table; |
|
| 12 | 12 | |
| 13 | - public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, $defaultSort, $table) { |
|
| 14 | - $this->adapter = $adapter; |
|
| 15 | - $this->databaseModify = $databaseModify; |
|
| 16 | - $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
|
| 17 | - $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
| 18 | - $this->defaultSort = $defaultSort; |
|
| 19 | - $this->table = $table; |
|
| 20 | - } |
|
| 13 | + public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, $defaultSort, $table) { |
|
| 14 | + $this->adapter = $adapter; |
|
| 15 | + $this->databaseModify = $databaseModify; |
|
| 16 | + $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
|
| 17 | + $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
| 18 | + $this->defaultSort = $defaultSort; |
|
| 19 | + $this->table = $table; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - public function findByField(array $fields, $options = []) { |
|
| 22 | + public function findByField(array $fields, $options = []) { |
|
| 23 | 23 | $cacheId = md5(serialize(func_get_args())); |
| 24 | 24 | if (!isset($this->resultCache[$cacheId])) { |
| 25 | 25 | $query = $this->whereBuilder->createSql($fields); |
@@ -39,11 +39,11 @@ discard block |
||
| 39 | 39 | return $this->resultCache[$cacheId]; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - private function selectQuery(\Maphper\Lib\Query $query) { |
|
| 43 | - return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
| 44 | - } |
|
| 42 | + private function selectQuery(\Maphper\Lib\Query $query) { |
|
| 43 | + return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - public function clearResultCache() { |
|
| 47 | - $this->resultCache = []; |
|
| 48 | - } |
|
| 46 | + public function clearResultCache() { |
|
| 47 | + $this->resultCache = []; |
|
| 48 | + } |
|
| 49 | 49 | } |
@@ -6,17 +6,17 @@ 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 = '*'; |
| 13 | 13 | private $defaultSort; |
| 14 | 14 | private $adapter; |
| 15 | 15 | private $crudBuilder; |
| 16 | - private $selectBuilder; |
|
| 17 | - private $whereBuilder; |
|
| 18 | - private $databaseModify; |
|
| 19 | - private $databaseSelect; |
|
| 16 | + private $selectBuilder; |
|
| 17 | + private $whereBuilder; |
|
| 18 | + private $databaseModify; |
|
| 19 | + private $databaseSelect; |
|
| 20 | 20 | |
| 21 | 21 | public function __construct($db, $table, $primaryKey = 'id', array $options = []) { |
| 22 | 22 | $this->options = new DatabaseOptions($db, $options); |
@@ -27,13 +27,13 @@ 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 | |
| 34 | 34 | $defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort') : implode(', ', $this->primaryKey); |
| 35 | - $this->databaseModify = new DatabaseModify($this->adapter, $this->options->getEditMode(), $this->table); |
|
| 36 | - $this->databaseSelect = new DatabaseSelect($this->adapter, $this->databaseModify, $defaultSort, $this->table); |
|
| 35 | + $this->databaseModify = new DatabaseModify($this->adapter, $this->options->getEditMode(), $this->table); |
|
| 36 | + $this->databaseSelect = new DatabaseSelect($this->adapter, $this->databaseModify, $defaultSort, $this->table); |
|
| 37 | 37 | |
| 38 | 38 | $this->databaseModify->optimizeColumns(); |
| 39 | 39 | } |
@@ -78,18 +78,18 @@ discard block |
||
| 78 | 78 | } |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - private function determineAggregateResult($result, $group, $field) { |
|
| 82 | - if ($group != null) { |
|
| 83 | - $ret = []; |
|
| 84 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 85 | - return $ret; |
|
| 86 | - } |
|
| 87 | - else if (isset($result[0])) return $result[0]->val; |
|
| 88 | - else return 0; |
|
| 89 | - } |
|
| 81 | + private function determineAggregateResult($result, $group, $field) { |
|
| 82 | + if ($group != null) { |
|
| 83 | + $ret = []; |
|
| 84 | + foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 85 | + return $ret; |
|
| 86 | + } |
|
| 87 | + else if (isset($result[0])) return $result[0]->val; |
|
| 88 | + else return 0; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | 91 | public function findByField(array $fields, $options = []) { |
| 92 | - return $this->databaseSelect->findByField($fields, $options); |
|
| 92 | + return $this->databaseSelect->findByField($fields, $options); |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | public function deleteByField(array $fields, array $options = []) { |
@@ -102,22 +102,22 @@ discard block |
||
| 102 | 102 | $this->databaseSelect->clearResultCache(); |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - private function getIfNew($data) { |
|
| 106 | - $new = false; |
|
| 107 | - foreach ($this->primaryKey as $k) { |
|
| 108 | - if (empty($data->$k)) { |
|
| 109 | - $data->$k = null; |
|
| 110 | - $new = true; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - return $new; |
|
| 114 | - } |
|
| 105 | + private function getIfNew($data) { |
|
| 106 | + $new = false; |
|
| 107 | + foreach ($this->primaryKey as $k) { |
|
| 108 | + if (empty($data->$k)) { |
|
| 109 | + $data->$k = null; |
|
| 110 | + $new = true; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + return $new; |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | 116 | public function save($data, $tryagain = true) { |
| 117 | - $new = $this->getIfNew($data); |
|
| 117 | + $new = $this->getIfNew($data); |
|
| 118 | 118 | |
| 119 | 119 | try { |
| 120 | - $result = $this->insert($this->table, $this->primaryKey, $data); |
|
| 120 | + $result = $this->insert($this->table, $this->primaryKey, $data); |
|
| 121 | 121 | |
| 122 | 122 | //If there was an error but PDO is silent, trigger the catch block anyway |
| 123 | 123 | if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
@@ -135,25 +135,25 @@ discard block |
||
| 135 | 135 | $this->updateCache($data); |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | - private function getTryAgain($tryagain) { |
|
| 139 | - return $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
|
| 140 | - } |
|
| 138 | + private function getTryAgain($tryagain) { |
|
| 139 | + return $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - private function updatePK($data, $new) { |
|
| 143 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 144 | - } |
|
| 142 | + private function updatePK($data, $new) { |
|
| 143 | + if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - private function checkIfUpdateWorked($data) { |
|
| 147 | - $updateWhere = $this->whereBuilder->createSql($data); |
|
| 148 | - $matched = $this->findByField($updateWhere['args']); |
|
| 149 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 150 | - } |
|
| 146 | + private function checkIfUpdateWorked($data) { |
|
| 147 | + $updateWhere = $this->whereBuilder->createSql($data); |
|
| 148 | + $matched = $this->findByField($updateWhere['args']); |
|
| 149 | + if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - private function updateCache($data) { |
|
| 153 | - $pkValue = $data->{$this->primaryKey[0]}; |
|
| 152 | + private function updateCache($data) { |
|
| 153 | + $pkValue = $data->{$this->primaryKey[0]}; |
|
| 154 | 154 | if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
| 155 | 155 | else $this->cache[$pkValue] = $data; |
| 156 | - } |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | 158 | private function insert($table, array $primaryKey, $data) { |
| 159 | 159 | $error = 0; |
@@ -165,20 +165,20 @@ discard block |
||
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | if ($error || $result->errorCode() !== '00000') { |
| 168 | - $result = $this->tryUpdate($table, $primaryKey, $data); |
|
| 169 | - } |
|
| 168 | + $result = $this->tryUpdate($table, $primaryKey, $data); |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | 171 | return $result; |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - private function tryUpdate($table, array $primaryKey, $data) { |
|
| 175 | - $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
| 176 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 174 | + private function tryUpdate($table, array $primaryKey, $data) { |
|
| 175 | + $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
| 176 | + if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 177 | 177 | |
| 178 | - return $result; |
|
| 179 | - } |
|
| 178 | + return $result; |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - private function selectQuery(\Maphper\Lib\Query $query) { |
|
| 182 | - return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
| 183 | - } |
|
| 181 | + private function selectQuery(\Maphper\Lib\Query $query) { |
|
| 182 | + return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
| 183 | + } |
|
| 184 | 184 | } |
@@ -2,21 +2,21 @@ |
||
| 2 | 2 | namespace Maphper\DataSource; |
| 3 | 3 | |
| 4 | 4 | class DatabaseModify { |
| 5 | - private $adapter; |
|
| 6 | - private $alterDb; |
|
| 7 | - private $table; |
|
| 5 | + private $adapter; |
|
| 6 | + private $alterDb; |
|
| 7 | + private $table; |
|
| 8 | 8 | |
| 9 | - public function __construct(DatabaseAdapter $adapter, $alterDb, $table) { |
|
| 10 | - $this->adapter = $adapter; |
|
| 11 | - $this->alterDb = $alterDb; |
|
| 12 | - $this->table = $table; |
|
| 13 | - } |
|
| 9 | + public function __construct(DatabaseAdapter $adapter, $alterDb, $table) { |
|
| 10 | + $this->adapter = $adapter; |
|
| 11 | + $this->alterDb = $alterDb; |
|
| 12 | + $this->table = $table; |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | - public function addIndex($args) { |
|
| 15 | + public function addIndex($args) { |
|
| 16 | 16 | if (Database::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | - public function optimizeColumns() { |
|
| 20 | - if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
| 21 | - } |
|
| 19 | + public function optimizeColumns() { |
|
| 20 | + if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
| 21 | + } |
|
| 22 | 22 | } |