@@ -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,18 +175,18 @@ 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 column($query, $params=[], $column=0){ |
|
| 186 | - if(!$this->connection()) return false; |
|
| 185 | + public function column($query, $params = [], $column = 0) { |
|
| 186 | + if (!$this->connection()) return false; |
|
| 187 | 187 | |
| 188 | 188 | $results = []; |
| 189 | - $res = $this->exec($query,$params); |
|
| 189 | + $res = $this->exec($query, $params); |
|
| 190 | 190 | |
| 191 | 191 | if (is_string($column)) |
| 192 | 192 | while ($x = $res->fetch(PDO::FETCH_OBJ)) $results[] = $x->$column; |
@@ -196,25 +196,25 @@ discard block |
||
| 196 | 196 | return $results; |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | - public function each($query, $params=[], callable $looper = null){ |
|
| 200 | - if(!$this->connection()) return false; |
|
| 199 | + public function each($query, $params = [], callable $looper = null) { |
|
| 200 | + if (!$this->connection()) return false; |
|
| 201 | 201 | |
| 202 | 202 | // ($query,$looper) shorthand |
| 203 | - if ($looper===null && is_callable($params)) {$looper = $params; $params = [];} |
|
| 204 | - if( $res = $this->exec($query,$params) ){ |
|
| 205 | - if(is_callable($looper)) |
|
| 203 | + if ($looper === null && is_callable($params)) {$looper = $params;$params = [];} |
|
| 204 | + if ($res = $this->exec($query, $params)) { |
|
| 205 | + if (is_callable($looper)) |
|
| 206 | 206 | while ($row = $res->fetchObject()) $looper($row); |
| 207 | 207 | else |
| 208 | 208 | return $res->fetchAll(PDO::FETCH_CLASS); |
| 209 | 209 | } |
| 210 | 210 | } |
| 211 | 211 | |
| 212 | - public function single($query, $params=[], callable $handler = null){ |
|
| 213 | - if(!$this->connection()) return false; |
|
| 212 | + public function single($query, $params = [], callable $handler = null) { |
|
| 213 | + if (!$this->connection()) return false; |
|
| 214 | 214 | |
| 215 | 215 | // ($query,$handler) shorthand |
| 216 | - if ($handler===null && is_callable($params)) {$handler = $params; $params = [];} |
|
| 217 | - if( $res = $this->exec($query,$params) ){ |
|
| 216 | + if ($handler === null && is_callable($params)) {$handler = $params;$params = [];} |
|
| 217 | + if ($res = $this->exec($query, $params)) { |
|
| 218 | 218 | if (is_callable($handler)) |
| 219 | 219 | $handler($res->fetchObject()); |
| 220 | 220 | else |
@@ -222,76 +222,76 @@ discard block |
||
| 222 | 222 | } |
| 223 | 223 | } |
| 224 | 224 | |
| 225 | - public function run($script){ |
|
| 226 | - if(!$this->connection()) return false; |
|
| 225 | + public function run($script) { |
|
| 226 | + if (!$this->connection()) return false; |
|
| 227 | 227 | |
| 228 | - $sql_path = Options::get('database.sql.path',APP_DIR.'/sql'); |
|
| 229 | - $sql_sep = Options::get('database.sql.separator',';'); |
|
| 230 | - if (is_file($f = "$sql_path/$script.sql")){ |
|
| 228 | + $sql_path = Options::get('database.sql.path', APP_DIR.'/sql'); |
|
| 229 | + $sql_sep = Options::get('database.sql.separator', ';'); |
|
| 230 | + if (is_file($f = "$sql_path/$script.sql")) { |
|
| 231 | 231 | $result = true; |
| 232 | - foreach(explode($sql_sep,file_get_contents($f)) as $statement) { |
|
| 232 | + foreach (explode($sql_sep, file_get_contents($f)) as $statement) { |
|
| 233 | 233 | $result = $this->exec($statement); |
| 234 | 234 | } |
| 235 | 235 | return $result; |
| 236 | 236 | } else return false; |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | - public function all($query, $params=[], callable $looper = null){ |
|
| 240 | - if(!$this->connection()) return false; |
|
| 241 | - return $this->each($query,$params,$looper); |
|
| 239 | + public function all($query, $params = [], callable $looper = null) { |
|
| 240 | + if (!$this->connection()) return false; |
|
| 241 | + return $this->each($query, $params, $looper); |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | - public function delete($table, $pks=null, $pk='id', $inclusive=true){ |
|
| 245 | - if(!$this->connection()) return false; |
|
| 244 | + public function delete($table, $pks = null, $pk = 'id', $inclusive = true) { |
|
| 245 | + if (!$this->connection()) return false; |
|
| 246 | 246 | |
| 247 | - if (null===$pks) { |
|
| 247 | + if (null === $pks) { |
|
| 248 | 248 | return $this->exec("DELETE FROM `$table`"); |
| 249 | 249 | } else { |
| 250 | - return $this->exec("DELETE FROM `$table` WHERE `$pk` ".($inclusive ? "" : "NOT " )."IN (" . implode( ',', array_fill_keys( (array)$pks, '?' ) ) . ")",(array)$pks); |
|
| 250 | + return $this->exec("DELETE FROM `$table` WHERE `$pk` ".($inclusive ? "" : "NOT ")."IN (".implode(',', array_fill_keys((array) $pks, '?')).")", (array) $pks); |
|
| 251 | 251 | } |
| 252 | 252 | } |
| 253 | 253 | |
| 254 | - public function insert($table, $data){ |
|
| 255 | - if(!$this->connection()) return false; |
|
| 254 | + public function insert($table, $data) { |
|
| 255 | + if (!$this->connection()) return false; |
|
| 256 | 256 | |
| 257 | - if (false==is_array($data)) $data = (array)$data; |
|
| 257 | + if (false == is_array($data)) $data = (array) $data; |
|
| 258 | 258 | $k = array_keys($data); |
| 259 | 259 | asort($k); |
| 260 | 260 | $pk = $k; |
| 261 | - array_walk($pk,function(&$e){ $e = ':'.$e;}); |
|
| 262 | - $q = "INSERT INTO `$table` (`".implode('`,`',$k)."`) VALUES (".implode(',',$pk).")"; |
|
| 263 | - $this->exec($q,$data); |
|
| 261 | + array_walk($pk, function(&$e) { $e = ':'.$e;}); |
|
| 262 | + $q = "INSERT INTO `$table` (`".implode('`,`', $k)."`) VALUES (".implode(',', $pk).")"; |
|
| 263 | + $this->exec($q, $data); |
|
| 264 | 264 | return $this->last_exec_success ? $this->connection()->lastInsertId() : false; |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | - public function updateWhere($table, $data, $where, $pk='id'){ |
|
| 268 | - if(!$this->connection()) return false; |
|
| 267 | + public function updateWhere($table, $data, $where, $pk = 'id') { |
|
| 268 | + if (!$this->connection()) return false; |
|
| 269 | 269 | |
| 270 | - if (false==is_array($data)) $data = (array)$data; |
|
| 270 | + if (false == is_array($data)) $data = (array) $data; |
|
| 271 | 271 | if (empty($data)) return false; |
| 272 | 272 | $k = array_keys($data); |
| 273 | 273 | asort($k); |
| 274 | 274 | |
| 275 | 275 | // Remove primary key from SET |
| 276 | - array_walk($k,function(&$e) use ($pk) { |
|
| 277 | - $e = ($e==$pk) ? null : "`$e`=:$e"; |
|
| 276 | + array_walk($k, function(&$e) use ($pk) { |
|
| 277 | + $e = ($e == $pk) ? null : "`$e`=:$e"; |
|
| 278 | 278 | }); |
| 279 | 279 | |
| 280 | - $q = "UPDATE `$table` SET ".implode(', ',array_filter($k))." WHERE $where"; |
|
| 281 | - $this->exec($q,$data); |
|
| 280 | + $q = "UPDATE `$table` SET ".implode(', ', array_filter($k))." WHERE $where"; |
|
| 281 | + $this->exec($q, $data); |
|
| 282 | 282 | return $this->last_exec_success; |
| 283 | 283 | } |
| 284 | 284 | |
| 285 | - public function update($table, $data, $pk='id', $extra_where=''){ |
|
| 285 | + public function update($table, $data, $pk = 'id', $extra_where = '') { |
|
| 286 | 286 | return $this->updateWhere($table, $data, "`$pk`=:$pk $extra_where", $pk); |
| 287 | 287 | } |
| 288 | 288 | |
| 289 | - public function insertOrUpdate($table, $data=[], $pk='id', $extra_where=''){ |
|
| 290 | - if(!$this->connection()) return false; |
|
| 289 | + public function insertOrUpdate($table, $data = [], $pk = 'id', $extra_where = '') { |
|
| 290 | + if (!$this->connection()) return false; |
|
| 291 | 291 | |
| 292 | - if (false==is_array($data)) $data = (array)$data; |
|
| 292 | + if (false == is_array($data)) $data = (array) $data; |
|
| 293 | 293 | if (empty($data[$pk])) return $this->insert($table, $data); |
| 294 | - if( (string) $this->value("SELECT `$pk` FROM `$table` WHERE `$pk`=? LIMIT 1", [$data[$pk]]) === (string) $data[$pk] ){ |
|
| 294 | + if ((string) $this->value("SELECT `$pk` FROM `$table` WHERE `$pk`=? LIMIT 1", [$data[$pk]]) === (string) $data[$pk]) { |
|
| 295 | 295 | return $this->update($table, $data, $pk, $extra_where); |
| 296 | 296 | } else { |
| 297 | 297 | 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,74 +189,93 @@ 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 column($query, $params=[], $column=0){ |
|
| 186 | - if(!$this->connection()) return false; |
|
| 201 | + public function column($query, $params=[], $column=0) { |
|
| 202 | + if(!$this->connection()) { |
|
| 203 | + return false; |
|
| 204 | + } |
|
| 187 | 205 | |
| 188 | 206 | $results = []; |
| 189 | 207 | $res = $this->exec($query,$params); |
| 190 | 208 | |
| 191 | - if (is_string($column)) |
|
| 192 | - while ($x = $res->fetch(PDO::FETCH_OBJ)) $results[] = $x->$column; |
|
| 193 | - else |
|
| 194 | - while ($x = $res->fetchColumn($column)) $results[] = $x; |
|
| 209 | + if (is_string($column)) { |
|
| 210 | + while ($x = $res->fetch(PDO::FETCH_OBJ)) $results[] = $x->$column; |
|
| 211 | + } else { |
|
| 212 | + while ($x = $res->fetchColumn($column)) $results[] = $x; |
|
| 213 | + } |
|
| 195 | 214 | |
| 196 | 215 | return $results; |
| 197 | 216 | } |
| 198 | 217 | |
| 199 | - public function each($query, $params=[], callable $looper = null){ |
|
| 200 | - if(!$this->connection()) return false; |
|
| 218 | + public function each($query, $params=[], callable $looper = null) { |
|
| 219 | + if(!$this->connection()) { |
|
| 220 | + return false; |
|
| 221 | + } |
|
| 201 | 222 | |
| 202 | 223 | // ($query,$looper) shorthand |
| 203 | 224 | if ($looper===null && is_callable($params)) {$looper = $params; $params = [];} |
| 204 | - if( $res = $this->exec($query,$params) ){ |
|
| 205 | - if(is_callable($looper)) |
|
| 206 | - while ($row = $res->fetchObject()) $looper($row); |
|
| 207 | - else |
|
| 208 | - return $res->fetchAll(PDO::FETCH_CLASS); |
|
| 225 | + if( $res = $this->exec($query,$params) ) { |
|
| 226 | + if(is_callable($looper)) { |
|
| 227 | + while ($row = $res->fetchObject()) $looper($row); |
|
| 228 | + } else { |
|
| 229 | + return $res->fetchAll(PDO::FETCH_CLASS); |
|
| 230 | + } |
|
| 209 | 231 | } |
| 210 | 232 | } |
| 211 | 233 | |
| 212 | - public function single($query, $params=[], callable $handler = null){ |
|
| 213 | - if(!$this->connection()) return false; |
|
| 234 | + public function single($query, $params=[], callable $handler = null) { |
|
| 235 | + if(!$this->connection()) { |
|
| 236 | + return false; |
|
| 237 | + } |
|
| 214 | 238 | |
| 215 | 239 | // ($query,$handler) shorthand |
| 216 | 240 | if ($handler===null && is_callable($params)) {$handler = $params; $params = [];} |
| 217 | - if( $res = $this->exec($query,$params) ){ |
|
| 218 | - if (is_callable($handler)) |
|
| 219 | - $handler($res->fetchObject()); |
|
| 220 | - else |
|
| 221 | - return $res->fetchObject(); |
|
| 241 | + if( $res = $this->exec($query,$params) ) { |
|
| 242 | + if (is_callable($handler)) { |
|
| 243 | + $handler($res->fetchObject()); |
|
| 244 | + } else { |
|
| 245 | + return $res->fetchObject(); |
|
| 246 | + } |
|
| 222 | 247 | } |
| 223 | 248 | } |
| 224 | 249 | |
| 225 | - public function run($script){ |
|
| 226 | - if(!$this->connection()) return false; |
|
| 250 | + public function run($script) { |
|
| 251 | + if(!$this->connection()) { |
|
| 252 | + return false; |
|
| 253 | + } |
|
| 227 | 254 | |
| 228 | 255 | $sql_path = Options::get('database.sql.path',APP_DIR.'/sql'); |
| 229 | 256 | $sql_sep = Options::get('database.sql.separator',';'); |
| 230 | - if (is_file($f = "$sql_path/$script.sql")){ |
|
| 257 | + if (is_file($f = "$sql_path/$script.sql")) { |
|
| 231 | 258 | $result = true; |
| 232 | 259 | foreach(explode($sql_sep,file_get_contents($f)) as $statement) { |
| 233 | 260 | $result = $this->exec($statement); |
| 234 | 261 | } |
| 235 | 262 | return $result; |
| 236 | - } else return false; |
|
| 263 | + } else { |
|
| 264 | + return false; |
|
| 265 | + } |
|
| 237 | 266 | } |
| 238 | 267 | |
| 239 | - public function all($query, $params=[], callable $looper = null){ |
|
| 240 | - if(!$this->connection()) return false; |
|
| 268 | + public function all($query, $params=[], callable $looper = null) { |
|
| 269 | + if(!$this->connection()) { |
|
| 270 | + return false; |
|
| 271 | + } |
|
| 241 | 272 | return $this->each($query,$params,$looper); |
| 242 | 273 | } |
| 243 | 274 | |
| 244 | - public function delete($table, $pks=null, $pk='id', $inclusive=true){ |
|
| 245 | - if(!$this->connection()) return false; |
|
| 275 | + public function delete($table, $pks=null, $pk='id', $inclusive=true) { |
|
| 276 | + if(!$this->connection()) { |
|
| 277 | + return false; |
|
| 278 | + } |
|
| 246 | 279 | |
| 247 | 280 | if (null===$pks) { |
| 248 | 281 | return $this->exec("DELETE FROM `$table`"); |
@@ -251,10 +284,14 @@ discard block |
||
| 251 | 284 | } |
| 252 | 285 | } |
| 253 | 286 | |
| 254 | - public function insert($table, $data){ |
|
| 255 | - if(!$this->connection()) return false; |
|
| 287 | + public function insert($table, $data) { |
|
| 288 | + if(!$this->connection()) { |
|
| 289 | + return false; |
|
| 290 | + } |
|
| 256 | 291 | |
| 257 | - if (false==is_array($data)) $data = (array)$data; |
|
| 292 | + if (false==is_array($data)) { |
|
| 293 | + $data = (array)$data; |
|
| 294 | + } |
|
| 258 | 295 | $k = array_keys($data); |
| 259 | 296 | asort($k); |
| 260 | 297 | $pk = $k; |
@@ -264,11 +301,17 @@ discard block |
||
| 264 | 301 | return $this->last_exec_success ? $this->connection()->lastInsertId() : false; |
| 265 | 302 | } |
| 266 | 303 | |
| 267 | - public function updateWhere($table, $data, $where, $pk='id'){ |
|
| 268 | - if(!$this->connection()) return false; |
|
| 304 | + public function updateWhere($table, $data, $where, $pk='id') { |
|
| 305 | + if(!$this->connection()) { |
|
| 306 | + return false; |
|
| 307 | + } |
|
| 269 | 308 | |
| 270 | - if (false==is_array($data)) $data = (array)$data; |
|
| 271 | - if (empty($data)) return false; |
|
| 309 | + if (false==is_array($data)) { |
|
| 310 | + $data = (array)$data; |
|
| 311 | + } |
|
| 312 | + if (empty($data)) { |
|
| 313 | + return false; |
|
| 314 | + } |
|
| 272 | 315 | $k = array_keys($data); |
| 273 | 316 | asort($k); |
| 274 | 317 | |
@@ -282,16 +325,22 @@ discard block |
||
| 282 | 325 | return $this->last_exec_success; |
| 283 | 326 | } |
| 284 | 327 | |
| 285 | - public function update($table, $data, $pk='id', $extra_where=''){ |
|
| 328 | + public function update($table, $data, $pk='id', $extra_where='') { |
|
| 286 | 329 | return $this->updateWhere($table, $data, "`$pk`=:$pk $extra_where", $pk); |
| 287 | 330 | } |
| 288 | 331 | |
| 289 | - public function insertOrUpdate($table, $data=[], $pk='id', $extra_where=''){ |
|
| 290 | - if(!$this->connection()) return false; |
|
| 332 | + public function insertOrUpdate($table, $data=[], $pk='id', $extra_where='') { |
|
| 333 | + if(!$this->connection()) { |
|
| 334 | + return false; |
|
| 335 | + } |
|
| 291 | 336 | |
| 292 | - if (false==is_array($data)) $data = (array)$data; |
|
| 293 | - if (empty($data[$pk])) return $this->insert($table, $data); |
|
| 294 | - if( (string) $this->value("SELECT `$pk` FROM `$table` WHERE `$pk`=? LIMIT 1", [$data[$pk]]) === (string) $data[$pk] ){ |
|
| 337 | + if (false==is_array($data)) { |
|
| 338 | + $data = (array)$data; |
|
| 339 | + } |
|
| 340 | + if (empty($data[$pk])) { |
|
| 341 | + return $this->insert($table, $data); |
|
| 342 | + } |
|
| 343 | + if( (string) $this->value("SELECT `$pk` FROM `$table` WHERE `$pk`=? LIMIT 1", [$data[$pk]]) === (string) $data[$pk] ) { |
|
| 295 | 344 | return $this->update($table, $data, $pk, $extra_where); |
| 296 | 345 | } else { |
| 297 | 346 | return $this->insert($table, $data); |