1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * MySQL Database Adapter Class
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.
25 * @package CodeIgniter
28 * @author ExpressionEngine Dev Team
29 * @link http://codeigniter.com/user_guide/database/
31 class CI_DB_mysql_driver extends CI_DB {
33 var $dbdriver = 'mysql';
35 // The character used for escaping
36 var $_escape_char = '`';
39 * Whether to use the MySQL "delete hack" which allows the number
40 * of affected rows to be shown. Uses a preg_replace when enabled,
41 * adding a bit more processing to all queries.
43 var $delete_hack = TRUE;
46 * The syntax to count rows is slightly different across different
47 * database engines, so this string appears in each driver and is
48 * used for the count_all() and count_all_results() functions.
50 var $_count_string = 'SELECT COUNT(*) AS ';
51 var $_random_keyword = ' RAND()'; // database specific random keyword
54 * Non-persistent database connection
56 * @access private called by the base class
61 if ($this->port != '')
63 $this->hostname .= ':'.$this->port;
66 return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
69 // --------------------------------------------------------------------
72 * Persistent database connection
74 * @access private called by the base class
77 function db_pconnect()
79 if ($this->port != '')
81 $this->hostname .= ':'.$this->port;
84 return @mysql_pconnect($this->hostname, $this->username, $this->password);
87 // --------------------------------------------------------------------
92 * @access private called by the base class
97 return @mysql_select_db($this->database, $this->conn_id);
100 // --------------------------------------------------------------------
103 * Set client character set
110 function db_set_charset($charset, $collation)
112 return @mysql_query("SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'", $this->conn_id);
115 // --------------------------------------------------------------------
118 * Version number query string
125 return "SELECT version() AS ver";
128 // --------------------------------------------------------------------
133 * @access private called by the base class
134 * @param string an SQL query
137 function _execute($sql)
139 $sql = $this->_prep_query($sql);
140 return @mysql_query($sql, $this->conn_id);
143 // --------------------------------------------------------------------
148 * If needed, each database adapter can prep the query string
150 * @access private called by execute()
151 * @param string an SQL query
154 function _prep_query($sql)
156 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
157 // the query so that it returns the number of affected rows
158 if ($this->delete_hack === TRUE)
160 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
162 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
169 // --------------------------------------------------------------------
177 function trans_begin($test_mode = FALSE)
179 if ( ! $this->trans_enabled)
184 // When transactions are nested we only begin/commit/rollback the outermost ones
185 if ($this->_trans_depth > 0)
190 // Reset the transaction failure flag.
191 // If the $test_mode flag is set to TRUE transactions will be rolled back
192 // even if the queries produce a successful result.
193 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
195 $this->simple_query('SET AUTOCOMMIT=0');
196 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
200 // --------------------------------------------------------------------
208 function trans_commit()
210 if ( ! $this->trans_enabled)
215 // When transactions are nested we only begin/commit/rollback the outermost ones
216 if ($this->_trans_depth > 0)
221 $this->simple_query('COMMIT');
222 $this->simple_query('SET AUTOCOMMIT=1');
226 // --------------------------------------------------------------------
229 * Rollback Transaction
234 function trans_rollback()
236 if ( ! $this->trans_enabled)
241 // When transactions are nested we only begin/commit/rollback the outermost ones
242 if ($this->_trans_depth > 0)
247 $this->simple_query('ROLLBACK');
248 $this->simple_query('SET AUTOCOMMIT=1');
252 // --------------------------------------------------------------------
261 function escape_str($str)
265 foreach($str as $key => $val)
267 $str[$key] = $this->escape_str($val);
273 if (function_exists('mysql_real_escape_string') AND is_resource($this->conn_id))
275 return mysql_real_escape_string($str, $this->conn_id);
277 elseif (function_exists('mysql_escape_string'))
279 return mysql_escape_string($str);
283 return addslashes($str);
287 // --------------------------------------------------------------------
295 function affected_rows()
297 return @mysql_affected_rows($this->conn_id);
300 // --------------------------------------------------------------------
310 return @mysql_insert_id($this->conn_id);
313 // --------------------------------------------------------------------
318 * Generates a platform-specific query string that counts all records in
319 * the specified database
325 function count_all($table = '')
330 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
332 if ($query->num_rows() == 0)
335 $row = $query->row();
336 return (int)$row->numrows;
339 // --------------------------------------------------------------------
344 * Generates a platform-specific query string so that the table names can be fetched
350 function _list_tables($prefix_limit = FALSE)
352 $sql = "SHOW TABLES FROM ".$this->_escape_char.$this->database.$this->_escape_char;
354 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
356 $sql .= " LIKE '".$this->dbprefix."%'";
362 // --------------------------------------------------------------------
367 * Generates a platform-specific query string so that the column names can be fetched
370 * @param string the table name
373 function _list_columns($table = '')
375 return "SHOW COLUMNS FROM ".$table;
378 // --------------------------------------------------------------------
383 * Generates a platform-specific query so that the column data can be retrieved
386 * @param string the table name
389 function _field_data($table)
391 return "SELECT * FROM ".$table." LIMIT 1";
394 // --------------------------------------------------------------------
397 * The error message string
402 function _error_message()
404 return mysql_error($this->conn_id);
407 // --------------------------------------------------------------------
410 * The error message number
415 function _error_number()
417 return mysql_errno($this->conn_id);
420 // --------------------------------------------------------------------
423 * Escape the SQL Identifiers
425 * This function escapes column and table names
431 function _escape_identifiers($item)
433 if ($this->_escape_char == '')
438 if (strpos($item, '.') !== FALSE)
440 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
444 $str = $this->_escape_char.$item.$this->_escape_char;
447 // remove duplicates if the user already included the escape
448 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
451 // --------------------------------------------------------------------
456 * This function implicitly groups FROM tables so there is no confusion
457 * about operator precedence in harmony with SQL standards
463 function _from_tables($tables)
465 if ( ! is_array($tables))
467 $tables = array($tables);
470 return '('.implode(', ', $tables).')';
473 // --------------------------------------------------------------------
478 * Generates a platform-specific insert string from the supplied data
481 * @param string the table name
482 * @param array the insert keys
483 * @param array the insert values
486 function _insert($table, $keys, $values)
488 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
491 // --------------------------------------------------------------------
496 * Generates a platform-specific update string from the supplied data
499 * @param string the table name
500 * @param array the update data
501 * @param array the where clause
502 * @param array the orderby clause
503 * @param array the limit clause
506 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
508 foreach($values as $key => $val)
510 $valstr[] = $key." = ".$val;
513 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
515 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
517 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
519 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
521 $sql .= $orderby.$limit;
526 // --------------------------------------------------------------------
531 * Generates a platform-specific truncate string from the supplied data
532 * If the database does not support the truncate() command
533 * This function maps to "DELETE FROM table"
536 * @param string the table name
539 function _truncate($table)
541 return "TRUNCATE ".$table;
544 // --------------------------------------------------------------------
549 * Generates a platform-specific delete string from the supplied data
552 * @param string the table name
553 * @param array the where clause
554 * @param string the limit clause
557 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
561 if (count($where) > 0 OR count($like) > 0)
563 $conditions = "\nWHERE ";
564 $conditions .= implode("\n", $this->ar_where);
566 if (count($where) > 0 && count($like) > 0)
568 $conditions .= " AND ";
570 $conditions .= implode("\n", $like);
573 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
575 return "DELETE FROM ".$table.$conditions.$limit;
578 // --------------------------------------------------------------------
583 * Generates a platform-specific LIMIT clause
586 * @param string the sql query string
587 * @param integer the number of rows to limit the query to
588 * @param integer the offset value
591 function _limit($sql, $limit, $offset)
602 return $sql."LIMIT ".$offset.$limit;
605 // --------------------------------------------------------------------
608 * Close DB Connection
614 function _close($conn_id)
616 @mysql_close($conn_id);
622 /* End of file mysql_driver.php */
623 /* Location: ./system/database/drivers/mysql/mysql_driver.php */