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