1 <?php
defined('IN_CMS') or die('No direct access allowed.');
5 private static $driver = 'mysql';
6 private static $hostname, $username, $password, $database;
7 private static $driver_options = array();
9 private static $dbh = null;
10 private static $debug = false;
11 private static $affected_rows = 0;
12 private static $profile = array();
14 public static function connect() {
16 $params = Config
::get('database');
19 static::$debug = Config
::get('debug', false);
22 $dsn = 'mysql:dbname=' . $params['name'] . ';host=' . $params['host'];
25 static::$dbh = new PDO($dsn, $params['username'], $params['password']);
27 // set error handling to exceptions
28 static::$dbh->setAttribute(PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_EXCEPTION
);
33 public static function close() {
37 private static function profiling($statement, $binds, $start) {
38 static::$profile[] = array(
39 'sql' => $statement->queryString
,
41 'time' => round(microtime(true) - $start, 4),
42 'rows' => $statement->rowCount()
46 public static function profile() {
47 return static::$profile;
53 public static function query($sql, $binds = array()) {
54 // make sure we have a connection
55 if(is_null(static::$dbh)) {
60 if(!is_array($binds)) {
61 $binds = array($binds);
64 // profile in debug mode
66 $start = microtime(true);
70 $sth = static::$dbh->prepare($sql);
73 $reflector = new ReflectionMethod('PDOStatement', 'bindValue');
75 foreach($binds as $index => $value) {
76 $key = is_int($index) ?
$index +
1 : $index;
77 $type = is_bool($value) ? PDO
::PARAM_BOOL
: (is_int($value) ? PDO
::PARAM_INT
: PDO
::PARAM_STR
);
78 $reflector->invokeArgs($sth, array($key, $value, $type));
84 // profile in debug mode
86 static::profiling($sth, $binds, $start);
89 // update affected rows
90 static::$affected_rows = $sth->rowCount();
97 * Simple query, returns TRUE or FALSE
99 public static function exec($sql, $binds = array()) {
100 // make sure we have a connection
101 if(is_null(static::$dbh)) {
106 if(!is_array($binds)) {
107 $binds = array($binds);
110 // profile in debug mode
112 $start = microtime(true);
116 $sth = static::$dbh->prepare($sql);
119 $reflector = new ReflectionMethod('PDOStatement', 'bindValue');
121 foreach($binds as $index => $value) {
122 $key = is_int($index) ?
$index +
1 : $index;
123 $type = is_bool($value) ? PDO
::PARAM_BOOL
: (is_int($value) ? PDO
::PARAM_INT
: PDO
::PARAM_STR
);
124 $reflector->invokeArgs($sth, array($key, $value, $type));
128 $result = $sth->execute();
130 // profile in debug mode
132 static::profiling($sth, $binds, $start);
135 // update affected rows
136 static::$affected_rows = $sth->rowCount();
145 public static function update($table, $columns = array(), $condition = array()) {
149 foreach($columns as $key => $value) {
150 $updates[] = '`' . $key . '` = ?';
154 $sql = "update `" . $table . "` set " . implode(', ', $updates);
156 if(count($condition)) {
159 foreach($condition as $key => $value) {
160 $where[] = '`' . $key . '` = ?';
164 $sql .= " where " . implode(' and ', $where);
167 return Db
::exec($sql, $args);
170 public static function insert($table, $row = array()) {
175 foreach($row as $key => $value) {
176 $keys[] = '`' . $key . '`';
181 $sql = "insert into `" . $table . "` (" . implode(', ', $keys) . ") values (" . implode(', ', $values) . ")";
183 return Db
::exec($sql, $args);
186 public static function delete($table, $condition = array()) {
187 $sql = "delete from `" . $table . "`";
189 if(count($condition)) {
192 foreach($condition as $key => $value) {
193 $where[] = '`' . $key . '` = ?';
197 $sql .= " where " . implode(' and ', $where);
200 return Db
::exec($sql, $args);
203 public static function row($sql, $binds = array(), $fetch_style = \PDO
::FETCH_OBJ
) {
205 $stm = static::query($sql, $binds);
208 return $stm->fetch($fetch_style);
211 public static function results($sql, $binds = array(), $fetch_style = \PDO
::FETCH_OBJ
) {
213 $stm = static::query($sql, $binds);
216 return $stm->fetchAll($fetch_style);
219 public static function insert_id() {
220 return static::$dbh->lastInsertId();
223 public static function affected_rows() {
224 return static::$affected_rows;