--- /dev/null
+<?php defined('IN_CMS') or die('No direct access allowed.');\r
+\r
+class Db {\r
+\r
+ private static $driver = 'mysql';\r
+ private static $hostname, $username, $password, $database;\r
+ private static $driver_options = array();\r
+\r
+ private static $dbh = null;\r
+ private static $debug = false;\r
+ private static $affected_rows = 0;\r
+ private static $profile = array();\r
+\r
+ public static function connect() {\r
+ // config\r
+ $params = Config::get('database');\r
+\r
+ // set debug mode\r
+ static::$debug = Config::get('debug', false);\r
+ \r
+ // build dns string\r
+ $dsn = 'mysql:dbname=' . $params['name'] . ';host=' . $params['host'];\r
+\r
+ // try connection\r
+ static::$dbh = new PDO($dsn, $params['username'], $params['password']);\r
+ \r
+ // set error handling to exceptions\r
+ static::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\r
+\r
+ return true;\r
+ }\r
+\r
+ public static function close() {\r
+ static::$dbh = null;\r
+ }\r
+\r
+ private static function profiling($statement, $binds, $start) {\r
+ static::$profile[] = array(\r
+ 'sql' => $statement->queryString,\r
+ 'binds' => $binds,\r
+ 'time' => round(microtime(true) - $start, 4),\r
+ 'rows' => $statement->rowCount()\r
+ );\r
+ }\r
+\r
+ public static function profile() {\r
+ return static::$profile;\r
+ }\r
+\r
+ /*\r
+ * Querying\r
+ */\r
+ public static function query($sql, $binds = array()) {\r
+ // make sure we have a connection\r
+ if(is_null(static::$dbh)) {\r
+ static::connect();\r
+ }\r
+ \r
+ // check binds\r
+ if(!is_array($binds)) {\r
+ $binds = array($binds);\r
+ }\r
+\r
+ // profile in debug mode\r
+ if(static::$debug) {\r
+ $start = microtime(true);\r
+ }\r
+\r
+ // prepare\r
+ $sth = static::$dbh->prepare($sql);\r
+\r
+ // bind params\r
+ $reflector = new ReflectionMethod('PDOStatement', 'bindValue');\r
+\r
+ foreach($binds as $index => $value) {\r
+ $key = is_int($index) ? $index + 1 : $index;\r
+ $type = is_bool($value) ? PDO::PARAM_BOOL : (is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR);\r
+ $reflector->invokeArgs($sth, array($key, $value, $type));\r
+ }\r
+\r
+ // get results\r
+ $sth->execute();\r
+\r
+ // profile in debug mode\r
+ if(static::$debug) {\r
+ static::profiling($sth, $binds, $start);\r
+ }\r
+\r
+ // update affected rows\r
+ static::$affected_rows = $sth->rowCount();\r
+\r
+ // return statement\r
+ return $sth;\r
+ }\r
+\r
+ /*\r
+ * Simple query, returns TRUE or FALSE\r
+ */\r
+ public static function exec($sql, $binds = array()) {\r
+ // make sure we have a connection\r
+ if(is_null(static::$dbh)) {\r
+ static::connect();\r
+ }\r
+\r
+ // check binds\r
+ if(!is_array($binds)) {\r
+ $binds = array($binds);\r
+ }\r
+\r
+ // profile in debug mode\r
+ if(static::$debug) {\r
+ $start = microtime(true);\r
+ }\r
+\r
+ // prepare\r
+ $sth = static::$dbh->prepare($sql);\r
+\r
+ // bind params\r
+ $reflector = new ReflectionMethod('PDOStatement', 'bindValue');\r
+\r
+ foreach($binds as $index => $value) {\r
+ $key = is_int($index) ? $index + 1 : $index;\r
+ $type = is_bool($value) ? PDO::PARAM_BOOL : (is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR);\r
+ $reflector->invokeArgs($sth, array($key, $value, $type));\r
+ }\r
+\r
+ // get results\r
+ $result = $sth->execute();\r
+\r
+ // profile in debug mode\r
+ if(static::$debug) {\r
+ static::profiling($sth, $binds, $start);\r
+ }\r
+\r
+ // update affected rows\r
+ static::$affected_rows = $sth->rowCount();\r
+\r
+ // return result\r
+ return $result;\r
+ }\r
+ \r
+ /*\r
+ * Shortcuts\r
+ */\r
+ public static function update($table, $columns = array(), $condition = array()) {\r
+ $updates = array();\r
+ $args = array();\r
+\r
+ foreach($columns as $key => $value) {\r
+ $updates[] = '`' . $key . '` = ?';\r
+ $args[] = $value;\r
+ }\r
+ \r
+ $sql = "update `" . $table . "` set " . implode(', ', $updates);\r
+ \r
+ if(count($condition)) {\r
+ $where = array();\r
+\r
+ foreach($condition as $key => $value) {\r
+ $where[] = '`' . $key . '` = ?';\r
+ $args[] = $value;\r
+ }\r
+\r
+ $sql .= " where " . implode(' and ', $where);\r
+ } \r
+ \r
+ return Db::exec($sql, $args);\r
+ }\r
+\r
+ public static function insert($table, $row = array()) {\r
+ $keys = array();\r
+ $values = array();\r
+ $args = array();\r
+ \r
+ foreach($row as $key => $value) {\r
+ $keys[] = '`' . $key . '`';\r
+ $values[] = '?';\r
+ $args[] = $value;\r
+ }\r
+ \r
+ $sql = "insert into `" . $table . "` (" . implode(', ', $keys) . ") values (" . implode(', ', $values) . ")"; \r
+\r
+ return Db::exec($sql, $args);\r
+ }\r
+\r
+ public static function delete($table, $condition = array()) {\r
+ $sql = "delete from `" . $table . "`";\r
+ \r
+ if(count($condition)) {\r
+ $where = array();\r
+\r
+ foreach($condition as $key => $value) {\r
+ $where[] = '`' . $key . '` = ?';\r
+ $args[] = $value;\r
+ }\r
+\r
+ $sql .= " where " . implode(' and ', $where);\r
+ } \r
+ \r
+ return Db::exec($sql, $args);\r
+ }\r
+\r
+ public static function row($sql, $binds = array(), $fetch_style = \PDO::FETCH_OBJ) {\r
+ // get statement\r
+ $stm = static::query($sql, $binds);\r
+\r
+ // return data\r
+ return $stm->fetch($fetch_style);\r
+ }\r
+\r
+ public static function results($sql, $binds = array(), $fetch_style = \PDO::FETCH_OBJ) {\r
+ // get statement\r
+ $stm = static::query($sql, $binds);\r
+\r
+ // return data array\r
+ return $stm->fetchAll($fetch_style);\r
+ }\r
+\r
+ public static function insert_id() {\r
+ return static::$dbh->lastInsertId();\r
+ }\r
+\r
+ public static function affected_rows() {\r
+ return static::$affected_rows;\r
+ }\r
+\r
+}\r