@@ -2,30 +2,30 @@ discard block |
||
| 2 | 2 | namespace Maphper\DataSource; |
| 3 | 3 | |
| 4 | 4 | class DatabaseSelect { |
| 5 | - private $resultCache = []; |
|
| 6 | - private $idCache = []; |
|
| 7 | - private $selectBuilder; |
|
| 8 | - private $whereBuilder; |
|
| 9 | - private $adapter; |
|
| 10 | - private $databaseModify; |
|
| 11 | - private $defaultSort; |
|
| 12 | - private $table; |
|
| 13 | - |
|
| 14 | - public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, $table) { |
|
| 15 | - $this->adapter = $adapter; |
|
| 16 | - $this->databaseModify = $databaseModify; |
|
| 17 | - $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
|
| 18 | - $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
| 19 | - $this->table = $table; |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - public function findById($id, $pk) { |
|
| 5 | + private $resultCache = []; |
|
| 6 | + private $idCache = []; |
|
| 7 | + private $selectBuilder; |
|
| 8 | + private $whereBuilder; |
|
| 9 | + private $adapter; |
|
| 10 | + private $databaseModify; |
|
| 11 | + private $defaultSort; |
|
| 12 | + private $table; |
|
| 13 | + |
|
| 14 | + public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, $table) { |
|
| 15 | + $this->adapter = $adapter; |
|
| 16 | + $this->databaseModify = $databaseModify; |
|
| 17 | + $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
|
| 18 | + $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
| 19 | + $this->table = $table; |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + public function findById($id, $pk) { |
|
| 23 | 23 | if (!isset($this->idCache[$id])) { |
| 24 | 24 | try { |
| 25 | 25 | $result = $this->selectQuery($this->selectBuilder->select($this->table, $pk . ' = :id', [':id' => $id], ['limit' => 1])); |
| 26 | 26 | } |
| 27 | 27 | catch (\Exception $e) { |
| 28 | - // Don't issue an error if it cannot be found since we return null |
|
| 28 | + // Don't issue an error if it cannot be found since we return null |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | if (isset($result[0])) $this->idCache[$id] = $result[0]; |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | return $this->idCache[$id]; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - public function findByField(array $fields, $options, $defaultSort) { |
|
| 37 | + public function findByField(array $fields, $options, $defaultSort) { |
|
| 38 | 38 | $cacheId = md5(serialize(func_get_args())); |
| 39 | 39 | if (!isset($this->resultCache[$cacheId])) { |
| 40 | 40 | $query = $this->whereBuilder->createSql($fields); |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | return $this->resultCache[$cacheId]; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
|
| 57 | + public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
|
| 58 | 58 | //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 |
| 59 | 59 | if (is_array($field)) $field = $field[0]; |
| 60 | 60 | $query = $this->whereBuilder->createSql($criteria); |
@@ -71,34 +71,34 @@ discard block |
||
| 71 | 71 | } |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - private function determineAggregateResult($result, $group, $field) { |
|
| 75 | - if ($group != null) { |
|
| 76 | - $ret = []; |
|
| 77 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 78 | - return $ret; |
|
| 79 | - } |
|
| 80 | - else if (isset($result[0])) return $result[0]->val; |
|
| 81 | - else return 0; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - private function selectQuery(\Maphper\Lib\Query $query) { |
|
| 85 | - return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - public function clearResultCache() { |
|
| 89 | - $this->resultCache = []; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - public function clearIDCache() { |
|
| 93 | - $this->idCache = []; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - public function updateCache($data, $pkValue) { |
|
| 74 | + private function determineAggregateResult($result, $group, $field) { |
|
| 75 | + if ($group != null) { |
|
| 76 | + $ret = []; |
|
| 77 | + foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 78 | + return $ret; |
|
| 79 | + } |
|
| 80 | + else if (isset($result[0])) return $result[0]->val; |
|
| 81 | + else return 0; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + private function selectQuery(\Maphper\Lib\Query $query) { |
|
| 85 | + return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + public function clearResultCache() { |
|
| 89 | + $this->resultCache = []; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + public function clearIDCache() { |
|
| 93 | + $this->idCache = []; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + public function updateCache($data, $pkValue) { |
|
| 97 | 97 | if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
| 98 | 98 | else $this->cache[$pkValue] = $data; |
| 99 | - } |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - public function deleteIDFromCache($id) { |
|
| 102 | - unset($this->idCache[$id]); |
|
| 103 | - } |
|
| 101 | + public function deleteIDFromCache($id) { |
|
| 102 | + unset($this->idCache[$id]); |
|
| 103 | + } |
|
| 104 | 104 | } |
@@ -6,16 +6,16 @@ discard block |
||
| 6 | 6 | const EDIT_OPTIMISE = 4; |
| 7 | 7 | |
| 8 | 8 | private $table; |
| 9 | - private $options; |
|
| 9 | + private $options; |
|
| 10 | 10 | private $primaryKey; |
| 11 | 11 | private $fields = '*'; |
| 12 | 12 | private $defaultSort; |
| 13 | 13 | private $adapter; |
| 14 | 14 | private $crudBuilder; |
| 15 | - private $whereBuilder; |
|
| 16 | - private $databaseModify; |
|
| 17 | - private $databaseSelect; |
|
| 18 | - private $alterDb; |
|
| 15 | + private $whereBuilder; |
|
| 16 | + private $databaseModify; |
|
| 17 | + private $databaseSelect; |
|
| 18 | + private $alterDb; |
|
| 19 | 19 | |
| 20 | 20 | public function __construct($db, $table, $primaryKey = 'id', array $options = []) { |
| 21 | 21 | $this->options = new DatabaseOptions($db, $options); |
@@ -25,16 +25,16 @@ discard block |
||
| 25 | 25 | $this->primaryKey = is_array($primaryKey) ? $primaryKey : [$primaryKey]; |
| 26 | 26 | |
| 27 | 27 | $this->crudBuilder = new \Maphper\Lib\CrudBuilder(); |
| 28 | - $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
| 28 | + $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
| 29 | 29 | |
| 30 | 30 | $this->fields = implode(',', array_map([$this->adapter, 'quote'], (array) $this->options->read('fields'))); |
| 31 | 31 | |
| 32 | 32 | $this->defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort') : implode(', ', $this->primaryKey); |
| 33 | 33 | |
| 34 | - $this->databaseModify = new DatabaseModify($this->adapter, $this->options->getEditMode(), $this->table); |
|
| 35 | - $this->databaseSelect = new DatabaseSelect($this->adapter, $this->databaseModify, $this->table); |
|
| 34 | + $this->databaseModify = new DatabaseModify($this->adapter, $this->options->getEditMode(), $this->table); |
|
| 35 | + $this->databaseSelect = new DatabaseSelect($this->adapter, $this->databaseModify, $this->table); |
|
| 36 | 36 | |
| 37 | - $this->alterDb = $this->options->getEditMode(); |
|
| 37 | + $this->alterDb = $this->options->getEditMode(); |
|
| 38 | 38 | |
| 39 | 39 | $this->databaseModify->optimizeColumns(); |
| 40 | 40 | } |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | public function findByField(array $fields, $options = []) { |
| 60 | - return $this->databaseSelect->findByField($fields, $options, $this->defaultSort); |
|
| 60 | + return $this->databaseSelect->findByField($fields, $options, $this->defaultSort); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | public function deleteByField(array $fields, array $options = []) { |
@@ -70,22 +70,22 @@ discard block |
||
| 70 | 70 | $this->databaseSelect->clearResultCache(); |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - private function getIfNew($data) { |
|
| 74 | - $new = false; |
|
| 75 | - foreach ($this->primaryKey as $k) { |
|
| 76 | - if (empty($data->$k)) { |
|
| 77 | - $data->$k = null; |
|
| 78 | - $new = true; |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - return $new; |
|
| 82 | - } |
|
| 73 | + private function getIfNew($data) { |
|
| 74 | + $new = false; |
|
| 75 | + foreach ($this->primaryKey as $k) { |
|
| 76 | + if (empty($data->$k)) { |
|
| 77 | + $data->$k = null; |
|
| 78 | + $new = true; |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + return $new; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | 84 | public function save($data, $tryagain = true) { |
| 85 | - $new = $this->getIfNew($data); |
|
| 85 | + $new = $this->getIfNew($data); |
|
| 86 | 86 | |
| 87 | 87 | try { |
| 88 | - $result = $this->insert($this->table, $this->primaryKey, $data); |
|
| 88 | + $result = $this->insert($this->table, $this->primaryKey, $data); |
|
| 89 | 89 | |
| 90 | 90 | //If there was an error but PDO is silent, trigger the catch block anyway |
| 91 | 91 | if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
@@ -103,19 +103,19 @@ discard block |
||
| 103 | 103 | $this->databaseSelect->updateCache($data, $data->{$this->primaryKey[0]}); |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | - private function getTryAgain($tryagain) { |
|
| 107 | - return $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
|
| 108 | - } |
|
| 106 | + private function getTryAgain($tryagain) { |
|
| 107 | + return $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - private function updatePK($data, $new) { |
|
| 111 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 112 | - } |
|
| 110 | + private function updatePK($data, $new) { |
|
| 111 | + if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - private function checkIfUpdateWorked($data) { |
|
| 115 | - $updateWhere = $this->whereBuilder->createSql($data); |
|
| 116 | - $matched = $this->findByField($updateWhere['args']); |
|
| 117 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 118 | - } |
|
| 114 | + private function checkIfUpdateWorked($data) { |
|
| 115 | + $updateWhere = $this->whereBuilder->createSql($data); |
|
| 116 | + $matched = $this->findByField($updateWhere['args']); |
|
| 117 | + if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | 120 | private function insert($table, array $primaryKey, $data) { |
| 121 | 121 | $error = 0; |
@@ -127,20 +127,20 @@ discard block |
||
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | if ($error || $result->errorCode() !== '00000') { |
| 130 | - $result = $this->tryUpdate($table, $primaryKey, $data); |
|
| 131 | - } |
|
| 130 | + $result = $this->tryUpdate($table, $primaryKey, $data); |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | 133 | return $result; |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | - private function tryUpdate($table, array $primaryKey, $data) { |
|
| 137 | - $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
| 138 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 136 | + private function tryUpdate($table, array $primaryKey, $data) { |
|
| 137 | + $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
| 138 | + if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 139 | 139 | |
| 140 | - return $result; |
|
| 141 | - } |
|
| 140 | + return $result; |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - private function selectQuery(\Maphper\Lib\Query $query) { |
|
| 144 | - return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
| 145 | - } |
|
| 143 | + private function selectQuery(\Maphper\Lib\Query $query) { |
|
| 144 | + return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
| 145 | + } |
|
| 146 | 146 | } |