@@ -7,8 +7,8 @@ discard block |
||
| 7 | 7 | |
| 8 | 8 | private $primaryKey; |
| 9 | 9 | private $fields = '*'; |
| 10 | - private $databaseSelect; |
|
| 11 | - private $databaseCrud; |
|
| 10 | + private $databaseSelect; |
|
| 11 | + private $databaseCrud; |
|
| 12 | 12 | |
| 13 | 13 | public function __construct($db, $table, $primaryKey = 'id', array $options = []) { |
| 14 | 14 | $options = new DatabaseOptions($db, $options); |
@@ -20,10 +20,10 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | $defaultSort = $options->read('defaultSort') !== false ? $options->read('defaultSort') : implode(', ', $this->primaryKey); |
| 22 | 22 | |
| 23 | - $databaseModify = new DatabaseModify($adapter, $options->getEditMode(), $table); |
|
| 23 | + $databaseModify = new DatabaseModify($adapter, $options->getEditMode(), $table); |
|
| 24 | 24 | |
| 25 | - $this->databaseSelect = new DatabaseSelect($adapter, $databaseModify, $table, $defaultSort, $options->getCacheMode()); |
|
| 26 | - $this->databaseCrud = new DatabaseCrud($adapter, $databaseModify, $this->databaseSelect, $table, $this->primaryKey); |
|
| 25 | + $this->databaseSelect = new DatabaseSelect($adapter, $databaseModify, $table, $defaultSort, $options->getCacheMode()); |
|
| 26 | + $this->databaseCrud = new DatabaseCrud($adapter, $databaseModify, $this->databaseSelect, $table, $this->primaryKey); |
|
| 27 | 27 | |
| 28 | 28 | $databaseModify->optimizeColumns(); |
| 29 | 29 | } |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | public function findByField(array $fields, $options = []) { |
| 48 | - return $this->databaseSelect->findByField($fields, $options); |
|
| 48 | + return $this->databaseSelect->findByField($fields, $options); |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | public function deleteByField(array $fields, array $options = []) { |
@@ -53,6 +53,6 @@ discard block |
||
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | public function save($data) { |
| 56 | - $this->databaseCrud->save($data, true); |
|
| 56 | + $this->databaseCrud->save($data, true); |
|
| 57 | 57 | } |
| 58 | 58 | } |
@@ -2,46 +2,46 @@ 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, $defaultSort, $cacheMode) { |
|
| 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->defaultSort = $defaultSort; |
|
| 20 | - $this->cacheMode = $cacheMode; |
|
| 21 | - $this->table = $table; |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - 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, $defaultSort, $cacheMode) { |
|
| 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->defaultSort = $defaultSort; |
|
| 20 | + $this->cacheMode = $cacheMode; |
|
| 21 | + $this->table = $table; |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + public function findById($id, $pk) { |
|
| 25 | 25 | if (($this->cacheMode && !isset($this->idCache[$id])) || !$this->cacheMode) { |
| 26 | 26 | try { |
| 27 | 27 | $result = $this->selectQuery($this->selectBuilder->select($this->table, $pk . ' = :id', [':id' => $id], ['limit' => 1])); |
| 28 | 28 | } |
| 29 | 29 | catch (\Exception $e) { |
| 30 | - // Don't issue an error if it cannot be found since we return null |
|
| 30 | + // Don't issue an error if it cannot be found since we return null |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | if (isset($result[0])) $result = $result[0]; |
| 34 | 34 | else return null; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - if (!$this->cacheMode) return $result; |
|
| 37 | + if (!$this->cacheMode) return $result; |
|
| 38 | 38 | else return $this->idCache[$id] = $result; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - public function findByField(array $fields, $options = []) { |
|
| 41 | + public function findByField(array $fields, $options = []) { |
|
| 42 | 42 | $cacheId = md5(serialize(func_get_args())); |
| 43 | 43 | |
| 44 | - if (($this->cacheMode && !isset($this->resultCache[$cacheId])) || !$this->cacheMode) { |
|
| 44 | + if (($this->cacheMode && !isset($this->resultCache[$cacheId])) || !$this->cacheMode) { |
|
| 45 | 45 | $query = $this->whereBuilder->createSql($fields); |
| 46 | 46 | |
| 47 | 47 | if (!isset($options['order'])) $options['order'] = $this->defaultSort; |
@@ -57,15 +57,15 @@ discard block |
||
| 57 | 57 | } |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - if ($this->cacheMode) { |
|
| 61 | - if (isset($result)) $this->resultCache[$cacheId] = $result; |
|
| 62 | - if (isset($this->resultCache[$cacheId])) return $this->resultCache[$cacheId]; |
|
| 63 | - } |
|
| 60 | + if ($this->cacheMode) { |
|
| 61 | + if (isset($result)) $this->resultCache[$cacheId] = $result; |
|
| 62 | + if (isset($this->resultCache[$cacheId])) return $this->resultCache[$cacheId]; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - return $result; |
|
| 65 | + return $result; |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
|
| 68 | + public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
|
| 69 | 69 | //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 |
| 70 | 70 | if (is_array($field)) $field = $field[0]; |
| 71 | 71 | $query = $this->whereBuilder->createSql($criteria); |
@@ -82,36 +82,36 @@ 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 | - } |
|
| 94 | - |
|
| 95 | - private function selectQuery(\Maphper\Lib\Query $query) { |
|
| 96 | - return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - public function clearResultCache() { |
|
| 100 | - if ($this->cacheMode) $this->resultCache = []; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - public function clearIDCache() { |
|
| 104 | - if ($this->cacheMode) $this->idCache = []; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - public function updateCache($data, $pkValue) { |
|
| 108 | - if ($this->cacheMode) { |
|
| 109 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 110 | - else $this->cache[$pkValue] = $data; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - public function deleteIDFromCache($id) { |
|
| 115 | - if ($this->cacheMode) unset($this->idCache[$id]); |
|
| 116 | - } |
|
| 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 | + |
|
| 95 | + private function selectQuery(\Maphper\Lib\Query $query) { |
|
| 96 | + return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + public function clearResultCache() { |
|
| 100 | + if ($this->cacheMode) $this->resultCache = []; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + public function clearIDCache() { |
|
| 104 | + if ($this->cacheMode) $this->idCache = []; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + public function updateCache($data, $pkValue) { |
|
| 108 | + if ($this->cacheMode) { |
|
| 109 | + if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 110 | + else $this->cache[$pkValue] = $data; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + public function deleteIDFromCache($id) { |
|
| 115 | + if ($this->cacheMode) unset($this->idCache[$id]); |
|
| 116 | + } |
|
| 117 | 117 | } |
@@ -106,7 +106,7 @@ |
||
| 106 | 106 | |
| 107 | 107 | public function updateCache($data, $pkValue) { |
| 108 | 108 | if ($this->cacheMode) { |
| 109 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 109 | + if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object)array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 110 | 110 | else $this->cache[$pkValue] = $data; |
| 111 | 111 | } |
| 112 | 112 | } |