@@ -1,14 +1,14 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | namespace Maphper\DataSource; |
3 | 3 | class StmtCache { |
4 | - private $pdo; |
|
5 | - private $queryCache = []; |
|
4 | + private $pdo; |
|
5 | + private $queryCache = []; |
|
6 | 6 | |
7 | - public function __construct(\PDO $pdo) { |
|
8 | - $this->pdo = $pdo; |
|
9 | - } |
|
7 | + public function __construct(\PDO $pdo) { |
|
8 | + $this->pdo = $pdo; |
|
9 | + } |
|
10 | 10 | |
11 | - public function getCachedStmt($sql) { |
|
11 | + public function getCachedStmt($sql) { |
|
12 | 12 | $queryId = $this->getQueryId($sql); |
13 | 13 | if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId]; |
14 | 14 | else { |
@@ -18,11 +18,11 @@ discard block |
||
18 | 18 | return $stmt; |
19 | 19 | } |
20 | 20 | |
21 | - private function getQueryId($sql) { |
|
22 | - return md5($sql); |
|
23 | - } |
|
21 | + private function getQueryId($sql) { |
|
22 | + return md5($sql); |
|
23 | + } |
|
24 | 24 | |
25 | - public function deleteQueryFromCache($sql) { |
|
26 | - unset($this->queryCache[$this->getQueryId($sql)]); |
|
27 | - } |
|
25 | + public function deleteQueryFromCache($sql) { |
|
26 | + unset($this->queryCache[$this->getQueryId($sql)]); |
|
27 | + } |
|
28 | 28 | } |
@@ -10,10 +10,13 @@ |
||
10 | 10 | |
11 | 11 | public function getCachedStmt($sql) { |
12 | 12 | $queryId = $this->getQueryId($sql); |
13 | - if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId]; |
|
14 | - else { |
|
13 | + if (isset($this->queryCache[$queryId])) { |
|
14 | + $stmt = $this->queryCache[$queryId]; |
|
15 | + } else { |
|
15 | 16 | $stmt = $this->pdo->prepare($sql, [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]); |
16 | - if ($stmt) $this->queryCache[$queryId] = $stmt; |
|
17 | + if ($stmt) { |
|
18 | + $this->queryCache[$queryId] = $stmt; |
|
19 | + } |
|
17 | 20 | } |
18 | 21 | return $stmt; |
19 | 22 | } |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | $this->pdo = $pdo; |
9 | 9 | //Set to strict mode to detect 'out of range' errors, action at a distance but it needs to be set for all INSERT queries |
10 | 10 | $this->pdo->query('SET sql_mode = STRICT_ALL_TABLES'); |
11 | - $this->stmtCache = new StmtCache($pdo); |
|
11 | + $this->stmtCache = new StmtCache($pdo); |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | public function quote($str) { |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | public function query(\Maphper\Lib\Query $query) { |
19 | 19 | $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
20 | 20 | $args = $query->getArgs(); |
21 | - $stmt->execute($args); |
|
21 | + $stmt->execute($args); |
|
22 | 22 | |
23 | 23 | return $stmt; |
24 | 24 | } |
@@ -24,10 +24,15 @@ discard block |
||
24 | 24 | } |
25 | 25 | |
26 | 26 | private function getType($val) { |
27 | - if ($val instanceof \DateTime) return 'DATETIME'; |
|
28 | - else if (is_int($val)) return 'INT(11)'; |
|
29 | - else if (is_double($val)) return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
30 | - else if (is_string($val)) return strlen($val) < 192 ? 'VARCHAR(191)' : 'LONGBLOB'; |
|
27 | + if ($val instanceof \DateTime) { |
|
28 | + return 'DATETIME'; |
|
29 | + } else if (is_int($val)) { |
|
30 | + return 'INT(11)'; |
|
31 | + } else if (is_double($val)) { |
|
32 | + return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
33 | + } else if (is_string($val)) { |
|
34 | + return strlen($val) < 192 ? 'VARCHAR(191)' : 'LONGBLOB'; |
|
35 | + } |
|
31 | 36 | return 'VARCHAR(191)'; |
32 | 37 | } |
33 | 38 | |
@@ -36,8 +41,11 @@ discard block |
||
36 | 41 | $parts = []; |
37 | 42 | foreach ($primaryKey as $key) { |
38 | 43 | $pk = $data->$key; |
39 | - if ($pk == null) $parts[] = $key . ' INT(11) NOT NULL AUTO_INCREMENT'; |
|
40 | - else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
44 | + if ($pk == null) { |
|
45 | + $parts[] = $key . ' INT(11) NOT NULL AUTO_INCREMENT'; |
|
46 | + } else { |
|
47 | + $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
48 | + } |
|
41 | 49 | } |
42 | 50 | |
43 | 51 | $pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')'; |
@@ -48,15 +56,20 @@ discard block |
||
48 | 56 | $this->createTable($table, $primaryKey, $data); |
49 | 57 | |
50 | 58 | foreach ($data as $key => $value) { |
51 | - if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) continue; |
|
52 | - if (in_array($key, $primaryKey)) continue; |
|
59 | + if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) { |
|
60 | + continue; |
|
61 | + } |
|
62 | + if (in_array($key, $primaryKey)) { |
|
63 | + continue; |
|
64 | + } |
|
53 | 65 | |
54 | 66 | $type = $this->getType($value); |
55 | 67 | |
56 | 68 | try { |
57 | - if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table'); |
|
58 | - } |
|
59 | - catch (\Exception $e) { |
|
69 | + if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) { |
|
70 | + throw new \Exception('Could not alter table'); |
|
71 | + } |
|
72 | + } catch (\Exception $e) { |
|
60 | 73 | $this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type); |
61 | 74 | } |
62 | 75 | } |
@@ -74,7 +87,9 @@ discard block |
||
74 | 87 | $keyName = $this->quote(implode('_', $fields)); |
75 | 88 | |
76 | 89 | $results = $this->pdo->query('SHOW INDEX FROM ' . $this->quote($table) . ' WHERE Key_Name = "' . $keyName . '"'); |
77 | - if ($results && count($results->fetchAll()) == 0) $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')'); |
|
90 | + if ($results && count($results->fetchAll()) == 0) { |
|
91 | + $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')'); |
|
92 | + } |
|
78 | 93 | } |
79 | 94 | |
80 | 95 | public function optimiseColumns($table) { |
@@ -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,7 +15,7 @@ discard block |
||
15 | 15 | private $alterDb = false; |
16 | 16 | private $adapter; |
17 | 17 | private $crudBuilder; |
18 | - private $selectBuilder; |
|
18 | + private $selectBuilder; |
|
19 | 19 | |
20 | 20 | public function __construct($db, $table, $primaryKey = 'id', array $options = []) { |
21 | 21 | $this->options = new DatabaseOptions($db, $options); |
@@ -36,9 +36,9 @@ discard block |
||
36 | 36 | $this->optimizeColumns(); |
37 | 37 | } |
38 | 38 | |
39 | - private function optimizeColumns() { |
|
40 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
41 | - } |
|
39 | + private function optimizeColumns() { |
|
40 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
41 | + } |
|
42 | 42 | |
43 | 43 | public function getPrimaryKey() { |
44 | 44 | return $this->primaryKey; |
@@ -80,15 +80,15 @@ discard block |
||
80 | 80 | } |
81 | 81 | } |
82 | 82 | |
83 | - private function determineAggregateResult($result, $group, $field) { |
|
84 | - if ($group != null) { |
|
85 | - $ret = []; |
|
86 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
87 | - return $ret; |
|
88 | - } |
|
89 | - else if (isset($result[0])) return $result[0]->val; |
|
90 | - else return 0; |
|
91 | - } |
|
83 | + private function determineAggregateResult($result, $group, $field) { |
|
84 | + if ($group != null) { |
|
85 | + $ret = []; |
|
86 | + foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
87 | + return $ret; |
|
88 | + } |
|
89 | + else if (isset($result[0])) return $result[0]->val; |
|
90 | + else return 0; |
|
91 | + } |
|
92 | 92 | |
93 | 93 | private function addIndex($args) { |
94 | 94 | if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
@@ -124,22 +124,22 @@ discard block |
||
124 | 124 | $this->resultCache = []; |
125 | 125 | } |
126 | 126 | |
127 | - private function getIfNew($data) { |
|
128 | - $new = false; |
|
129 | - foreach ($this->primaryKey as $k) { |
|
130 | - if (empty($data->$k)) { |
|
131 | - $data->$k = null; |
|
132 | - $new = true; |
|
133 | - } |
|
134 | - } |
|
135 | - return $new; |
|
136 | - } |
|
127 | + private function getIfNew($data) { |
|
128 | + $new = false; |
|
129 | + foreach ($this->primaryKey as $k) { |
|
130 | + if (empty($data->$k)) { |
|
131 | + $data->$k = null; |
|
132 | + $new = true; |
|
133 | + } |
|
134 | + } |
|
135 | + return $new; |
|
136 | + } |
|
137 | 137 | |
138 | 138 | public function save($data, $tryagain = true) { |
139 | - $new = $this->getIfNew($data); |
|
139 | + $new = $this->getIfNew($data); |
|
140 | 140 | |
141 | 141 | try { |
142 | - $result = $this->insert($this->table, $this->primaryKey, $data); |
|
142 | + $result = $this->insert($this->table, $this->primaryKey, $data); |
|
143 | 143 | |
144 | 144 | //If there was an error but PDO is silent, trigger the catch block anyway |
145 | 145 | if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
@@ -157,26 +157,26 @@ discard block |
||
157 | 157 | $this->updateCache($data); |
158 | 158 | } |
159 | 159 | |
160 | - private function getTryAgain($tryagain) { |
|
161 | - return $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
|
162 | - } |
|
160 | + private function getTryAgain($tryagain) { |
|
161 | + return $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
|
162 | + } |
|
163 | 163 | |
164 | - private function updatePK($data, $new) { |
|
165 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
166 | - } |
|
164 | + private function updatePK($data, $new) { |
|
165 | + if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
166 | + } |
|
167 | 167 | |
168 | - private function checkIfUpdateWorked($data) { |
|
169 | - $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
|
170 | - $matched = $this->findByField($updateWhere->getArgs()); |
|
168 | + private function checkIfUpdateWorked($data) { |
|
169 | + $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
|
170 | + $matched = $this->findByField($updateWhere->getArgs()); |
|
171 | 171 | |
172 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
173 | - } |
|
172 | + if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
173 | + } |
|
174 | 174 | |
175 | - private function updateCache($data) { |
|
176 | - $pkValue = $data->{$this->primaryKey[0]}; |
|
175 | + private function updateCache($data) { |
|
176 | + $pkValue = $data->{$this->primaryKey[0]}; |
|
177 | 177 | if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
178 | 178 | else $this->cache[$pkValue] = $data; |
179 | - } |
|
179 | + } |
|
180 | 180 | |
181 | 181 | private function insert($table, array $primaryKey, $data) { |
182 | 182 | $error = 0; |
@@ -188,20 +188,20 @@ discard block |
||
188 | 188 | } |
189 | 189 | |
190 | 190 | if ($error || $result->errorCode() !== '00000') { |
191 | - $result = $this->tryUpdate($table, $primaryKey, $data); |
|
192 | - } |
|
191 | + $result = $this->tryUpdate($table, $primaryKey, $data); |
|
192 | + } |
|
193 | 193 | |
194 | 194 | return $result; |
195 | 195 | } |
196 | 196 | |
197 | - private function tryUpdate($table, array $primaryKey, $data) { |
|
198 | - $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
199 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
197 | + private function tryUpdate($table, array $primaryKey, $data) { |
|
198 | + $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
199 | + if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
200 | 200 | |
201 | - return $result; |
|
202 | - } |
|
201 | + return $result; |
|
202 | + } |
|
203 | 203 | |
204 | - private function selectQuery(\Maphper\Lib\Query $query) { |
|
205 | - return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
206 | - } |
|
204 | + private function selectQuery(\Maphper\Lib\Query $query) { |
|
205 | + return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
206 | + } |
|
207 | 207 | } |
@@ -37,7 +37,9 @@ discard block |
||
37 | 37 | } |
38 | 38 | |
39 | 39 | private function optimizeColumns() { |
40 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
40 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) { |
|
41 | + $this->adapter->optimiseColumns($this->table); |
|
42 | + } |
|
41 | 43 | } |
42 | 44 | |
43 | 45 | public function getPrimaryKey() { |
@@ -53,19 +55,23 @@ discard block |
||
53 | 55 | if (!isset($this->cache[$id])) { |
54 | 56 | try { |
55 | 57 | $result = $this->selectQuery($this->selectBuilder->select($this->table, $this->getPrimaryKey()[0] . ' = :id', [':id' => $id], ['limit' => 1])); |
56 | - } |
|
57 | - catch (\Exception $e) { |
|
58 | + } catch (\Exception $e) { |
|
58 | 59 | } |
59 | 60 | |
60 | - if (isset($result[0])) $this->cache[$id] = $result[0]; |
|
61 | - else return null; |
|
61 | + if (isset($result[0])) { |
|
62 | + $this->cache[$id] = $result[0]; |
|
63 | + } else { |
|
64 | + return null; |
|
65 | + } |
|
62 | 66 | } |
63 | 67 | return $this->cache[$id]; |
64 | 68 | } |
65 | 69 | |
66 | 70 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
67 | 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 |
68 | - if (is_array($field)) $field = $field[0]; |
|
72 | + if (is_array($field)) { |
|
73 | + $field = $field[0]; |
|
74 | + } |
|
69 | 75 | $query = $this->selectBuilder->createSql($criteria, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
70 | 76 | |
71 | 77 | try { |
@@ -74,8 +80,7 @@ discard block |
||
74 | 80 | $result = $this->selectQuery($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group)); |
75 | 81 | |
76 | 82 | return $this->determineAggregateResult($result, $group, $field); |
77 | - } |
|
78 | - catch (\Exception $e) { |
|
83 | + } catch (\Exception $e) { |
|
79 | 84 | return $group ? [] : 0; |
80 | 85 | } |
81 | 86 | } |
@@ -83,15 +88,21 @@ discard block |
||
83 | 88 | private function determineAggregateResult($result, $group, $field) { |
84 | 89 | if ($group != null) { |
85 | 90 | $ret = []; |
86 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
91 | + foreach ($result as $res) { |
|
92 | + $ret[$res->$field] = $res->val; |
|
93 | + } |
|
87 | 94 | return $ret; |
95 | + } else if (isset($result[0])) { |
|
96 | + return $result[0]->val; |
|
97 | + } else { |
|
98 | + return 0; |
|
88 | 99 | } |
89 | - else if (isset($result[0])) return $result[0]->val; |
|
90 | - else return 0; |
|
91 | 100 | } |
92 | 101 | |
93 | 102 | private function addIndex($args) { |
94 | - if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
|
103 | + if (self::EDIT_INDEX & $this->alterDb) { |
|
104 | + $this->adapter->addIndex($this->table, $args); |
|
105 | + } |
|
95 | 106 | } |
96 | 107 | |
97 | 108 | public function findByField(array $fields, $options = []) { |
@@ -99,14 +110,15 @@ discard block |
||
99 | 110 | if (!isset($this->resultCache[$cacheId])) { |
100 | 111 | $query = $this->selectBuilder->createSql($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
101 | 112 | |
102 | - if (!isset($options['order'])) $options['order'] = $this->defaultSort; |
|
113 | + if (!isset($options['order'])) { |
|
114 | + $options['order'] = $this->defaultSort; |
|
115 | + } |
|
103 | 116 | |
104 | 117 | try { |
105 | 118 | $this->resultCache[$cacheId] = $this->selectQuery($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options)); |
106 | 119 | $this->addIndex(array_keys($query['args'])); |
107 | 120 | $this->addIndex(explode(',', $options['order'])); |
108 | - } |
|
109 | - catch (\Exception $e) { |
|
121 | + } catch (\Exception $e) { |
|
110 | 122 | $this->errors[] = $e; |
111 | 123 | $this->resultCache[$cacheId] = []; |
112 | 124 | } |
@@ -142,10 +154,13 @@ discard block |
||
142 | 154 | $result = $this->insert($this->table, $this->primaryKey, $data); |
143 | 155 | |
144 | 156 | //If there was an error but PDO is silent, trigger the catch block anyway |
145 | - if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
|
146 | - } |
|
147 | - catch (\Exception $e) { |
|
148 | - if (!$this->getTryAgain($tryagain)) throw $e; |
|
157 | + if ($result->errorCode() !== '00000') { |
|
158 | + throw new \Exception('Could not insert into ' . $this->table); |
|
159 | + } |
|
160 | + } catch (\Exception $e) { |
|
161 | + if (!$this->getTryAgain($tryagain)) { |
|
162 | + throw $e; |
|
163 | + } |
|
149 | 164 | |
150 | 165 | $this->adapter->alterDatabase($this->table, $this->primaryKey, $data); |
151 | 166 | $this->save($data, false); |
@@ -162,28 +177,34 @@ discard block |
||
162 | 177 | } |
163 | 178 | |
164 | 179 | private function updatePK($data, $new) { |
165 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
180 | + if ($new && count($this->primaryKey) == 1) { |
|
181 | + $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
182 | + } |
|
166 | 183 | } |
167 | 184 | |
168 | 185 | private function checkIfUpdateWorked($data) { |
169 | 186 | $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
170 | 187 | $matched = $this->findByField($updateWhere->getArgs()); |
171 | 188 | |
172 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
189 | + if (count($matched) == 0) { |
|
190 | + throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
191 | + } |
|
173 | 192 | } |
174 | 193 | |
175 | 194 | private function updateCache($data) { |
176 | 195 | $pkValue = $data->{$this->primaryKey[0]}; |
177 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
178 | - else $this->cache[$pkValue] = $data; |
|
196 | + if (isset($this->cache[$pkValue])) { |
|
197 | + $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
198 | + } else { |
|
199 | + $this->cache[$pkValue] = $data; |
|
200 | + } |
|
179 | 201 | } |
180 | 202 | |
181 | 203 | private function insert($table, array $primaryKey, $data) { |
182 | 204 | $error = 0; |
183 | 205 | try { |
184 | 206 | $result = $this->adapter->query($this->crudBuilder->insert($table, $data)); |
185 | - } |
|
186 | - catch (\Exception $e) { |
|
207 | + } catch (\Exception $e) { |
|
187 | 208 | $error = 1; |
188 | 209 | } |
189 | 210 | |
@@ -196,7 +217,9 @@ discard block |
||
196 | 217 | |
197 | 218 | private function tryUpdate($table, array $primaryKey, $data) { |
198 | 219 | $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
199 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
220 | + if ($result->rowCount() === 0) { |
|
221 | + $this->checkIfUpdateWorked($data); |
|
222 | + } |
|
200 | 223 | |
201 | 224 | return $result; |
202 | 225 | } |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | |
8 | 8 | public function __construct(\PDO $pdo) { |
9 | 9 | $this->pdo = $pdo; |
10 | - $this->stmtCache = new StmtCache($pdo); |
|
10 | + $this->stmtCache = new StmtCache($pdo); |
|
11 | 11 | } |
12 | 12 | |
13 | 13 | public function quote($str) { |
@@ -15,19 +15,19 @@ discard block |
||
15 | 15 | } |
16 | 16 | |
17 | 17 | public function query(\Maphper\Lib\Query $query) { |
18 | - $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
|
18 | + $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
|
19 | 19 | $args = $query->getArgs(); |
20 | 20 | |
21 | - //Handle SQLite when PDO_ERRMODE is set to SILENT |
|
22 | - if ($stmt === false) throw new \Exception('Invalid query'); |
|
21 | + //Handle SQLite when PDO_ERRMODE is set to SILENT |
|
22 | + if ($stmt === false) throw new \Exception('Invalid query'); |
|
23 | 23 | |
24 | - $stmt->execute($args); |
|
25 | - if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') { |
|
24 | + $stmt->execute($args); |
|
25 | + if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') { |
|
26 | 26 | $this->stmtCache->deleteQueryFromCache($query->getSql()); |
27 | 27 | return $this->query($query); |
28 | - } |
|
28 | + } |
|
29 | 29 | |
30 | - return $stmt; |
|
30 | + return $stmt; |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | public function lastInsertId() { |
@@ -19,7 +19,9 @@ discard block |
||
19 | 19 | $args = $query->getArgs(); |
20 | 20 | |
21 | 21 | //Handle SQLite when PDO_ERRMODE is set to SILENT |
22 | - if ($stmt === false) throw new \Exception('Invalid query'); |
|
22 | + if ($stmt === false) { |
|
23 | + throw new \Exception('Invalid query'); |
|
24 | + } |
|
23 | 25 | |
24 | 26 | $stmt->execute($args); |
25 | 27 | if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') { |
@@ -35,12 +37,19 @@ discard block |
||
35 | 37 | } |
36 | 38 | |
37 | 39 | private function getType($val) { |
38 | - if ($val instanceof \DateTime) return 'DATETIME'; |
|
39 | - else if (is_int($val)) return 'INTEGER'; |
|
40 | - else if (is_double($val)) return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
41 | - else if (is_string($val) && strlen($val) < 256) return 'VARCHAR(255)'; |
|
42 | - else if (is_string($val) && strlen($val) > 256) return 'LONGBLOG'; |
|
43 | - else return 'VARCHAR(255)'; |
|
40 | + if ($val instanceof \DateTime) { |
|
41 | + return 'DATETIME'; |
|
42 | + } else if (is_int($val)) { |
|
43 | + return 'INTEGER'; |
|
44 | + } else if (is_double($val)) { |
|
45 | + return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
46 | + } else if (is_string($val) && strlen($val) < 256) { |
|
47 | + return 'VARCHAR(255)'; |
|
48 | + } else if (is_string($val) && strlen($val) > 256) { |
|
49 | + return 'LONGBLOG'; |
|
50 | + } else { |
|
51 | + return 'VARCHAR(255)'; |
|
52 | + } |
|
44 | 53 | } |
45 | 54 | |
46 | 55 | private function tableExists($name) { |
@@ -74,8 +83,7 @@ discard block |
||
74 | 83 | $this->pdo->query('INSERT INTO ' . $this->quote($table . $affix) . '(' . $columns . ') SELECT ' . $columns . ' FROM ' . $this->quote($table)); |
75 | 84 | $this->pdo->query('DROP TABLE IF EXISTS ' . $table ); |
76 | 85 | } |
77 | - } |
|
78 | - catch (\PDOException $e) { |
|
86 | + } catch (\PDOException $e) { |
|
79 | 87 | // No data to copy |
80 | 88 | echo $e->getMessage(); |
81 | 89 | } |
@@ -89,8 +97,11 @@ discard block |
||
89 | 97 | $parts = []; |
90 | 98 | foreach ($primaryKey as $key) { |
91 | 99 | $pk = $data->$key; |
92 | - if ($pk == null) $parts[] = $key . ' INTEGER'; |
|
93 | - else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
100 | + if ($pk == null) { |
|
101 | + $parts[] = $key . ' INTEGER'; |
|
102 | + } else { |
|
103 | + $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
104 | + } |
|
94 | 105 | } |
95 | 106 | |
96 | 107 | $pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')'; |
@@ -99,8 +110,12 @@ discard block |
||
99 | 110 | $this->pdo->query('CREATE TABLE ' . $table . ' (' . $pkField . ')'); |
100 | 111 | |
101 | 112 | foreach ($data as $key => $value) { |
102 | - if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) continue; |
|
103 | - if (in_array($key, $primaryKey)) continue; |
|
113 | + if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) { |
|
114 | + continue; |
|
115 | + } |
|
116 | + if (in_array($key, $primaryKey)) { |
|
117 | + continue; |
|
118 | + } |
|
104 | 119 | |
105 | 120 | $type = $this->getType($value); |
106 | 121 | |
@@ -110,10 +125,14 @@ discard block |
||
110 | 125 | |
111 | 126 | |
112 | 127 | public function addIndex($table, array $fields) { |
113 | - if (empty($fields)) return false; |
|
128 | + if (empty($fields)) { |
|
129 | + return false; |
|
130 | + } |
|
114 | 131 | |
115 | 132 | //SQLite doesn't support ASC/DESC indexes, remove the keywords |
116 | - foreach ($fields as &$field) $field = str_ireplace([' desc', ' asc'], '', $field); |
|
133 | + foreach ($fields as &$field) { |
|
134 | + $field = str_ireplace([' desc', ' asc'], '', $field); |
|
135 | + } |
|
117 | 136 | sort($fields); |
118 | 137 | $fields = array_map('strtolower', $fields); |
119 | 138 | $fields = array_map('trim', $fields); |
@@ -122,8 +141,7 @@ discard block |
||
122 | 141 | |
123 | 142 | try { |
124 | 143 | $this->pdo->query('CREATE INDEX IF NOT EXISTS ' . $keyName . ' ON ' . $table . ' (' . implode(', ', $fields) . ')'); |
125 | - } |
|
126 | - catch (\Exception $e) { |
|
144 | + } catch (\Exception $e) { |
|
127 | 145 | |
128 | 146 | } |
129 | 147 | } |