@@ -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 | } |
@@ -94,7 +94,7 @@ |
||
94 | 94 | } |
95 | 95 | |
96 | 96 | public function updateCache($data, $pkValue) { |
97 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
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 |
@@ -23,13 +23,15 @@ discard block |
||
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 | - } |
|
27 | - catch (\Exception $e) { |
|
26 | + } catch (\Exception $e) { |
|
28 | 27 | // Don't issue an error if it cannot be found since we return null |
29 | 28 | } |
30 | 29 | |
31 | - if (isset($result[0])) $this->idCache[$id] = $result[0]; |
|
32 | - else return null; |
|
30 | + if (isset($result[0])) { |
|
31 | + $this->idCache[$id] = $result[0]; |
|
32 | + } else { |
|
33 | + return null; |
|
34 | + } |
|
33 | 35 | } |
34 | 36 | return $this->idCache[$id]; |
35 | 37 | } |
@@ -39,14 +41,15 @@ discard block |
||
39 | 41 | if (!isset($this->resultCache[$cacheId])) { |
40 | 42 | $query = $this->whereBuilder->createSql($fields); |
41 | 43 | |
42 | - if (!isset($options['order'])) $options['order'] = $defaultSort; |
|
44 | + if (!isset($options['order'])) { |
|
45 | + $options['order'] = $defaultSort; |
|
46 | + } |
|
43 | 47 | |
44 | 48 | try { |
45 | 49 | $this->resultCache[$cacheId] = $this->selectQuery($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options)); |
46 | 50 | $this->databaseModify->addIndex(array_keys($query['args'])); |
47 | 51 | $this->databaseModify->addIndex(explode(',', $options['order'])); |
48 | - } |
|
49 | - catch (\Exception $e) { |
|
52 | + } catch (\Exception $e) { |
|
50 | 53 | $this->errors[] = $e; |
51 | 54 | $this->resultCache[$cacheId] = []; |
52 | 55 | } |
@@ -56,7 +59,9 @@ discard block |
||
56 | 59 | |
57 | 60 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
58 | 61 | //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 | - if (is_array($field)) $field = $field[0]; |
|
62 | + if (is_array($field)) { |
|
63 | + $field = $field[0]; |
|
64 | + } |
|
60 | 65 | $query = $this->whereBuilder->createSql($criteria); |
61 | 66 | |
62 | 67 | try { |
@@ -65,8 +70,7 @@ discard block |
||
65 | 70 | $result = $this->selectQuery($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group)); |
66 | 71 | |
67 | 72 | return $this->determineAggregateResult($result, $group, $field); |
68 | - } |
|
69 | - catch (\Exception $e) { |
|
73 | + } catch (\Exception $e) { |
|
70 | 74 | return $group ? [] : 0; |
71 | 75 | } |
72 | 76 | } |
@@ -74,11 +78,15 @@ discard block |
||
74 | 78 | private function determineAggregateResult($result, $group, $field) { |
75 | 79 | if ($group != null) { |
76 | 80 | $ret = []; |
77 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
81 | + foreach ($result as $res) { |
|
82 | + $ret[$res->$field] = $res->val; |
|
83 | + } |
|
78 | 84 | return $ret; |
85 | + } else if (isset($result[0])) { |
|
86 | + return $result[0]->val; |
|
87 | + } else { |
|
88 | + return 0; |
|
79 | 89 | } |
80 | - else if (isset($result[0])) return $result[0]->val; |
|
81 | - else return 0; |
|
82 | 90 | } |
83 | 91 | |
84 | 92 | private function selectQuery(\Maphper\Lib\Query $query) { |
@@ -94,8 +102,11 @@ discard block |
||
94 | 102 | } |
95 | 103 | |
96 | 104 | public function updateCache($data, $pkValue) { |
97 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
98 | - else $this->cache[$pkValue] = $data; |
|
105 | + if (isset($this->cache[$pkValue])) { |
|
106 | + $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
107 | + } else { |
|
108 | + $this->cache[$pkValue] = $data; |
|
109 | + } |
|
99 | 110 | } |
100 | 111 | |
101 | 112 | public function deleteIDFromCache($id) { |
@@ -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 | } |
@@ -27,9 +27,9 @@ |
||
27 | 27 | $this->crudBuilder = new \Maphper\Lib\CrudBuilder(); |
28 | 28 | $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
29 | 29 | |
30 | - $this->fields = implode(',', array_map([$this->adapter, 'quote'], (array) $this->options->read('fields'))); |
|
30 | + $this->fields = implode(',', array_map([$this->adapter, 'quote'], (array)$this->options->read('fields'))); |
|
31 | 31 | |
32 | - $this->defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort') : implode(', ', $this->primaryKey); |
|
32 | + $this->defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort') : implode(', ', $this->primaryKey); |
|
33 | 33 | |
34 | 34 | $this->databaseModify = new DatabaseModify($this->adapter, $this->options->getEditMode(), $this->table); |
35 | 35 | $this->databaseSelect = new DatabaseSelect($this->adapter, $this->databaseModify, $this->table); |
@@ -88,10 +88,13 @@ discard block |
||
88 | 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 | - if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
|
92 | - } |
|
93 | - catch (\Exception $e) { |
|
94 | - if (!$this->getTryAgain($tryagain)) throw $e; |
|
91 | + if ($result->errorCode() !== '00000') { |
|
92 | + throw new \Exception('Could not insert into ' . $this->table); |
|
93 | + } |
|
94 | + } catch (\Exception $e) { |
|
95 | + if (!$this->getTryAgain($tryagain)) { |
|
96 | + throw $e; |
|
97 | + } |
|
95 | 98 | |
96 | 99 | $this->adapter->alterDatabase($this->table, $this->primaryKey, $data); |
97 | 100 | $this->save($data, false); |
@@ -108,21 +111,24 @@ discard block |
||
108 | 111 | } |
109 | 112 | |
110 | 113 | private function updatePK($data, $new) { |
111 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
114 | + if ($new && count($this->primaryKey) == 1) { |
|
115 | + $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
116 | + } |
|
112 | 117 | } |
113 | 118 | |
114 | 119 | private function checkIfUpdateWorked($data) { |
115 | 120 | $updateWhere = $this->whereBuilder->createSql($data); |
116 | 121 | $matched = $this->findByField($updateWhere['args']); |
117 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
122 | + if (count($matched) == 0) { |
|
123 | + throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
124 | + } |
|
118 | 125 | } |
119 | 126 | |
120 | 127 | private function insert($table, array $primaryKey, $data) { |
121 | 128 | $error = 0; |
122 | 129 | try { |
123 | 130 | $result = $this->adapter->query($this->crudBuilder->insert($table, $data)); |
124 | - } |
|
125 | - catch (\Exception $e) { |
|
131 | + } catch (\Exception $e) { |
|
126 | 132 | $error = 1; |
127 | 133 | } |
128 | 134 | |
@@ -135,7 +141,9 @@ discard block |
||
135 | 141 | |
136 | 142 | private function tryUpdate($table, array $primaryKey, $data) { |
137 | 143 | $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
138 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
144 | + if ($result->rowCount() === 0) { |
|
145 | + $this->checkIfUpdateWorked($data); |
|
146 | + } |
|
139 | 147 | |
140 | 148 | return $result; |
141 | 149 | } |