今天花了点时间把CodeIgniter的Active Record类整理成了一个单文件版, 目前只分离了mysql的驱动,
mysqlclass.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_DB_driver {
var $username;
var $password;
var $hostname;
var $database;
var $dbdriver = 'mysql';
var $dbprefix = '';
var $char_set = 'utf8';
var $dbcollat = 'utf8_general_ci';
var $autoinit = TRUE; // Whether to automatically initialize the DB
var $swap_pre = '';
var $port = '';
var $pconnect = FALSE;
var $conn_id = FALSE;
var $result_id = FALSE;
var $db_debug = FALSE;
var $benchmark = 0;
var $query_count = 0;
var $bind_marker = '?';
var $save_queries = TRUE;
var $queries = array();
var $query_times = array();
var $data_cache = array();
var $trans_enabled = TRUE;
var $trans_strict = TRUE;
var $_trans_depth = 0;
var $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
var $cache_on = FALSE;
var $cachedir = '';
var $cache_autodel = FALSE;
var $CACHE; // The cache class object
var $_protect_identifiers = TRUE;
var $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
var $stmt_id;
var $curs_id;
var $limit_used;
function __construct($params)
{
if (is_array($params))
{
foreach ($params as $key => $val)
{
$this->$key = $val;
}
}
}
function initialize()
{
if (is_resource($this->conn_id) OR is_object($this->conn_id))
{
return TRUE;
}
$this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
if ( ! $this->conn_id)
{
log_message('error', 'Unable to connect to the database');
if ($this->db_debug)
{
$this->display_error('db_unable_to_connect');
}
return FALSE;
}
if ($this->database != '')
{
if ( ! $this->db_select())
{
log_message('error', 'Unable to select database: '.$this->database);
if ($this->db_debug)
{
$this->display_error('db_unable_to_select', $this->database);
}
return FALSE;
}
else
{
if ( ! $this->db_set_charset($this->char_set, $this->dbcollat))
{
return FALSE;
}
return TRUE;
}
}
return TRUE;
}
function db_set_charset($charset, $collation)
{
if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat))
{
log_message('error', 'Unable to set database connection charset: '.$this->char_set);
if ($this->db_debug)
{
$this->display_error('db_unable_to_set_charset', $this->char_set);
}
return FALSE;
}
return TRUE;
}
function platform()
{
return $this->dbdriver;
}
function version()
{
if (FALSE === ($sql = $this->_version()))
{
if ($this->db_debug)
{
return $this->display_error('db_unsupported_function');
}
return FALSE;
}
$driver_version_exceptions = array('oci8', 'sqlite', 'cubrid');
if (in_array($this->dbdriver, $driver_version_exceptions))
{
return $sql;
}
else
{
$query = $this->query($sql);
return $query->row('ver');
}
}
function query($sql, $binds = FALSE, $return_object = TRUE)
{
if ($sql == '')
{
if ($this->db_debug)
{
log_message('error', 'Invalid query: '.$sql);
return $this->display_error('db_invalid_query');
}
return FALSE;
}
if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) )
{
$sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql);
}
if ($binds !== FALSE)
{
$sql = $this->compile_binds($sql, $binds);
}
if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
{
if ($this->_cache_init())
{
$this->load_rdriver();
if (FALSE !== ($cache = $this->CACHE->read($sql)))
{
return $cache;
}
}
}
if ($this->save_queries == TRUE)
{
$this->queries[] = $sql;
}
$time_start = list($sm, $ss) = explode(' ', microtime());
if (FALSE === ($this->result_id = $this->simple_query($sql)))
{
if ($this->save_queries == TRUE)
{
$this->query_times[] = 0;
}
$this->_trans_status = FALSE;
if ($this->db_debug)
{
$error_no = $this->_error_number();
$error_msg = $this->_error_message();
$this->trans_complete();
log_message('error', 'Query error: '.$error_msg);
return $this->display_error(
array(
'Error Number: '.$error_no,
$error_msg,
$sql
)
);
}
return FALSE;
}
$time_end = list($em, $es) = explode(' ', microtime());
$this->benchmark += ($em + $es) - ($sm + $ss);
if ($this->save_queries == TRUE)
{
$this->query_times[] = ($em + $es) - ($sm + $ss);
}
$this->query_count++;
if ($this->is_write_type($sql) === TRUE)
{
if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
{
$this->CACHE->delete();
}
return TRUE;
}
if ($return_object !== TRUE)
{
return TRUE;
}
$driver = $this->load_rdriver();
$RES = new $driver();
$RES->conn_id = $this->conn_id;
$RES->result_id = $this->result_id;
if ($this->dbdriver == 'oci8')
{
$RES->stmt_id = $this->stmt_id;
$RES->curs_id = NULL;
$RES->limit_used = $this->limit_used;
$this->stmt_id = FALSE;
}
$RES->num_rows = $RES->num_rows();
if ($this->cache_on == TRUE AND $this->_cache_init())
{
$CR = new CI_DB_result();
$CR->num_rows = $RES->num_rows();
$CR->result_object = $RES->result_object();
$CR->result_array = $RES->result_array();
$CR->conn_id = NULL;
$CR->result_id = NULL;
$this->CACHE->write($sql, $CR);
}
return $RES;
}
function load_rdriver()
{
$driver = 'CI_DB_'.$this->dbdriver.'_result';
if ( ! class_exists($driver))
{
include_once(BASEPATH.'database/DB_result.php');
include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
}
return $driver;
}
function simple_query($sql)
{
if ( ! $this->conn_id)
{
$this->initialize();
}
return $this->_execute($sql);
}
function trans_off()
{
$this->trans_enabled = FALSE;
}
function trans_strict($mode = TRUE)
{
$this->trans_strict = is_bool($mode) ? $mode : TRUE;
}
function trans_start($test_mode = FALSE)
{
if ( ! $this->trans_enabled)
{
return FALSE;
}
if ($this->_trans_depth > 0)
{
$this->_trans_depth += 1;
return;
}
$this->trans_begin($test_mode);
}
function trans_complete()
{
if ( ! $this->trans_enabled)
{
return FALSE;
}
if ($this->_trans_depth > 1)
{
$this->_trans_depth -= 1;
return TRUE;
}
if ($this->_trans_status === FALSE)
{
$this->trans_rollback();
if ($this->trans_strict === FALSE)
{
$this->_trans_status = TRUE;
}
log_message('debug', 'DB Transaction Failure');
return FALSE;
}
$this->trans_commit();
return TRUE;
}
function trans_status()
{
return $this->_trans_status;
}
function compile_binds($sql, $binds)
{
if (strpos($sql, $this->bind_marker) === FALSE)
{
return $sql;
}
if ( ! is_array($binds))
{
$binds = array($binds);
}
$segments = explode($this->bind_marker, $sql);
if (count($binds) >= count($segments)) {
$binds = array_slice($binds, 0, count($segments)-1);
}
$result = $segments[0];
$i = 0;
foreach ($binds as $bind)
{
$result .= $this->escape($bind);
$result .= $segments[++$i];
}
return $result;
}
function is_write_type($sql)
{
if ( ! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql))
{
return FALSE;
}
return TRUE;
}
function elapsed_time($decimals = 6)
{
return number_format($this->benchmark, $decimals);
}
function total_queries()
{
return $this->query_count;
}
function last_query()
{
return end($this->queries);
}
function escape($str)
{
if (is_string($str))
{
$str = "'".$this->escape_str($str)."'";
}
elseif (is_bool($str))
{
$str = ($str === FALSE) ? 0 : 1;
}
elseif (is_null($str))
{
$str = 'NULL';
}
return $str;
}
function escape_like_str($str)
{
return $this->escape_str($str, TRUE);
}
function primary($table = '')
{
$fields = $this->list_fields($table);
if ( ! is_array($fields))
{
return FALSE;
}
return current($fields);
}
function list_tables($constrain_by_prefix = FALSE)
{
if (isset($this->data_cache['table_names']))
{
return $this->data_cache['table_names'];
}
if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
{
if ($this->db_debug)
{
return $this->display_error('db_unsupported_function');
}
return FALSE;
}
$retval = array();
$query = $this->query($sql);
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
if (isset($row['TABLE_NAME']))
{
$retval[] = $row['TABLE_NAME'];
}
else
{
$retval[] = array_shift($row);
}
}
}
$this->data_cache['table_names'] = $retval;
return $this->data_cache['table_names'];
}
function table_exists($table_name)
{
return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE;
}
function list_fields($table = '')
{
if (isset($this->data_cache['field_names'][$table]))
{
return $this->data_cache['field_names'][$table];
}
if ($table == '')
{
if ($this->db_debug)
{
return $this->display_error('db_field_param_missing');
}
return FALSE;
}
if (FALSE === ($sql = $this->_list_columns($table)))
{
if ($this->db_debug)
{
return $this->display_error('db_unsupported_function');
}
return FALSE;
}
$query = $this->query($sql);
$retval = array();
foreach ($query->result_array() as $row)
{
if (isset($row['COLUMN_NAME']))
{
$retval[] = $row['COLUMN_NAME'];
}
else
{
$retval[] = current($row);
}
}
$this->data_cache['field_names'][$table] = $retval;
return $this->data_cache['field_names'][$table];
}
function field_exists($field_name, $table_name)
{
return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
}
function field_data($table = '')
{
if ($table == '')
{
if ($this->db_debug)
{
return $this->display_error('db_field_param_missing');
}
return FALSE;
}
$query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
return $query->field_data();
}
function insert_string($table, $data)
{
$fields = array();
$values = array();
foreach ($data as $key => $val)
{
$fields[] = $this->_escape_identifiers($key);
$values[] = $this->escape($val);
}
return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
}
function update_string($table, $data, $where)
{
if ($where == '')
{
return false;
}
$fields = array();
foreach ($data as $key => $val)
{
$fields[$this->_protect_identifiers($key)] = $this->escape($val);
}
if ( ! is_array($where))
{
$dest = array($where);
}
else
{
$dest = array();
foreach ($where as $key => $val)
{
$prefix = (count($dest) == 0) ? '' : ' AND ';
if ($val !== '')
{
if ( ! $this->_has_operator($key))
{
$key .= ' =';
}
$val = ' '.$this->escape($val);
}
$dest[] = $prefix.$key.$val;
}
}
return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
}
function _has_operator($str)
{
$str = trim($str);
if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
{
return FALSE;
}
return TRUE;
}
function call_function($function)
{
$driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
if (FALSE === strpos($driver, $function))
{
$function = $driver.$function;
}
if ( ! function_exists($function))
{
if ($this->db_debug)
{
return $this->display_error('db_unsupported_function');
}
return FALSE;
}
else
{
$args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
if (is_null($args))
{
return call_user_func($function);
}
else
{
return call_user_func_array($function, $args);
}
}
}
function cache_set_path($path = '')
{
$this->cachedir = $path;
}
function cache_on()
{
$this->cache_on = TRUE;
return TRUE;
}
function cache_off()
{
$this->cache_on = FALSE;
return FALSE;
}
function cache_delete($segment_one = '', $segment_two = '')
{
if ( ! $this->_cache_init())
{
return FALSE;
}
return $this->CACHE->delete($segment_one, $segment_two);
}
function cache_delete_all()
{
if ( ! $this->_cache_init())
{
return FALSE;
}
return $this->CACHE->delete_all();
}
function _cache_init()
{
if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
{
return TRUE;
}
if ( ! class_exists('CI_DB_Cache'))
{
if ( ! @include(BASEPATH.'database/DB_cache.php'))
{
return $this->cache_off();
}
}
$this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
return TRUE;
}
function close()
{
if (is_resource($this->conn_id) OR is_object($this->conn_id))
{
$this->_close($this->conn_id);
}
$this->conn_id = FALSE;
}
function display_error($error = '', $swap = '', $native = FALSE)
{
$LANG =& load_class('Lang', 'core');
$LANG->load('db');
$heading = $LANG->line('db_error_heading');
if ($native == TRUE)
{
$message = $error;
}
else
{
$message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
}
$trace = debug_backtrace();
foreach ($trace as $call)
{
if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
{
$message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
$message[] = 'Line Number: '.$call['line'];
break;
}
}
$error =& load_class('Exceptions', 'core');
echo $error->show_error($heading, $message, 'error_db');
exit;
}
function protect_identifiers($item, $prefix_single = FALSE)
{
return $this->_protect_identifiers($item, $prefix_single);
}
function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
{
if ( ! is_bool($protect_identifiers))
{
$protect_identifiers = $this->_protect_identifiers;
}
if (is_array($item))
{
$escaped_array = array();
foreach ($item as $k => $v)
{
$escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
}
return $escaped_array;
}
$item = preg_replace('/[\t ]+/', ' ', $item);
if (strpos($item, ' ') !== FALSE)
{
$alias = strstr($item, ' ');
$item = substr($item, 0, - strlen($alias));
}
else
{
$alias = '';
}
if (strpos($item, '(') !== FALSE)
{
return $item.$alias;
}
if (strpos($item, '.') !== FALSE)
{
$parts = explode('.', $item);
if (in_array($parts[0], $this->ar_aliased_tables))
{
if ($protect_identifiers === TRUE)
{
foreach ($parts as $key => $val)
{
if ( ! in_array($val, $this->_reserved_identifiers))
{
$parts[$key] = $this->_escape_identifiers($val);
}
}
$item = implode('.', $parts);
}
return $item.$alias;
}
if ($this->dbprefix != '')
{
if (isset($parts[3]))
{
$i = 2;
}
elseif (isset($parts[2]))
{
$i = 1;
}
else
{
$i = 0;
}
if ($field_exists == FALSE)
{
$i++;
}
if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
{
$parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
}
if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
{
$parts[$i] = $this->dbprefix.$parts[$i];
}
$item = implode('.', $parts);
}
if ($protect_identifiers === TRUE)
{
$item = $this->_escape_identifiers($item);
}
return $item.$alias;
}
if ($this->dbprefix != '')
{
if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
{
$item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
}
if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
{
$item = $this->dbprefix.$item;
}
}
if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
{
$item = $this->_escape_identifiers($item);
}
return $item.$alias;
}
protected function _reset_select()
{
}
}
//end class db_driver
// class DB_active_rec.php
class CI_DB_active_record extends CI_DB_driver {
var $ar_select = array();
var $ar_distinct = FALSE;
var $ar_from = array();
var $ar_join = array();
var $ar_where = array();
var $ar_like = array();
var $ar_groupby = array();
var $ar_having = array();
var $ar_keys = array();
var $ar_limit = FALSE;
var $ar_offset = FALSE;
var $ar_order = FALSE;
var $ar_orderby = array();
var $ar_set = array();
var $ar_wherein = array();
var $ar_aliased_tables = array();
var $ar_store_array = array();
var $ar_caching = FALSE;
var $ar_cache_exists = array();
var $ar_cache_select = array();
var $ar_cache_from = array();
var $ar_cache_join = array();
var $ar_cache_where = array();
var $ar_cache_like = array();
var $ar_cache_groupby = array();
var $ar_cache_having = array();
var $ar_cache_orderby = array();
var $ar_cache_set = array();
var $ar_no_escape = array();
var $ar_cache_no_escape = array();
public function select($select = '*', $escape = NULL)
{
if (is_string($select))
{
$select = explode(',', $select);
}
foreach ($select as $val)
{
$val = trim($val);
if ($val != '')
{
$this->ar_select[] = $val;
$this->ar_no_escape[] = $escape;
if ($this->ar_caching === TRUE)
{
$this->ar_cache_select[] = $val;
$this->ar_cache_exists[] = 'select';
$this->ar_cache_no_escape[] = $escape;
}
}
}
return $this;
}
public function select_max($select = '', $alias = '')
{
return $this->_max_min_avg_sum($select, $alias, 'MAX');
}
public function select_min($select = '', $alias = '')
{
return $this->_max_min_avg_sum($select, $alias, 'MIN');
}
public function select_avg($select = '', $alias = '')
{
return $this->_max_min_avg_sum($select, $alias, 'AVG');
}
public function select_sum($select = '', $alias = '')
{
return $this->_max_min_avg_sum($select, $alias, 'SUM');
}
protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
{
if ( ! is_string($select) OR $select == '')
{
$this->display_error('db_invalid_query');
}
$type = strtoupper($type);
if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
{
show_error('Invalid function type: '.$type);
}
if ($alias == '')
{
$alias = $this->_create_alias_from_table(trim($select));
}
$sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$alias;
$this->ar_select[] = $sql;
if ($this->ar_caching === TRUE)
{
$this->ar_cache_select[] = $sql;
$this->ar_cache_exists[] = 'select';
}
return $this;
}
protected function _create_alias_from_table($item)
{
if (strpos($item, '.') !== FALSE)
{
return end(explode('.', $item));
}
return $item;
}
public function distinct($val = TRUE)
{
$this->ar_distinct = (is_bool($val)) ? $val : TRUE;
return $this;
}
public function from($from)
{
foreach ((array) $from as $val)
{
if (strpos($val, ',') !== FALSE)
{
foreach (explode(',', $val) as $v)
{
$v = trim($v);
$this->_track_aliases($v);
$this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
if ($this->ar_caching === TRUE)
{
$this->ar_cache_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
$this->ar_cache_exists[] = 'from';
}
}
}
else
{
$val = trim($val);
$this->_track_aliases($val);
$this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
if ($this->ar_caching === TRUE)
{
$this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
$this->ar_cache_exists[] = 'from';
}
}
}
return $this;
}
public function join($table, $cond, $type = '')
{
if ($type != '')
{
$type = strtoupper(trim($type));
if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
{
$type = '';
}
else
{
$type .= ' ';
}
}
$this->_track_aliases($table);
if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
{
$match[1] = $this->_protect_identifiers($match[1]);
$match[3] = $this->_protect_identifiers($match[3]);
$cond = $match[1].$match[2].$match[3];
}
$join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
$this->ar_join[] = $join;
if ($this->ar_caching === TRUE)
{
$this->ar_cache_join[] = $join;
$this->ar_cache_exists[] = 'join';
}
return $this;
}
public function where($key, $value = NULL, $escape = TRUE)
{
return $this->_where($key, $value, 'AND ', $escape);
}
public function or_where($key, $value = NULL, $escape = TRUE)
{
return $this->_where($key, $value, 'OR ', $escape);
}
protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
{
if ( ! is_array($key))
{
$key = array($key => $value);
}
if ( ! is_bool($escape))
{
$escape = $this->_protect_identifiers;
}
foreach ($key as $k => $v)
{
$prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
if (is_null($v) && ! $this->_has_operator($k))
{
$k .= ' IS NULL';
}
if ( ! is_null($v))
{
if ($escape === TRUE)
{
$k = $this->_protect_identifiers($k, FALSE, $escape);
$v = ' '.$this->escape($v);
}
if ( ! $this->_has_operator($k))
{
$k .= ' = ';
}
}
else
{
$k = $this->_protect_identifiers($k, FALSE, $escape);
}
$this->ar_where[] = $prefix.$k.$v;
if ($this->ar_caching === TRUE)
{
$this->ar_cache_where[] = $prefix.$k.$v;
$this->ar_cache_exists[] = 'where';
}
}
return $this;
}
public function where_in($key = NULL, $values = NULL)
{
return $this->_where_in($key, $values);
}
public function or_where_in($key = NULL, $values = NULL)
{
return $this->_where_in($key, $values, FALSE, 'OR ');
}
public function where_not_in($key = NULL, $values = NULL)
{
return $this->_where_in($key, $values, TRUE);
}
public function or_where_not_in($key = NULL, $values = NULL)
{
return $this->_where_in($key, $values, TRUE, 'OR ');
}
protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
{
if ($key === NULL OR $values === NULL)
{
return;
}
if ( ! is_array($values))
{
$values = array($values);
}
$not = ($not) ? ' NOT' : '';
foreach ($values as $value)
{
$this->ar_wherein[] = $this->escape($value);
}
$prefix = (count($this->ar_where) == 0) ? '' : $type;
$where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
$this->ar_where[] = $where_in;
if ($this->ar_caching === TRUE)
{
$this->ar_cache_where[] = $where_in;
$this->ar_cache_exists[] = 'where';
}
$this->ar_wherein = array();
return $this;
}
public function like($field, $match = '', $side = 'both')
{
return $this->_like($field, $match, 'AND ', $side);
}
public function not_like($field, $match = '', $side = 'both')
{
return $this->_like($field, $match, 'AND ', $side, 'NOT');
}
public function or_like($field, $match = '', $side = 'both')
{
return $this->_like($field, $match, 'OR ', $side);
}
public function or_not_like($field, $match = '', $side = 'both')
{
return $this->_like($field, $match, 'OR ', $side, 'NOT');
}
protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
{
if ( ! is_array($field))
{
$field = array($field => $match);
}
foreach ($field as $k => $v)
{
$k = $this->_protect_identifiers($k);
$prefix = (count($this->ar_like) == 0) ? '' : $type;
$v = $this->escape_like_str($v);
if ($side == 'none')
{
$like_statement = $prefix." $k $not LIKE '{$v}'";
}
elseif ($side == 'before')
{
$like_statement = $prefix." $k $not LIKE '%{$v}'";
}
elseif ($side == 'after')
{
$like_statement = $prefix." $k $not LIKE '{$v}%'";
}
else
{
$like_statement = $prefix." $k $not LIKE '%{$v}%'";
}
if ($this->_like_escape_str != '')
{
$like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
}
$this->ar_like[] = $like_statement;
if ($this->ar_caching === TRUE)
{
$this->ar_cache_like[] = $like_statement;
$this->ar_cache_exists[] = 'like';
}
}
return $this;
}
public function group_by($by)
{
if (is_string($by))
{
$by = explode(',', $by);
}
foreach ($by as $val)
{
$val = trim($val);
if ($val != '')
{
$this->ar_groupby[] = $this->_protect_identifiers($val);
if ($this->ar_caching === TRUE)
{
$this->ar_cache_groupby[] = $this->_protect_identifiers($val);
$this->ar_cache_exists[] = 'groupby';
}
}
}
return $this;
}
public function having($key, $value = '', $escape = TRUE)
{
return $this->_having($key, $value, 'AND ', $escape);
}
public function or_having($key, $value = '', $escape = TRUE)
{
return $this->_having($key, $value, 'OR ', $escape);
}
protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
{
if ( ! is_array($key))
{
$key = array($key => $value);
}
foreach ($key as $k => $v)
{
$prefix = (count($this->ar_having) == 0) ? '' : $type;
if ($escape === TRUE)
{
$k = $this->_protect_identifiers($k);
}
if ( ! $this->_has_operator($k))
{
$k .= ' = ';
}
if ($v != '')
{
$v = ' '.$this->escape($v);
}
$this->ar_having[] = $prefix.$k.$v;
if ($this->ar_caching === TRUE)
{
$this->ar_cache_having[] = $prefix.$k.$v;
$this->ar_cache_exists[] = 'having';
}
}
return $this;
}
public function order_by($orderby, $direction = '')
{
if (strtolower($direction) == 'random')
{
$orderby = ''; // Random results want or don't need a field name
$direction = $this->_random_keyword;
}
elseif (trim($direction) != '')
{
$direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
}
if (strpos($orderby, ',') !== FALSE)
{
$temp = array();
foreach (explode(',', $orderby) as $part)
{
$part = trim($part);
if ( ! in_array($part, $this->ar_aliased_tables))
{
$part = $this->_protect_identifiers(trim($part));
}
$temp[] = $part;
}
$orderby = implode(', ', $temp);
}
else if ($direction != $this->_random_keyword)
{
$orderby = $this->_protect_identifiers($orderby);
}
$orderby_statement = $orderby.$direction;
$this->ar_orderby[] = $orderby_statement;
if ($this->ar_caching === TRUE)
{
$this->ar_cache_orderby[] = $orderby_statement;
$this->ar_cache_exists[] = 'orderby';
}
return $this;
}
public function limit($value, $offset = '')
{
$this->ar_limit = (int) $value;
if ($offset != '')
{
$this->ar_offset = (int) $offset;
}
return $this;
}
public function offset($offset)
{
$this->ar_offset = $offset;
return $this;
}
public function set($key, $value = '', $escape = TRUE)
{
$key = $this->_object_to_array($key);
if ( ! is_array($key))
{
$key = array($key => $value);
}
foreach ($key as $k => $v)
{
if ($escape === FALSE)
{
$this->ar_set[$this->_protect_identifiers($k)] = $v;
}
else
{
$this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
}
}
return $this;
}
public function get($table = '', $limit = null, $offset = null)
{
if ($table != '')
{
$this->_track_aliases($table);
$this->from($table);
}
if ( ! is_null($limit))
{
$this->limit($limit, $offset);
}
$sql = $this->_compile_select();
$result = $this->query($sql);
$this->_reset_select();
return $result;
}
public function count_all_results($table = '')
{
if ($table != '')
{
$this->_track_aliases($table);
$this->from($table);
}
$sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
$query = $this->query($sql);
$this->_reset_select();
if ($query->num_rows() == 0)
{
return 0;
}
$row = $query->row();
return (int) $row->numrows;
}
public function get_where($table = '', $where = null, $limit = null, $offset = null)
{
if ($table != '')
{
$this->from($table);
}
if ( ! is_null($where))
{
$this->where($where);
}
if ( ! is_null($limit))
{
$this->limit($limit, $offset);
}
$sql = $this->_compile_select();
$result = $this->query($sql);
$this->_reset_select();
return $result;
}
public function insert_batch($table = '', $set = NULL)
{
if ( ! is_null($set))
{
$this->set_insert_batch($set);
}
if (count($this->ar_set) == 0)
{
if ($this->db_debug)
{
return $this->display_error('db_must_use_set');
}
return FALSE;
}
if ($table == '')
{
if ( ! isset($this->ar_from[0]))
{
if ($this->db_debug)
{
return $this->display_error('db_must_set_table');
}
return FALSE;
}
$table = $this->ar_from[0];
}
for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
{
$sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
$this->query($sql);
}
$this->_reset_write();
return TRUE;
}
public function set_insert_batch($key, $value = '', $escape = TRUE)
{
$key = $this->_object_to_array_batch($key);
if ( ! is_array($key))
{
$key = array($key => $value);
}
$keys = array_keys(current($key));
sort($keys);
foreach ($key as $row)
{
if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
{
$this->ar_set[] = array();
return;
}
ksort($row); // puts $row in the same order as our keys
if ($escape === FALSE)
{
$this->ar_set[] = '('.implode(',', $row).')';
}
else
{
$clean = array();
foreach ($row as $value)
{
$clean[] = $this->escape($value);
}
$this->ar_set[] = '('.implode(',', $clean).')';
}
}
foreach ($keys as $k)
{
$this->ar_keys[] = $this->_protect_identifiers($k);
}
return $this;
}
function insert($table = '', $set = NULL)
{
if ( ! is_null($set))
{
$this->set($set);
}
if (count($this->ar_set) == 0)
{
if ($this->db_debug)
{
return $this->display_error('db_must_use_set');
}
return FALSE;
}
if ($table == '')
{
if ( ! isset($this->ar_from[0]))
{
if ($this->db_debug)
{
return $this->display_error('db_must_set_table');
}
return FALSE;
}
$table = $this->ar_from[0];
}
$sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
$this->_reset_write();
return $this->query($sql);
}
public function replace($table = '', $set = NULL)
{
if ( ! is_null($set))
{
$this->set($set);
}
if (count($this->ar_set) == 0)
{
if ($this->db_debug)
{
return $this->display_error('db_must_use_set');
}
return FALSE;
}
if ($table == '')
{
if ( ! isset($this->ar_from[0]))
{
if ($this->db_debug)
{
return $this->display_error('db_must_set_table');
}
return FALSE;
}
$table = $this->ar_from[0];
}
$sql = $this->_replace($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
$this->_reset_write();
return $this->query($sql);
}
public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
{
$this->_merge_cache();
if ( ! is_null($set))
{
$this->set($set);
}
if (count($this->ar_set) == 0)
{
if ($this->db_debug)
{
return $this->display_error('db_must_use_set');
}
return FALSE;
}
if ($table == '')
{
if ( ! isset($this->ar_from[0]))
{
if ($this->db_debug)
{
return $this->display_error('db_must_set_table');
}
return FALSE;
}
$table = $this->ar_from[0];
}
if ($where != NULL)
{
$this->where($where);
}
if ($limit != NULL)
{
$this->limit($limit);
}
$sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
$this->_reset_write();
return $this->query($sql);
}
public function update_batch($table = '', $set = NULL, $index = NULL)
{
$this->_merge_cache();
if (is_null($index))
{
if ($this->db_debug)
{
return $this->display_error('db_must_use_index');
}
return FALSE;
}
if ( ! is_null($set))
{
$this->set_update_batch($set, $index);
}
if (count($this->ar_set) == 0)
{
if ($this->db_debug)
{
return $this->display_error('db_must_use_set');
}
return FALSE;
}
if ($table == '')
{
if ( ! isset($this->ar_from[0]))
{
if ($this->db_debug)
{
return $this->display_error('db_must_set_table');
}
return FALSE;
}
$table = $this->ar_from[0];
}
for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
{
$sql = $this->_update_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->ar_set, $i, 100), $this->_protect_identifiers($index), $this->ar_where);
$this->query($sql);
}
$this->_reset_write();
}
public function set_update_batch($key, $index = '', $escape = TRUE)
{
$key = $this->_object_to_array_batch($key);
if ( ! is_array($key))
{
// @todo error
}
foreach ($key as $k => $v)
{
$index_set = FALSE;
$clean = array();
foreach ($v as $k2 => $v2)
{
if ($k2 == $index)
{
$index_set = TRUE;
}
else
{
$not[] = $k2.'-'.$v2;
}
if ($escape === FALSE)
{
$clean[$this->_protect_identifiers($k2)] = $v2;
}
else
{
$clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
}
}
if ($index_set == FALSE)
{
return $this->display_error('db_batch_missing_index');
}
$this->ar_set[] = $clean;
}
return $this;
}
public function empty_table($table = '')
{
if ($table == '')
{
if ( ! isset($this->ar_from[0]))
{
if ($this->db_debug)
{
return $this->display_error('db_must_set_table');
}
return FALSE;
}
$table = $this->ar_from[0];
}
else
{
$table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
}
$sql = $this->_delete($table);
$this->_reset_write();
return $this->query($sql);
}
public function truncate($table = '')
{
if ($table == '')
{
if ( ! isset($this->ar_from[0]))
{
if ($this->db_debug)
{
return $this->display_error('db_must_set_table');
}
return FALSE;
}
$table = $this->ar_from[0];
}
else
{
$table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
}
$sql = $this->_truncate($table);
$this->_reset_write();
return $this->query($sql);
}
public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
{
$this->_merge_cache();
if ($table == '')
{
if ( ! isset($this->ar_from[0]))
{
if ($this->db_debug)
{
return $this->display_error('db_must_set_table');
}
return FALSE;
}
$table = $this->ar_from[0];
}
elseif (is_array($table))
{
foreach ($table as $single_table)
{
$this->delete($single_table, $where, $limit, FALSE);
}
$this->_reset_write();
return;
}
else
{
$table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
}
if ($where != '')
{
$this->where($where);
}
if ($limit != NULL)
{
$this->limit($limit);
}
if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
{
if ($this->db_debug)
{
return $this->display_error('db_del_must_use_where');
}
return FALSE;
}
$sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
if ($reset_data)
{
$this->_reset_write();
}
return $this->query($sql);
}
public function dbprefix($table = '')
{
if ($table == '')
{
$this->display_error('db_table_name_required');
}
return $this->dbprefix.$table;
}
public function set_dbprefix($prefix = '')
{
return $this->dbprefix = $prefix;
}
protected function _track_aliases($table)
{
if (is_array($table))
{
foreach ($table as $t)
{
$this->_track_aliases($t);
}
return;
}
if (strpos($table, ',') !== FALSE)
{
return $this->_track_aliases(explode(',', $table));
}
if (strpos($table, " ") !== FALSE)
{
$table = preg_replace('/\s+AS\s+/i', ' ', $table);
$table = trim(strrchr($table, " "));
if ( ! in_array($table, $this->ar_aliased_tables))
{
$this->ar_aliased_tables[] = $table;
}
}
}
protected function _compile_select($select_override = FALSE)
{
$this->_merge_cache();
if ($select_override !== FALSE)
{
$sql = $select_override;
}
else
{
$sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
if (count($this->ar_select) == 0)
{
$sql .= '*';
}
else
{
foreach ($this->ar_select as $key => $val)
{
$no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL;
$this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape);
}
$sql .= implode(', ', $this->ar_select);
}
}
if (count($this->ar_from) > 0)
{
$sql .= "\nFROM ";
$sql .= $this->_from_tables($this->ar_from);
}
if (count($this->ar_join) > 0)
{
$sql .= "\n";
$sql .= implode("\n", $this->ar_join);
}
if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
{
$sql .= "\nWHERE ";
}
$sql .= implode("\n", $this->ar_where);
if (count($this->ar_like) > 0)
{
if (count($this->ar_where) > 0)
{
$sql .= "\nAND ";
}
$sql .= implode("\n", $this->ar_like);
}
if (count($this->ar_groupby) > 0)
{
$sql .= "\nGROUP BY ";
$sql .= implode(', ', $this->ar_groupby);
}
if (count($this->ar_having) > 0)
{
$sql .= "\nHAVING ";
$sql .= implode("\n", $this->ar_having);
}
if (count($this->ar_orderby) > 0)
{
$sql .= "\nORDER BY ";
$sql .= implode(', ', $this->ar_orderby);
if ($this->ar_order !== FALSE)
{
$sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
}
}
if (is_numeric($this->ar_limit))
{
$sql .= "\n";
$sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
}
return $sql;
}
public function _object_to_array($object)
{
if ( ! is_object($object))
{
return $object;
}
$array = array();
foreach (get_object_vars($object) as $key => $val)
{
if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
{
$array[$key] = $val;
}
}
return $array;
}
public function _object_to_array_batch($object)
{
if ( ! is_object($object))
{
return $object;
}
$array = array();
$out = get_object_vars($object);
$fields = array_keys($out);
foreach ($fields as $val)
{
if ($val != '_parent_name')
{
$i = 0;
foreach ($out[$val] as $data)
{
$array[$i][$val] = $data;
$i++;
}
}
}
return $array;
}
public function start_cache()
{
$this->ar_caching = TRUE;
}
public function stop_cache()
{
$this->ar_caching = FALSE;
}
public function flush_cache()
{
$this->_reset_run(array(
'ar_cache_select' => array(),
'ar_cache_from' => array(),
'ar_cache_join' => array(),
'ar_cache_where' => array(),
'ar_cache_like' => array(),
'ar_cache_groupby' => array(),
'ar_cache_having' => array(),
'ar_cache_orderby' => array(),
'ar_cache_set' => array(),
'ar_cache_exists' => array(),
'ar_cache_no_escape' => array()
));
}
protected function _merge_cache()
{
if (count($this->ar_cache_exists) == 0)
{
return;
}
foreach ($this->ar_cache_exists as $val)
{
$ar_variable = 'ar_'.$val;
$ar_cache_var = 'ar_cache_'.$val;
if (count($this->$ar_cache_var) == 0)
{
continue;
}
$this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
}
if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
{
$this->_track_aliases($this->ar_from);
}
$this->ar_no_escape = $this->ar_cache_no_escape;
}
protected function _reset_run($ar_reset_items)
{
foreach ($ar_reset_items as $item => $default_value)
{
if ( ! in_array($item, $this->ar_store_array))
{
$this->$item = $default_value;
}
}
}
protected function _reset_select()
{
$ar_reset_items = array(
'ar_select' => array(),
'ar_from' => array(),
'ar_join' => array(),
'ar_where' => array(),
'ar_like' => array(),
'ar_groupby' => array(),
'ar_having' => array(),
'ar_orderby' => array(),
'ar_wherein' => array(),
'ar_aliased_tables' => array(),
'ar_no_escape' => array(),
'ar_distinct' => FALSE,
'ar_limit' => FALSE,
'ar_offset' => FALSE,
'ar_order' => FALSE,
);
$this->_reset_run($ar_reset_items);
}
protected function _reset_write()
{
$ar_reset_items = array(
'ar_set' => array(),
'ar_from' => array(),
'ar_where' => array(),
'ar_like' => array(),
'ar_orderby' => array(),
'ar_keys' => array(),
'ar_limit' => FALSE,
'ar_order' => FALSE
);
$this->_reset_run($ar_reset_items);
}
}
class CI_DB extends CI_DB_active_record{
}
//end db_active_rec
// class mysql_driver.php
class CI_DB_mysql_driver extends CI_DB {
var $dbdriver = 'mysql';
var $_escape_char = '`';
var $_like_escape_str = '';
var $_like_escape_chr = '';
var $delete_hack = TRUE;
var $_count_string = 'SELECT COUNT(*) AS ';
var $_random_keyword = ' RAND()'; // database specific random keyword
var $use_set_names;
function db_connect()
{
if ($this->port != '')
{
$this->hostname .= ':'.$this->port;
}
return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
}
function db_pconnect()
{
if ($this->port != '')
{
$this->hostname .= ':'.$this->port;
}
return @mysql_pconnect($this->hostname, $this->username, $this->password);
}
function reconnect()
{
if (mysql_ping($this->conn_id) === FALSE)
{
$this->conn_id = FALSE;
}
}
function db_select()
{
return @mysql_select_db($this->database, $this->conn_id);
}
function db_set_charset($charset, $collation)
{
if ( ! isset($this->use_set_names))
{
$this->use_set_names = (version_compare(PHP_VERSION, '5.2.3', '>=') && version_compare(mysql_get_server_info(), '5.0.7', '>=')) ? FALSE : TRUE;
}
if ($this->use_set_names === TRUE)
{
return @mysql_query("SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'", $this->conn_id);
}
else
{
return @mysql_set_charset($charset, $this->conn_id);
}
}
function _version()
{
return "SELECT version() AS ver";
}
function _execute($sql)
{
$sql = $this->_prep_query($sql);
$result=mysql_query($sql, $this->conn_id);
if(!$result){
die(mysql_error());
}
return $result;
}
function _prep_query($sql)
{
if ($this->delete_hack === TRUE)
{
if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
{
$sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
}
}
return $sql;
}
function trans_begin($test_mode = FALSE)
{
if ( ! $this->trans_enabled)
{
return TRUE;
}
if ($this->_trans_depth > 0)
{
return TRUE;
}
$this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
$this->simple_query('SET AUTOCOMMIT=0');
$this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
return TRUE;
}
function trans_commit()
{
if ( ! $this->trans_enabled)
{
return TRUE;
}
if ($this->_trans_depth > 0)
{
return TRUE;
}
$this->simple_query('COMMIT');
$this->simple_query('SET AUTOCOMMIT=1');
return TRUE;
}
function trans_rollback()
{
if ( ! $this->trans_enabled)
{
return TRUE;
}
if ($this->_trans_depth > 0)
{
return TRUE;
}
$this->simple_query('ROLLBACK');
$this->simple_query('SET AUTOCOMMIT=1');
return TRUE;
}
function escape_str($str, $like = FALSE)
{
if (is_array($str))
{
foreach ($str as $key => $val)
{
$str[$key] = $this->escape_str($val, $like);
}
return $str;
}
if (function_exists('mysql_real_escape_string') AND is_resource($this->conn_id))
{
$str = mysql_real_escape_string($str, $this->conn_id);
}
elseif (function_exists('mysql_escape_string'))
{
$str = mysql_escape_string($str);
}
else
{
$str = addslashes($str);
}
if ($like === TRUE)
{
$str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
}
return $str;
}
function affected_rows()
{
return @mysql_affected_rows($this->conn_id);
}
function insert_id()
{
return @mysql_insert_id($this->conn_id);
}
function count_all($table = '')
{
if ($table == '')
{
return 0;
}
$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
if ($query->num_rows() == 0)
{
return 0;
}
$row = $query->row();
$this->_reset_select();
return (int) $row->numrows;
}
function _list_tables($prefix_limit = FALSE)
{
$sql = "SHOW TABLES FROM ".$this->_escape_char.$this->database.$this->_escape_char;
if ($prefix_limit !== FALSE AND $this->dbprefix != '')
{
$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
}
return $sql;
}
function _list_columns($table = '')
{
return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE);
}
function _field_data($table)
{
return "DESCRIBE ".$table;
}
function _error_message()
{
return mysql_error($this->conn_id);
}
function _error_number()
{
return mysql_errno($this->conn_id);
}
function _escape_identifiers($item)
{
if ($this->_escape_char == '')
{
return $item;
}
foreach ($this->_reserved_identifiers as $id)
{
if (strpos($item, '.'.$id) !== FALSE)
{
$str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
}
}
if (strpos($item, '.') !== FALSE)
{
$str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
}
else
{
$str = $this->_escape_char.$item.$this->_escape_char;
}
return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
}
function _from_tables($tables)
{
if ( ! is_array($tables))
{
$tables = array($tables);
}
return '('.implode(', ', $tables).')';
}
function _insert($table, $keys, $values)
{
return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
}
function _replace($table, $keys, $values)
{
return "REPLACE INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
}
function _insert_batch($table, $keys, $values)
{
return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
}
function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
{
foreach ($values as $key => $val)
{
$valstr[] = $key . ' = ' . $val;
}
$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
$orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
$sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
$sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
$sql .= $orderby.$limit;
return $sql;
}
function _update_batch($table, $values, $index, $where = NULL)
{
$ids = array();
$where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
foreach ($values as $key => $val)
{
$ids[] = $val[$index];
foreach (array_keys($val) as $field)
{
if ($field != $index)
{
$final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
}
}
}
$sql = "UPDATE ".$table." SET ";
$cases = '';
foreach ($final as $k => $v)
{
$cases .= $k.' = CASE '."\n";
foreach ($v as $row)
{
$cases .= $row."\n";
}
$cases .= 'ELSE '.$k.' END, ';
}
$sql .= substr($cases, 0, -2);
$sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
return $sql;
}
function _truncate($table)
{
return "TRUNCATE ".$table;
}
function _delete($table, $where = array(), $like = array(), $limit = FALSE)
{
$conditions = '';
if (count($where) > 0 OR count($like) > 0)
{
$conditions = "\nWHERE ";
$conditions .= implode("\n", $this->ar_where);
if (count($where) > 0 && count($like) > 0)
{
$conditions .= " AND ";
}
$conditions .= implode("\n", $like);
}
$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
return "DELETE FROM ".$table.$conditions.$limit;
}
function _limit($sql, $limit, $offset)
{
if ($offset == 0)
{
$offset = '';
}
else
{
$offset .= ", ";
}
return $sql."LIMIT ".$offset.$limit;
}
function _close($conn_id)
{
@mysql_close($conn_id);
}
}
//end mysql_driver.php
function &DB($params = '', $active_record_override = NULL)
{
if (is_string($params) AND strpos($params, '://') === FALSE)
{
if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database.php'))
{
if ( ! file_exists($file_path = APPPATH.'config/database.php'))
{
show_error('The configuration file database.php does not exist.');
}
}
include($file_path);
if ( ! isset($db) OR count($db) == 0)
{
show_error('No database connection settings were found in the database config file.');
}
if ($params != '')
{
$active_group = $params;
}
if ( ! isset($active_group) OR ! isset($db[$active_group]))
{
show_error('You have specified an invalid database connection group.');
}
$params = $db[$active_group];
}
elseif (is_string($params))
{
if (($dns = @parse_url($params)) === FALSE)
{
show_error('Invalid DB Connection String');
}
$params = array(
'dbdriver' => $dns['scheme'],
'hostname' => (isset($dns['host'])) ? rawurldecode($dns['host']) : '',
'username' => (isset($dns['user'])) ? rawurldecode($dns['user']) : '',
'password' => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '',
'database' => (isset($dns['path'])) ? rawurldecode(substr($dns['path'], 1)) : ''
);
if (isset($dns['query']))
{
parse_str($dns['query'], $extra);
foreach ($extra as $key => $val)
{
// booleans please
if (strtoupper($val) == "TRUE")
{
$val = TRUE;
}
elseif (strtoupper($val) == "FALSE")
{
$val = FALSE;
}
$params[$key] = $val;
}
}
}
if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
{
show_error('You have not selected a database type to connect to.');
}
if ($active_record_override !== NULL)
{
$active_record = $active_record_override;
}
if ( ! isset($active_record) OR $active_record == TRUE)
{
if ( ! class_exists('CI_DB'))
{
eval('class CI_DB extends CI_DB_active_record { }');
}
}
else
{
if ( ! class_exists('CI_DB'))
{
eval('class CI_DB extends CI_DB_driver { }');
}
}
$driver = 'CI_DB_'.$params['dbdriver'].'_driver';
$DB = new $driver($params);
if ($DB->autoinit == TRUE)
{
$DB->initialize();
}
if (isset($params['stricton']) && $params['stricton'] == TRUE)
{
$DB->query('SET SESSION sql_mode="STRICT_ALL_TABLES"');
}
return $DB;
}
调用如下:
demo.php
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
define('BASEPATH', realpath(dirname(__FILE__)).'/');
include_once('mysqlclass/mysqlclass.php');
function log_message($a,$b){
var_dump($a);
var_dump($b);
}
function &load_database($params = '', $active_record_override = false)
{
$database =& DB($params, $active_record_override);
return $database;
}
$db =& load_database("mysql://root:123456@127.0.0.1/testa", true);
$tt=$db->get('user')->result_array();
var_dump($tt);
$db->where('id',1)->update('user',array('username'=>"123333"));
$db->where('id',2)->delete('user');
$str = $db->last_query();
var_dump($str);