@@ -2,48 +2,48 @@ 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 | - if (!$this->cacheMode) return $result; |
|
| 37 | - else return $this->idCache[$id] = $result; |
|
| 36 | + if (!$this->cacheMode) return $result; |
|
| 37 | + else return $this->idCache[$id] = $result; |
|
| 38 | 38 | } |
| 39 | 39 | // cacheMode is true and cache is set |
| 40 | - return $this->idCache[$id]; |
|
| 40 | + return $this->idCache[$id]; |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - public function findByField(array $fields, $options = []) { |
|
| 43 | + public function findByField(array $fields, $options = []) { |
|
| 44 | 44 | $cacheId = md5(serialize(func_get_args())); |
| 45 | 45 | |
| 46 | - if (($this->cacheMode && !isset($this->resultCache[$cacheId])) || !$this->cacheMode) { |
|
| 46 | + if (($this->cacheMode && !isset($this->resultCache[$cacheId])) || !$this->cacheMode) { |
|
| 47 | 47 | $query = $this->whereBuilder->createSql($fields); |
| 48 | 48 | |
| 49 | 49 | if (!isset($options['order'])) $options['order'] = $this->defaultSort; |
@@ -59,15 +59,15 @@ discard block |
||
| 59 | 59 | } |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - if ($this->cacheMode) { |
|
| 63 | - if (isset($result)) $this->resultCache[$cacheId] = $result; |
|
| 64 | - if (isset($this->resultCache[$cacheId])) return $this->resultCache[$cacheId]; |
|
| 65 | - } |
|
| 62 | + if ($this->cacheMode) { |
|
| 63 | + if (isset($result)) $this->resultCache[$cacheId] = $result; |
|
| 64 | + if (isset($this->resultCache[$cacheId])) return $this->resultCache[$cacheId]; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - return $result; |
|
| 67 | + return $result; |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
|
| 70 | + public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
|
| 71 | 71 | //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 |
| 72 | 72 | if (is_array($field)) $field = $field[0]; |
| 73 | 73 | $query = $this->whereBuilder->createSql($criteria); |
@@ -84,36 +84,36 @@ discard block |
||
| 84 | 84 | } |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - private function determineAggregateResult($result, $group, $field) { |
|
| 88 | - if ($group != null) { |
|
| 89 | - $ret = []; |
|
| 90 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 91 | - return $ret; |
|
| 92 | - } |
|
| 93 | - else if (isset($result[0])) return $result[0]->val; |
|
| 94 | - else return 0; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - private function selectQuery(\Maphper\Lib\Query $query) { |
|
| 98 | - return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - public function clearResultCache() { |
|
| 102 | - if ($this->cacheMode) $this->resultCache = []; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - public function clearIDCache() { |
|
| 106 | - if ($this->cacheMode) $this->idCache = []; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - public function updateCache($data, $pkValue) { |
|
| 110 | - if ($this->cacheMode) { |
|
| 111 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 112 | - else $this->cache[$pkValue] = $data; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - public function deleteIDFromCache($id) { |
|
| 117 | - if ($this->cacheMode) unset($this->idCache[$id]); |
|
| 118 | - } |
|
| 87 | + private function determineAggregateResult($result, $group, $field) { |
|
| 88 | + if ($group != null) { |
|
| 89 | + $ret = []; |
|
| 90 | + foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 91 | + return $ret; |
|
| 92 | + } |
|
| 93 | + else if (isset($result[0])) return $result[0]->val; |
|
| 94 | + else return 0; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + private function selectQuery(\Maphper\Lib\Query $query) { |
|
| 98 | + return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + public function clearResultCache() { |
|
| 102 | + if ($this->cacheMode) $this->resultCache = []; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + public function clearIDCache() { |
|
| 106 | + if ($this->cacheMode) $this->idCache = []; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + public function updateCache($data, $pkValue) { |
|
| 110 | + if ($this->cacheMode) { |
|
| 111 | + if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 112 | + else $this->cache[$pkValue] = $data; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + public function deleteIDFromCache($id) { |
|
| 117 | + if ($this->cacheMode) unset($this->idCache[$id]); |
|
| 118 | + } |
|
| 119 | 119 | } |
@@ -25,16 +25,21 @@ discard block |
||
| 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 | - } |
|
| 29 | - catch (\Exception $e) { |
|
| 28 | + } catch (\Exception $e) { |
|
| 30 | 29 | // Don't issue an error if it cannot be found since we return null |
| 31 | 30 | } |
| 32 | 31 | |
| 33 | - if (isset($result[0])) $result = $result[0]; |
|
| 34 | - else return null; |
|
| 32 | + if (isset($result[0])) { |
|
| 33 | + $result = $result[0]; |
|
| 34 | + } else { |
|
| 35 | + return null; |
|
| 36 | + } |
|
| 35 | 37 | |
| 36 | - if (!$this->cacheMode) return $result; |
|
| 37 | - else return $this->idCache[$id] = $result; |
|
| 38 | + if (!$this->cacheMode) { |
|
| 39 | + return $result; |
|
| 40 | + } else { |
|
| 41 | + return $this->idCache[$id] = $result; |
|
| 42 | + } |
|
| 38 | 43 | } |
| 39 | 44 | // cacheMode is true and cache is set |
| 40 | 45 | return $this->idCache[$id]; |
@@ -46,22 +51,27 @@ discard block |
||
| 46 | 51 | if (($this->cacheMode && !isset($this->resultCache[$cacheId])) || !$this->cacheMode) { |
| 47 | 52 | $query = $this->whereBuilder->createSql($fields); |
| 48 | 53 | |
| 49 | - if (!isset($options['order'])) $options['order'] = $this->defaultSort; |
|
| 54 | + if (!isset($options['order'])) { |
|
| 55 | + $options['order'] = $this->defaultSort; |
|
| 56 | + } |
|
| 50 | 57 | |
| 51 | 58 | try { |
| 52 | 59 | $result = $this->selectQuery($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options)); |
| 53 | 60 | $this->databaseModify->addIndex(array_keys($query['args'])); |
| 54 | 61 | $this->databaseModify->addIndex(explode(',', $options['order'])); |
| 55 | - } |
|
| 56 | - catch (\Exception $e) { |
|
| 62 | + } catch (\Exception $e) { |
|
| 57 | 63 | $this->errors[] = $e; |
| 58 | 64 | $result = []; |
| 59 | 65 | } |
| 60 | 66 | } |
| 61 | 67 | |
| 62 | 68 | if ($this->cacheMode) { |
| 63 | - if (isset($result)) $this->resultCache[$cacheId] = $result; |
|
| 64 | - if (isset($this->resultCache[$cacheId])) return $this->resultCache[$cacheId]; |
|
| 69 | + if (isset($result)) { |
|
| 70 | + $this->resultCache[$cacheId] = $result; |
|
| 71 | + } |
|
| 72 | + if (isset($this->resultCache[$cacheId])) { |
|
| 73 | + return $this->resultCache[$cacheId]; |
|
| 74 | + } |
|
| 65 | 75 | } |
| 66 | 76 | |
| 67 | 77 | return $result; |
@@ -69,7 +79,9 @@ discard block |
||
| 69 | 79 | |
| 70 | 80 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
| 71 | 81 | //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 |
| 72 | - if (is_array($field)) $field = $field[0]; |
|
| 82 | + if (is_array($field)) { |
|
| 83 | + $field = $field[0]; |
|
| 84 | + } |
|
| 73 | 85 | $query = $this->whereBuilder->createSql($criteria); |
| 74 | 86 | |
| 75 | 87 | try { |
@@ -78,8 +90,7 @@ discard block |
||
| 78 | 90 | $result = $this->selectQuery($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group)); |
| 79 | 91 | |
| 80 | 92 | return $this->determineAggregateResult($result, $group, $field); |
| 81 | - } |
|
| 82 | - catch (\Exception $e) { |
|
| 93 | + } catch (\Exception $e) { |
|
| 83 | 94 | return $group ? [] : 0; |
| 84 | 95 | } |
| 85 | 96 | } |
@@ -87,11 +98,15 @@ discard block |
||
| 87 | 98 | private function determineAggregateResult($result, $group, $field) { |
| 88 | 99 | if ($group != null) { |
| 89 | 100 | $ret = []; |
| 90 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 101 | + foreach ($result as $res) { |
|
| 102 | + $ret[$res->$field] = $res->val; |
|
| 103 | + } |
|
| 91 | 104 | return $ret; |
| 105 | + } else if (isset($result[0])) { |
|
| 106 | + return $result[0]->val; |
|
| 107 | + } else { |
|
| 108 | + return 0; |
|
| 92 | 109 | } |
| 93 | - else if (isset($result[0])) return $result[0]->val; |
|
| 94 | - else return 0; |
|
| 95 | 110 | } |
| 96 | 111 | |
| 97 | 112 | private function selectQuery(\Maphper\Lib\Query $query) { |
@@ -99,21 +114,30 @@ discard block |
||
| 99 | 114 | } |
| 100 | 115 | |
| 101 | 116 | public function clearResultCache() { |
| 102 | - if ($this->cacheMode) $this->resultCache = []; |
|
| 117 | + if ($this->cacheMode) { |
|
| 118 | + $this->resultCache = []; |
|
| 119 | + } |
|
| 103 | 120 | } |
| 104 | 121 | |
| 105 | 122 | public function clearIDCache() { |
| 106 | - if ($this->cacheMode) $this->idCache = []; |
|
| 123 | + if ($this->cacheMode) { |
|
| 124 | + $this->idCache = []; |
|
| 125 | + } |
|
| 107 | 126 | } |
| 108 | 127 | |
| 109 | 128 | public function updateCache($data, $pkValue) { |
| 110 | 129 | if ($this->cacheMode) { |
| 111 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 112 | - else $this->cache[$pkValue] = $data; |
|
| 130 | + if (isset($this->cache[$pkValue])) { |
|
| 131 | + $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 132 | + } else { |
|
| 133 | + $this->cache[$pkValue] = $data; |
|
| 134 | + } |
|
| 113 | 135 | } |
| 114 | 136 | } |
| 115 | 137 | |
| 116 | 138 | public function deleteIDFromCache($id) { |
| 117 | - if ($this->cacheMode) unset($this->idCache[$id]); |
|
| 139 | + if ($this->cacheMode) { |
|
| 140 | + unset($this->idCache[$id]); |
|
| 141 | + } |
|
| 118 | 142 | } |
| 119 | 143 | } |