blob: 59420912f10c9afed0a4d9755b0ef0d00d0ce1ab [file] [log] [blame]
adminff2d2512006-09-24 18:12:18 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminff2d2512006-09-24 18:12:18 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
adminff2d2512006-09-24 18:12:18 +000016// ------------------------------------------------------------------------
17
18/**
19 * MySQLi Database Adapter Class - MySQLi only works with PHP 5
20 *
21 * Note: _DB is an extender class that the app controller
22 * creates dynamically based on whether the active record
23 * class is being used or not.
24 *
25 * @package CodeIgniter
26 * @subpackage Drivers
27 * @category Database
28 * @author Rick Ellis
29 * @link http://www.codeigniter.com/user_guide/database/
30 */
31class CI_DB_mysqli_driver extends CI_DB {
32
33 /**
34 * Whether to use the MySQL "delete hack" which allows the number
admine334c472006-10-21 19:44:22 +000035 * of affected rows to be shown. Uses a preg_replace when enabled,
adminff2d2512006-09-24 18:12:18 +000036 * adding a bit more processing to all queries.
37 */
admine334c472006-10-21 19:44:22 +000038 var $delete_hack = TRUE;
adminff2d2512006-09-24 18:12:18 +000039
40 // --------------------------------------------------------------------
41
42 /**
43 * Non-persistent database connection
44 *
45 * @access private called by the base class
46 * @return resource
47 */
48 function db_connect()
49 {
50 return mysqli_connect($this->hostname, $this->username, $this->password);
51 }
52
53 // --------------------------------------------------------------------
54
55 /**
56 * Persistent database connection
57 *
58 * @access private called by the base class
59 * @return resource
60 */
61 function db_pconnect()
62 {
63 return $this->db_connect();
64 }
65
66 // --------------------------------------------------------------------
67
68 /**
69 * Select the database
70 *
71 * @access private called by the base class
72 * @return resource
73 */
74 function db_select()
75 {
76 return @mysqli_select_db($this->conn_id, $this->database);
77 }
admin9cd4e8e2006-09-25 23:26:25 +000078
79 // --------------------------------------------------------------------
80
81 /**
82 * Version number query string
83 *
84 * @access public
85 * @return string
86 */
87 function _version()
88 {
89 return "SELECT version() AS ver";
90 }
91
adminff2d2512006-09-24 18:12:18 +000092 // --------------------------------------------------------------------
93
94 /**
95 * Execute the query
96 *
97 * @access private called by the base class
98 * @param string an SQL query
99 * @return resource
100 */
101 function _execute($sql)
102 {
103 $sql = $this->_prep_query($sql);
104 $result = @mysqli_query($this->conn_id, $sql);
105 return $result;
106 }
107
108 // --------------------------------------------------------------------
109
110 /**
111 * Prep the query
112 *
113 * If needed, each database adapter can prep the query string
114 *
115 * @access private called by execute()
116 * @param string an SQL query
117 * @return string
118 */
admine334c472006-10-21 19:44:22 +0000119 function _prep_query($sql)
120 {
121 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
adminff2d2512006-09-24 18:12:18 +0000122 // the query so that it returns the number of affected rows
123 if ($this->delete_hack === TRUE)
124 {
admine334c472006-10-21 19:44:22 +0000125 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
adminff2d2512006-09-24 18:12:18 +0000126 {
127 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
128 }
129 }
130
131 return $sql;
admine334c472006-10-21 19:44:22 +0000132 }
adminff2d2512006-09-24 18:12:18 +0000133
134 // --------------------------------------------------------------------
135
136 /**
137 * Begin Transaction
admine334c472006-10-21 19:44:22 +0000138 *
adminff2d2512006-09-24 18:12:18 +0000139 * @access public
admine334c472006-10-21 19:44:22 +0000140 * @return bool
adminff2d2512006-09-24 18:12:18 +0000141 */
142 function trans_begin($test_mode = FALSE)
143 {
144 if ( ! $this->trans_enabled)
145 {
146 return TRUE;
147 }
148
149 // When transactions are nested we only begin/commit/rollback the outermost ones
150 if ($this->_trans_depth > 0)
151 {
152 return TRUE;
153 }
154
155 // Reset the transaction failure flag.
admine334c472006-10-21 19:44:22 +0000156 // If the $test_mode flag is set to TRUE transactions will be rolled back
157 // even if the queries produce a successful result.
adminff2d2512006-09-24 18:12:18 +0000158 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
159
160 $this->simple_query('SET AUTOCOMMIT=0');
161 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
162 return TRUE;
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Commit Transaction
admine334c472006-10-21 19:44:22 +0000169 *
adminff2d2512006-09-24 18:12:18 +0000170 * @access public
admine334c472006-10-21 19:44:22 +0000171 * @return bool
adminff2d2512006-09-24 18:12:18 +0000172 */
173 function trans_commit()
174 {
175 if ( ! $this->trans_enabled)
176 {
177 return TRUE;
178 }
179
180 // When transactions are nested we only begin/commit/rollback the outermost ones
181 if ($this->_trans_depth > 0)
182 {
183 return TRUE;
184 }
185
186 $this->simple_query('COMMIT');
187 $this->simple_query('SET AUTOCOMMIT=1');
188 return TRUE;
189 }
190
191 // --------------------------------------------------------------------
192
193 /**
194 * Rollback Transaction
admine334c472006-10-21 19:44:22 +0000195 *
adminff2d2512006-09-24 18:12:18 +0000196 * @access public
admine334c472006-10-21 19:44:22 +0000197 * @return bool
adminff2d2512006-09-24 18:12:18 +0000198 */
199 function trans_rollback()
200 {
201 if ( ! $this->trans_enabled)
202 {
203 return TRUE;
204 }
205
206 // When transactions are nested we only begin/commit/rollback the outermost ones
207 if ($this->_trans_depth > 0)
208 {
209 return TRUE;
210 }
211
212 $this->simple_query('ROLLBACK');
213 $this->simple_query('SET AUTOCOMMIT=1');
214 return TRUE;
215 }
216
217 // --------------------------------------------------------------------
218
219 /**
220 * Escape String
221 *
222 * @access public
223 * @param string
224 * @return string
225 */
226 function escape_str($str)
227 {
admin17a890d2006-09-27 20:42:42 +0000228 if (get_magic_quotes_gpc())
229 {
230 return $str;
231 }
232
admin33de9a12006-09-28 06:50:16 +0000233 return mysqli_real_escape_string($this->conn_id, $str);
adminff2d2512006-09-24 18:12:18 +0000234 }
235
236 // --------------------------------------------------------------------
237
238 /**
239 * Affected Rows
240 *
241 * @access public
242 * @return integer
243 */
244 function affected_rows()
245 {
246 return @mysqli_affected_rows($this->conn_id);
247 }
248
249 // --------------------------------------------------------------------
250
251 /**
252 * Insert ID
253 *
254 * @access public
255 * @return integer
256 */
257 function insert_id()
258 {
259 return @mysqli_insert_id($this->conn_id);
260 }
261
262 // --------------------------------------------------------------------
263
264 /**
265 * "Count All" query
266 *
267 * Generates a platform-specific query string that counts all records in
268 * the specified database
269 *
270 * @access public
271 * @param string
272 * @return string
273 */
274 function count_all($table = '')
275 {
276 if ($table == '')
277 return '0';
278
279 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
280
281 if ($query->num_rows() == 0)
282 return '0';
283
284 $row = $query->row();
285 return $row->numrows;
286 }
admin3dd978f2006-09-30 19:24:45 +0000287
288 // --------------------------------------------------------------------
289
290 /**
admin3dd978f2006-09-30 19:24:45 +0000291 * List table query
292 *
293 * Generates a platform-specific query string so that the table names can be fetched
294 *
295 * @access private
296 * @return string
297 */
298 function _list_tables()
299 {
300 return "SHOW TABLES FROM `".$this->database."`";
301 }
302
adminff2d2512006-09-24 18:12:18 +0000303 // --------------------------------------------------------------------
304
305 /**
adminfafe28b2006-10-21 19:08:17 +0000306 * Show column query
admin9cd4e8e2006-09-25 23:26:25 +0000307 *
308 * Generates a platform-specific query string so that the column names can be fetched
309 *
310 * @access public
311 * @param string the table name
312 * @return string
313 */
314 function _list_columns($table = '')
315 {
316 return "SHOW COLUMNS FROM ".$this->_escape_table($table);
317 }
318
319 // --------------------------------------------------------------------
320
321 /**
322 * Field data query
323 *
324 * Generates a platform-specific query so that the column data can be retrieved
325 *
326 * @access public
327 * @param string the table name
328 * @return object
329 */
330 function _field_data($table)
331 {
332 return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
333 }
334
335 // --------------------------------------------------------------------
336
337 /**
adminff2d2512006-09-24 18:12:18 +0000338 * The error message string
339 *
adminbb1d4392006-09-24 20:14:38 +0000340 * @access private
adminff2d2512006-09-24 18:12:18 +0000341 * @return string
342 */
adminbb1d4392006-09-24 20:14:38 +0000343 function _error_message()
adminff2d2512006-09-24 18:12:18 +0000344 {
345 return mysqli_error($this->conn_id);
346 }
347
348 // --------------------------------------------------------------------
349
350 /**
351 * The error message number
352 *
adminbb1d4392006-09-24 20:14:38 +0000353 * @access private
adminff2d2512006-09-24 18:12:18 +0000354 * @return integer
355 */
adminbb1d4392006-09-24 20:14:38 +0000356 function _error_number()
adminff2d2512006-09-24 18:12:18 +0000357 {
358 return mysqli_errno($this->conn_id);
359 }
360
361 // --------------------------------------------------------------------
362
363 /**
364 * Escape Table Name
365 *
366 * This function adds backticks if the table name has a period
367 * in it. Some DBs will get cranky unless periods are escaped
368 *
adminbb1d4392006-09-24 20:14:38 +0000369 * @access private
adminff2d2512006-09-24 18:12:18 +0000370 * @param string the table name
371 * @return string
372 */
adminbb1d4392006-09-24 20:14:38 +0000373 function _escape_table($table)
adminff2d2512006-09-24 18:12:18 +0000374 {
375 if (stristr($table, '.'))
376 {
377 $table = preg_replace("/\./", "`.`", $table);
378 }
379
380 return $table;
381 }
adminbb1d4392006-09-24 20:14:38 +0000382
adminff2d2512006-09-24 18:12:18 +0000383 // --------------------------------------------------------------------
384
385 /**
386 * Insert statement
387 *
388 * Generates a platform-specific insert string from the supplied data
389 *
390 * @access public
391 * @param string the table name
392 * @param array the insert keys
393 * @param array the insert values
394 * @return string
395 */
396 function _insert($table, $keys, $values)
397 {
adminbb1d4392006-09-24 20:14:38 +0000398 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
adminff2d2512006-09-24 18:12:18 +0000399 }
400
401 // --------------------------------------------------------------------
402
403 /**
404 * Update statement
405 *
406 * Generates a platform-specific update string from the supplied data
407 *
408 * @access public
409 * @param string the table name
410 * @param array the update data
411 * @param array the where clause
412 * @return string
413 */
414 function _update($table, $values, $where)
415 {
416 foreach($values as $key => $val)
417 {
418 $valstr[] = $key." = ".$val;
419 }
420
adminbb1d4392006-09-24 20:14:38 +0000421 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
adminff2d2512006-09-24 18:12:18 +0000422 }
423
424 // --------------------------------------------------------------------
425
426 /**
427 * Delete statement
428 *
429 * Generates a platform-specific delete string from the supplied data
430 *
431 * @access public
432 * @param string the table name
433 * @param array the where clause
434 * @return string
435 */
436 function _delete($table, $where)
437 {
adminbb1d4392006-09-24 20:14:38 +0000438 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
adminff2d2512006-09-24 18:12:18 +0000439 }
adminbb1d4392006-09-24 20:14:38 +0000440
441 // --------------------------------------------------------------------
442
443 /**
444 * Limit string
445 *
446 * Generates a platform-specific LIMIT clause
447 *
448 * @access public
449 * @param string the sql query string
450 * @param integer the number of rows to limit the query to
451 * @param integer the offset value
452 * @return string
453 */
454 function _limit($sql, $limit, $offset)
455 {
456 $sql .= "LIMIT ".$limit;
adminff2d2512006-09-24 18:12:18 +0000457
adminbb1d4392006-09-24 20:14:38 +0000458 if ($offset > 0)
459 {
460 $sql .= " OFFSET ".$offset;
461 }
462
463 return $sql;
464 }
465
466 // --------------------------------------------------------------------
467
468 /**
469 * Close DB Connection
470 *
471 * @access public
472 * @param resource
473 * @return void
474 */
475 function _close($conn_id)
476 {
477 mysqli_close($conn_id);
478 }
479
adminff2d2512006-09-24 18:12:18 +0000480
481}
482
483?>