@@ -7,17 +7,23 @@ |
||
7 | 7 | |
8 | 8 | if (isset($options['offset'])) { |
9 | 9 | $offset = ' OFFSET ' . $options['offset']; |
10 | - if (!$limit) $limit = ' LIMIT 1000'; |
|
10 | + if (!$limit) { |
|
11 | + $limit = ' LIMIT 1000'; |
|
12 | + } |
|
13 | + } else { |
|
14 | + $offset = ''; |
|
11 | 15 | } |
12 | - else $offset = ''; |
|
13 | 16 | |
14 | 17 | $order = isset($options['order']) ? ' ORDER BY ' . $options['order'] : ''; |
15 | 18 | return new Query('SELECT * FROM ' . $table . ' ' . $where . $order . $limit . $offset, $args); |
16 | 19 | } |
17 | 20 | |
18 | 21 | public function aggregate($table, $function, $field, $where, $args, $group) { |
19 | - if ($group == true) $groupBy = ' GROUP BY ' . $field; |
|
20 | - else $groupBy = ''; |
|
22 | + if ($group == true) { |
|
23 | + $groupBy = ' GROUP BY ' . $field; |
|
24 | + } else { |
|
25 | + $groupBy = ''; |
|
26 | + } |
|
21 | 27 | return new Query('SELECT ' . $function . '(' . $field . ') as val, ' . $field . ' FROM ' . $table . ($where != null ? ' WHERE ' . $where : '') . ' ' . $groupBy, $args); |
22 | 28 | } |
23 | 29 | } |
@@ -2,18 +2,18 @@ discard block |
||
2 | 2 | namespace Maphper\Lib\Sql; |
3 | 3 | |
4 | 4 | class WhereBuilder { |
5 | - //Needs to be broken up into better methods |
|
5 | + //Needs to be broken up into better methods |
|
6 | 6 | public function createSql($fields, $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND){ |
7 | 7 | $args = []; |
8 | 8 | $sql = []; |
9 | 9 | |
10 | 10 | foreach ($fields as $key => $value) { |
11 | - if ($value instanceof \DateTime) { |
|
12 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
13 | - else $value = $value->format('Y-m-d H:i:s'); |
|
14 | - } |
|
11 | + if ($value instanceof \DateTime) { |
|
12 | + if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
13 | + else $value = $value->format('Y-m-d H:i:s'); |
|
14 | + } |
|
15 | 15 | |
16 | - if (is_object($value)) continue; |
|
16 | + if (is_object($value)) continue; |
|
17 | 17 | if (is_numeric($key) && is_array($value)) { |
18 | 18 | $result = $this->createSql($value, $key); |
19 | 19 | foreach ($result['args'] as $arg_key => $arg) $args[$arg_key] = $arg; |
@@ -64,17 +64,17 @@ discard block |
||
64 | 64 | return ['args' => $args, 'sql' => $query]; |
65 | 65 | } |
66 | 66 | |
67 | - private function getOperator($mode) { |
|
68 | - $operator = ""; |
|
67 | + private function getOperator($mode) { |
|
68 | + $operator = ""; |
|
69 | 69 | |
70 | - if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE'; |
|
71 | - else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&'; |
|
72 | - else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
73 | - else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
74 | - else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!='; |
|
70 | + if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE'; |
|
71 | + else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&'; |
|
72 | + else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
73 | + else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
74 | + else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!='; |
|
75 | 75 | |
76 | - if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
76 | + if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
77 | 77 | |
78 | - return $operator; |
|
79 | - } |
|
78 | + return $operator; |
|
79 | + } |
|
80 | 80 | } |
@@ -3,13 +3,13 @@ discard block |
||
3 | 3 | |
4 | 4 | class WhereBuilder { |
5 | 5 | //Needs to be broken up into better methods |
6 | - public function createSql($fields, $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND){ |
|
6 | + public function createSql($fields, $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND) { |
|
7 | 7 | $args = []; |
8 | 8 | $sql = []; |
9 | 9 | |
10 | 10 | foreach ($fields as $key => $value) { |
11 | 11 | if ($value instanceof \DateTime) { |
12 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
12 | + if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
13 | 13 | else $value = $value->format('Y-m-d H:i:s'); |
14 | 14 | } |
15 | 15 | |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | $inSql[] = ':' . $key . $i; |
35 | 35 | } |
36 | 36 | if (count($inSql) == 0) return []; |
37 | - else $sql[] = $key . ' IN ( ' . implode(', ', $inSql) . ')'; |
|
37 | + else $sql[] = $key . ' IN ( ' . implode(', ', $inSql) . ')'; |
|
38 | 38 | } |
39 | 39 | else if ($value === NULL) { |
40 | 40 | $nullSql = $key . ' IS '; |
@@ -9,71 +9,94 @@ |
||
9 | 9 | |
10 | 10 | foreach ($fields as $key => $value) { |
11 | 11 | if ($value instanceof \DateTime) { |
12 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
13 | - else $value = $value->format('Y-m-d H:i:s'); |
|
12 | + if ($value->format('H:i:s') == '00:00:00') { |
|
13 | + $value = $value->format('Y-m-d'); |
|
14 | + } else { |
|
15 | + $value = $value->format('Y-m-d H:i:s'); |
|
16 | + } |
|
14 | 17 | } |
15 | 18 | |
16 | - if (is_object($value)) continue; |
|
19 | + if (is_object($value)) { |
|
20 | + continue; |
|
21 | + } |
|
17 | 22 | if (is_numeric($key) && is_array($value)) { |
18 | 23 | $result = $this->createSql($value, $key); |
19 | - foreach ($result['args'] as $arg_key => $arg) $args[$arg_key] = $arg; |
|
20 | - foreach ($result['sql'] as $arg) $sql[] = $arg; |
|
21 | - } |
|
22 | - else if (\Maphper\Maphper::FIND_BETWEEN & $mode) { |
|
24 | + foreach ($result['args'] as $arg_key => $arg) { |
|
25 | + $args[$arg_key] = $arg; |
|
26 | + } |
|
27 | + foreach ($result['sql'] as $arg) { |
|
28 | + $sql[] = $arg; |
|
29 | + } |
|
30 | + } else if (\Maphper\Maphper::FIND_BETWEEN & $mode) { |
|
23 | 31 | $sql[] = $key . '>= :' . $key . 'from'; |
24 | 32 | $sql[] = $key . ' <= :' . $key . 'to'; |
25 | 33 | |
26 | 34 | $args[$key . 'from'] = $value[0]; |
27 | 35 | $args[$key . 'to'] = $value[1]; |
28 | - } |
|
29 | - else if (!is_numeric($key) && is_array($value)) { |
|
36 | + } else if (!is_numeric($key) && is_array($value)) { |
|
30 | 37 | $inSql = []; |
31 | 38 | $count = count($value); |
32 | 39 | for ($i = 0; $i < $count; $i++) { |
33 | 40 | $args[$key . $i] = $value[$i]; |
34 | 41 | $inSql[] = ':' . $key . $i; |
35 | 42 | } |
36 | - if (count($inSql) == 0) return []; |
|
37 | - else $sql[] = $key . ' IN ( ' . implode(', ', $inSql) . ')'; |
|
38 | - } |
|
39 | - else if ($value === NULL) { |
|
43 | + if (count($inSql) == 0) { |
|
44 | + return []; |
|
45 | + } else { |
|
46 | + $sql[] = $key . ' IN ( ' . implode(', ', $inSql) . ')'; |
|
47 | + } |
|
48 | + } else if ($value === NULL) { |
|
40 | 49 | $nullSql = $key . ' IS '; |
41 | - if (\Maphper\Maphper::FIND_NOT & $mode) $nullSql .= 'NOT '; |
|
50 | + if (\Maphper\Maphper::FIND_NOT & $mode) { |
|
51 | + $nullSql .= 'NOT '; |
|
52 | + } |
|
42 | 53 | $sql[] = $nullSql . 'NULL'; |
43 | - } |
|
44 | - else { |
|
54 | + } else { |
|
45 | 55 | |
46 | 56 | if (\Maphper\Maphper::FIND_LIKE & $mode) { |
47 | 57 | $operator = 'LIKE'; |
48 | 58 | $value = '%' . $value . '%'; |
49 | - } |
|
50 | - else if (\Maphper\Maphper::FIND_STARTS & $mode) { |
|
59 | + } else if (\Maphper\Maphper::FIND_STARTS & $mode) { |
|
51 | 60 | $operator = 'LIKE'; |
52 | 61 | $value = $value . '%'; |
62 | + } else { |
|
63 | + $operator = $this->getOperator($mode); |
|
53 | 64 | } |
54 | - else $operator = $this->getOperator($mode); |
|
55 | 65 | |
56 | 66 | $args[$key] = $value; |
57 | 67 | $sql[] = $key . ' ' . $operator . ' :' . $key; |
58 | 68 | } |
59 | 69 | } |
60 | 70 | |
61 | - if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR ', $sql); |
|
62 | - else $query = implode(' AND ', $sql); |
|
63 | - if (!empty($query)) $query = '(' . $query . ')'; |
|
71 | + if (\Maphper\Maphper::FIND_OR & $mode) { |
|
72 | + $query = implode(' OR ', $sql); |
|
73 | + } else { |
|
74 | + $query = implode(' AND ', $sql); |
|
75 | + } |
|
76 | + if (!empty($query)) { |
|
77 | + $query = '(' . $query . ')'; |
|
78 | + } |
|
64 | 79 | return ['args' => $args, 'sql' => $query]; |
65 | 80 | } |
66 | 81 | |
67 | 82 | private function getOperator($mode) { |
68 | 83 | $operator = ""; |
69 | 84 | |
70 | - if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE'; |
|
71 | - else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&'; |
|
72 | - else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
73 | - else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
74 | - else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!='; |
|
85 | + if (\Maphper\Maphper::FIND_NOCASE & $mode) { |
|
86 | + $operator = 'LIKE'; |
|
87 | + } else if (\Maphper\Maphper::FIND_BIT & $mode) { |
|
88 | + $operator = '&'; |
|
89 | + } else if (\Maphper\Maphper::FIND_GREATER & $mode) { |
|
90 | + $operator = '>'; |
|
91 | + } else if (\Maphper\Maphper::FIND_LESS & $mode) { |
|
92 | + $operator = '<'; |
|
93 | + } else if (\Maphper\Maphper::FIND_NOT & $mode) { |
|
94 | + $operator = '!='; |
|
95 | + } |
|
75 | 96 | |
76 | - if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
97 | + if (\Maphper\Maphper::FIND_EXACT & $mode) { |
|
98 | + $operator .= '='; |
|
99 | + } |
|
77 | 100 | |
78 | 101 | return $operator; |
79 | 102 | } |
@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | const EDIT_OPTIMISE = 4; |
7 | 7 | |
8 | 8 | private $table; |
9 | - private $options; |
|
9 | + private $options; |
|
10 | 10 | private $cache = []; |
11 | 11 | private $primaryKey; |
12 | 12 | private $fields = '*'; |
@@ -15,8 +15,8 @@ discard block |
||
15 | 15 | private $alterDb = false; |
16 | 16 | private $adapter; |
17 | 17 | private $crudBuilder; |
18 | - private $selectBuilder; |
|
19 | - private $whereBuilder; |
|
18 | + private $selectBuilder; |
|
19 | + private $whereBuilder; |
|
20 | 20 | |
21 | 21 | public function __construct($db, $table, $primaryKey = 'id', array $options = []) { |
22 | 22 | $this->options = new DatabaseOptions($db, $options); |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | |
28 | 28 | $this->crudBuilder = new \Maphper\Lib\CrudBuilder(); |
29 | 29 | $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
30 | - $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
30 | + $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
31 | 31 | |
32 | 32 | $this->fields = implode(',', array_map([$this->adapter, 'quote'], (array) $this->options->read('fields'))); |
33 | 33 | |
@@ -38,9 +38,9 @@ discard block |
||
38 | 38 | $this->optimizeColumns(); |
39 | 39 | } |
40 | 40 | |
41 | - private function optimizeColumns() { |
|
42 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
43 | - } |
|
41 | + private function optimizeColumns() { |
|
42 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
43 | + } |
|
44 | 44 | |
45 | 45 | public function getPrimaryKey() { |
46 | 46 | return $this->primaryKey; |
@@ -82,15 +82,15 @@ 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 | - } |
|
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 | 94 | |
95 | 95 | private function addIndex($args) { |
96 | 96 | if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
@@ -126,22 +126,22 @@ discard block |
||
126 | 126 | $this->resultCache = []; |
127 | 127 | } |
128 | 128 | |
129 | - private function getIfNew($data) { |
|
130 | - $new = false; |
|
131 | - foreach ($this->primaryKey as $k) { |
|
132 | - if (empty($data->$k)) { |
|
133 | - $data->$k = null; |
|
134 | - $new = true; |
|
135 | - } |
|
136 | - } |
|
137 | - return $new; |
|
138 | - } |
|
129 | + private function getIfNew($data) { |
|
130 | + $new = false; |
|
131 | + foreach ($this->primaryKey as $k) { |
|
132 | + if (empty($data->$k)) { |
|
133 | + $data->$k = null; |
|
134 | + $new = true; |
|
135 | + } |
|
136 | + } |
|
137 | + return $new; |
|
138 | + } |
|
139 | 139 | |
140 | 140 | public function save($data, $tryagain = true) { |
141 | - $new = $this->getIfNew($data); |
|
141 | + $new = $this->getIfNew($data); |
|
142 | 142 | |
143 | 143 | try { |
144 | - $result = $this->insert($this->table, $this->primaryKey, $data); |
|
144 | + $result = $this->insert($this->table, $this->primaryKey, $data); |
|
145 | 145 | |
146 | 146 | //If there was an error but PDO is silent, trigger the catch block anyway |
147 | 147 | if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
@@ -159,26 +159,26 @@ discard block |
||
159 | 159 | $this->updateCache($data); |
160 | 160 | } |
161 | 161 | |
162 | - private function getTryAgain($tryagain) { |
|
163 | - return $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
|
164 | - } |
|
162 | + private function getTryAgain($tryagain) { |
|
163 | + return $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
|
164 | + } |
|
165 | 165 | |
166 | - private function updatePK($data, $new) { |
|
167 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
168 | - } |
|
166 | + private function updatePK($data, $new) { |
|
167 | + if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
168 | + } |
|
169 | 169 | |
170 | - private function checkIfUpdateWorked($data) { |
|
171 | - $updateWhere = $this->whereBuilder->createSql($data); |
|
172 | - $matched = $this->findByField($updateWhere['args']); |
|
170 | + private function checkIfUpdateWorked($data) { |
|
171 | + $updateWhere = $this->whereBuilder->createSql($data); |
|
172 | + $matched = $this->findByField($updateWhere['args']); |
|
173 | 173 | |
174 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
175 | - } |
|
174 | + if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
175 | + } |
|
176 | 176 | |
177 | - private function updateCache($data) { |
|
178 | - $pkValue = $data->{$this->primaryKey[0]}; |
|
177 | + private function updateCache($data) { |
|
178 | + $pkValue = $data->{$this->primaryKey[0]}; |
|
179 | 179 | if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
180 | 180 | else $this->cache[$pkValue] = $data; |
181 | - } |
|
181 | + } |
|
182 | 182 | |
183 | 183 | private function insert($table, array $primaryKey, $data) { |
184 | 184 | $error = 0; |
@@ -190,20 +190,20 @@ discard block |
||
190 | 190 | } |
191 | 191 | |
192 | 192 | if ($error || $result->errorCode() !== '00000') { |
193 | - $result = $this->tryUpdate($table, $primaryKey, $data); |
|
194 | - } |
|
193 | + $result = $this->tryUpdate($table, $primaryKey, $data); |
|
194 | + } |
|
195 | 195 | |
196 | 196 | return $result; |
197 | 197 | } |
198 | 198 | |
199 | - private function tryUpdate($table, array $primaryKey, $data) { |
|
200 | - $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
201 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
199 | + private function tryUpdate($table, array $primaryKey, $data) { |
|
200 | + $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
201 | + if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
202 | 202 | |
203 | - return $result; |
|
204 | - } |
|
203 | + return $result; |
|
204 | + } |
|
205 | 205 | |
206 | - private function selectQuery(\Maphper\Lib\Query $query) { |
|
207 | - return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
208 | - } |
|
206 | + private function selectQuery(\Maphper\Lib\Query $query) { |
|
207 | + return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
208 | + } |
|
209 | 209 | } |
@@ -29,9 +29,9 @@ discard block |
||
29 | 29 | $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
30 | 30 | $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
31 | 31 | |
32 | - $this->fields = implode(',', array_map([$this->adapter, 'quote'], (array) $this->options->read('fields'))); |
|
32 | + $this->fields = implode(',', array_map([$this->adapter, 'quote'], (array)$this->options->read('fields'))); |
|
33 | 33 | |
34 | - $this->defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort') : implode(', ', $this->primaryKey); |
|
34 | + $this->defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort') : implode(', ', $this->primaryKey); |
|
35 | 35 | |
36 | 36 | $this->alterDb = $this->options->getEditMode(); |
37 | 37 | |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | } |
40 | 40 | |
41 | 41 | private function optimizeColumns() { |
42 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
42 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0, 500) == 1) $this->adapter->optimiseColumns($this->table); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | public function getPrimaryKey() { |
@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | |
177 | 177 | private function updateCache($data) { |
178 | 178 | $pkValue = $data->{$this->primaryKey[0]}; |
179 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
179 | + if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object)array_merge((array)$this->cache[$pkValue], (array)$data); |
|
180 | 180 | else $this->cache[$pkValue] = $data; |
181 | 181 | } |
182 | 182 |
@@ -39,7 +39,9 @@ discard block |
||
39 | 39 | } |
40 | 40 | |
41 | 41 | private function optimizeColumns() { |
42 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
42 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) { |
|
43 | + $this->adapter->optimiseColumns($this->table); |
|
44 | + } |
|
43 | 45 | } |
44 | 46 | |
45 | 47 | public function getPrimaryKey() { |
@@ -55,19 +57,23 @@ discard block |
||
55 | 57 | if (!isset($this->cache[$id])) { |
56 | 58 | try { |
57 | 59 | $result = $this->selectQuery($this->selectBuilder->select($this->table, $this->getPrimaryKey()[0] . ' = :id', [':id' => $id], ['limit' => 1])); |
58 | - } |
|
59 | - catch (\Exception $e) { |
|
60 | + } catch (\Exception $e) { |
|
60 | 61 | } |
61 | 62 | |
62 | - if (isset($result[0])) $this->cache[$id] = $result[0]; |
|
63 | - else return null; |
|
63 | + if (isset($result[0])) { |
|
64 | + $this->cache[$id] = $result[0]; |
|
65 | + } else { |
|
66 | + return null; |
|
67 | + } |
|
64 | 68 | } |
65 | 69 | return $this->cache[$id]; |
66 | 70 | } |
67 | 71 | |
68 | 72 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
69 | 73 | //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]; |
|
74 | + if (is_array($field)) { |
|
75 | + $field = $field[0]; |
|
76 | + } |
|
71 | 77 | $query = $this->whereBuilder->createSql($criteria); |
72 | 78 | |
73 | 79 | try { |
@@ -76,8 +82,7 @@ discard block |
||
76 | 82 | $result = $this->selectQuery($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group)); |
77 | 83 | |
78 | 84 | return $this->determineAggregateResult($result, $group, $field); |
79 | - } |
|
80 | - catch (\Exception $e) { |
|
85 | + } catch (\Exception $e) { |
|
81 | 86 | return $group ? [] : 0; |
82 | 87 | } |
83 | 88 | } |
@@ -85,15 +90,21 @@ discard block |
||
85 | 90 | private function determineAggregateResult($result, $group, $field) { |
86 | 91 | if ($group != null) { |
87 | 92 | $ret = []; |
88 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
93 | + foreach ($result as $res) { |
|
94 | + $ret[$res->$field] = $res->val; |
|
95 | + } |
|
89 | 96 | return $ret; |
97 | + } else if (isset($result[0])) { |
|
98 | + return $result[0]->val; |
|
99 | + } else { |
|
100 | + return 0; |
|
90 | 101 | } |
91 | - else if (isset($result[0])) return $result[0]->val; |
|
92 | - else return 0; |
|
93 | 102 | } |
94 | 103 | |
95 | 104 | private function addIndex($args) { |
96 | - if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
|
105 | + if (self::EDIT_INDEX & $this->alterDb) { |
|
106 | + $this->adapter->addIndex($this->table, $args); |
|
107 | + } |
|
97 | 108 | } |
98 | 109 | |
99 | 110 | public function findByField(array $fields, $options = []) { |
@@ -101,14 +112,15 @@ discard block |
||
101 | 112 | if (!isset($this->resultCache[$cacheId])) { |
102 | 113 | $query = $this->whereBuilder->createSql($fields); |
103 | 114 | |
104 | - if (!isset($options['order'])) $options['order'] = $this->defaultSort; |
|
115 | + if (!isset($options['order'])) { |
|
116 | + $options['order'] = $this->defaultSort; |
|
117 | + } |
|
105 | 118 | |
106 | 119 | try { |
107 | 120 | $this->resultCache[$cacheId] = $this->selectQuery($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options)); |
108 | 121 | $this->addIndex(array_keys($query['args'])); |
109 | 122 | $this->addIndex(explode(',', $options['order'])); |
110 | - } |
|
111 | - catch (\Exception $e) { |
|
123 | + } catch (\Exception $e) { |
|
112 | 124 | $this->errors[] = $e; |
113 | 125 | $this->resultCache[$cacheId] = []; |
114 | 126 | } |
@@ -144,10 +156,13 @@ discard block |
||
144 | 156 | $result = $this->insert($this->table, $this->primaryKey, $data); |
145 | 157 | |
146 | 158 | //If there was an error but PDO is silent, trigger the catch block anyway |
147 | - if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
|
148 | - } |
|
149 | - catch (\Exception $e) { |
|
150 | - if (!$this->getTryAgain($tryagain)) throw $e; |
|
159 | + if ($result->errorCode() !== '00000') { |
|
160 | + throw new \Exception('Could not insert into ' . $this->table); |
|
161 | + } |
|
162 | + } catch (\Exception $e) { |
|
163 | + if (!$this->getTryAgain($tryagain)) { |
|
164 | + throw $e; |
|
165 | + } |
|
151 | 166 | |
152 | 167 | $this->adapter->alterDatabase($this->table, $this->primaryKey, $data); |
153 | 168 | $this->save($data, false); |
@@ -164,28 +179,34 @@ discard block |
||
164 | 179 | } |
165 | 180 | |
166 | 181 | private function updatePK($data, $new) { |
167 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
182 | + if ($new && count($this->primaryKey) == 1) { |
|
183 | + $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
184 | + } |
|
168 | 185 | } |
169 | 186 | |
170 | 187 | private function checkIfUpdateWorked($data) { |
171 | 188 | $updateWhere = $this->whereBuilder->createSql($data); |
172 | 189 | $matched = $this->findByField($updateWhere['args']); |
173 | 190 | |
174 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
191 | + if (count($matched) == 0) { |
|
192 | + throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
193 | + } |
|
175 | 194 | } |
176 | 195 | |
177 | 196 | private function updateCache($data) { |
178 | 197 | $pkValue = $data->{$this->primaryKey[0]}; |
179 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
180 | - else $this->cache[$pkValue] = $data; |
|
198 | + if (isset($this->cache[$pkValue])) { |
|
199 | + $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
200 | + } else { |
|
201 | + $this->cache[$pkValue] = $data; |
|
202 | + } |
|
181 | 203 | } |
182 | 204 | |
183 | 205 | private function insert($table, array $primaryKey, $data) { |
184 | 206 | $error = 0; |
185 | 207 | try { |
186 | 208 | $result = $this->adapter->query($this->crudBuilder->insert($table, $data)); |
187 | - } |
|
188 | - catch (\Exception $e) { |
|
209 | + } catch (\Exception $e) { |
|
189 | 210 | $error = 1; |
190 | 211 | } |
191 | 212 | |
@@ -198,7 +219,9 @@ discard block |
||
198 | 219 | |
199 | 220 | private function tryUpdate($table, array $primaryKey, $data) { |
200 | 221 | $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
201 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
222 | + if ($result->rowCount() === 0) { |
|
223 | + $this->checkIfUpdateWorked($data); |
|
224 | + } |
|
202 | 225 | |
203 | 226 | return $result; |
204 | 227 | } |