blob: adc482dfef962100117fdb300aefb6b6ccc55e0f [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 *
adminbb1d4392006-09-24 20:14:38 +0000275 * @access private
adminff2d2512006-09-24 18:12:18 +0000276 * @return string
277 */
adminbb1d4392006-09-24 20:14:38 +0000278 function _error_message()
adminff2d2512006-09-24 18:12:18 +0000279 {
280 return mysqli_error($this->conn_id);
281 }
282
283 // --------------------------------------------------------------------
284
285 /**
286 * The error message number
287 *
adminbb1d4392006-09-24 20:14:38 +0000288 * @access private
adminff2d2512006-09-24 18:12:18 +0000289 * @return integer
290 */
adminbb1d4392006-09-24 20:14:38 +0000291 function _error_number()
adminff2d2512006-09-24 18:12:18 +0000292 {
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 *
adminbb1d4392006-09-24 20:14:38 +0000304 * @access private
adminff2d2512006-09-24 18:12:18 +0000305 * @param string the table name
306 * @return string
307 */
adminbb1d4392006-09-24 20:14:38 +0000308 function _escape_table($table)
adminff2d2512006-09-24 18:12:18 +0000309 {
310 if (stristr($table, '.'))
311 {
312 $table = preg_replace("/\./", "`.`", $table);
313 }
314
315 return $table;
316 }
adminbb1d4392006-09-24 20:14:38 +0000317
adminff2d2512006-09-24 18:12:18 +0000318 // --------------------------------------------------------------------
319
320 /**
321 * Insert statement
322 *
323 * Generates a platform-specific insert string from the supplied data
324 *
325 * @access public
326 * @param string the table name
327 * @param array the insert keys
328 * @param array the insert values
329 * @return string
330 */
331 function _insert($table, $keys, $values)
332 {
adminbb1d4392006-09-24 20:14:38 +0000333 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
adminff2d2512006-09-24 18:12:18 +0000334 }
335
336 // --------------------------------------------------------------------
337
338 /**
339 * Update statement
340 *
341 * Generates a platform-specific update string from the supplied data
342 *
343 * @access public
344 * @param string the table name
345 * @param array the update data
346 * @param array the where clause
347 * @return string
348 */
349 function _update($table, $values, $where)
350 {
351 foreach($values as $key => $val)
352 {
353 $valstr[] = $key." = ".$val;
354 }
355
adminbb1d4392006-09-24 20:14:38 +0000356 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
adminff2d2512006-09-24 18:12:18 +0000357 }
358
359 // --------------------------------------------------------------------
360
361 /**
362 * Delete statement
363 *
364 * Generates a platform-specific delete string from the supplied data
365 *
366 * @access public
367 * @param string the table name
368 * @param array the where clause
369 * @return string
370 */
371 function _delete($table, $where)
372 {
adminbb1d4392006-09-24 20:14:38 +0000373 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
adminff2d2512006-09-24 18:12:18 +0000374 }
adminbb1d4392006-09-24 20:14:38 +0000375
376 // --------------------------------------------------------------------
377
378 /**
379 * Limit string
380 *
381 * Generates a platform-specific LIMIT clause
382 *
383 * @access public
384 * @param string the sql query string
385 * @param integer the number of rows to limit the query to
386 * @param integer the offset value
387 * @return string
388 */
389 function _limit($sql, $limit, $offset)
390 {
391 $sql .= "LIMIT ".$limit;
adminff2d2512006-09-24 18:12:18 +0000392
adminbb1d4392006-09-24 20:14:38 +0000393 if ($offset > 0)
394 {
395 $sql .= " OFFSET ".$offset;
396 }
397
398 return $sql;
399 }
400
401 // --------------------------------------------------------------------
402
403 /**
404 * Close DB Connection
405 *
406 * @access public
407 * @param resource
408 * @return void
409 */
410 function _close($conn_id)
411 {
412 mysqli_close($conn_id);
413 }
414
adminff2d2512006-09-24 18:12:18 +0000415
416}
417
418?>