@@ -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 | } |