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