@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | * @param array $options Options to pass to the PDO constructor |
26 | 26 | * @return SQLConnection The datasource resource |
27 | 27 | */ |
28 | - public static function register($name, $dsn, $username=null, $password=null, $options=[]){ |
|
28 | + public static function register($name, $dsn, $username = null, $password = null, $options = []) { |
|
29 | 29 | return self::$connections[$name] = new SQLConnection($dsn, $username, $password, $options); |
30 | 30 | } |
31 | 31 | |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | * @param array $options Options to pass to the PDO constructor |
38 | 38 | * @return SQLConnection The datasource resource |
39 | 39 | */ |
40 | - public static function connect($dsn, $username=null, $password=null, $options=[]){ |
|
40 | + public static function connect($dsn, $username = null, $password = null, $options = []) { |
|
41 | 41 | return self::register('default', $dsn, $username, $password, $options); |
42 | 42 | } |
43 | 43 | |
@@ -46,8 +46,8 @@ discard block |
||
46 | 46 | * @param string $name The datasource name |
47 | 47 | * @return bool `true` if correctly changed |
48 | 48 | */ |
49 | - public static function defaultTo($name){ |
|
50 | - if (isset(self::$connections[$name])){ |
|
49 | + public static function defaultTo($name) { |
|
50 | + if (isset(self::$connections[$name])) { |
|
51 | 51 | self::$current = $name; |
52 | 52 | return true; |
53 | 53 | } else return false; |
@@ -58,11 +58,11 @@ discard block |
||
58 | 58 | * @param string $name The datasource name, omit for close all of them |
59 | 59 | * @return bool `true` if one or more datasource where closed |
60 | 60 | */ |
61 | - public static function close($name=null){ |
|
61 | + public static function close($name = null) { |
|
62 | 62 | if ($name === null) { |
63 | 63 | foreach (self::$connections as $conn) $conn->close(); |
64 | 64 | return true; |
65 | - } else if (isset(self::$connections[$name])){ |
|
65 | + } else if (isset(self::$connections[$name])) { |
|
66 | 66 | self::$connections[$name]->close(); |
67 | 67 | return true; |
68 | 68 | } else return false; |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | * @param strinf $name The datasource name |
74 | 74 | * @return SQLConnect The datasource connection |
75 | 75 | */ |
76 | - public static function using($name){ |
|
76 | + public static function using($name) { |
|
77 | 77 | if (empty(self::$connections[$name])) throw new \Exception("[SQL] Unknown connection named '$name'."); |
78 | 78 | return self::$connections[$name]; |
79 | 79 | } |
@@ -84,9 +84,9 @@ discard block |
||
84 | 84 | * @param array $args The method arguments |
85 | 85 | * @return mixed The method return value |
86 | 86 | */ |
87 | - public static function __callStatic($method, $args){ |
|
87 | + public static function __callStatic($method, $args) { |
|
88 | 88 | if (empty(self::$connections[self::$current])) throw new \Exception("[SQL] No default connection defined."); |
89 | - return call_user_func_array([self::$connections[self::$current],$method],$args); |
|
89 | + return call_user_func_array([self::$connections[self::$current], $method], $args); |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | } |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | $queries = [], |
102 | 102 | $last_exec_success = true; |
103 | 103 | |
104 | - public function __construct($dsn, $username=null, $password=null, $options=[]){ |
|
104 | + public function __construct($dsn, $username = null, $password = null, $options = []) { |
|
105 | 105 | $this->connection = [ |
106 | 106 | 'dsn' => $dsn, |
107 | 107 | 'pdo' => null, |
@@ -111,20 +111,20 @@ discard block |
||
111 | 111 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
112 | 112 | PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, |
113 | 113 | PDO::ATTR_EMULATE_PREPARES => true, |
114 | - ],$options), |
|
114 | + ], $options), |
|
115 | 115 | ]; |
116 | 116 | // "The auto-commit mode cannot be changed for this driver" SQLite workaround |
117 | - if (strpos($dsn,'sqlite:') === 0) { |
|
117 | + if (strpos($dsn, 'sqlite:') === 0) { |
|
118 | 118 | $this->connection['options'] = $options; |
119 | 119 | } |
120 | 120 | } |
121 | 121 | |
122 | - public function close(){ |
|
122 | + public function close() { |
|
123 | 123 | $this->connection['pdo'] = null; |
124 | 124 | } |
125 | 125 | |
126 | - public function connection(){ |
|
127 | - if(empty($this->connection['pdo'])) { |
|
126 | + public function connection() { |
|
127 | + if (empty($this->connection['pdo'])) { |
|
128 | 128 | try { |
129 | 129 | $this->connection['pdo'] = new PDO( |
130 | 130 | $this->connection['dsn'], |
@@ -133,26 +133,26 @@ discard block |
||
133 | 133 | $this->connection['options'] |
134 | 134 | |
135 | 135 | ); |
136 | - Event::trigger('core.sql.connect',$this); |
|
137 | - } catch(Exception $e) { |
|
136 | + Event::trigger('core.sql.connect', $this); |
|
137 | + } catch (Exception $e) { |
|
138 | 138 | $this->connection['pdo'] = null; |
139 | 139 | } |
140 | 140 | } |
141 | 141 | return $this->connection['pdo']; |
142 | 142 | } |
143 | 143 | |
144 | - public function prepare($query){ |
|
145 | - if(!$this->connection()) return false; |
|
144 | + public function prepare($query) { |
|
145 | + if (!$this->connection()) return false; |
|
146 | 146 | return isset($this->queries[$query]) ? $this->queries[$query] : ($this->queries[$query] = $this->connection()->prepare($query)); |
147 | 147 | } |
148 | 148 | |
149 | - public function exec($query, $params=[]){ |
|
150 | - if(!$this->connection()) return false; |
|
149 | + public function exec($query, $params = []) { |
|
150 | + if (!$this->connection()) return false; |
|
151 | 151 | |
152 | - if (false==is_array($params)) $params = (array)$params; |
|
153 | - $query = Filter::with('core.sql.query',$query); |
|
154 | - if($statement = $this->prepare($query)){ |
|
155 | - Event::trigger('core.sql.query',$query,$params,(bool)$statement); |
|
152 | + if (false == is_array($params)) $params = (array) $params; |
|
153 | + $query = Filter::with('core.sql.query', $query); |
|
154 | + if ($statement = $this->prepare($query)) { |
|
155 | + Event::trigger('core.sql.query', $query, $params, (bool) $statement); |
|
156 | 156 | |
157 | 157 | foreach ($params as $key => $val) { |
158 | 158 | $type = PDO::PARAM_STR; |
@@ -164,10 +164,10 @@ discard block |
||
164 | 164 | $type = PDO::PARAM_INT; |
165 | 165 | } |
166 | 166 | // bindValue need a 1-based numeric parameter |
167 | - $statement->bindValue(is_numeric($key)?$key+1:':'.$key, $val, $type); |
|
167 | + $statement->bindValue(is_numeric($key) ? $key + 1 : ':'.$key, $val, $type); |
|
168 | 168 | } |
169 | 169 | } else { |
170 | - Event::trigger('core.sql.error',$query,$params); |
|
170 | + Event::trigger('core.sql.error', $query, $params); |
|
171 | 171 | return false; |
172 | 172 | } |
173 | 173 | |
@@ -175,32 +175,32 @@ discard block |
||
175 | 175 | return $statement; |
176 | 176 | } |
177 | 177 | |
178 | - public function value($query, $params=[], $column=0){ |
|
179 | - if(!$this->connection()) return false; |
|
178 | + public function value($query, $params = [], $column = 0) { |
|
179 | + if (!$this->connection()) return false; |
|
180 | 180 | |
181 | - $res = $this->exec($query,$params); |
|
181 | + $res = $this->exec($query, $params); |
|
182 | 182 | return $res ? $res->fetchColumn($column) : null; |
183 | 183 | } |
184 | 184 | |
185 | - public function each($query, $params=[], callable $looper = null){ |
|
186 | - if(!$this->connection()) return false; |
|
185 | + public function each($query, $params = [], callable $looper = null) { |
|
186 | + if (!$this->connection()) return false; |
|
187 | 187 | |
188 | 188 | // ($query,$looper) shorthand |
189 | - if ($looper===null && is_callable($params)) {$looper = $params; $params = [];} |
|
190 | - if( $res = $this->exec($query,$params) ){ |
|
191 | - if(is_callable($looper)) |
|
189 | + if ($looper === null && is_callable($params)) {$looper = $params;$params = [];} |
|
190 | + if ($res = $this->exec($query, $params)) { |
|
191 | + if (is_callable($looper)) |
|
192 | 192 | while ($row = $res->fetchObject()) $looper($row); |
193 | 193 | else |
194 | 194 | return $res->fetchAll(PDO::FETCH_CLASS); |
195 | 195 | } |
196 | 196 | } |
197 | 197 | |
198 | - public function single($query, $params=[], callable $handler = null){ |
|
199 | - if(!$this->connection()) return false; |
|
198 | + public function single($query, $params = [], callable $handler = null) { |
|
199 | + if (!$this->connection()) return false; |
|
200 | 200 | |
201 | 201 | // ($query,$handler) shorthand |
202 | - if ($handler===null && is_callable($params)) {$handler = $params; $params = [];} |
|
203 | - if( $res = $this->exec($query,$params) ){ |
|
202 | + if ($handler === null && is_callable($params)) {$handler = $params;$params = [];} |
|
203 | + if ($res = $this->exec($query, $params)) { |
|
204 | 204 | if (is_callable($handler)) |
205 | 205 | $handler($res->fetchObject()); |
206 | 206 | else |
@@ -208,76 +208,76 @@ discard block |
||
208 | 208 | } |
209 | 209 | } |
210 | 210 | |
211 | - public function run($script){ |
|
212 | - if(!$this->connection()) return false; |
|
211 | + public function run($script) { |
|
212 | + if (!$this->connection()) return false; |
|
213 | 213 | |
214 | - $sql_path = Options::get('database.sql.path',APP_DIR.'/sql'); |
|
215 | - $sql_sep = Options::get('database.sql.separator',';'); |
|
216 | - if (is_file($f = "$sql_path/$script.sql")){ |
|
214 | + $sql_path = Options::get('database.sql.path', APP_DIR.'/sql'); |
|
215 | + $sql_sep = Options::get('database.sql.separator', ';'); |
|
216 | + if (is_file($f = "$sql_path/$script.sql")) { |
|
217 | 217 | $result = true; |
218 | - foreach(explode($sql_sep,file_get_contents($f)) as $statement) { |
|
218 | + foreach (explode($sql_sep, file_get_contents($f)) as $statement) { |
|
219 | 219 | $result = $this->exec($statement); |
220 | 220 | } |
221 | 221 | return $result; |
222 | 222 | } else return false; |
223 | 223 | } |
224 | 224 | |
225 | - public function all($query, $params=[], callable $looper = null){ |
|
226 | - if(!$this->connection()) return false; |
|
227 | - return $this->each($query,$params,$looper); |
|
225 | + public function all($query, $params = [], callable $looper = null) { |
|
226 | + if (!$this->connection()) return false; |
|
227 | + return $this->each($query, $params, $looper); |
|
228 | 228 | } |
229 | 229 | |
230 | - public function delete($table, $pks=null, $pk='id', $inclusive=true){ |
|
231 | - if(!$this->connection()) return false; |
|
230 | + public function delete($table, $pks = null, $pk = 'id', $inclusive = true) { |
|
231 | + if (!$this->connection()) return false; |
|
232 | 232 | |
233 | - if (null===$pks) { |
|
233 | + if (null === $pks) { |
|
234 | 234 | return $this->exec("DELETE FROM `$table`"); |
235 | 235 | } else { |
236 | - return $this->exec("DELETE FROM `$table` WHERE `$pk` ".($inclusive ? "" : "NOT " )."IN (" . implode( ',', array_fill_keys( (array)$pks, '?' ) ) . ")",(array)$pks); |
|
236 | + return $this->exec("DELETE FROM `$table` WHERE `$pk` ".($inclusive ? "" : "NOT ")."IN (".implode(',', array_fill_keys((array) $pks, '?')).")", (array) $pks); |
|
237 | 237 | } |
238 | 238 | } |
239 | 239 | |
240 | - public function insert($table, $data){ |
|
241 | - if(!$this->connection()) return false; |
|
240 | + public function insert($table, $data) { |
|
241 | + if (!$this->connection()) return false; |
|
242 | 242 | |
243 | - if (false==is_array($data)) $data = (array)$data; |
|
243 | + if (false == is_array($data)) $data = (array) $data; |
|
244 | 244 | $k = array_keys($data); |
245 | 245 | asort($k); |
246 | 246 | $pk = $k; |
247 | - array_walk($pk,function(&$e){ $e = ':'.$e;}); |
|
248 | - $q = "INSERT INTO `$table` (`".implode('`,`',$k)."`) VALUES (".implode(',',$pk).")"; |
|
249 | - $this->exec($q,$data); |
|
247 | + array_walk($pk, function(&$e) { $e = ':'.$e;}); |
|
248 | + $q = "INSERT INTO `$table` (`".implode('`,`', $k)."`) VALUES (".implode(',', $pk).")"; |
|
249 | + $this->exec($q, $data); |
|
250 | 250 | return $this->last_exec_success ? $this->connection()->lastInsertId() : false; |
251 | 251 | } |
252 | 252 | |
253 | - public function updateWhere($table, $data, $where, $pk='id'){ |
|
254 | - if(!$this->connection()) return false; |
|
253 | + public function updateWhere($table, $data, $where, $pk = 'id') { |
|
254 | + if (!$this->connection()) return false; |
|
255 | 255 | |
256 | - if (false==is_array($data)) $data = (array)$data; |
|
256 | + if (false == is_array($data)) $data = (array) $data; |
|
257 | 257 | if (empty($data)) return false; |
258 | 258 | $k = array_keys($data); |
259 | 259 | asort($k); |
260 | 260 | |
261 | 261 | // Remove primary key from SET |
262 | - array_walk($k,function(&$e) use ($pk) { |
|
263 | - $e = ($e==$pk) ? null : "`$e`=:$e"; |
|
262 | + array_walk($k, function(&$e) use ($pk) { |
|
263 | + $e = ($e == $pk) ? null : "`$e`=:$e"; |
|
264 | 264 | }); |
265 | 265 | |
266 | - $q = "UPDATE `$table` SET ".implode(', ',array_filter($k))." WHERE $where"; |
|
267 | - $this->exec($q,$data); |
|
266 | + $q = "UPDATE `$table` SET ".implode(', ', array_filter($k))." WHERE $where"; |
|
267 | + $this->exec($q, $data); |
|
268 | 268 | return $this->last_exec_success; |
269 | 269 | } |
270 | 270 | |
271 | - public function update($table, $data, $pk='id', $extra_where=''){ |
|
271 | + public function update($table, $data, $pk = 'id', $extra_where = '') { |
|
272 | 272 | return $this->updateWhere($table, $data, "`$pk`=:$pk $extra_where", $pk); |
273 | 273 | } |
274 | 274 | |
275 | - public function insertOrUpdate($table, $data=[], $pk='id', $extra_where=''){ |
|
276 | - if(!$this->connection()) return false; |
|
275 | + public function insertOrUpdate($table, $data = [], $pk = 'id', $extra_where = '') { |
|
276 | + if (!$this->connection()) return false; |
|
277 | 277 | |
278 | - if (false==is_array($data)) $data = (array)$data; |
|
278 | + if (false == is_array($data)) $data = (array) $data; |
|
279 | 279 | if (empty($data[$pk])) return $this->insert($table, $data); |
280 | - if( (string) $this->value("SELECT `$pk` FROM `$table` WHERE `$pk`=? LIMIT 1", [$data[$pk]]) === (string) $data[$pk] ){ |
|
280 | + if ((string) $this->value("SELECT `$pk` FROM `$table` WHERE `$pk`=? LIMIT 1", [$data[$pk]]) === (string) $data[$pk]) { |
|
281 | 281 | return $this->update($table, $data, $pk, $extra_where); |
282 | 282 | } else { |
283 | 283 | return $this->insert($table, $data); |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | * @param array $options Options to pass to the PDO constructor |
26 | 26 | * @return SQLConnection The datasource resource |
27 | 27 | */ |
28 | - public static function register($name, $dsn, $username=null, $password=null, $options=[]){ |
|
28 | + public static function register($name, $dsn, $username=null, $password=null, $options=[]) { |
|
29 | 29 | return self::$connections[$name] = new SQLConnection($dsn, $username, $password, $options); |
30 | 30 | } |
31 | 31 | |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | * @param array $options Options to pass to the PDO constructor |
38 | 38 | * @return SQLConnection The datasource resource |
39 | 39 | */ |
40 | - public static function connect($dsn, $username=null, $password=null, $options=[]){ |
|
40 | + public static function connect($dsn, $username=null, $password=null, $options=[]) { |
|
41 | 41 | return self::register('default', $dsn, $username, $password, $options); |
42 | 42 | } |
43 | 43 | |
@@ -46,11 +46,13 @@ discard block |
||
46 | 46 | * @param string $name The datasource name |
47 | 47 | * @return bool `true` if correctly changed |
48 | 48 | */ |
49 | - public static function defaultTo($name){ |
|
50 | - if (isset(self::$connections[$name])){ |
|
49 | + public static function defaultTo($name) { |
|
50 | + if (isset(self::$connections[$name])) { |
|
51 | 51 | self::$current = $name; |
52 | 52 | return true; |
53 | - } else return false; |
|
53 | + } else { |
|
54 | + return false; |
|
55 | + } |
|
54 | 56 | } |
55 | 57 | |
56 | 58 | /** |
@@ -58,14 +60,16 @@ discard block |
||
58 | 60 | * @param string $name The datasource name, omit for close all of them |
59 | 61 | * @return bool `true` if one or more datasource where closed |
60 | 62 | */ |
61 | - public static function close($name=null){ |
|
63 | + public static function close($name=null) { |
|
62 | 64 | if ($name === null) { |
63 | 65 | foreach (self::$connections as $conn) $conn->close(); |
64 | 66 | return true; |
65 | - } else if (isset(self::$connections[$name])){ |
|
67 | + } else if (isset(self::$connections[$name])) { |
|
66 | 68 | self::$connections[$name]->close(); |
67 | 69 | return true; |
68 | - } else return false; |
|
70 | + } else { |
|
71 | + return false; |
|
72 | + } |
|
69 | 73 | } |
70 | 74 | |
71 | 75 | /** |
@@ -73,8 +77,10 @@ discard block |
||
73 | 77 | * @param strinf $name The datasource name |
74 | 78 | * @return SQLConnect The datasource connection |
75 | 79 | */ |
76 | - public static function using($name){ |
|
77 | - if (empty(self::$connections[$name])) throw new \Exception("[SQL] Unknown connection named '$name'."); |
|
80 | + public static function using($name) { |
|
81 | + if (empty(self::$connections[$name])) { |
|
82 | + throw new \Exception("[SQL] Unknown connection named '$name'."); |
|
83 | + } |
|
78 | 84 | return self::$connections[$name]; |
79 | 85 | } |
80 | 86 | |
@@ -84,8 +90,10 @@ discard block |
||
84 | 90 | * @param array $args The method arguments |
85 | 91 | * @return mixed The method return value |
86 | 92 | */ |
87 | - public static function __callStatic($method, $args){ |
|
88 | - if (empty(self::$connections[self::$current])) throw new \Exception("[SQL] No default connection defined."); |
|
93 | + public static function __callStatic($method, $args) { |
|
94 | + if (empty(self::$connections[self::$current])) { |
|
95 | + throw new \Exception("[SQL] No default connection defined."); |
|
96 | + } |
|
89 | 97 | return call_user_func_array([self::$connections[self::$current],$method],$args); |
90 | 98 | } |
91 | 99 | |
@@ -101,7 +109,7 @@ discard block |
||
101 | 109 | $queries = [], |
102 | 110 | $last_exec_success = true; |
103 | 111 | |
104 | - public function __construct($dsn, $username=null, $password=null, $options=[]){ |
|
112 | + public function __construct($dsn, $username=null, $password=null, $options=[]) { |
|
105 | 113 | $this->connection = [ |
106 | 114 | 'dsn' => $dsn, |
107 | 115 | 'pdo' => null, |
@@ -119,11 +127,11 @@ discard block |
||
119 | 127 | } |
120 | 128 | } |
121 | 129 | |
122 | - public function close(){ |
|
130 | + public function close() { |
|
123 | 131 | $this->connection['pdo'] = null; |
124 | 132 | } |
125 | 133 | |
126 | - public function connection(){ |
|
134 | + public function connection() { |
|
127 | 135 | if(empty($this->connection['pdo'])) { |
128 | 136 | try { |
129 | 137 | $this->connection['pdo'] = new PDO( |
@@ -141,17 +149,23 @@ discard block |
||
141 | 149 | return $this->connection['pdo']; |
142 | 150 | } |
143 | 151 | |
144 | - public function prepare($query){ |
|
145 | - if(!$this->connection()) return false; |
|
152 | + public function prepare($query) { |
|
153 | + if(!$this->connection()) { |
|
154 | + return false; |
|
155 | + } |
|
146 | 156 | return isset($this->queries[$query]) ? $this->queries[$query] : ($this->queries[$query] = $this->connection()->prepare($query)); |
147 | 157 | } |
148 | 158 | |
149 | - public function exec($query, $params=[]){ |
|
150 | - if(!$this->connection()) return false; |
|
159 | + public function exec($query, $params=[]) { |
|
160 | + if(!$this->connection()) { |
|
161 | + return false; |
|
162 | + } |
|
151 | 163 | |
152 | - if (false==is_array($params)) $params = (array)$params; |
|
164 | + if (false==is_array($params)) { |
|
165 | + $params = (array)$params; |
|
166 | + } |
|
153 | 167 | $query = Filter::with('core.sql.query',$query); |
154 | - if($statement = $this->prepare($query)){ |
|
168 | + if($statement = $this->prepare($query)) { |
|
155 | 169 | Event::trigger('core.sql.query',$query,$params,(bool)$statement); |
156 | 170 | |
157 | 171 | foreach ($params as $key => $val) { |
@@ -175,60 +189,76 @@ discard block |
||
175 | 189 | return $statement; |
176 | 190 | } |
177 | 191 | |
178 | - public function value($query, $params=[], $column=0){ |
|
179 | - if(!$this->connection()) return false; |
|
192 | + public function value($query, $params=[], $column=0) { |
|
193 | + if(!$this->connection()) { |
|
194 | + return false; |
|
195 | + } |
|
180 | 196 | |
181 | 197 | $res = $this->exec($query,$params); |
182 | 198 | return $res ? $res->fetchColumn($column) : null; |
183 | 199 | } |
184 | 200 | |
185 | - public function each($query, $params=[], callable $looper = null){ |
|
186 | - if(!$this->connection()) return false; |
|
201 | + public function each($query, $params=[], callable $looper = null) { |
|
202 | + if(!$this->connection()) { |
|
203 | + return false; |
|
204 | + } |
|
187 | 205 | |
188 | 206 | // ($query,$looper) shorthand |
189 | 207 | if ($looper===null && is_callable($params)) {$looper = $params; $params = [];} |
190 | - if( $res = $this->exec($query,$params) ){ |
|
191 | - if(is_callable($looper)) |
|
192 | - while ($row = $res->fetchObject()) $looper($row); |
|
193 | - else |
|
194 | - return $res->fetchAll(PDO::FETCH_CLASS); |
|
208 | + if( $res = $this->exec($query,$params) ) { |
|
209 | + if(is_callable($looper)) { |
|
210 | + while ($row = $res->fetchObject()) $looper($row); |
|
211 | + } else { |
|
212 | + return $res->fetchAll(PDO::FETCH_CLASS); |
|
213 | + } |
|
195 | 214 | } |
196 | 215 | } |
197 | 216 | |
198 | - public function single($query, $params=[], callable $handler = null){ |
|
199 | - if(!$this->connection()) return false; |
|
217 | + public function single($query, $params=[], callable $handler = null) { |
|
218 | + if(!$this->connection()) { |
|
219 | + return false; |
|
220 | + } |
|
200 | 221 | |
201 | 222 | // ($query,$handler) shorthand |
202 | 223 | if ($handler===null && is_callable($params)) {$handler = $params; $params = [];} |
203 | - if( $res = $this->exec($query,$params) ){ |
|
204 | - if (is_callable($handler)) |
|
205 | - $handler($res->fetchObject()); |
|
206 | - else |
|
207 | - return $res->fetchObject(); |
|
224 | + if( $res = $this->exec($query,$params) ) { |
|
225 | + if (is_callable($handler)) { |
|
226 | + $handler($res->fetchObject()); |
|
227 | + } else { |
|
228 | + return $res->fetchObject(); |
|
229 | + } |
|
208 | 230 | } |
209 | 231 | } |
210 | 232 | |
211 | - public function run($script){ |
|
212 | - if(!$this->connection()) return false; |
|
233 | + public function run($script) { |
|
234 | + if(!$this->connection()) { |
|
235 | + return false; |
|
236 | + } |
|
213 | 237 | |
214 | 238 | $sql_path = Options::get('database.sql.path',APP_DIR.'/sql'); |
215 | 239 | $sql_sep = Options::get('database.sql.separator',';'); |
216 | - if (is_file($f = "$sql_path/$script.sql")){ |
|
240 | + if (is_file($f = "$sql_path/$script.sql")) { |
|
217 | 241 | $result = true; |
218 | 242 | foreach(explode($sql_sep,file_get_contents($f)) as $statement) { |
219 | 243 | $result = $this->exec($statement); |
220 | 244 | } |
221 | 245 | return $result; |
222 | - } else return false; |
|
246 | + } else { |
|
247 | + return false; |
|
248 | + } |
|
223 | 249 | } |
224 | 250 | |
225 | - public function all($query, $params=[], callable $looper = null){ |
|
226 | - if(!$this->connection()) return false; |
|
251 | + public function all($query, $params=[], callable $looper = null) { |
|
252 | + if(!$this->connection()) { |
|
253 | + return false; |
|
254 | + } |
|
227 | 255 | return $this->each($query,$params,$looper); |
228 | 256 | } |
229 | 257 | |
230 | - public function delete($table, $pks=null, $pk='id', $inclusive=true){ |
|
231 | - if(!$this->connection()) return false; |
|
258 | + public function delete($table, $pks=null, $pk='id', $inclusive=true) { |
|
259 | + if(!$this->connection()) { |
|
260 | + return false; |
|
261 | + } |
|
232 | 262 | |
233 | 263 | if (null===$pks) { |
234 | 264 | return $this->exec("DELETE FROM `$table`"); |
@@ -237,10 +267,14 @@ discard block |
||
237 | 267 | } |
238 | 268 | } |
239 | 269 | |
240 | - public function insert($table, $data){ |
|
241 | - if(!$this->connection()) return false; |
|
270 | + public function insert($table, $data) { |
|
271 | + if(!$this->connection()) { |
|
272 | + return false; |
|
273 | + } |
|
242 | 274 | |
243 | - if (false==is_array($data)) $data = (array)$data; |
|
275 | + if (false==is_array($data)) { |
|
276 | + $data = (array)$data; |
|
277 | + } |
|
244 | 278 | $k = array_keys($data); |
245 | 279 | asort($k); |
246 | 280 | $pk = $k; |
@@ -250,11 +284,17 @@ discard block |
||
250 | 284 | return $this->last_exec_success ? $this->connection()->lastInsertId() : false; |
251 | 285 | } |
252 | 286 | |
253 | - public function updateWhere($table, $data, $where, $pk='id'){ |
|
254 | - if(!$this->connection()) return false; |
|
287 | + public function updateWhere($table, $data, $where, $pk='id') { |
|
288 | + if(!$this->connection()) { |
|
289 | + return false; |
|
290 | + } |
|
255 | 291 | |
256 | - if (false==is_array($data)) $data = (array)$data; |
|
257 | - if (empty($data)) return false; |
|
292 | + if (false==is_array($data)) { |
|
293 | + $data = (array)$data; |
|
294 | + } |
|
295 | + if (empty($data)) { |
|
296 | + return false; |
|
297 | + } |
|
258 | 298 | $k = array_keys($data); |
259 | 299 | asort($k); |
260 | 300 | |
@@ -268,16 +308,22 @@ discard block |
||
268 | 308 | return $this->last_exec_success; |
269 | 309 | } |
270 | 310 | |
271 | - public function update($table, $data, $pk='id', $extra_where=''){ |
|
311 | + public function update($table, $data, $pk='id', $extra_where='') { |
|
272 | 312 | return $this->updateWhere($table, $data, "`$pk`=:$pk $extra_where", $pk); |
273 | 313 | } |
274 | 314 | |
275 | - public function insertOrUpdate($table, $data=[], $pk='id', $extra_where=''){ |
|
276 | - if(!$this->connection()) return false; |
|
315 | + public function insertOrUpdate($table, $data=[], $pk='id', $extra_where='') { |
|
316 | + if(!$this->connection()) { |
|
317 | + return false; |
|
318 | + } |
|
277 | 319 | |
278 | - if (false==is_array($data)) $data = (array)$data; |
|
279 | - if (empty($data[$pk])) return $this->insert($table, $data); |
|
280 | - if( (string) $this->value("SELECT `$pk` FROM `$table` WHERE `$pk`=? LIMIT 1", [$data[$pk]]) === (string) $data[$pk] ){ |
|
320 | + if (false==is_array($data)) { |
|
321 | + $data = (array)$data; |
|
322 | + } |
|
323 | + if (empty($data[$pk])) { |
|
324 | + return $this->insert($table, $data); |
|
325 | + } |
|
326 | + if( (string) $this->value("SELECT `$pk` FROM `$table` WHERE `$pk`=? LIMIT 1", [$data[$pk]]) === (string) $data[$pk] ) { |
|
281 | 327 | return $this->update($table, $data, $pk, $extra_where); |
282 | 328 | } else { |
283 | 329 | return $this->insert($table, $data); |