blob: 288f552dd49147efe659d6d6fcd177fddb1f3861 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +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.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
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/libraries/database/
30 */
31class CI_DB_mysqli extends CI_DB {
32
33 /**
34 * Whether to use the MySQL "delete hack" which allows the number
35 * of affected rows to be shown. Uses a preg_replace when enabled,
36 * adding a bit more processing to all queries.
37 */
38 var $delete_hack = TRUE;
39
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 }
78
79 // --------------------------------------------------------------------
80
81 /**
82 * Execute the query
83 *
84 * @access private called by the base class
85 * @param string an SQL query
86 * @return resource
87 */
admine885d782006-09-23 20:25:05 +000088 function _execute($sql)
adminb0dd10f2006-08-25 17:25:49 +000089 {
90 $sql = $this->_prep_query($sql);
admin1082bdd2006-08-27 19:32:02 +000091 $result = @mysqli_query($this->conn_id, $sql);
admin1082bdd2006-08-27 19:32:02 +000092 return $result;
adminb0dd10f2006-08-25 17:25:49 +000093 }
94
95 // --------------------------------------------------------------------
96
97 /**
98 * Prep the query
99 *
100 * If needed, each database adapter can prep the query string
101 *
102 * @access private called by execute()
103 * @param string an SQL query
104 * @return string
105 */
adminb071bb52006-08-26 19:28:37 +0000106 function _prep_query($sql)
adminb0dd10f2006-08-25 17:25:49 +0000107 {
108 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
109 // the query so that it returns the number of affected rows
110 if ($this->delete_hack === TRUE)
111 {
112 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
113 {
114 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
115 }
116 }
117
118 return $sql;
119 }
admine885d782006-09-23 20:25:05 +0000120
121
122 // --------------------------------------------------------------------
123
124 /**
125 * Begin Transaction
126 *
127 * @access public
128 * @return bool
129 */
admin8b180be2006-09-24 01:12:22 +0000130 function trans_begin($test_mode = FALSE)
admine885d782006-09-23 20:25:05 +0000131 {
132 if ( ! $this->trans_enabled)
133 {
134 return TRUE;
135 }
136
137 // When transactions are nested we only begin/commit/rollback the outermost ones
138 if ($this->_trans_depth > 0)
139 {
140 return TRUE;
141 }
142
admin8b180be2006-09-24 01:12:22 +0000143 // Reset the transaction failure flag.
144 // If the $test_mode flag is set to TRUE transactions will be rolled back
145 // even if the queries produce a successful result.
146 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
147
admine885d782006-09-23 20:25:05 +0000148 $this->simple_query('SET AUTOCOMMIT=0');
149 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
150 return TRUE;
151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Commit Transaction
157 *
158 * @access public
159 * @return bool
160 */
161 function trans_commit()
162 {
163 if ( ! $this->trans_enabled)
164 {
165 return TRUE;
166 }
167
168 // When transactions are nested we only begin/commit/rollback the outermost ones
169 if ($this->_trans_depth > 0)
170 {
171 return TRUE;
172 }
173
174 $this->simple_query('COMMIT');
175 $this->simple_query('SET AUTOCOMMIT=1');
176 return TRUE;
177 }
178
179 // --------------------------------------------------------------------
180
181 /**
182 * Rollback Transaction
183 *
184 * @access public
185 * @return bool
186 */
187 function trans_rollback()
188 {
189 if ( ! $this->trans_enabled)
190 {
191 return TRUE;
192 }
193
194 // When transactions are nested we only begin/commit/rollback the outermost ones
195 if ($this->_trans_depth > 0)
196 {
197 return TRUE;
198 }
199
200 $this->simple_query('ROLLBACK');
201 $this->simple_query('SET AUTOCOMMIT=1');
202 return TRUE;
203 }
204
adminb0dd10f2006-08-25 17:25:49 +0000205 // --------------------------------------------------------------------
206
207 /**
208 * Escape String
209 *
210 * @access public
211 * @param string
212 * @return string
213 */
214 function escape_str($str)
215 {
adminb0dd10f2006-08-25 17:25:49 +0000216 return mysqli_real_escape_string($this->conn_id, $str);
217 }
218
219 // --------------------------------------------------------------------
220
221 /**
222 * Close DB Connection
223 *
224 * @access public
225 * @param resource
226 * @return void
227 */
228 function destroy($conn_id)
229 {
230 mysqli_close($conn_id);
231 }
232
233 // --------------------------------------------------------------------
234
235 /**
236 * Affected Rows
237 *
238 * @access public
239 * @return integer
240 */
241 function affected_rows()
242 {
243 return @mysqli_affected_rows($this->conn_id);
244 }
245
246 // --------------------------------------------------------------------
247
248 /**
249 * Insert ID
250 *
251 * @access public
252 * @return integer
253 */
254 function insert_id()
255 {
256 return @mysqli_insert_id($this->conn_id);
257 }
258
259 // --------------------------------------------------------------------
260
261 /**
262 * "Count All" query
263 *
264 * Generates a platform-specific query string that counts all records in
265 * the specified database
266 *
267 * @access public
268 * @param string
269 * @return string
270 */
271 function count_all($table = '')
272 {
273 if ($table == '')
274 return '0';
275
276 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
277
278 if ($query->num_rows() == 0)
279 return '0';
280
281 $row = $query->row();
282 return $row->numrows;
283 }
284
285 // --------------------------------------------------------------------
286
287 /**
288 * The error message string
289 *
290 * @access public
291 * @return string
292 */
293 function error_message()
294 {
295 return mysqli_error($this->conn_id);
296 }
297
298 // --------------------------------------------------------------------
299
300 /**
301 * The error message number
302 *
303 * @access public
304 * @return integer
305 */
306 function error_number()
307 {
308 return mysqli_errno($this->conn_id);
309 }
310
311 // --------------------------------------------------------------------
312
313 /**
314 * Escape Table Name
315 *
316 * This function adds backticks if the table name has a period
317 * in it. Some DBs will get cranky unless periods are escaped
318 *
319 * @access public
320 * @param string the table name
321 * @return string
322 */
323 function escape_table($table)
324 {
325 if (stristr($table, '.'))
326 {
327 $table = preg_replace("/\./", "`.`", $table);
328 }
329
330 return $table;
331 }
332
333 // --------------------------------------------------------------------
334
335 /**
336 * Field data query
337 *
338 * Generates a platform-specific query so that the column data can be retrieved
339 *
340 * @access public
341 * @param string the table name
342 * @return object
343 */
344 function _field_data($table)
345 {
346 $sql = "SELECT * FROM ".$this->escape_table($table)." LIMIT 1";
347 $query = $this->query($sql);
348 return $query->field_data();
349 }
350
351 // --------------------------------------------------------------------
352
353 /**
354 * Insert statement
355 *
356 * Generates a platform-specific insert string from the supplied data
357 *
358 * @access public
359 * @param string the table name
360 * @param array the insert keys
361 * @param array the insert values
362 * @return string
363 */
364 function _insert($table, $keys, $values)
365 {
366 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
367 }
368
369 // --------------------------------------------------------------------
370
371 /**
372 * Update statement
373 *
374 * Generates a platform-specific update string from the supplied data
375 *
376 * @access public
377 * @param string the table name
378 * @param array the update data
379 * @param array the where clause
380 * @return string
381 */
382 function _update($table, $values, $where)
383 {
384 foreach($values as $key => $val)
385 {
386 $valstr[] = $key." = ".$val;
387 }
388
389 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
390 }
391
392 // --------------------------------------------------------------------
393
394 /**
395 * Delete statement
396 *
397 * Generates a platform-specific delete string from the supplied data
398 *
399 * @access public
400 * @param string the table name
401 * @param array the where clause
402 * @return string
403 */
404 function _delete($table, $where)
405 {
406 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
407 }
408
409 // --------------------------------------------------------------------
410
411 /**
412 * Version number query string
413 *
414 * @access public
415 * @return string
416 */
417 function _version()
418 {
419 return "SELECT version() AS ver";
420 }
421
422 // --------------------------------------------------------------------
423
424 /**
425 * Show table query
426 *
427 * Generates a platform-specific query string so that the table names can be fetched
428 *
429 * @access public
430 * @return string
431 */
432 function _show_tables()
433 {
434 return "SHOW TABLES FROM `".$this->database."`";
435 }
436
437 // --------------------------------------------------------------------
438
439 /**
440 * Show columnn query
441 *
442 * Generates a platform-specific query string so that the column names can be fetched
443 *
444 * @access public
445 * @param string the table name
446 * @return string
447 */
448 function _show_columns($table = '')
449 {
450 return "SHOW COLUMNS FROM ".$this->escape_table($table);
451 }
452
453 // --------------------------------------------------------------------
454
455 /**
456 * Limit string
457 *
458 * Generates a platform-specific LIMIT clause
459 *
460 * @access public
461 * @param string the sql query string
462 * @param integer the number of rows to limit the query to
463 * @param integer the offset value
464 * @return string
465 */
466 function _limit($sql, $limit, $offset)
467 {
468 $sql .= "LIMIT ".$limit;
469
470 if ($offset > 0)
471 {
472 $sql .= " OFFSET ".$offset;
473 }
474
475 return $sql;
476 }
477
478}
479
480
481
482/**
483 * MySQLi Result Class
484 *
485 * This class extends the parent result class: CI_DB_result
486 *
487 * @category Database
488 * @author Rick Ellis
489 * @link http://www.codeigniter.com/user_guide/libraries/database/
490 */
491class CI_DB_mysqli_result extends CI_DB_result {
492
493 /**
494 * Number of rows in the result set
495 *
496 * @access public
497 * @return integer
498 */
499 function num_rows()
500 {
501 return @mysqli_num_rows($this->result_id);
502 }
503
504 // --------------------------------------------------------------------
505
506 /**
507 * Number of fields in the result set
508 *
509 * @access public
510 * @return integer
511 */
512 function num_fields()
513 {
514 return @mysqli_num_fields($this->result_id);
515 }
516
517 // --------------------------------------------------------------------
518
519 /**
520 * Field data
521 *
522 * Generates an array of objects containing field meta-data
523 *
524 * @access public
525 * @return array
526 */
527 function field_data()
528 {
529 $retval = array();
530 while ($field = mysqli_fetch_field($this->result_id))
531 {
admine348efb2006-09-20 21:13:26 +0000532 $F = new stdClass();
adminb0dd10f2006-08-25 17:25:49 +0000533 $F->name = $field->name;
534 $F->type = $field->type;
535 $F->default = $field->def;
536 $F->max_length = $field->max_length;
adminb68745e2006-09-17 18:13:34 +0000537 $F->primary_key = ($field->flags & MYSQLI_PRI_KEY_FLAG) ? 1 : 0;
adminb0dd10f2006-08-25 17:25:49 +0000538
539 $retval[] = $F;
540 }
541
542 return $retval;
543 }
544
545 // --------------------------------------------------------------------
546
547 /**
548 * Result - associative array
549 *
550 * Returns the result set as an array
551 *
552 * @access private
553 * @return array
554 */
555 function _fetch_assoc()
556 {
557 return mysqli_fetch_assoc($this->result_id);
558 }
559
560 // --------------------------------------------------------------------
561
562 /**
563 * Result - object
564 *
565 * Returns the result set as an object
566 *
567 * @access private
568 * @return object
569 */
570 function _fetch_object()
571 {
572 return mysqli_fetch_object($this->result_id);
573 }
574
575}
576
577?>