Daan is sweet to Kelley and delegates himself TestCMS code reflection
[ssproject1617.git] / testcms-final-anon / system / classes / db.php
1 <?php defined('IN_CMS') or die('No direct access allowed.');
2
3 class Db {
4
5 private static $driver = 'mysql';
6 private static $hostname, $username, $password, $database;
7 private static $driver_options = array();
8
9 private static $dbh = null;
10 private static $debug = false;
11 private static $affected_rows = 0;
12 private static $profile = array();
13
14 public static function connect() {
15 // config
16 $params = Config::get('database');
17
18 // set debug mode
19 static::$debug = Config::get('debug', false);
20
21 // build dns string
22 $dsn = 'mysql:dbname=' . $params['name'] . ';host=' . $params['host'];
23
24 // try connection
25 static::$dbh = new PDO($dsn, $params['username'], $params['password']);
26
27 // set error handling to exceptions
28 static::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
29
30 return true;
31 }
32
33 public static function close() {
34 static::$dbh = null;
35 }
36
37 private static function profiling($statement, $binds, $start) {
38 static::$profile[] = array(
39 'sql' => $statement->queryString,
40 'binds' => $binds,
41 'time' => round(microtime(true) - $start, 4),
42 'rows' => $statement->rowCount()
43 );
44 }
45
46 public static function profile() {
47 return static::$profile;
48 }
49
50 /*
51 * Querying
52 */
53 public static function query($sql, $binds = array()) {
54 // make sure we have a connection
55 if(is_null(static::$dbh)) {
56 static::connect();
57 }
58
59 // check binds
60 if(!is_array($binds)) {
61 $binds = array($binds);
62 }
63
64 // profile in debug mode
65 if(static::$debug) {
66 $start = microtime(true);
67 }
68
69 // prepare
70 $sth = static::$dbh->prepare($sql);
71
72 // bind params
73 $reflector = new ReflectionMethod('PDOStatement', 'bindValue');
74
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));
79 }
80
81 // get results
82 $sth->execute();
83
84 // profile in debug mode
85 if(static::$debug) {
86 static::profiling($sth, $binds, $start);
87 }
88
89 // update affected rows
90 static::$affected_rows = $sth->rowCount();
91
92 // return statement
93 return $sth;
94 }
95
96 /*
97 * Simple query, returns TRUE or FALSE
98 */
99 public static function exec($sql, $binds = array()) {
100 // make sure we have a connection
101 if(is_null(static::$dbh)) {
102 static::connect();
103 }
104
105 // check binds
106 if(!is_array($binds)) {
107 $binds = array($binds);
108 }
109
110 // profile in debug mode
111 if(static::$debug) {
112 $start = microtime(true);
113 }
114
115 // prepare
116 $sth = static::$dbh->prepare($sql);
117
118 // bind params
119 $reflector = new ReflectionMethod('PDOStatement', 'bindValue');
120
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));
125 }
126
127 // get results
128 $result = $sth->execute();
129
130 // profile in debug mode
131 if(static::$debug) {
132 static::profiling($sth, $binds, $start);
133 }
134
135 // update affected rows
136 static::$affected_rows = $sth->rowCount();
137
138 // return result
139 return $result;
140 }
141
142 /*
143 * Shortcuts
144 */
145 public static function update($table, $columns = array(), $condition = array()) {
146 $updates = array();
147 $args = array();
148
149 foreach($columns as $key => $value) {
150 $updates[] = '`' . $key . '` = ?';
151 $args[] = $value;
152 }
153
154 $sql = "update `" . $table . "` set " . implode(', ', $updates);
155
156 if(count($condition)) {
157 $where = array();
158
159 foreach($condition as $key => $value) {
160 $where[] = '`' . $key . '` = ?';
161 $args[] = $value;
162 }
163
164 $sql .= " where " . implode(' and ', $where);
165 }
166
167 return Db::exec($sql, $args);
168 }
169
170 public static function insert($table, $row = array()) {
171 $keys = array();
172 $values = array();
173 $args = array();
174
175 foreach($row as $key => $value) {
176 $keys[] = '`' . $key . '`';
177 $values[] = '?';
178 $args[] = $value;
179 }
180
181 $sql = "insert into `" . $table . "` (" . implode(', ', $keys) . ") values (" . implode(', ', $values) . ")";
182
183 return Db::exec($sql, $args);
184 }
185
186 public static function delete($table, $condition = array()) {
187 $sql = "delete from `" . $table . "`";
188
189 if(count($condition)) {
190 $where = array();
191
192 foreach($condition as $key => $value) {
193 $where[] = '`' . $key . '` = ?';
194 $args[] = $value;
195 }
196
197 $sql .= " where " . implode(' and ', $where);
198 }
199
200 return Db::exec($sql, $args);
201 }
202
203 public static function row($sql, $binds = array(), $fetch_style = \PDO::FETCH_OBJ) {
204 // get statement
205 $stm = static::query($sql, $binds);
206
207 // return data
208 return $stm->fetch($fetch_style);
209 }
210
211 public static function results($sql, $binds = array(), $fetch_style = \PDO::FETCH_OBJ) {
212 // get statement
213 $stm = static::query($sql, $binds);
214
215 // return data array
216 return $stm->fetchAll($fetch_style);
217 }
218
219 public static function insert_id() {
220 return static::$dbh->lastInsertId();
221 }
222
223 public static function affected_rows() {
224 return static::$affected_rows;
225 }
226
227 }