1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5
* An open source application development framework for PHP 5.1.6 or newer
8
* @author ExpressionEngine Dev Team
9
* @copyright Copyright (c) 2008 - 2011, 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 = '`';
38
// clause and character used for LIKE escape sequences - not used in MySQL
39
var $_like_escape_str = '';
40
var $_like_escape_chr = '';
43
* Whether to use the MySQL "delete hack" which allows the number
44
* of affected rows to be shown. Uses a preg_replace when enabled,
45
* adding a bit more processing to all queries.
47
var $delete_hack = TRUE;
50
* The syntax to count rows is slightly different across different
51
* database engines, so this string appears in each driver and is
52
* used for the count_all() and count_all_results() functions.
54
var $_count_string = 'SELECT COUNT(*) AS ';
55
var $_random_keyword = ' RAND()'; // database specific random keyword
57
// whether SET NAMES must be used to set the character set
61
* Non-persistent database connection
63
* @access private called by the base class
68
if ($this->port != '')
70
$this->hostname .= ':'.$this->port;
73
return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
76
// --------------------------------------------------------------------
79
* Persistent database connection
81
* @access private called by the base class
84
function db_pconnect()
86
if ($this->port != '')
88
$this->hostname .= ':'.$this->port;
91
return @mysql_pconnect($this->hostname, $this->username, $this->password);
94
// --------------------------------------------------------------------
99
* Keep / reestablish the db connection if no queries have been
100
* sent for a length of time exceeding the server's idle timeout
107
if (mysql_ping($this->conn_id) === FALSE)
109
$this->conn_id = FALSE;
113
// --------------------------------------------------------------------
116
* Select the database
118
* @access private called by the base class
123
return @mysql_select_db($this->database, $this->conn_id);
126
// --------------------------------------------------------------------
129
* Set client character set
136
function db_set_charset($charset, $collation)
138
if ( ! isset($this->use_set_names))
140
// mysql_set_charset() requires PHP >= 5.2.3 and MySQL >= 5.0.7, use SET NAMES as fallback
141
$this->use_set_names = (version_compare(PHP_VERSION, '5.2.3', '>=') && version_compare(mysql_get_server_info(), '5.0.7', '>=')) ? FALSE : TRUE;
144
if ($this->use_set_names === TRUE)
146
return @mysql_query("SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'", $this->conn_id);
150
return @mysql_set_charset($charset, $this->conn_id);
154
// --------------------------------------------------------------------
157
* Version number query string
164
return "SELECT version() AS ver";
167
// --------------------------------------------------------------------
172
* @access private called by the base class
173
* @param string an SQL query
176
function _execute($sql)
178
$sql = $this->_prep_query($sql);
179
return @mysql_query($sql, $this->conn_id);
182
// --------------------------------------------------------------------
187
* If needed, each database adapter can prep the query string
189
* @access private called by execute()
190
* @param string an SQL query
193
function _prep_query($sql)
195
// "DELETE FROM TABLE" returns 0 affected rows This hack modifies
196
// the query so that it returns the number of affected rows
197
if ($this->delete_hack === TRUE)
199
if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
201
$sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
208
// --------------------------------------------------------------------
216
function trans_begin($test_mode = FALSE)
218
if ( ! $this->trans_enabled)
223
// When transactions are nested we only begin/commit/rollback the outermost ones
224
if ($this->_trans_depth > 0)
229
// Reset the transaction failure flag.
230
// If the $test_mode flag is set to TRUE transactions will be rolled back
231
// even if the queries produce a successful result.
232
$this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
234
$this->simple_query('SET AUTOCOMMIT=0');
235
$this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
239
// --------------------------------------------------------------------
247
function trans_commit()
249
if ( ! $this->trans_enabled)
254
// When transactions are nested we only begin/commit/rollback the outermost ones
255
if ($this->_trans_depth > 0)
260
$this->simple_query('COMMIT');
261
$this->simple_query('SET AUTOCOMMIT=1');
265
// --------------------------------------------------------------------
268
* Rollback Transaction
273
function trans_rollback()
275
if ( ! $this->trans_enabled)
280
// When transactions are nested we only begin/commit/rollback the outermost ones
281
if ($this->_trans_depth > 0)
286
$this->simple_query('ROLLBACK');
287
$this->simple_query('SET AUTOCOMMIT=1');
291
// --------------------------------------------------------------------
298
* @param bool whether or not the string will be used in a LIKE condition
301
function escape_str($str, $like = FALSE)
305
foreach ($str as $key => $val)
307
$str[$key] = $this->escape_str($val, $like);
313
if (function_exists('mysql_real_escape_string') AND is_resource($this->conn_id))
315
$str = mysql_real_escape_string($str, $this->conn_id);
317
elseif (function_exists('mysql_escape_string'))
319
$str = mysql_escape_string($str);
323
$str = addslashes($str);
326
// escape LIKE condition wildcards
329
$str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
335
// --------------------------------------------------------------------
343
function affected_rows()
345
return @mysql_affected_rows($this->conn_id);
348
// --------------------------------------------------------------------
358
return @mysql_insert_id($this->conn_id);
361
// --------------------------------------------------------------------
366
* Generates a platform-specific query string that counts all records in
367
* the specified database
373
function count_all($table = '')
380
$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
382
if ($query->num_rows() == 0)
387
$row = $query->row();
388
$this->_reset_select();
389
return (int) $row->numrows;
392
// --------------------------------------------------------------------
397
* Generates a platform-specific query string so that the table names can be fetched
403
function _list_tables($prefix_limit = FALSE)
405
$sql = "SHOW TABLES FROM ".$this->_escape_char.$this->database.$this->_escape_char;
407
if ($prefix_limit !== FALSE AND $this->dbprefix != '')
409
$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
415
// --------------------------------------------------------------------
420
* Generates a platform-specific query string so that the column names can be fetched
423
* @param string the table name
426
function _list_columns($table = '')
428
return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE);
431
// --------------------------------------------------------------------
436
* Generates a platform-specific query so that the column data can be retrieved
439
* @param string the table name
442
function _field_data($table)
444
return "DESCRIBE ".$table;
447
// --------------------------------------------------------------------
450
* The error message string
455
function _error_message()
457
return mysql_error($this->conn_id);
460
// --------------------------------------------------------------------
463
* The error message number
468
function _error_number()
470
return mysql_errno($this->conn_id);
473
// --------------------------------------------------------------------
476
* Escape the SQL Identifiers
478
* This function escapes column and table names
484
function _escape_identifiers($item)
486
if ($this->_escape_char == '')
491
foreach ($this->_reserved_identifiers as $id)
493
if (strpos($item, '.'.$id) !== FALSE)
495
$str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
497
// remove duplicates if the user already included the escape
498
return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
502
if (strpos($item, '.') !== FALSE)
504
$str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
508
$str = $this->_escape_char.$item.$this->_escape_char;
511
// remove duplicates if the user already included the escape
512
return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
515
// --------------------------------------------------------------------
520
* This function implicitly groups FROM tables so there is no confusion
521
* about operator precedence in harmony with SQL standards
527
function _from_tables($tables)
529
if ( ! is_array($tables))
531
$tables = array($tables);
534
return '('.implode(', ', $tables).')';
537
// --------------------------------------------------------------------
542
* Generates a platform-specific insert string from the supplied data
545
* @param string the table name
546
* @param array the insert keys
547
* @param array the insert values
550
function _insert($table, $keys, $values)
552
return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
555
// --------------------------------------------------------------------
561
* Generates a platform-specific replace string from the supplied data
564
* @param string the table name
565
* @param array the insert keys
566
* @param array the insert values
569
function _replace($table, $keys, $values)
571
return "REPLACE INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
574
// --------------------------------------------------------------------
577
* Insert_batch statement
579
* Generates a platform-specific insert string from the supplied data
582
* @param string the table name
583
* @param array the insert keys
584
* @param array the insert values
587
function _insert_batch($table, $keys, $values)
589
return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
592
// --------------------------------------------------------------------
598
* Generates a platform-specific update string from the supplied data
601
* @param string the table name
602
* @param array the update data
603
* @param array the where clause
604
* @param array the orderby clause
605
* @param array the limit clause
608
function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
610
foreach ($values as $key => $val)
612
$valstr[] = $key . ' = ' . $val;
615
$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
617
$orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
619
$sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
621
$sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
623
$sql .= $orderby.$limit;
628
// --------------------------------------------------------------------
632
* Update_Batch statement
634
* Generates a platform-specific batch update string from the supplied data
637
* @param string the table name
638
* @param array the update data
639
* @param array the where clause
642
function _update_batch($table, $values, $index, $where = NULL)
645
$where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
647
foreach ($values as $key => $val)
649
$ids[] = $val[$index];
651
foreach (array_keys($val) as $field)
653
if ($field != $index)
655
$final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
660
$sql = "UPDATE ".$table." SET ";
663
foreach ($final as $k => $v)
665
$cases .= $k.' = CASE '."\n";
671
$cases .= 'ELSE '.$k.' END, ';
674
$sql .= substr($cases, 0, -2);
676
$sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
681
// --------------------------------------------------------------------
687
* Generates a platform-specific truncate string from the supplied data
688
* If the database does not support the truncate() command
689
* This function maps to "DELETE FROM table"
692
* @param string the table name
695
function _truncate($table)
697
return "TRUNCATE ".$table;
700
// --------------------------------------------------------------------
705
* Generates a platform-specific delete string from the supplied data
708
* @param string the table name
709
* @param array the where clause
710
* @param string the limit clause
713
function _delete($table, $where = array(), $like = array(), $limit = FALSE)
717
if (count($where) > 0 OR count($like) > 0)
719
$conditions = "\nWHERE ";
720
$conditions .= implode("\n", $this->ar_where);
722
if (count($where) > 0 && count($like) > 0)
724
$conditions .= " AND ";
726
$conditions .= implode("\n", $like);
729
$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
731
return "DELETE FROM ".$table.$conditions.$limit;
734
// --------------------------------------------------------------------
739
* Generates a platform-specific LIMIT clause
742
* @param string the sql query string
743
* @param integer the number of rows to limit the query to
744
* @param integer the offset value
747
function _limit($sql, $limit, $offset)
758
return $sql."LIMIT ".$offset.$limit;
761
// --------------------------------------------------------------------
764
* Close DB Connection
770
function _close($conn_id)
772
@mysql_close($conn_id);
778
/* End of file mysql_driver.php */
779
/* Location: ./system/database/drivers/mysql/mysql_driver.php */
b'\\ No newline at end of file'