blob: 2104b9abd8bbd3fbf08248abdaab240e31efebb3 [file] [log] [blame]
admin046000b2006-09-24 18:12:07 +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
admin046000b2006-09-24 18:12:07 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
admin046000b2006-09-24 18:12:07 +000016// ------------------------------------------------------------------------
17
18/**
19 * MySQL Database Adapter Class
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_mysql_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,
admin046000b2006-09-24 18:12:07 +000036 * adding a bit more processing to all queries.
37 */
admine334c472006-10-21 19:44:22 +000038 var $delete_hack = TRUE;
admin046000b2006-09-24 18:12:07 +000039
40 /**
41 * Non-persistent database connection
42 *
43 * @access private called by the base class
44 * @return resource
45 */
46 function db_connect()
47 {
admin7acd5812006-10-23 21:37:22 +000048 return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
admin046000b2006-09-24 18:12:07 +000049 }
50
51 // --------------------------------------------------------------------
52
53 /**
54 * Persistent database connection
55 *
56 * @access private called by the base class
57 * @return resource
58 */
59 function db_pconnect()
60 {
admin7acd5812006-10-23 21:37:22 +000061 return @mysql_pconnect($this->hostname, $this->username, $this->password);
admin046000b2006-09-24 18:12:07 +000062 }
63
64 // --------------------------------------------------------------------
65
66 /**
67 * Select the database
68 *
69 * @access private called by the base class
70 * @return resource
71 */
72 function db_select()
73 {
74 return @mysql_select_db($this->database, $this->conn_id);
75 }
admin9cd4e8e2006-09-25 23:26:25 +000076
77 // --------------------------------------------------------------------
78
79 /**
80 * Version number query string
81 *
82 * @access public
83 * @return string
84 */
85 function _version()
86 {
87 return "SELECT version() AS ver";
88 }
89
admin046000b2006-09-24 18:12:07 +000090 // --------------------------------------------------------------------
91
92 /**
93 * Execute the query
94 *
95 * @access private called by the base class
96 * @param string an SQL query
97 * @return resource
98 */
99 function _execute($sql)
100 {
101 $sql = $this->_prep_query($sql);
102 return @mysql_query($sql, $this->conn_id);
103 }
104
105 // --------------------------------------------------------------------
106
107 /**
108 * Prep the query
109 *
110 * If needed, each database adapter can prep the query string
111 *
112 * @access private called by execute()
113 * @param string an SQL query
114 * @return string
115 */
admine334c472006-10-21 19:44:22 +0000116 function _prep_query($sql)
117 {
118 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
admin046000b2006-09-24 18:12:07 +0000119 // the query so that it returns the number of affected rows
120 if ($this->delete_hack === TRUE)
121 {
admine334c472006-10-21 19:44:22 +0000122 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
admin046000b2006-09-24 18:12:07 +0000123 {
124 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
125 }
126 }
127
128 return $sql;
admine334c472006-10-21 19:44:22 +0000129 }
130
admin046000b2006-09-24 18:12:07 +0000131 // --------------------------------------------------------------------
132
133 /**
134 * Begin Transaction
admine334c472006-10-21 19:44:22 +0000135 *
admin046000b2006-09-24 18:12:07 +0000136 * @access public
admine334c472006-10-21 19:44:22 +0000137 * @return bool
admin046000b2006-09-24 18:12:07 +0000138 */
139 function trans_begin($test_mode = FALSE)
140 {
141 if ( ! $this->trans_enabled)
142 {
143 return TRUE;
144 }
145
146 // When transactions are nested we only begin/commit/rollback the outermost ones
147 if ($this->_trans_depth > 0)
148 {
149 return TRUE;
150 }
151
152 // Reset the transaction failure flag.
admine334c472006-10-21 19:44:22 +0000153 // If the $test_mode flag is set to TRUE transactions will be rolled back
154 // even if the queries produce a successful result.
admin046000b2006-09-24 18:12:07 +0000155 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
156
157 $this->simple_query('SET AUTOCOMMIT=0');
158 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
159 return TRUE;
160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Commit Transaction
admine334c472006-10-21 19:44:22 +0000166 *
admin046000b2006-09-24 18:12:07 +0000167 * @access public
admine334c472006-10-21 19:44:22 +0000168 * @return bool
admin046000b2006-09-24 18:12:07 +0000169 */
170 function trans_commit()
171 {
172 if ( ! $this->trans_enabled)
173 {
174 return TRUE;
175 }
176
177 // When transactions are nested we only begin/commit/rollback the outermost ones
178 if ($this->_trans_depth > 0)
179 {
180 return TRUE;
181 }
182
183 $this->simple_query('COMMIT');
184 $this->simple_query('SET AUTOCOMMIT=1');
185 return TRUE;
186 }
187
188 // --------------------------------------------------------------------
189
190 /**
191 * Rollback Transaction
admine334c472006-10-21 19:44:22 +0000192 *
admin046000b2006-09-24 18:12:07 +0000193 * @access public
admine334c472006-10-21 19:44:22 +0000194 * @return bool
admin046000b2006-09-24 18:12:07 +0000195 */
196 function trans_rollback()
197 {
198 if ( ! $this->trans_enabled)
199 {
200 return TRUE;
201 }
202
203 // When transactions are nested we only begin/commit/rollback the outermost ones
204 if ($this->_trans_depth > 0)
205 {
206 return TRUE;
207 }
208
209 $this->simple_query('ROLLBACK');
210 $this->simple_query('SET AUTOCOMMIT=1');
211 return TRUE;
212 }
213
214 // --------------------------------------------------------------------
215
216 /**
217 * Escape String
218 *
219 * @access public
220 * @param string
221 * @return string
222 */
223 function escape_str($str)
224 {
admin17a890d2006-09-27 20:42:42 +0000225 if (get_magic_quotes_gpc())
226 {
227 return $str;
228 }
229
admine334c472006-10-21 19:44:22 +0000230 if (function_exists('mysql_real_escape_string'))
231 {
admin33de9a12006-09-28 06:50:16 +0000232 return mysql_real_escape_string($str, $this->conn_id);
233 }
234 elseif (function_exists('mysql_escape_string'))
235 {
236 return mysql_escape_string($str);
admin17a890d2006-09-27 20:42:42 +0000237 }
238 else
239 {
admine334c472006-10-21 19:44:22 +0000240 return addslashes($str);
241 }
admin046000b2006-09-24 18:12:07 +0000242 }
243
244 // --------------------------------------------------------------------
245
246 /**
247 * Affected Rows
248 *
249 * @access public
250 * @return integer
251 */
252 function affected_rows()
253 {
254 return @mysql_affected_rows($this->conn_id);
255 }
256
257 // --------------------------------------------------------------------
258
259 /**
260 * Insert ID
261 *
262 * @access public
263 * @return integer
264 */
265 function insert_id()
266 {
267 return @mysql_insert_id($this->conn_id);
268 }
269
270 // --------------------------------------------------------------------
271
272 /**
273 * "Count All" query
274 *
275 * Generates a platform-specific query string that counts all records in
276 * the specified database
277 *
278 * @access public
279 * @param string
280 * @return string
281 */
282 function count_all($table = '')
283 {
284 if ($table == '')
285 return '0';
286
287 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
288
289 if ($query->num_rows() == 0)
290 return '0';
291
292 $row = $query->row();
293 return $row->numrows;
294 }
admin3dd978f2006-09-30 19:24:45 +0000295
296 // --------------------------------------------------------------------
297
298 /**
admin3dd978f2006-09-30 19:24:45 +0000299 * List table query
300 *
301 * Generates a platform-specific query string so that the table names can be fetched
302 *
303 * @access private
304 * @return string
305 */
306 function _list_tables()
307 {
308 return "SHOW TABLES FROM `".$this->database."`";
309 }
admin046000b2006-09-24 18:12:07 +0000310
311 // --------------------------------------------------------------------
312
313 /**
adminfafe28b2006-10-21 19:08:17 +0000314 * Show column query
admin9cd4e8e2006-09-25 23:26:25 +0000315 *
316 * Generates a platform-specific query string so that the column names can be fetched
317 *
318 * @access public
319 * @param string the table name
320 * @return string
321 */
322 function _list_columns($table = '')
323 {
324 return "SHOW COLUMNS FROM ".$this->_escape_table($table);
325 }
326
327 // --------------------------------------------------------------------
328
329 /**
330 * Field data query
331 *
332 * Generates a platform-specific query so that the column data can be retrieved
333 *
334 * @access public
335 * @param string the table name
336 * @return object
337 */
338 function _field_data($table)
339 {
340 return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
341 }
342
343 // --------------------------------------------------------------------
344
345 /**
admin046000b2006-09-24 18:12:07 +0000346 * The error message string
347 *
adminbb1d4392006-09-24 20:14:38 +0000348 * @access private
admin046000b2006-09-24 18:12:07 +0000349 * @return string
350 */
adminbb1d4392006-09-24 20:14:38 +0000351 function _error_message()
admin046000b2006-09-24 18:12:07 +0000352 {
353 return mysql_error($this->conn_id);
354 }
355
356 // --------------------------------------------------------------------
357
358 /**
359 * The error message number
360 *
adminbb1d4392006-09-24 20:14:38 +0000361 * @access private
admin046000b2006-09-24 18:12:07 +0000362 * @return integer
363 */
adminbb1d4392006-09-24 20:14:38 +0000364 function _error_number()
admin046000b2006-09-24 18:12:07 +0000365 {
366 return mysql_errno($this->conn_id);
367 }
368
369 // --------------------------------------------------------------------
370
371 /**
372 * Escape Table Name
373 *
374 * This function adds backticks if the table name has a period
375 * in it. Some DBs will get cranky unless periods are escaped
376 *
adminbb1d4392006-09-24 20:14:38 +0000377 * @access private
admin046000b2006-09-24 18:12:07 +0000378 * @param string the table name
379 * @return string
380 */
adminbb1d4392006-09-24 20:14:38 +0000381 function _escape_table($table)
admin046000b2006-09-24 18:12:07 +0000382 {
383 if (stristr($table, '.'))
384 {
385 $table = preg_replace("/\./", "`.`", $table);
386 }
387
388 return $table;
389 }
adminbb1d4392006-09-24 20:14:38 +0000390
admin046000b2006-09-24 18:12:07 +0000391 // --------------------------------------------------------------------
392
393 /**
394 * Insert statement
395 *
396 * Generates a platform-specific insert string from the supplied data
397 *
398 * @access public
399 * @param string the table name
400 * @param array the insert keys
401 * @param array the insert values
402 * @return string
403 */
404 function _insert($table, $keys, $values)
405 {
adminbb1d4392006-09-24 20:14:38 +0000406 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
admin046000b2006-09-24 18:12:07 +0000407 }
408
409 // --------------------------------------------------------------------
410
411 /**
412 * Update statement
413 *
414 * Generates a platform-specific update string from the supplied data
415 *
416 * @access public
417 * @param string the table name
418 * @param array the update data
419 * @param array the where clause
420 * @return string
421 */
422 function _update($table, $values, $where)
423 {
424 foreach($values as $key => $val)
425 {
426 $valstr[] = $key." = ".$val;
427 }
428
adminbb1d4392006-09-24 20:14:38 +0000429 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
admin046000b2006-09-24 18:12:07 +0000430 }
431
432 // --------------------------------------------------------------------
433
434 /**
435 * Delete statement
436 *
437 * Generates a platform-specific delete string from the supplied data
438 *
439 * @access public
440 * @param string the table name
441 * @param array the where clause
442 * @return string
443 */
444 function _delete($table, $where)
445 {
adminbb1d4392006-09-24 20:14:38 +0000446 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
admin046000b2006-09-24 18:12:07 +0000447 }
adminbb1d4392006-09-24 20:14:38 +0000448
449 // --------------------------------------------------------------------
450
451 /**
452 * Limit string
453 *
454 * Generates a platform-specific LIMIT clause
455 *
456 * @access public
457 * @param string the sql query string
458 * @param integer the number of rows to limit the query to
459 * @param integer the offset value
460 * @return string
461 */
462 function _limit($sql, $limit, $offset)
463 {
464 if ($offset == 0)
465 {
466 $offset = '';
467 }
468 else
469 {
470 $offset .= ", ";
471 }
472
473 return $sql."LIMIT ".$offset.$limit;
474 }
475
476 // --------------------------------------------------------------------
477
478 /**
479 * Close DB Connection
480 *
481 * @access public
482 * @param resource
483 * @return void
484 */
485 function _close($conn_id)
486 {
adminbefd4c22006-10-26 01:49:26 +0000487 @mysql_close($conn_id);
adminbb1d4392006-09-24 20:14:38 +0000488 }
admin046000b2006-09-24 18:12:07 +0000489
admin046000b2006-09-24 18:12:07 +0000490}
491
492?>