blob: 8d28c2c5f6a8e0cf5b619498c17b15105666b815 [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.
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/database/
30 */
31class CI_DB_mysqli_driver 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 */
88 function _execute($sql)
89 {
90 $sql = $this->_prep_query($sql);
91 $result = @mysqli_query($this->conn_id, $sql);
92 return $result;
93 }
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 */
106 function _prep_query($sql)
107 {
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 }
120
121 // --------------------------------------------------------------------
122
123 /**
124 * Begin Transaction
125 *
126 * @access public
127 * @return bool
128 */
129 function trans_begin($test_mode = FALSE)
130 {
131 if ( ! $this->trans_enabled)
132 {
133 return TRUE;
134 }
135
136 // When transactions are nested we only begin/commit/rollback the outermost ones
137 if ($this->_trans_depth > 0)
138 {
139 return TRUE;
140 }
141
142 // Reset the transaction failure flag.
143 // If the $test_mode flag is set to TRUE transactions will be rolled back
144 // even if the queries produce a successful result.
145 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
146
147 $this->simple_query('SET AUTOCOMMIT=0');
148 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
149 return TRUE;
150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * Commit Transaction
156 *
157 * @access public
158 * @return bool
159 */
160 function trans_commit()
161 {
162 if ( ! $this->trans_enabled)
163 {
164 return TRUE;
165 }
166
167 // When transactions are nested we only begin/commit/rollback the outermost ones
168 if ($this->_trans_depth > 0)
169 {
170 return TRUE;
171 }
172
173 $this->simple_query('COMMIT');
174 $this->simple_query('SET AUTOCOMMIT=1');
175 return TRUE;
176 }
177
178 // --------------------------------------------------------------------
179
180 /**
181 * Rollback Transaction
182 *
183 * @access public
184 * @return bool
185 */
186 function trans_rollback()
187 {
188 if ( ! $this->trans_enabled)
189 {
190 return TRUE;
191 }
192
193 // When transactions are nested we only begin/commit/rollback the outermost ones
194 if ($this->_trans_depth > 0)
195 {
196 return TRUE;
197 }
198
199 $this->simple_query('ROLLBACK');
200 $this->simple_query('SET AUTOCOMMIT=1');
201 return TRUE;
202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Escape String
208 *
209 * @access public
210 * @param string
211 * @return string
212 */
213 function escape_str($str)
214 {
215 return mysqli_real_escape_string($this->conn_id, $str);
216 }
217
218 // --------------------------------------------------------------------
219
220 /**
221 * Affected Rows
222 *
223 * @access public
224 * @return integer
225 */
226 function affected_rows()
227 {
228 return @mysqli_affected_rows($this->conn_id);
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Insert ID
235 *
236 * @access public
237 * @return integer
238 */
239 function insert_id()
240 {
241 return @mysqli_insert_id($this->conn_id);
242 }
243
244 // --------------------------------------------------------------------
245
246 /**
247 * "Count All" query
248 *
249 * Generates a platform-specific query string that counts all records in
250 * the specified database
251 *
252 * @access public
253 * @param string
254 * @return string
255 */
256 function count_all($table = '')
257 {
258 if ($table == '')
259 return '0';
260
261 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
262
263 if ($query->num_rows() == 0)
264 return '0';
265
266 $row = $query->row();
267 return $row->numrows;
268 }
269
270 // --------------------------------------------------------------------
271
272 /**
273 * The error message string
274 *
275 * @access public
276 * @return string
277 */
278 function error_message()
279 {
280 return mysqli_error($this->conn_id);
281 }
282
283 // --------------------------------------------------------------------
284
285 /**
286 * The error message number
287 *
288 * @access public
289 * @return integer
290 */
291 function error_number()
292 {
293 return mysqli_errno($this->conn_id);
294 }
295
296 // --------------------------------------------------------------------
297
298 /**
299 * Escape Table Name
300 *
301 * This function adds backticks if the table name has a period
302 * in it. Some DBs will get cranky unless periods are escaped
303 *
304 * @access public
305 * @param string the table name
306 * @return string
307 */
308 function escape_table($table)
309 {
310 if (stristr($table, '.'))
311 {
312 $table = preg_replace("/\./", "`.`", $table);
313 }
314
315 return $table;
316 }
317
318 // --------------------------------------------------------------------
319
320 /**
321 * Field data query
322 *
323 * Generates a platform-specific query so that the column data can be retrieved
324 *
325 * @access public
326 * @param string the table name
327 * @return object
328 */
329 function _field_data($table)
330 {
331 $sql = "SELECT * FROM ".$this->escape_table($table)." LIMIT 1";
332 $query = $this->query($sql);
333 return $query->field_data();
334 }
335
336 // --------------------------------------------------------------------
337
338 /**
339 * Insert statement
340 *
341 * Generates a platform-specific insert string from the supplied data
342 *
343 * @access public
344 * @param string the table name
345 * @param array the insert keys
346 * @param array the insert values
347 * @return string
348 */
349 function _insert($table, $keys, $values)
350 {
351 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
352 }
353
354 // --------------------------------------------------------------------
355
356 /**
357 * Update statement
358 *
359 * Generates a platform-specific update string from the supplied data
360 *
361 * @access public
362 * @param string the table name
363 * @param array the update data
364 * @param array the where clause
365 * @return string
366 */
367 function _update($table, $values, $where)
368 {
369 foreach($values as $key => $val)
370 {
371 $valstr[] = $key." = ".$val;
372 }
373
374 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
375 }
376
377 // --------------------------------------------------------------------
378
379 /**
380 * Delete statement
381 *
382 * Generates a platform-specific delete string from the supplied data
383 *
384 * @access public
385 * @param string the table name
386 * @param array the where clause
387 * @return string
388 */
389 function _delete($table, $where)
390 {
391 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
392 }
393
394 // --------------------------------------------------------------------
395
396 /**
397 * Version number query string
398 *
399 * @access public
400 * @return string
401 */
402 function _version()
403 {
404 return "SELECT version() AS ver";
405 }
406
407 // --------------------------------------------------------------------
408
409 /**
410 * Show table query
411 *
412 * Generates a platform-specific query string so that the table names can be fetched
413 *
414 * @access public
415 * @return string
416 */
417 function _show_tables()
418 {
419 return "SHOW TABLES FROM `".$this->database."`";
420 }
421
422 // --------------------------------------------------------------------
423
424 /**
425 * Show columnn query
426 *
427 * Generates a platform-specific query string so that the column names can be fetched
428 *
429 * @access public
430 * @param string the table name
431 * @return string
432 */
433 function _show_columns($table = '')
434 {
435 return "SHOW COLUMNS FROM ".$this->escape_table($table);
436 }
437
438 // --------------------------------------------------------------------
439
440 /**
441 * Limit string
442 *
443 * Generates a platform-specific LIMIT clause
444 *
445 * @access public
446 * @param string the sql query string
447 * @param integer the number of rows to limit the query to
448 * @param integer the offset value
449 * @return string
450 */
451 function _limit($sql, $limit, $offset)
452 {
453 $sql .= "LIMIT ".$limit;
454
455 if ($offset > 0)
456 {
457 $sql .= " OFFSET ".$offset;
458 }
459
460 return $sql;
461 }
462
463 // --------------------------------------------------------------------
464
465 /**
466 * Close DB Connection
467 *
468 * @access public
469 * @param resource
470 * @return void
471 */
472 function _close($conn_id)
473 {
474 mysqli_close($conn_id);
475 }
476
477}
478
479?>