summaryrefslogtreecommitdiffstats
path: root/system/database
diff options
context:
space:
mode:
Diffstat (limited to 'system/database')
-rw-r--r--system/database/DB.php24
-rw-r--r--system/database/DB_active_rec.php321
-rw-r--r--system/database/DB_cache.php12
-rw-r--r--system/database/DB_driver.php186
-rw-r--r--system/database/DB_forge.php323
-rw-r--r--system/database/DB_result.php50
-rw-r--r--system/database/DB_utility.php79
-rw-r--r--system/database/drivers/mssql/mssql_driver.php98
-rw-r--r--system/database/drivers/mssql/mssql_forge.php219
-rw-r--r--system/database/drivers/mssql/mssql_utility.php77
-rw-r--r--system/database/drivers/mysql/mysql_driver.php127
-rw-r--r--system/database/drivers/mysql/mysql_forge.php223
-rw-r--r--system/database/drivers/mysql/mysql_utility.php79
-rw-r--r--system/database/drivers/mysqli/mysqli_driver.php121
-rw-r--r--system/database/drivers/mysqli/mysqli_forge.php217
-rw-r--r--system/database/drivers/mysqli/mysqli_utility.php77
-rw-r--r--system/database/drivers/oci8/oci8_driver.php148
-rw-r--r--system/database/drivers/oci8/oci8_forge.php216
-rw-r--r--system/database/drivers/oci8/oci8_utility.php77
-rw-r--r--system/database/drivers/odbc/odbc_driver.php137
-rw-r--r--system/database/drivers/odbc/odbc_forge.php236
-rw-r--r--system/database/drivers/odbc/odbc_result.php2
-rw-r--r--system/database/drivers/odbc/odbc_utility.php101
-rw-r--r--system/database/drivers/postgre/postgre_driver.php129
-rw-r--r--system/database/drivers/postgre/postgre_forge.php219
-rw-r--r--system/database/drivers/postgre/postgre_utility.php77
-rw-r--r--system/database/drivers/sqlite/sqlite_driver.php129
-rw-r--r--system/database/drivers/sqlite/sqlite_forge.php233
-rw-r--r--system/database/drivers/sqlite/sqlite_result.php9
-rw-r--r--system/database/drivers/sqlite/sqlite_utility.php103
30 files changed, 3463 insertions, 586 deletions
diff --git a/system/database/DB.php b/system/database/DB.php
index 9055a22c4..425c8077b 100644
--- a/system/database/DB.php
+++ b/system/database/DB.php
@@ -29,14 +29,22 @@ function &DB($params = '', $active_record = FALSE)
{
include(APPPATH.'config/database'.EXT);
- $group = ($params == '') ? $active_group : $params;
+ 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($db[$group]))
+ if ( ! isset($active_group) OR ! isset($db[$active_group]))
{
- show_error('You have specified an invalid database connection group: '.$group);
+ show_error('You have specified an invalid database connection group.');
}
- $params = $db[$group];
+ $params = $db[$active_group];
}
// No DB specified yet? Beat them senseless...
@@ -78,7 +86,13 @@ function &DB($params = '', $active_record = FALSE)
// Instantiate the DB adapter
$driver = 'CI_DB_'.$params['dbdriver'].'_driver';
- $DB =& new $driver($params);
+ $DB =& new $driver($params);
+
+ if ($DB->autoinit == TRUE)
+ {
+ $DB->initialize();
+ }
+
return $DB;
}
diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php
index e697cac77..3cc65aff4 100644
--- a/system/database/DB_active_rec.php
+++ b/system/database/DB_active_rec.php
@@ -44,7 +44,6 @@ class CI_DB_active_record extends CI_DB_driver {
var $ar_wherein = array();
var $ar_aliased_tables = array();
-
/**
* Select
*
@@ -54,7 +53,7 @@ class CI_DB_active_record extends CI_DB_driver {
* @param string
* @return object
*/
- function select($select = '*')
+ function select($select = '*', $protect_identifiers = TRUE)
{
if (is_string($select))
{
@@ -64,16 +63,137 @@ class CI_DB_active_record extends CI_DB_driver {
foreach ($select as $val)
{
$val = trim($val);
+
+ if ($val != '*' && $protect_identifiers !== FALSE)
+ {
+ $val = $this->_protect_identifiers($val);
+ }
if ($val != '')
+ {
$this->ar_select[] = $val;
+ }
}
return $this;
}
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Select Max
+ *
+ * Generates a SELECT MAX(field) portion of a query
+ *
+ * @access public
+ * @param string the field
+ * @param string an alias
+ * @return object
+ */
+ function select_max($select = '', $alias='')
+ {
+ if (!is_string($select) || $select == '')
+ {
+ $this->display_error('db_invalid_query');
+ }
+
+ $alias = ($alias != '') ? $alias : $select;
+
+ $sql = 'MAX('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
+
+ $this->ar_select[] = $sql;
+
+ return $this;
+
+ return $this;
+ }
// --------------------------------------------------------------------
/**
+ * Select Min
+ *
+ * Generates a SELECT MIN(field) portion of a query
+ *
+ * @access public
+ * @param string the field
+ * @param string an alias
+ * @return object
+ */
+ function select_min($select = '', $alias='')
+ {
+ if (!is_string($select) || $select == '')
+ {
+ $this->display_error('db_invalid_query');
+ }
+
+ $alias = ($alias != '') ? $alias : $select;
+
+ $sql = 'MIN('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
+
+ $this->ar_select[] = $sql;
+
+ return $this;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Select Average
+ *
+ * Generates a SELECT AVG(field) portion of a query
+ *
+ * @access public
+ * @param string the field
+ * @param string an alias
+ * @return object
+ */
+ function select_avg($select = '', $alias='')
+ {
+ if (!is_string($select) || $select == '')
+ {
+ $this->display_error('db_invalid_query');
+ }
+
+ $alias = ($alias != '') ? $alias : $select;
+
+ $sql = 'AVG('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
+
+ $this->ar_select[] = $sql;
+
+ return $this;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Select Sum
+ *
+ * Generates a SELECT SUM(field) portion of a query
+ *
+ * @access public
+ * @param string the field
+ * @param string an alias
+ * @return object
+ */
+ function select_sum($select = '', $alias='')
+ {
+ if (!is_string($select) || $select == '')
+ {
+ $this->display_error('db_invalid_query');
+ }
+
+ $alias = ($alias != '') ? $alias : $select;
+
+ $sql = 'SUM('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
+
+ $this->ar_select[] = $sql;
+
+ return $this;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* DISTINCT
*
* Sets a flag which tells the query string compiler to add DISTINCT
@@ -103,8 +223,7 @@ class CI_DB_active_record extends CI_DB_driver {
{
foreach ((array)$from as $val)
{
- $this->_track_aliases($val);
- $this->ar_from[] = $this->dbprefix.$val;
+ $this->ar_from[] = $this->_protect_identifiers($this->_track_aliases($val));
}
return $this;
@@ -142,17 +261,16 @@ class CI_DB_active_record extends CI_DB_driver {
// If a DB prefix is used we might need to add it to the column names
if ($this->dbprefix)
{
+ $this->_track_aliases($table);
+
// First we remove any existing prefixes in the condition to avoid duplicates
$cond = preg_replace('|('.$this->dbprefix.')([\w\.]+)([\W\s]+)|', "$2$3", $cond);
// Next we add the prefixes to the condition
$cond = preg_replace('|([\w\.]+)([\W\s]+)(.+)|', $this->dbprefix . "$1$2" . $this->dbprefix . "$3", $cond);
-
- $this->_track_aliases($table);
-
}
- $this->ar_join[] = $type.'JOIN '.$this->dbprefix.$table.' ON '.$cond;
+ $this->ar_join[] = $type.'JOIN '.$this->_protect_identifiers($this->dbprefix.$table, TRUE).' ON '.$cond;
return $this;
}
@@ -169,9 +287,9 @@ class CI_DB_active_record extends CI_DB_driver {
* @param mixed
* @return object
*/
- function where($key, $value = NULL)
+ function where($key, $value = NULL, $escape = TRUE)
{
- return $this->_where($key, $value, 'AND ');
+ return $this->_where($key, $value, 'AND ', $escape);
}
// --------------------------------------------------------------------
@@ -187,9 +305,9 @@ class CI_DB_active_record extends CI_DB_driver {
* @param mixed
* @return object
*/
- function or_where($key, $value = NULL)
+ function or_where($key, $value = NULL, $escape = TRUE)
{
- return $this->_where($key, $value, 'OR ');
+ return $this->_where($key, $value, 'OR ', $escape);
}
// --------------------------------------------------------------------
@@ -199,9 +317,9 @@ class CI_DB_active_record extends CI_DB_driver {
* this function is here for backwards compatibility, as
* orwhere() has been deprecated
*/
- function orwhere($key, $value = NULL)
+ function orwhere($key, $value = NULL, $escape = TRUE)
{
- return $this->or_where($key, $value);
+ return $this->or_where($key, $value, $escape);
}
// --------------------------------------------------------------------
@@ -217,7 +335,7 @@ class CI_DB_active_record extends CI_DB_driver {
* @param string
* @return object
*/
- function _where($key, $value = NULL, $type = 'AND ')
+ function _where($key, $value = NULL, $type = 'AND ', $escape = TRUE)
{
if ( ! is_array($key))
{
@@ -226,10 +344,9 @@ class CI_DB_active_record extends CI_DB_driver {
foreach ($key as $k => $v)
{
-
$prefix = (count($this->ar_where) == 0) ? '' : $type;
- if (is_null($key[$k]))
+ if ( ! $this->_has_operator($k) && is_null($key[$k]))
{
// value appears not to have been set, assign the test to IS NULL
$k .= ' IS NULL';
@@ -237,12 +354,25 @@ class CI_DB_active_record extends CI_DB_driver {
if ( ! is_null($v))
{
-
+
+ if ($escape === TRUE)
+ {
+ // exception for "field<=" keys
+ if ($this->_has_operator($k))
+ {
+ $k = preg_replace("/([A-Za-z_0-9]+)/", $this->_protect_identifiers('$1'), $k);
+ }
+ else
+ {
+ $k = $this->_protect_identifiers($k);
+ }
+ }
+
if ( ! $this->_has_operator($k))
{
$k .= ' =';
}
-
+
$v = ' '.$this->escape($v);
}
@@ -358,7 +488,7 @@ class CI_DB_active_record extends CI_DB_driver {
$prefix = (count($this->ar_where) == 0) ? '' : $type;
- $this->ar_where[] = $prefix.$key.$not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
+ $this->ar_where[] = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
return $this;
}
@@ -470,6 +600,8 @@ class CI_DB_active_record extends CI_DB_driver {
foreach ($field as $k => $v)
{
+ $k = $this->_protect_identifiers($k);
+
$prefix = (count($this->ar_like) == 0) ? '' : $type;
$v = $this->escape_str($v);
@@ -511,7 +643,7 @@ class CI_DB_active_record extends CI_DB_driver {
$val = trim($val);
if ($val != '')
- $this->ar_groupby[] = $this->dbprefix.$val;
+ $this->ar_groupby[] = $this->_protect_identifiers($val);
}
return $this;
}
@@ -617,7 +749,7 @@ class CI_DB_active_record extends CI_DB_driver {
$direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
}
- $this->ar_orderby[] = $orderby.$direction;
+ $this->ar_orderby[] = $this->_protect_identifiers($orderby).$direction;
return $this;
}
@@ -676,9 +808,10 @@ class CI_DB_active_record extends CI_DB_driver {
* @access public
* @param mixed
* @param string
+ * @param boolean
* @return object
*/
- function set($key, $value = '')
+ function set($key, $value = '', $escape = TRUE)
{
$key = $this->_object_to_array($key);
@@ -689,7 +822,15 @@ class CI_DB_active_record extends CI_DB_driver {
foreach ($key as $k => $v)
{
- $this->ar_set[$k] = $this->escape($v);
+ if ($escape === FALSE)
+ {
+ $this->ar_set[$this->_protect_identifiers($k)] = $v;
+ }
+ else
+ {
+ $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
+ }
+
}
return $this;
@@ -729,8 +870,6 @@ class CI_DB_active_record extends CI_DB_driver {
return $result;
}
- // --------------------------------------------------------------------
-
/**
* "Count All Results" query
*
@@ -749,7 +888,7 @@ class CI_DB_active_record extends CI_DB_driver {
$this->from($table);
}
- $sql = $this->_compile_select($this->_count_string);
+ $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
$query = $this->query($sql);
$this->_reset_select();
@@ -854,8 +993,8 @@ class CI_DB_active_record extends CI_DB_driver {
$table = $this->ar_from[0];
}
-
- $sql = $this->_insert($this->dbprefix.$table, array_keys($this->ar_set), array_values($this->ar_set));
+
+ $sql = $this->_insert($this->_protect_identifiers($this->dbprefix.$table), array_keys($this->ar_set), array_values($this->ar_set));
$this->_reset_write();
return $this->query($sql);
@@ -914,9 +1053,89 @@ class CI_DB_active_record extends CI_DB_driver {
$this->limit($limit);
}
- $sql = $this->_update($this->dbprefix.$table, $this->ar_set, $this->ar_where, $this->ar_limit);
+ $sql = $this->_update($this->_protect_identifiers($this->dbprefix.$table), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
+
+ $this->_reset_write();
+ return $this->query($sql);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Empty Table
+ *
+ * Compiles a delete string and runs "DELETE FROM table"
+ *
+ * @access public
+ * @param string the table to empty
+ * @return object
+ */
+ 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($this->dbprefix.$table);
+ }
+
+
+ $sql = $this->_delete($table);
+
+ $this->_reset_write();
+ return $this->query($sql);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Truncate
+ *
+ * Compiles a truncate string and runs the query
+ * If the database does not support the truncate() command
+ * This function maps to "DELETE FROM table"
+ *
+ * @access public
+ * @param string the table to truncate
+ * @return object
+ */
+ 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($this->dbprefix.$table);
+ }
+
+
+ $sql = $this->_truncate($table);
+
$this->_reset_write();
+
return $this->query($sql);
}
@@ -946,19 +1165,23 @@ class CI_DB_active_record extends CI_DB_driver {
}
return FALSE;
}
-
+
$table = $this->ar_from[0];
}
-
- if (is_array($table))
+ elseif (is_array($table))
{
foreach($table as $single_table)
{
- $this->delete($this->dbprefix.$single_table, $where, $limit, FALSE);
+ $this->delete($single_table, $where, $limit, FALSE);
}
+
$this->_reset_write();
return;
}
+ else
+ {
+ $table = $this->_protect_identifiers($this->dbprefix.$table);
+ }
if ($where != '')
{
@@ -970,21 +1193,23 @@ class CI_DB_active_record extends CI_DB_driver {
$this->limit($limit);
}
- if (count($this->ar_where) == 0)
+ if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
{
if ($this->db_debug)
{
return $this->display_error('db_del_must_use_where');
}
+
return FALSE;
}
- $sql = $this->_delete($this->dbprefix.$table, $this->ar_where, $this->ar_limit);
+ $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
if ($reset_data)
{
$this->_reset_write();
}
+
return $this->query($sql);
}
@@ -1038,9 +1263,9 @@ class CI_DB_active_record extends CI_DB_driver {
if (strpos($table, " ") !== FALSE)
{
// if the alias is written with the AS keyowrd, get it out
- $table = preg_replace('/AS/i', '', $table);
+ $table = preg_replace('/ AS /i', ' ', $table);
- $this->ar_aliased_tables[] = trim(strrchr($table, " ") . '.');
+ $this->ar_aliased_tables[] = trim(strrchr($table, " "));
}
return $this->dbprefix.$table;
@@ -1059,23 +1284,20 @@ class CI_DB_active_record extends CI_DB_driver {
*/
function _filter_table_aliases($statements)
{
- $filter_tables_with_aliases = array();
+ $statements_without_aliases = array();
- foreach ($statements as $statement)
+ foreach ($statements as $k => $v)
{
- $tables_with_dbprefix = array();
-
- foreach ($this->ar_aliased_tables as $k => $v)
+ foreach ($this->ar_aliased_tables as $table)
{
- $tables_with_dbprefix[$k] = '/'.$this->dbprefix.str_replace('.', '', $v).'\./';
+ $statement = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $v); // makes `table.field`
+ $statement = str_replace(array($this->dbprefix.$table, '.'), array($table, $this->_protect_identifiers('.')), $statement);
}
- $statement = preg_replace($tables_with_dbprefix, $this->ar_aliased_tables, $statement);
-
- $filter_tables_with_aliases[] = $statement;
+ $statements[$k] = $statement;
}
- return $filter_tables_with_aliases;
+ return $statements;
}
// --------------------------------------------------------------------
@@ -1254,7 +1476,10 @@ class CI_DB_active_record extends CI_DB_driver {
$this->ar_set = array();
$this->ar_from = array();
$this->ar_where = array();
+ $this->ar_like = array();
$this->ar_limit = FALSE;
+ $this->ar_order = FALSE;
+ $this->ar_orderby = array();
}
}
diff --git a/system/database/DB_cache.php b/system/database/DB_cache.php
index 1e7410708..77b951500 100644
--- a/system/database/DB_cache.php
+++ b/system/database/DB_cache.php
@@ -63,14 +63,12 @@ class CI_DB_Cache {
// Add a trailing slash to the path if needed
$path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
-
- if ( ! is_dir($path) OR ! is_writable($path))
+
+ // Load the file helper
+ $this->CI->load->helper('file');
+
+ if ( ! is_dir($path) OR ! is_really_writable($path))
{
- if ($this->CI->db->db_debug)
- {
- return $this->CI->db->display_error('db_invalid_cache_path');
- }
-
// If the path is wrong we'll turn off caching
return $this->CI->db->cache_off();
}
diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php
index a7f03e3e5..c2fa70a72 100644
--- a/system/database/DB_driver.php
+++ b/system/database/DB_driver.php
@@ -36,6 +36,8 @@ class CI_DB_driver {
var $database;
var $dbdriver = 'mysql';
var $dbprefix = '';
+ var $autoinit = TRUE; // Whether to automatically initialize the DB
+ var $swap_pre = '';
var $port = '';
var $pconnect = FALSE;
var $conn_id = FALSE;
@@ -76,41 +78,11 @@ class CI_DB_driver {
*/
function CI_DB_driver($params)
{
- $this->initialize($params);
- log_message('debug', 'Database Driver Class Initialized');
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Initialize Database Settings
- *
- * @access private Called by the constructor
- * @param mixed
- * @return void
- */
- function initialize($params = '')
- {
if (is_array($params))
{
- $defaults = array(
- 'hostname' => '',
- 'username' => '',
- 'password' => '',
- 'database' => '',
- 'conn_id' => FALSE,
- 'dbdriver' => 'mysql',
- 'dbprefix' => '',
- 'port' => '',
- 'pconnect' => FALSE,
- 'db_debug' => FALSE,
- 'cachedir' => '',
- 'cache_on' => FALSE
- );
-
- foreach ($defaults as $key => $val)
+ foreach ($params as $key => $val)
{
- $this->$key = ( ! isset($params[$key])) ? $val : $params[$key];
+ $this->$key = $val;
}
}
elseif (strpos($params, '://'))
@@ -131,7 +103,21 @@ class CI_DB_driver {
$this->password = ( ! isset($dsn['pass'])) ? '' : rawurldecode($dsn['pass']);
$this->database = ( ! isset($dsn['path'])) ? '' : rawurldecode(substr($dsn['path'], 1));
}
-
+
+ log_message('debug', 'Database Driver Class Initialized');
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Initialize Database Settings
+ *
+ * @access private Called by the constructor
+ * @param mixed
+ * @return void
+ */
+ function initialize($create_db = FALSE)
+ {
// If an existing DB connection resource is supplied
// there is no need to connect and select the database
if (is_resource($this->conn_id))
@@ -159,6 +145,46 @@ class CI_DB_driver {
{
if ( ! $this->db_select())
{
+ // Should we attempt to create the database?
+ if ($create_db == TRUE)
+ {
+ // Load the DB utility class
+ $CI =& get_instance();
+ $CI->load->dbutil();
+
+ // Create the DB
+ if ( ! $CI->dbutil->create_database($this->database))
+ {
+ log_message('error', 'Unable to create database: '.$this->database);
+
+ if ($this->db_debug)
+ {
+ $this->display_error('db_unable_to_create', $this->database);
+ }
+ return FALSE;
+ }
+ else
+ {
+ // In the event the DB was created we need to select it
+ if ($this->db_select())
+ {
+ 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;
+ }
+ }
+ }
+
log_message('error', 'Unable to select database: '.$this->database);
if ($this->db_debug)
@@ -167,6 +193,18 @@ class CI_DB_driver {
}
return FALSE;
}
+
+ 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;
@@ -211,8 +249,7 @@ class CI_DB_driver {
}
$query = $this->query($sql);
- $row = $query->row();
- return $row->ver;
+ return $query->row('ver');
}
// --------------------------------------------------------------------
@@ -242,6 +279,12 @@ class CI_DB_driver {
}
return FALSE;
}
+
+ // Verify table prefix and replace if necessary
+ 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);
+ }
// Is query caching enabled? If the query is a "read type"
// we will load the caching class and return the previously
@@ -269,7 +312,7 @@ class CI_DB_driver {
{
$this->queries[] = $sql;
}
-
+
// Start the Query Timer
$time_start = list($sm, $ss) = explode(' ', microtime());
@@ -291,7 +334,7 @@ class CI_DB_driver {
);
}
- return FALSE;
+ return FALSE;
}
// Stop and aggregate the query time results
@@ -329,6 +372,7 @@ class CI_DB_driver {
$RES = new $driver();
$RES->conn_id = $this->conn_id;
$RES->result_id = $this->result_id;
+ $RES->num_rows = $RES->num_rows();
if ($this->dbdriver == 'oci8')
{
@@ -336,9 +380,7 @@ class CI_DB_driver {
$RES->curs_id = NULL;
$RES->limit_used = $this->limit_used;
}
-
- $RES->num_rows = $RES->num_rows();
-
+
// Is query caching enabled? If so, we'll serialize the
// result object and save it to a cache file.
if ($this->cache_on == TRUE AND $this->_cache_init())
@@ -593,6 +635,23 @@ class CI_DB_driver {
// --------------------------------------------------------------------
/**
+ * Protect Identifiers
+ *
+ * This function adds backticks if appropriate based on db type
+ *
+ * @access private
+ * @param mixed the item to escape
+ * @param boolean only affect the first word
+ * @return mixed the item with backticks
+ */
+ function protect_identifiers($item, $first_word_only = FALSE)
+ {
+ return $this->_protect_identifiers($item, $first_word_only = FALSE);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* "Smart" Escape String
*
* Escapes data based on type
@@ -649,7 +708,7 @@ class CI_DB_driver {
* @access public
* @return array
*/
- function list_tables()
+ function list_tables($constrain_by_prefix = FALSE)
{
// Is there a cached result?
if (isset($this->data_cache['table_names']))
@@ -657,7 +716,7 @@ class CI_DB_driver {
return $this->data_cache['table_names'];
}
- if (FALSE === ($sql = $this->_list_tables()))
+ if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
{
if ($this->db_debug)
{
@@ -697,7 +756,7 @@ class CI_DB_driver {
*/
function table_exists($table_name)
{
- return ( ! in_array($this->dbprefix.$table_name, $this->list_tables())) ? FALSE : TRUE;
+ return ( ! in_array($this->prep_tablename($table_name), $this->list_tables())) ? FALSE : TRUE;
}
// --------------------------------------------------------------------
@@ -726,7 +785,7 @@ class CI_DB_driver {
return FALSE;
}
- if (FALSE === ($sql = $this->_list_columns($this->dbprefix.$table)))
+ if (FALSE === ($sql = $this->_list_columns($this->prep_tablename($table))))
{
if ($this->db_debug)
{
@@ -798,7 +857,7 @@ class CI_DB_driver {
return FALSE;
}
- $query = $this->query($this->_field_data($this->dbprefix.$table));
+ $query = $this->query($this->_field_data($this->prep_tablename($table)));
return $query->field_data();
}
@@ -822,9 +881,10 @@ class CI_DB_driver {
$fields[] = $key;
$values[] = $this->escape($val);
}
-
- return $this->_insert($this->dbprefix.$table, $fields, $values);
- }
+
+
+ return $this->_insert($this->prep_tablename($table), $fields, $values);
+ }
// --------------------------------------------------------------------
@@ -859,7 +919,7 @@ class CI_DB_driver {
{
$prefix = (count($dest) == 0) ? '' : ' AND ';
- if ($val != '')
+ if ($val !== '')
{
if ( ! $this->_has_operator($key))
{
@@ -873,12 +933,35 @@ class CI_DB_driver {
}
}
- return $this->_update($this->dbprefix.$table, $fields, $dest);
+ return $this->_update($this->prep_tablename($table), $fields, $dest);
}
// --------------------------------------------------------------------
/**
+ * Prep the table name - simply adds the table prefix if needed
+ *
+ * @access public
+ * @param string the table name
+ * @return string
+ */
+ function prep_tablename($table = '')
+ {
+ // Do we need to add the table prefix?
+ if ($this->dbprefix != '')
+ {
+ if (substr($table, 0, strlen($this->dbprefix)) != $this->dbprefix)
+ {
+ $table = $this->dbprefix.$table;
+ }
+ }
+
+ return $table;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Enables a native PHP function to be run, using a platform agnostic wrapper.
*
* @access public
@@ -1013,7 +1096,6 @@ class CI_DB_driver {
return TRUE;
}
-
// --------------------------------------------------------------------
/**
@@ -1044,6 +1126,7 @@ class CI_DB_driver {
*/
function display_error($error = '', $swap = '', $native = FALSE)
{
+// $LANG = new CI_Lang();
$LANG = new CI_Language();
$LANG->load('db');
@@ -1060,6 +1143,7 @@ class CI_DB_driver {
if ( ! class_exists('CI_Exceptions'))
{
+// include(BASEPATH.'core/Exceptions'.EXT);
include(BASEPATH.'libraries/Exceptions'.EXT);
}
diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php
new file mode 100644
index 000000000..d02589457
--- /dev/null
+++ b/system/database/DB_forge.php
@@ -0,0 +1,323 @@
+<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * Code Igniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package CodeIgniter
+ * @author Rick Ellis
+ * @copyright Copyright (c) 2006, EllisLab, Inc.
+ * @license http://www.codeigniter.com/user_guide/license.html
+ * @link http://www.codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * Database Utility Class
+ *
+ * @category Database
+ * @author Rick Ellis
+ * @link http://www.codeigniter.com/user_guide/database/
+ */
+class CI_DB_forge {
+
+ var $fields = array();
+ var $keys = array();
+ var $primary_keys = array();
+ var $db_char_set = '';
+
+ /**
+ * Constructor
+ *
+ * Grabs the CI super object instance so we can access it.
+ *
+ */
+ function CI_DB_forge()
+ {
+ // Assign the main database object to $this->db
+ $CI =& get_instance();
+ $this->db =& $CI->db;
+
+ log_message('debug', "Database Forge Class Initialized");
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Create database
+ *
+ * @access public
+ * @param string the database name
+ * @return bool
+ */
+ function create_database($db_name)
+ {
+ $sql = $this->_create_database($db_name);
+
+ if (is_bool($sql))
+ {
+ return $sql;
+ }
+
+ return $this->db->query($sql);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop database
+ *
+ * @access public
+ * @param string the database name
+ * @return bool
+ */
+ function drop_database($db_name)
+ {
+ $sql = $this->_drop_database($db_name);
+
+ if (is_bool($sql))
+ {
+ return $sql;
+ }
+
+ return $this->db->query($sql);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Add Key
+ *
+ * @access public
+ * @param string key
+ * @param string type
+ * @return void
+ */
+ function add_key($key = '', $primary = FALSE)
+ {
+ if ($key == '')
+ {
+ show_error('Key information is required for that operation.');
+ }
+
+ if ($primary === TRUE)
+ {
+ $this->primary_keys[] = $key;
+ }
+ else
+ {
+ $this->keys[] = $key;
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Add Field
+ *
+ * @access public
+ * @param string collation
+ * @return void
+ */
+ function add_field($field = '')
+ {
+ if ($field == '')
+ {
+ show_error('Field information is required.');
+ }
+
+ if (is_string($field))
+ {
+ if ($field == 'id')
+ {
+ $this->fields[] = array('id' => array(
+ 'type' => 'INT',
+ 'constraint' => 9,
+ 'auto_increment' => TRUE
+ )
+ );
+ $this->add_key('id', TRUE);
+ }
+ else
+ {
+ if (strpos($field, ' ') === FALSE)
+ {
+ show_error('Field information is required for that operation.');
+ }
+
+ $this->fields[] = $field;
+ }
+ }
+
+ if (is_array($field))
+ {
+ $this->fields = array_merge($this->fields, $field);
+ }
+
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Create Table
+ *
+ * @access public
+ * @param string the table name
+ * @return bool
+ */
+ function create_table($table = '', $if_not_exists = FALSE)
+ {
+ if ($table == '')
+ {
+ show_error('A table name is required for that operation.');
+ }
+
+ if (count($this->fields) == 0)
+ {
+ show_error('Field information is required.');
+ }
+
+ $sql = $this->_create_table($table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);
+
+ $this->_reset();
+ return $this->db->query($sql);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop Table
+ *
+ * @access public
+ * @param string the table name
+ * @return bool
+ */
+ function drop_table($table_name)
+ {
+ $sql = $this->_drop_table($table_name);
+
+ if (is_bool($sql))
+ {
+ return $sql;
+ }
+
+ return $this->db->query($sql);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Column Add
+ *
+ * @access public
+ * @param string the table name
+ * @param string the column name
+ * @param string the column definition
+ * @return bool
+ */
+ function add_column($table = '', $field = array(), $after_field = '')
+ {
+ if ($table == '')
+ {
+ show_error('A table name is required for that operation.');
+ }
+
+ // add field info into field array, but we can only do one at a time
+ // so only grab the first field in the event there are more then one
+ $this->add_field(array_slice($field, 0, 1));
+
+ if (count($this->fields) == 0)
+ {
+ show_error('Field information is required.');
+ }
+
+ $sql = $this->_alter_table('ADD', $table, $this->fields, $after_field);
+
+ $this->_reset();
+ return $this->db->query($sql);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Column Drop
+ *
+ * @access public
+ * @param string the table name
+ * @param string the column name
+ * @return bool
+ */
+ function drop_column($table = '', $column_name = '')
+ {
+
+ if ($table == '')
+ {
+ show_error('A table name is required for that operation.');
+ }
+
+ if ($column_name == '')
+ {
+ show_error('A column name is required for that operation.');
+ }
+
+ $sql = $this->_alter_table('DROP', $table, $column_name);
+
+ return $this->db->query($sql);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Column Modify
+ *
+ * @access public
+ * @param string the table name
+ * @param string the column name
+ * @param string the column definition
+ * @return bool
+ */
+ function modify_column($table = '', $field = array())
+ {
+
+ if ($table == '')
+ {
+ show_error('A table name is required for that operation.');
+ }
+
+ // add field info into field array, but we can only do one at a time
+ // so only grab the first field in the event there are more then one
+ $this->add_field(array_slice($field, 0, 1));
+
+ if (count($this->fields) == 0)
+ {
+ show_error('Field information is required.');
+ }
+
+ $sql = $this->_alter_table('CHANGE', $table, $this->fields);
+
+ $this->_reset();
+ return $this->db->query($sql);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Reset
+ *
+ * Resets table creation vars
+ *
+ * @access private
+ * @return void
+ */
+ function _reset()
+ {
+ $this->fields = array();
+ $this->keys = array();
+ $this->primary_keys = array();
+ }
+
+}
+?> \ No newline at end of file
diff --git a/system/database/DB_result.php b/system/database/DB_result.php
index b438ad1d8..36eddd840 100644
--- a/system/database/DB_result.php
+++ b/system/database/DB_result.php
@@ -34,6 +34,7 @@ class CI_DB_result {
var $result_object = array();
var $current_row = 0;
var $num_rows = 0;
+ var $row_data = NULL;
/**
@@ -118,17 +119,66 @@ class CI_DB_result {
* Query result. Acts as a wrapper function for the following functions.
*
* @access public
+ * @param string
* @param string can be "object" or "array"
* @return mixed either a result object or array
*/
function row($n = 0, $type = 'object')
{
+ if ( ! is_numeric($n))
+ {
+ // We cache the row data for subsequent uses
+ if ( ! is_array($this->row_data))
+ {
+ $this->row_data = $this->row_array(0);
+ }
+
+ if (isset($this->row_data[$n]))
+ {
+ return $this->row_data[$n];
+ }
+ // reset the $n variable if the result was not achieved
+ $n = 0;
+ }
+
return ($type == 'object') ? $this->row_object($n) : $this->row_array($n);
}
// --------------------------------------------------------------------
/**
+ * Assigns an item into a particular column slot
+ *
+ * @access public
+ * @return object
+ */
+ function set_row($key, $value = NULL)
+ {
+ // We cache the row data for subsequent uses
+ if ( ! is_array($this->row_data))
+ {
+ $this->row_data = $this->row_array(0);
+ }
+
+ if (is_array($key))
+ {
+ foreach ($key as $k => $v)
+ {
+ $this->row_data[$k] = $v;
+ }
+
+ return;
+ }
+
+ if ($key != '' AND ! is_null($value))
+ {
+ $this->row_data[$key] = $value;
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Returns a single result row - object version
*
* @access public
diff --git a/system/database/DB_utility.php b/system/database/DB_utility.php
index e9caf9f3e..372c88f1f 100644
--- a/system/database/DB_utility.php
+++ b/system/database/DB_utility.php
@@ -6,7 +6,7 @@
*
* @package CodeIgniter
* @author Rick Ellis
- * @copyright Copyright (c) 2006, pMachine, Inc.
+ * @copyright Copyright (c) 2006, EllisLab, Inc.
* @license http://www.codeigniter.com/user_guide/license.html
* @link http://www.codeigniter.com
* @since Version 1.0
@@ -22,10 +22,10 @@
* @author Rick Ellis
* @link http://www.codeigniter.com/user_guide/database/
*/
-class CI_DB_utility {
+class CI_DB_utility extends CI_DB_forge {
var $db;
- var $data_cache = array();
+ var $data_cache = array();
/**
* Constructor
@@ -45,48 +45,6 @@ class CI_DB_utility {
// --------------------------------------------------------------------
/**
- * Create database
- *
- * @access public
- * @param string the database name
- * @return bool
- */
- function create_database($db_name)
- {
- $sql = $this->_create_database($db_name);
-
- if (is_bool($sql))
- {
- return $sql;
- }
-
- return $this->db->query($sql);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Drop database
- *
- * @access public
- * @param string the database name
- * @return bool
- */
- function drop_database($db_name)
- {
- $sql = $this->_drop_database($db_name);
-
- if (is_bool($sql))
- {
- return $sql;
- }
-
- return $this->db->query($sql);
- }
-
- // --------------------------------------------------------------------
-
- /**
* List databases
*
* @access public
@@ -129,7 +87,7 @@ class CI_DB_utility {
if (is_bool($sql))
{
- return $sql;
+ show_error('db_must_use_set');
}
$query = $this->db->query($sql);
@@ -180,13 +138,12 @@ class CI_DB_utility {
// --------------------------------------------------------------------
/**
- * Optimize Table
+ * Repair Table
*
* @access public
* @param string the table name
* @return bool
*/
-
function repair_table($table_name)
{
$sql = $this->_repair_table($table_name);
@@ -203,28 +160,7 @@ class CI_DB_utility {
$res = $query->result_array();
return current($res);
}
-
- // --------------------------------------------------------------------
-
- /**
- * Drop Table
- *
- * @access public
- * @param string the table name
- * @return bool
- */
- function drop_table($table_name)
- {
- $sql = $this->_drop_table($table_name);
-
- if (is_bool($sql))
- {
- return $sql;
- }
- return $this->db->query($sql);
- }
-
// --------------------------------------------------------------------
/**
@@ -445,11 +381,6 @@ class CI_DB_utility {
}
-
-
-
-
-
}
?> \ No newline at end of file
diff --git a/system/database/drivers/mssql/mssql_driver.php b/system/database/drivers/mssql/mssql_driver.php
index 044fb3ced..06a508e71 100644
--- a/system/database/drivers/mssql/mssql_driver.php
+++ b/system/database/drivers/mssql/mssql_driver.php
@@ -35,7 +35,8 @@ class CI_DB_mssql_driver extends CI_DB {
* database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $_count_string = "SELECT COUNT(*) AS numrows ";
+ var $_count_string = "SELECT COUNT(*) AS ";
+ var $_random_keyword = ' ASC'; // not currently supported
/**
* Non-persistent database connection
@@ -77,6 +78,22 @@ class CI_DB_mssql_driver extends CI_DB {
// --------------------------------------------------------------------
/**
+ * Set client character set
+ *
+ * @access public
+ * @param string
+ * @param string
+ * @return resource
+ */
+ function db_set_charset($charset, $collation)
+ {
+ // TODO - add support if needed
+ return TRUE;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Execute the query
*
* @access private called by the base class
@@ -297,11 +314,21 @@ class CI_DB_mssql_driver extends CI_DB {
* Generates a platform-specific query string so that the table names can be fetched
*
* @access private
+ * @param boolean
* @return string
*/
- function _list_tables()
+ function _list_tables($prefix_limit = FALSE)
{
- return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
+ $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
+
+ // for future compatibility
+ if ($prefix_limit !== FALSE AND $this->dbprefix != '')
+ {
+ //$sql .= " LIKE '".$this->dbprefix."%'";
+ return FALSE; // not currently supported
+ }
+
+ return $sql;
}
// --------------------------------------------------------------------
@@ -393,6 +420,25 @@ class CI_DB_mssql_driver extends CI_DB {
// --------------------------------------------------------------------
/**
+ * Protect Identifiers
+ *
+ * This function adds backticks if appropriate based on db type
+ *
+ * @access private
+ * @param mixed the item(s)
+ * @param boolean should spaces be backticked
+ * @param boolean only affect the first word
+ * @return mixed the item with backticks
+ */
+ function _protect_identifiers($item, $affect_spaces = TRUE, $first_word_only = FALSE)
+ {
+ // MSSQL doesn't use backticks
+ return $item;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Insert statement
*
* Generates a platform-specific insert string from the supplied data
@@ -419,9 +465,11 @@ class CI_DB_mssql_driver extends CI_DB {
* @param string the table name
* @param array the update data
* @param array the where clause
+ * @param array the orderby clause
+ * @param array the limit clause
* @return string
*/
- function _update($table, $values, $where, $limit = FALSE)
+ function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
{
foreach($values as $key => $val)
{
@@ -429,8 +477,29 @@ class CI_DB_mssql_driver extends CI_DB {
}
$limit = (!$limit) ? '' : ' LIMIT '.$limit;
+
+ $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
- return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$limit;
+ return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$orderby.$limit;
+ }
+
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Truncate statement
+ *
+ * Generates a platform-specific truncate string from the supplied data
+ * If the database does not support the truncate() command
+ * This function maps to "DELETE FROM table"
+ *
+ * @access public
+ * @param string the table name
+ * @return string
+ */
+ function _truncate($table)
+ {
+ return "TRUNCATE ".$this->_escape_table($table);
}
// --------------------------------------------------------------------
@@ -443,13 +512,28 @@ class CI_DB_mssql_driver extends CI_DB {
* @access public
* @param string the table name
* @param array the where clause
+ * @param string the limit clause
* @return string
*/
- function _delete($table, $where, $limit = FALSE)
+ function _delete($table, $where = array(), $like = array(), $limit = FALSE)
{
+ $conditions = '';
+
+ if (count($where) > 0 || 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 ".$this->_escape_table($table)." WHERE ".implode(" ", $where).$limit;
+ return "DELETE FROM ".$table.$conditions.$limit;
}
// --------------------------------------------------------------------
diff --git a/system/database/drivers/mssql/mssql_forge.php b/system/database/drivers/mssql/mssql_forge.php
new file mode 100644
index 000000000..63a9d8396
--- /dev/null
+++ b/system/database/drivers/mssql/mssql_forge.php
@@ -0,0 +1,219 @@
+<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package CodeIgniter
+ * @author Rick Ellis
+ * @copyright Copyright (c) 2006, EllisLab, Inc.
+ * @license http://www.codeigniter.com/user_guide/license.html
+ * @link http://www.codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * MS SQL Forge Class
+ *
+ * @category Database
+ * @author Rick Ellis
+ * @link http://www.codeigniter.com/user_guide/database/
+ */
+class CI_DB_mssql_forge extends CI_DB_forge {
+
+ /**
+ * Create database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _create_database($name)
+ {
+ return "CREATE DATABASE ".$name;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _drop_database($name)
+ {
+ return "DROP DATABASE ".$name;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop Table
+ *
+ * @access private
+ * @return bool
+ */
+ function _drop_table($table)
+ {
+ return "DROP TABLE ".$this->db->_escape_table($table);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Create Table
+ *
+ * @access private
+ * @param string the table name
+ * @param array the fields
+ * @param mixed primary key(s)
+ * @param mixed key(s)
+ * @param boolean should 'IF NOT EXISTS' be added to the SQL
+ * @return bool
+ */
+ function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
+ {
+ $sql = 'CREATE TABLE ';
+
+ if ($if_not_exists === TRUE)
+ {
+ $sql .= 'IF NOT EXISTS ';
+ }
+
+ $sql .= $this->db->_escape_table($table)." (";
+ $current_field_count = 0;
+
+ foreach ($fields as $field=>$attributes)
+ {
+ // Numeric field names aren't allowed in databases, so if the key is
+ // numeric, we know it was assigned by PHP and the developer manually
+ // entered the field information, so we'll simply add it to the list
+ if (is_numeric($field))
+ {
+ $sql .= "\n\t$attributes";
+ }
+ else
+ {
+ $attributes = array_change_key_case($attributes, CASE_UPPER);
+
+ $sql .= "\n\t".$this->db->_protect_identifiers($field);
+
+ $sql .= ' '.$attributes['TYPE'];
+
+ if (array_key_exists('CONSTRAINT', $attributes))
+ {
+ $sql .= '('.$attributes['CONSTRAINT'].')';
+ }
+
+ if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
+ {
+ $sql .= ' UNSIGNED';
+ }
+
+ if (array_key_exists('DEFAULT', $attributes))
+ {
+ $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
+ }
+
+ if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
+ {
+ $sql .= ' NULL';
+ }
+ else
+ {
+ $sql .= ' NOT NULL';
+ }
+
+ if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
+ {
+ $sql .= ' AUTO_INCREMENT';
+ }
+ }
+
+ // don't add a comma on the end of the last field
+ if (++$current_field_count < count($fields))
+ {
+ $sql .= ',';
+ }
+ }
+
+ if (count($primary_keys) > 0)
+ {
+ $primary_keys = $this->db->_protect_identifiers($primary_keys);
+ $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
+ }
+
+ if (is_array($keys) && count($keys) > 0)
+ {
+ $keys = $this->db->_protect_identifiers($keys);
+ foreach ($keys as $key)
+ {
+ $sql .= ",\n\tFOREIGN KEY ($key)";
+ }
+ }
+
+ $sql .= "\n)";
+
+ return $sql;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Alter table query
+ *
+ * Generates a platform-specific query so that a table can be altered
+ * Called by add_column(), drop_column(), and column_alter(),
+ *
+ * @access private
+ * @param string the ALTER type (ADD, DROP, CHANGE)
+ * @param string the column name
+ * @param string the table name
+ * @param string the column definition
+ * @param string the default value
+ * @param boolean should 'NOT NULL' be added
+ * @param string the field after which we should add the new field
+ * @return object
+ */
+ function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
+ {
+ $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
+
+ // DROP has everything it needs now.
+ if ($alter_type == 'DROP')
+ {
+ return $sql;
+ }
+
+ $sql .= " $column_definition";
+
+ if ($default_value != '')
+ {
+ $sql .= " DEFAULT \"$default_value\"";
+ }
+
+ if ($null === NULL)
+ {
+ $sql .= ' NULL';
+ }
+ else
+ {
+ $sql .= ' NOT NULL';
+ }
+
+ if ($after_field != '')
+ {
+ $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
+ }
+
+ return $sql;
+
+ }
+
+}
+?> \ No newline at end of file
diff --git a/system/database/drivers/mssql/mssql_utility.php b/system/database/drivers/mssql/mssql_utility.php
index 3a102212b..b020a2a6e 100644
--- a/system/database/drivers/mssql/mssql_utility.php
+++ b/system/database/drivers/mssql/mssql_utility.php
@@ -24,48 +24,6 @@
*/
class CI_DB_mssql_utility extends CI_DB_utility {
-
- /**
- * Create database
- *
- * @access private
- * @param string the database name
- * @return bool
- */
- function _create_database($name)
- {
- return "CREATE DATABASE ".$name;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Drop database
- *
- * @access private
- * @param string the database name
- * @return bool
- */
- function _drop_database($name)
- {
- return "DROP DATABASE ".$name;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Drop Table
- *
- * @access private
- * @return bool
- */
- function _drop_table($table)
- {
- return "DROP TABLE ".$this->db->_escape_table($table);
- }
-
- // --------------------------------------------------------------------
-
/**
* List databases
*
@@ -94,7 +52,7 @@ class CI_DB_mssql_utility extends CI_DB_utility {
}
// --------------------------------------------------------------------
-
+
/**
* Repair table query
*
@@ -124,6 +82,39 @@ class CI_DB_mssql_utility extends CI_DB_utility {
return $this->db->display_error('db_unsuported_feature');
}
+ /**
+ *
+ * The functions below have been deprecated as of 1.6, and are only here for backwards
+ * compatibility. They now reside in dbforge(). The use of dbutils for database manipulation
+ * is STRONGLY discouraged in favour if using dbforge.
+ *
+ */
+
+ /**
+ * Create database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _create_database($name)
+ {
+ return "CREATE DATABASE ".$name;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _drop_database($name)
+ {
+ return "DROP DATABASE ".$name;
+ }
}
diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php
index cd86ebf52..a164552ed 100644
--- a/system/database/drivers/mysql/mysql_driver.php
+++ b/system/database/drivers/mysql/mysql_driver.php
@@ -42,7 +42,7 @@ class CI_DB_mysql_driver extends CI_DB {
* database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $_count_string = "SELECT COUNT(*) AS numrows ";
+ var $_count_string = 'SELECT COUNT(*) AS ';
var $_random_keyword = ' RAND()'; // database specific random keyword
/**
@@ -85,6 +85,21 @@ class CI_DB_mysql_driver extends CI_DB {
// --------------------------------------------------------------------
/**
+ * Set client character set
+ *
+ * @access public
+ * @param string
+ * @param string
+ * @return resource
+ */
+ function db_set_charset($charset, $collation)
+ {
+ return @mysql_query("SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'", $this->conn_id);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Version number query string
*
* @access public
@@ -314,17 +329,18 @@ class CI_DB_mysql_driver extends CI_DB {
* Generates a platform-specific query string so that the table names can be fetched
*
* @access private
+ * @param boolean
* @return string
*/
function _list_tables($prefix_limit = FALSE)
{
$sql = "SHOW TABLES FROM `".$this->database."`";
-
- if ($prefix_limit !== FALSE AND $this->_stdprefix != '')
+
+ if ($prefix_limit !== FALSE AND $this->dbprefix != '')
{
- $sql .= " LIKE '".$this->_stdprefix."%'";
+ $sql .= " LIKE '".$this->dbprefix."%'";
}
-
+
return $sql;
}
@@ -400,14 +416,66 @@ class CI_DB_mysql_driver extends CI_DB {
*/
function _escape_table($table)
{
- if (stristr($table, '.'))
+ if (strpos($table, '.') !== FALSE)
{
- $table = preg_replace("/\./", "`.`", $table);
+ $table = str_replace('.', '`.`', $table);
}
return $table;
}
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Protect Identifiers
+ *
+ * This function adds backticks if appropriate based on db type
+ *
+ * @access private
+ * @param mixed the item to escape
+ * @param boolean only affect the first word
+ * @return mixed the item with backticks
+ */
+ function _protect_identifiers($item, $first_word_only = FALSE)
+ {
+ if (is_array($item))
+ {
+ $escaped_array = array();
+
+ foreach($item as $k=>$v)
+ {
+ $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
+ }
+
+ return $escaped_array;
+ }
+
+ // This function may get "item1 item2" as a string, and so
+ // we may need "`item1` `item2`" and not "`item1 item2`"
+ if (strpos($item, ' ') !== FALSE)
+ {
+ // This function may get "field >= 1", and need it to return "`field` >= 1"
+ if ($first_word_only === TRUE)
+ {
+ return '`'.preg_replace('/ /', '` ', $item, 1);
+ }
+
+ $item = preg_replace('/(^|\s|\()([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
+ }
+
+ $exceptions = array('AS', '/', '-', '%', '+', '*');
+
+ foreach ($exceptions as $exception)
+ {
+ if (stristr($item, " `{$exception}` ") !== FALSE)
+ {
+ $item = preg_replace('/ `('.preg_quote($exception).')` /i', ' $1 ', $item);
+ }
+ }
+ return $item;
+ }
+
// --------------------------------------------------------------------
/**
@@ -437,9 +505,11 @@ class CI_DB_mysql_driver extends CI_DB {
* @param string the table name
* @param array the update data
* @param array the where clause
+ * @param array the orderby clause
+ * @param array the limit clause
* @return string
*/
- function _update($table, $values, $where, $limit = FALSE)
+ function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
{
foreach($values as $key => $val)
{
@@ -447,8 +517,28 @@ class CI_DB_mysql_driver extends CI_DB {
}
$limit = (!$limit) ? '' : ' LIMIT '.$limit;
+
+ $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
- return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$limit;
+ return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$orderby.$limit;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Truncate statement
+ *
+ * Generates a platform-specific truncate string from the supplied data
+ * If the database does not support the truncate() command
+ * This function maps to "DELETE FROM table"
+ *
+ * @access public
+ * @param string the table name
+ * @return string
+ */
+ function _truncate($table)
+ {
+ return "TRUNCATE ".$this->_escape_table($table);
}
// --------------------------------------------------------------------
@@ -461,13 +551,28 @@ class CI_DB_mysql_driver extends CI_DB {
* @access public
* @param string the table name
* @param array the where clause
+ * @param string the limit clause
* @return string
*/
- function _delete($table, $where, $limit = FALSE)
+ function _delete($table, $where = array(), $like = array(), $limit = FALSE)
{
+ $conditions = '';
+
+ if (count($where) > 0 || 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 ".$this->_escape_table($table)." WHERE ".implode(" ", $where).$limit;
+ return "DELETE FROM ".$table.$conditions.$limit;
}
// --------------------------------------------------------------------
diff --git a/system/database/drivers/mysql/mysql_forge.php b/system/database/drivers/mysql/mysql_forge.php
new file mode 100644
index 000000000..8a918c026
--- /dev/null
+++ b/system/database/drivers/mysql/mysql_forge.php
@@ -0,0 +1,223 @@
+<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package CodeIgniter
+ * @author Rick Ellis
+ * @copyright Copyright (c) 2006, EllisLab, Inc.
+ * @license http://www.codeigniter.com/user_guide/license.html
+ * @link http://www.codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * MySQL Forge Class
+ *
+ * @category Database
+ * @author Rick Ellis
+ * @link http://www.codeigniter.com/user_guide/database/
+ */
+class CI_DB_mysql_forge extends CI_DB_forge {
+
+ /**
+ * Create database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _create_database($name)
+ {
+ return "CREATE DATABASE ".$name;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _drop_database($name)
+ {
+ return "DROP DATABASE ".$name;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Process Fields
+ *
+ * @access private
+ * @param mixed the fields
+ * @return string
+ */
+ function _process_fields($fields)
+ {
+ $current_field_count = 0;
+ $sql = '';
+
+ foreach ($fields as $field=>$attributes)
+ {
+ // Numeric field names aren't allowed in databases, so if the key is
+ // numeric, we know it was assigned by PHP and the developer manually
+ // entered the field information, so we'll simply add it to the list
+ if (is_numeric($field))
+ {
+ $sql .= "\n\t$attributes";
+ }
+ else
+ {
+ $attributes = array_change_key_case($attributes, CASE_UPPER);
+
+ $sql .= "\n\t".$this->db->_protect_identifiers($field);
+
+ if (array_key_exists('NAME', $attributes))
+ {
+ $sql .= ' '.$this->db->_protect_identifiers($attributes['NAME']).' ';
+ }
+
+ if (array_key_exists('TYPE', $attributes))
+ {
+ $sql .= ' '.$attributes['TYPE'];
+ }
+
+ if (array_key_exists('CONSTRAINT', $attributes))
+ {
+ $sql .= '('.$attributes['CONSTRAINT'].')';
+ }
+
+ if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
+ {
+ $sql .= ' UNSIGNED';
+ }
+
+ if (array_key_exists('DEFAULT', $attributes))
+ {
+ $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
+ }
+
+ if (array_key_exists('NULL', $attributes))
+ {
+ $sql .= ($attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL';
+ }
+
+ if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
+ {
+ $sql .= ' AUTO_INCREMENT';
+ }
+ }
+
+ // don't add a comma on the end of the last field
+ if (++$current_field_count < count($fields))
+ {
+ $sql .= ',';
+ }
+ }
+
+ return $sql;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Create Table
+ *
+ * @access private
+ * @param string the table name
+ * @param mixed the fields
+ * @param mixed primary key(s)
+ * @param mixed key(s)
+ * @param boolean should 'IF NOT EXISTS' be added to the SQL
+ * @return bool
+ */
+ function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
+ {
+ $sql = 'CREATE TABLE ';
+
+ if ($if_not_exists === TRUE)
+ {
+ $sql .= 'IF NOT EXISTS ';
+ }
+
+ $sql .= $this->db->_escape_table($table)." (";
+
+ $sql .= $this->_process_fields($fields);
+
+ if (count($primary_keys) > 0)
+ {
+ $primary_keys = $this->db->_protect_identifiers($primary_keys);
+ $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
+ }
+
+ if (is_array($keys) && count($keys) > 0)
+ {
+ $keys = $this->db->_protect_identifiers($keys);
+ foreach ($keys as $key)
+ {
+ $sql .= ",\n\tKEY ($key)";
+ }
+ }
+
+ $sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
+
+ return $sql;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop Table
+ *
+ * @access private
+ * @return bool
+ */
+ function _drop_table($table)
+ {
+ return "DROP TABLE IF EXISTS ".$this->db->_escape_table($table);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Alter table query
+ *
+ * Generates a platform-specific query so that a table can be altered
+ * Called by add_column(), drop_column(), and column_alter(),
+ *
+ * @access private
+ * @param string the ALTER type (ADD, DROP, CHANGE)
+ * @param string the column name
+ * @param array fields
+ * @param string the field after which we should add the new field
+ * @return object
+ */
+ function _alter_table($alter_type, $table, $fields, $after_field = '')
+ {
+ $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ";
+
+ // DROP has everything it needs now.
+ if ($alter_type == 'DROP')
+ {
+ return $sql.$this->db->_protect_identifiers($fields);
+ }
+
+ $sql .= $this->_process_fields($fields);
+
+ if ($after_field != '')
+ {
+ $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
+ }
+
+ return $sql;
+ }
+
+}
+?> \ No newline at end of file
diff --git a/system/database/drivers/mysql/mysql_utility.php b/system/database/drivers/mysql/mysql_utility.php
index 54c110e09..c24a008fa 100644
--- a/system/database/drivers/mysql/mysql_utility.php
+++ b/system/database/drivers/mysql/mysql_utility.php
@@ -23,34 +23,6 @@
* @link http://www.codeigniter.com/user_guide/database/
*/
class CI_DB_mysql_utility extends CI_DB_utility {
-
- /**
- * Create database
- *
- * @access private
- * @param string the database name
- * @return bool
- */
- function _create_database($name)
- {
- return "CREATE DATABASE ".$name;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Drop database
- *
- * @access private
- * @param string the database name
- * @return bool
- */
- function _drop_database($name)
- {
- return "DROP DATABASE ".$name;
- }
-
- // --------------------------------------------------------------------
/**
* List databases
@@ -66,19 +38,6 @@ class CI_DB_mysql_utility extends CI_DB_utility {
// --------------------------------------------------------------------
/**
- * Drop Table
- *
- * @access private
- * @return bool
- */
- function _drop_table($table)
- {
- return "DROP TABLE IF EXISTS ".$this->db->_escape_table($table);
- }
-
- // --------------------------------------------------------------------
-
- /**
* Optimize table query
*
* Generates a platform-specific query so that a table can be optimized
@@ -109,7 +68,6 @@ class CI_DB_mysql_utility extends CI_DB_utility {
}
// --------------------------------------------------------------------
-
/**
* MySQL Export
*
@@ -187,9 +145,10 @@ class CI_DB_mysql_utility extends CI_DB_utility {
$is_int = array();
while ($field = mysql_fetch_field($query->result_id))
{
+ // Most versions of MySQL store timestamp as a string
$is_int[$i] = (in_array(
strtolower(mysql_field_type($query->result_id, $i)),
- array('tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'timestamp'),
+ array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), //, 'timestamp'),
TRUE)
) ? TRUE : FALSE;
@@ -255,7 +214,39 @@ class CI_DB_mysql_utility extends CI_DB_utility {
return $output;
}
+ /**
+ *
+ * The functions below have been deprecated as of 1.6, and are only here for backwards
+ * compatibility. They now reside in dbforge(). The use of dbutils for database manipulation
+ * is STRONGLY discouraged in favour if using dbforge.
+ *
+ */
-}
+ /**
+ * Create database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _create_database($name)
+ {
+ return "CREATE DATABASE ".$name;
+ }
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _drop_database($name)
+ {
+ return "DROP DATABASE ".$name;
+ }
+
+}
?> \ No newline at end of file
diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php
index ebed81390..d295ca9f2 100644
--- a/system/database/drivers/mysqli/mysqli_driver.php
+++ b/system/database/drivers/mysqli/mysqli_driver.php
@@ -35,7 +35,7 @@ class CI_DB_mysqli_driver extends CI_DB {
* database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $_count_string = "SELECT COUNT(*) AS numrows ";
+ var $_count_string = "SELECT COUNT(*) AS ";
var $_random_keyword = ' RAND()'; // database specific random keyword
/**
@@ -87,6 +87,22 @@ class CI_DB_mysqli_driver extends CI_DB {
// --------------------------------------------------------------------
/**
+ * Set client character set
+ *
+ * @access public
+ * @param string
+ * @param string
+ * @return resource
+ */
+ function db_set_charset($charset, $collation)
+ {
+ // TODO - add support if needed
+ return TRUE;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Version number query string
*
* @access public
@@ -307,15 +323,16 @@ class CI_DB_mysqli_driver extends CI_DB {
* Generates a platform-specific query string so that the table names can be fetched
*
* @access private
+ * @param boolean
* @return string
*/
function _list_tables($prefix_limit = FALSE)
{
$sql = "SHOW TABLES FROM `".$this->database."`";
- if ($prefix_limit !== FALSE AND $this->_stdprefix != '')
+ if ($prefix_limit !== FALSE AND $this->dbprefix != '')
{
- $sql .= " LIKE '".$this->_stdprefix."%'";
+ $sql .= " LIKE '".$this->dbprefix."%'";
}
return $sql;
@@ -404,6 +421,58 @@ class CI_DB_mysqli_driver extends CI_DB {
// --------------------------------------------------------------------
/**
+ * Protect Identifiers
+ *
+ * This function adds backticks if appropriate based on db type
+ *
+ * @access private
+ * @param mixed the item to escape
+ * @param boolean only affect the first word
+ * @return mixed the item with backticks
+ */
+ function _protect_identifiers($item, $first_word_only = FALSE)
+ {
+ if (is_array($item))
+ {
+ $escaped_array = array();
+
+ foreach($item as $k=>$v)
+ {
+ $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
+ }
+
+ return $escaped_array;
+ }
+
+ // This function may get "item1 item2" as a string, and so
+ // we may need "`item1` `item2`" and not "`item1 item2`"
+ if (strpos($item, ' ') !== FALSE)
+ {
+ // This function may get "field >= 1", and need it to return "`field` >= 1"
+ if ($first_word_only === TRUE)
+ {
+ return '`'.preg_replace('/ /', '` ', $item, 1);
+ }
+
+ $item = preg_replace('/(^|\s|\()([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
+ }
+
+ $exceptions = array('AS', '/', '-', '%', '+', '*');
+
+ foreach ($exceptions as $exception)
+ {
+ if (stristr($item, " `{$exception}` ") !== FALSE)
+ {
+ $item = preg_replace('/ `('.preg_quote($exception).')` /i', ' $1 ', $item);
+ }
+ }
+
+ return $item;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Insert statement
*
* Generates a platform-specific insert string from the supplied data
@@ -430,9 +499,11 @@ class CI_DB_mysqli_driver extends CI_DB {
* @param string the table name
* @param array the update data
* @param array the where clause
+ * @param array the orderby clause
+ * @param array the limit clause
* @return string
*/
- function _update($table, $values, $where, $limit = FALSE)
+ function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
{
foreach($values as $key => $val)
{
@@ -440,8 +511,29 @@ class CI_DB_mysqli_driver extends CI_DB {
}
$limit = (!$limit) ? '' : ' LIMIT '.$limit;
+
+ $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
- return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$limit;
+ return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$orderby.$limit;
+ }
+
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Truncate statement
+ *
+ * Generates a platform-specific truncate string from the supplied data
+ * If the database does not support the truncate() command
+ * This function maps to "DELETE FROM table"
+ *
+ * @access public
+ * @param string the table name
+ * @return string
+ */
+ function _truncate($table)
+ {
+ return "TRUNCATE ".$this->_escape_table($table);
}
// --------------------------------------------------------------------
@@ -454,13 +546,28 @@ class CI_DB_mysqli_driver extends CI_DB {
* @access public
* @param string the table name
* @param array the where clause
+ * @param string the limit clause
* @return string
*/
- function _delete($table, $where, $limit = FALSE)
+ function _delete($table, $where = array(), $like = array(), $limit = FALSE)
{
+ $conditions = '';
+
+ if (count($where) > 0 || 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 ".$this->_escape_table($table)." WHERE ".implode(" ", $where).$limit;
+ return "DELETE FROM ".$table.$conditions.$limit;
}
// --------------------------------------------------------------------
diff --git a/system/database/drivers/mysqli/mysqli_forge.php b/system/database/drivers/mysqli/mysqli_forge.php
new file mode 100644
index 000000000..3da5d2c49
--- /dev/null
+++ b/system/database/drivers/mysqli/mysqli_forge.php
@@ -0,0 +1,217 @@
+<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package CodeIgniter
+ * @author Rick Ellis
+ * @copyright Copyright (c) 2006, EllisLab, Inc.
+ * @license http://www.codeigniter.com/user_guide/license.html
+ * @link http://www.codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * MySQLi Forge Class
+ *
+ * @category Database
+ * @author Rick Ellis
+ * @link http://www.codeigniter.com/user_guide/database/
+ */
+class CI_DB_mysqli_forge extends CI_DB_forge {
+
+ /**
+ * Create database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _create_database($name)
+ {
+ return "CREATE DATABASE ".$name;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _drop_database($name)
+ {
+ return "DROP DATABASE ".$name;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop Table
+ *
+ * @access private
+ * @return bool
+ */
+ function _drop_table($table)
+ {
+ return "DROP TABLE IF EXISTS ".$this->db->_escape_table($table);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Create Table
+ *
+ * @access private
+ * @param string the table name
+ * @param array the fields
+ * @param mixed primary key(s)
+ * @param mixed key(s)
+ * @param boolean should 'IF NOT EXISTS' be added to the SQL
+ * @return bool
+ */
+ function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
+ {
+ $sql = 'CREATE TABLE ';
+
+ if ($if_not_exists === TRUE)
+ {
+ $sql .= 'IF NOT EXISTS ';
+ }
+
+ $sql .= $this->db->_escape_table($table)." (";
+ $current_field_count = 0;
+
+ foreach ($fields as $field=>$attributes)
+ {
+ // Numeric field names aren't allowed in databases, so if the key is
+ // numeric, we know it was assigned by PHP and the developer manually
+ // entered the field information, so we'll simply add it to the list
+ if (is_numeric($field))
+ {
+ $sql .= "\n\t$attributes";
+ }
+ else
+ {
+ $attributes = array_change_key_case($attributes, CASE_UPPER);
+
+ $sql .= "\n\t".$this->db->_protect_identifiers($field);
+
+ $sql .= ' '.$attributes['TYPE'];
+
+ if (array_key_exists('CONSTRAINT', $attributes))
+ {
+ $sql .= '('.$attributes['CONSTRAINT'].')';
+ }
+
+ if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
+ {
+ $sql .= ' UNSIGNED';
+ }
+
+ if (array_key_exists('DEFAULT', $attributes))
+ {
+ $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
+ }
+
+ if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
+ {
+ $sql .= ' NULL';
+ }
+ else
+ {
+ $sql .= ' NOT NULL';
+ }
+
+ if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
+ {
+ $sql .= ' AUTO_INCREMENT';
+ }
+ }
+
+ // don't add a comma on the end of the last field
+ if (++$current_field_count < count($fields))
+ {
+ $sql .= ',';
+ }
+ }
+
+ if (count($primary_keys) > 0)
+ {
+ $primary_keys = $this->db->_protect_identifiers($primary_keys);
+ $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
+ }
+
+ if (is_array($keys) && count($keys) > 0)
+ {
+ $keys = $this->db->_protect_identifiers($keys);
+ foreach ($keys as $key)
+ {
+ $sql .= ",\n\tKEY ($key)";
+ }
+ }
+
+ $sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
+
+ return $sql;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Alter table query
+ *
+ * Generates a platform-specific query so that a table can be altered
+ * Called by add_column(), drop_column(), and column_alter(),
+ *
+ * @access private
+ * @param string the ALTER type (ADD, DROP, CHANGE)
+ * @param string the column name
+ * @param string the table name
+ * @param string the column definition
+ * @param string the default value
+ * @param boolean should 'NOT NULL' be added
+ * @param string the field after which we should add the new field
+ * @return object
+ */
+ function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
+ {
+ $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
+
+ // DROP has everything it needs now.
+ if ($alter_type == 'DROP')
+ {
+ return $sql;
+ }
+
+ $sql .= " $column_definition";
+
+ if ($default_value != '')
+ {
+ $sql .= " DEFAULT \"$default_value\"";
+ }
+
+ if ($null === NULL)
+ {
+ $sql .= ' NULL';
+ }
+ else
+ {
+ $sql .= ' NOT NULL';
+ }
+
+ if ($after_field != '')
+ {
+ $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
+ }
+
+ return $sql;
+ }
+}
+?> \ No newline at end of file
diff --git a/system/database/drivers/mysqli/mysqli_utility.php b/system/database/drivers/mysqli/mysqli_utility.php
index c904e92d7..1d910638f 100644
--- a/system/database/drivers/mysqli/mysqli_utility.php
+++ b/system/database/drivers/mysqli/mysqli_utility.php
@@ -25,47 +25,6 @@
class CI_DB_mysqli_utility extends CI_DB_utility {
/**
- * Create database
- *
- * @access private
- * @param string the database name
- * @return bool
- */
- function _create_database($name)
- {
- return "CREATE DATABASE ".$name;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Drop database
- *
- * @access private
- * @param string the database name
- * @return bool
- */
- function _drop_database($name)
- {
- return "DROP DATABASE ".$name;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Drop Table
- *
- * @access private
- * @return bool
- */
- function _drop_table($table)
- {
- return "DROP TABLE IF EXISTS ".$this->db->_escape_table($table);
- }
-
- // --------------------------------------------------------------------
-
- /**
* List databases
*
* @access private
@@ -187,9 +146,10 @@ class CI_DB_mysqli_utility extends CI_DB_utility {
$is_int = array();
while ($field = mysqli_fetch_field($query->result_id))
{
+ // Most versions of MySQL store timestamp as a string
$is_int[$i] = (in_array(
strtolower(mysql_field_type($query->result_id, $i)),
- array('tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'timestamp'),
+ array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), // 'timestamp'),
TRUE)
) ? TRUE : FALSE;
@@ -255,8 +215,39 @@ class CI_DB_mysqli_utility extends CI_DB_utility {
return $output;
}
+ /**
+ *
+ * The functions below have been deprecated as of 1.6, and are only here for backwards
+ * compatibility. They now reside in dbforge(). The use of dbutils for database manipulation
+ * is STRONGLY discouraged in favour if using dbforge.
+ *
+ */
+
+ /**
+ * Create database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _create_database($name)
+ {
+ return "CREATE DATABASE ".$name;
+ }
+
+ // --------------------------------------------------------------------
+ /**
+ * Drop database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _drop_database($name)
+ {
+ return "DROP DATABASE ".$name;
+ }
}
-
?> \ No newline at end of file
diff --git a/system/database/drivers/oci8/oci8_driver.php b/system/database/drivers/oci8/oci8_driver.php
index c4ab70051..6d3b72261 100644
--- a/system/database/drivers/oci8/oci8_driver.php
+++ b/system/database/drivers/oci8/oci8_driver.php
@@ -48,7 +48,8 @@ class CI_DB_oci8_driver extends CI_DB {
* database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $_count_string = "SELECT COUNT(1) AS numrows ";
+ var $_count_string = "SELECT COUNT(1) AS ";
+ var $_random_keyword = ' ASC'; // not currently supported
// Set "auto commit" by default
var $_commit = OCI_COMMIT_ON_SUCCESS;
@@ -101,6 +102,22 @@ class CI_DB_oci8_driver extends CI_DB {
// --------------------------------------------------------------------
/**
+ * Set client character set
+ *
+ * @access public
+ * @param string
+ * @param string
+ * @return resource
+ */
+ function db_set_charset($charset, $collation)
+ {
+ // TODO - add support if needed
+ return TRUE;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Version number query string
*
* @access public
@@ -415,11 +432,19 @@ class CI_DB_oci8_driver extends CI_DB {
* Generates a platform-specific query string so that the table names can be fetched
*
* @access private
+ * @param boolean
* @return string
*/
- function _list_tables()
+ function _list_tables($prefix_limit = FALSE)
{
- return "SELECT TABLE_NAME FROM ALL_TABLES";
+ $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
+
+ if ($prefix_limit !== FALSE AND $this->dbprefix != '')
+ {
+ $sql .= " WHERE TABLE_NAME LIKE '".$this->dbprefix."%'";
+ }
+
+ return $sql;
}
// --------------------------------------------------------------------
@@ -507,6 +532,58 @@ class CI_DB_oci8_driver extends CI_DB {
// --------------------------------------------------------------------
/**
+ * Protect Identifiers
+ *
+ * This function adds backticks if appropriate based on db type
+ *
+ * @access private
+ * @param mixed the item to escape
+ * @param boolean only affect the first word
+ * @return mixed the item with backticks
+ */
+ function _protect_identifiers($item, $first_word_only = FALSE)
+ {
+ if (is_array($item))
+ {
+ $escaped_array = array();
+
+ foreach($item as $k=>$v)
+ {
+ $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
+ }
+
+ return $escaped_array;
+ }
+
+ // This function may get "item1 item2" as a string, and so
+ // we may need "`item1` `item2`" and not "`item1 item2`"
+ if (strpos($item, ' ') !== FALSE)
+ {
+ // This function may get "field >= 1", and need it to return "`field` >= 1"
+ if ($first_word_only === TRUE)
+ {
+ return '`'.preg_replace('/ /', '` ', $item, 1);
+ }
+
+ $item = preg_replace('/(^|\s|\()([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
+ }
+
+ $exceptions = array('AS', '/', '-', '%', '+', '*');
+
+ foreach ($exceptions as $exception)
+ {
+ if (stristr($item, " `{$exception}` ") !== FALSE)
+ {
+ $item = preg_replace('/ `('.preg_quote($exception).')` /i', ' $1 ', $item);
+ }
+ }
+
+ return $item;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Insert statement
*
* Generates a platform-specific insert string from the supplied data
@@ -529,13 +606,15 @@ class CI_DB_oci8_driver extends CI_DB {
*
* Generates a platform-specific update string from the supplied data
*
- * @access public
- * @param string the table name
- * @param array the update data
- * @param array the where clause
- * @return string
+ * @access public
+ * @param string the table name
+ * @param array the update data
+ * @param array the where clause
+ * @param array the orderby clause
+ * @param array the limit clause
+ * @return string
*/
- function _update($table, $values, $where, $limit = FALSE)
+ function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
{
foreach($values as $key => $val)
{
@@ -543,27 +622,62 @@ class CI_DB_oci8_driver extends CI_DB {
}
$limit = (!$limit) ? '' : ' LIMIT '.$limit;
+
+ $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
- return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$limit;
+ return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$orderby.$limit;
}
// --------------------------------------------------------------------
/**
+ * Truncate statement
+ *
+ * Generates a platform-specific truncate string from the supplied data
+ * If the database does not support the truncate() command
+ * This function maps to "DELETE FROM table"
+ *
+ * @access public
+ * @param string the table name
+ * @return string
+ */
+ function _truncate($table)
+ {
+ return "TRUNCATE TABLE ".$this->_escape_table($table);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Delete statement
*
* Generates a platform-specific delete string from the supplied data
*
- * @access public
- * @param string the table name
- * @param array the where clause
- * @return string
- */
- function _delete($table, $where, $limit = FALSE)
+ * @access public
+ * @param string the table name
+ * @param array the where clause
+ * @param string the limit clause
+ * @return string
+ */
+ function _delete($table, $where = array(), $like = array(), $limit = FALSE)
{
+ $conditions = '';
+
+ if (count($where) > 0 || 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 ".$this->_escape_table($table)." WHERE ".implode(" ", $where).$limit;
+ return "DELETE FROM ".$table.$conditions.$limit;
}
// --------------------------------------------------------------------
diff --git a/system/database/drivers/oci8/oci8_forge.php b/system/database/drivers/oci8/oci8_forge.php
new file mode 100644
index 000000000..c982e66a3
--- /dev/null
+++ b/system/database/drivers/oci8/oci8_forge.php
@@ -0,0 +1,216 @@
+<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package CodeIgniter
+ * @author Rick Ellis
+ * @copyright Copyright (c) 2006, EllisLab, Inc.
+ * @license http://www.codeigniter.com/user_guide/license.html
+ * @link http://www.codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * Oracle Forge Class
+ *
+ * @category Database
+ * @author Rick Ellis
+ * @link http://www.codeigniter.com/user_guide/database/
+ */
+class CI_DB_oci8_forge extends CI_DB_forge {
+
+ /**
+ * Create database
+ *
+ * @access public
+ * @param string the database name
+ * @return bool
+ */
+ function _create_database($name)
+ {
+ return FALSE;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _drop_database($name)
+ {
+ return FALSE;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Create Table
+ *
+ * @access private
+ * @param string the table name
+ * @param array the fields
+ * @param mixed primary key(s)
+ * @param mixed key(s)
+ * @param boolean should 'IF NOT EXISTS' be added to the SQL
+ * @return bool
+ */
+ function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
+ {
+ $sql = 'CREATE TABLE ';
+
+ if ($if_not_exists === TRUE)
+ {
+ $sql .= 'IF NOT EXISTS ';
+ }
+
+ $sql .= $this->db->_escape_table($table)." (";
+ $current_field_count = 0;
+
+ foreach ($fields as $field=>$attributes)
+ {
+ // Numeric field names aren't allowed in databases, so if the key is
+ // numeric, we know it was assigned by PHP and the developer manually
+ // entered the field information, so we'll simply add it to the list
+ if (is_numeric($field))
+ {
+ $sql .= "\n\t$attributes";
+ }
+ else
+ {
+ $attributes = array_change_key_case($attributes, CASE_UPPER);
+
+ $sql .= "\n\t".$this->db->_protect_identifiers($field);
+
+ $sql .= ' '.$attributes['TYPE'];
+
+ if (array_key_exists('CONSTRAINT', $attributes))
+ {
+ $sql .= '('.$attributes['CONSTRAINT'].')';
+ }
+
+ if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
+ {
+ $sql .= ' UNSIGNED';
+ }
+
+ if (array_key_exists('DEFAULT', $attributes))
+ {
+ $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
+ }
+
+ if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
+ {
+ $sql .= ' NULL';
+ }
+ else
+ {
+ $sql .= ' NOT NULL';
+ }
+
+ if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
+ {
+ $sql .= ' AUTO_INCREMENT';
+ }
+ }
+
+ // don't add a comma on the end of the last field
+ if (++$current_field_count < count($fields))
+ {
+ $sql .= ',';
+ }
+ }
+
+ if (count($primary_keys) > 0)
+ {
+ $primary_keys = $this->db->_protect_identifiers($primary_keys);
+ $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
+ }
+
+ if (count($keys) > 0)
+ {
+ $keys = $this->db->_protect_identifiers($keys);
+ $sql .= ",\n\tUNIQUE COLUMNS (" . implode(', ', $keys) . ")";
+ }
+
+ $sql .= "\n)";
+
+ return $sql;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop Table
+ *
+ * @access private
+ * @return bool
+ */
+ function _drop_table($table)
+ {
+ return FALSE;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Alter table query
+ *
+ * Generates a platform-specific query so that a table can be altered
+ * Called by add_column(), drop_column(), and column_alter(),
+ *
+ * @access private
+ * @param string the ALTER type (ADD, DROP, CHANGE)
+ * @param string the column name
+ * @param string the table name
+ * @param string the column definition
+ * @param string the default value
+ * @param boolean should 'NOT NULL' be added
+ * @param string the field after which we should add the new field
+ * @return object
+ */
+ function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
+ {
+ $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
+
+ // DROP has everything it needs now.
+ if ($alter_type == 'DROP')
+ {
+ return $sql;
+ }
+
+ $sql .= " $column_definition";
+
+ if ($default_value != '')
+ {
+ $sql .= " DEFAULT \"$default_value\"";
+ }
+
+ if ($null === NULL)
+ {
+ $sql .= ' NULL';
+ }
+ else
+ {
+ $sql .= ' NOT NULL';
+ }
+
+ if ($after_field != '')
+ {
+ $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
+ }
+
+ return $sql;
+
+ }
+
+}
+?> \ No newline at end of file
diff --git a/system/database/drivers/oci8/oci8_utility.php b/system/database/drivers/oci8/oci8_utility.php
index 0f9e4e838..67296da5b 100644
--- a/system/database/drivers/oci8/oci8_utility.php
+++ b/system/database/drivers/oci8/oci8_utility.php
@@ -24,35 +24,6 @@
*/
class CI_DB_oci8_utility extends CI_DB_utility {
-
- /**
- * Create database
- *
- * @access public
- * @param string the database name
- * @return bool
- */
- function _create_database($name)
- {
- return FALSE;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Drop database
- *
- * @access private
- * @param string the database name
- * @return bool
- */
- function _drop_database($name)
- {
- return FALSE;
- }
-
- // --------------------------------------------------------------------
-
/**
* List databases
*
@@ -67,19 +38,6 @@ class CI_DB_oci8_utility extends CI_DB_utility {
// --------------------------------------------------------------------
/**
- * Drop Table
- *
- * @access private
- * @return bool
- */
- function _drop_table($table)
- {
- return FALSE;
- }
-
- // --------------------------------------------------------------------
-
- /**
* Optimize table query
*
* Generates a platform-specific query so that a table can be optimized
@@ -124,6 +82,39 @@ class CI_DB_oci8_utility extends CI_DB_utility {
return $this->db->display_error('db_unsuported_feature');
}
-}
+ /**
+ *
+ * The functions below have been deprecated as of 1.6, and are only here for backwards
+ * compatibility. They now reside in dbforge(). The use of dbutils for database manipulation
+ * is STRONGLY discouraged in favour if using dbforge.
+ *
+ */
+
+ /**
+ * Create database
+ *
+ * @access public
+ * @param string the database name
+ * @return bool
+ */
+ function _create_database($name)
+ {
+ return FALSE;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _drop_database($name)
+ {
+ return FALSE;
+ }
+}
?> \ No newline at end of file
diff --git a/system/database/drivers/odbc/odbc_driver.php b/system/database/drivers/odbc/odbc_driver.php
index 040ffed9e..0bdfc41d5 100644
--- a/system/database/drivers/odbc/odbc_driver.php
+++ b/system/database/drivers/odbc/odbc_driver.php
@@ -35,8 +35,14 @@ class CI_DB_odbc_driver extends CI_DB {
* database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $_count_string = "SELECT COUNT(*) AS numrows ";
- var $_random_keyword = ' RND('.time().')'; // database specific random keyword
+ var $_count_string = "SELECT COUNT(*) AS ";
+ var $_random_keyword;
+
+
+ function CI_DB_odbc_driver()
+ {
+ $_random_keyword = ' RND('.time().')'; // database specific random keyword
+ }
/**
* Non-persistent database connection
@@ -79,6 +85,22 @@ class CI_DB_odbc_driver extends CI_DB {
// --------------------------------------------------------------------
/**
+ * Set client character set
+ *
+ * @access public
+ * @param string
+ * @param string
+ * @return resource
+ */
+ function db_set_charset($charset, $collation)
+ {
+ // TODO - add support if needed
+ return TRUE;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Version number query string
*
* @access public
@@ -276,11 +298,20 @@ class CI_DB_odbc_driver extends CI_DB {
* Generates a platform-specific query string so that the table names can be fetched
*
* @access private
+ * @param boolean
* @return string
*/
- function _list_tables()
+ function _list_tables($prefix_limit = FALSE)
{
- return "SHOW TABLES FROM `".$this->database."`";
+ $sql = "SHOW TABLES FROM `".$this->database."`";
+
+ if ($prefix_limit !== FALSE AND $this->dbprefix != '')
+ {
+ //$sql .= " LIKE '".$this->dbprefix."%'";
+ return FALSE; // not currently supported
+ }
+
+ return $sql;
}
// --------------------------------------------------------------------
@@ -366,6 +397,58 @@ class CI_DB_odbc_driver extends CI_DB {
// --------------------------------------------------------------------
/**
+ * Protect Identifiers
+ *
+ * This function adds backticks if appropriate based on db type
+ *
+ * @access private
+ * @param mixed the item to escape
+ * @param boolean only affect the first word
+ * @return mixed the item with backticks
+ */
+ function _protect_identifiers($item, $first_word_only = FALSE)
+ {
+ if (is_array($item))
+ {
+ $escaped_array = array();
+
+ foreach($item as $k=>$v)
+ {
+ $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
+ }
+
+ return $escaped_array;
+ }
+
+ // This function may get "item1 item2" as a string, and so
+ // we may need "`item1` `item2`" and not "`item1 item2`"
+ if (strpos($item, ' ') !== FALSE)
+ {
+ // This function may get "field >= 1", and need it to return "`field` >= 1"
+ if ($first_word_only === TRUE)
+ {
+ return '`'.preg_replace('/ /', '` ', $item, 1);
+ }
+
+ $item = preg_replace('/(^|\s|\()([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
+ }
+
+ $exceptions = array('AS', '/', '-', '%', '+', '*');
+
+ foreach ($exceptions as $exception)
+ {
+ if (stristr($item, " `{$exception}` ") !== FALSE)
+ {
+ $item = preg_replace('/ `('.preg_quote($exception).')` /i', ' $1 ', $item);
+ }
+ }
+
+ return $item;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Insert statement
*
* Generates a platform-specific insert string from the supplied data
@@ -392,9 +475,11 @@ class CI_DB_odbc_driver extends CI_DB {
* @param string the table name
* @param array the update data
* @param array the where clause
+ * @param array the orderby clause
+ * @param array the limit clause
* @return string
*/
- function _update($table, $values, $where, $limit = FALSE)
+ function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
{
foreach($values as $key => $val)
{
@@ -402,8 +487,29 @@ class CI_DB_odbc_driver extends CI_DB {
}
$limit = (!$limit) ? '' : ' LIMIT '.$limit;
+
+ $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
+
+ return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$orderby.$limit;
+ }
+
- return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$limit;
+ // --------------------------------------------------------------------
+
+ /**
+ * Truncate statement
+ *
+ * Generates a platform-specific truncate string from the supplied data
+ * If the database does not support the truncate() command
+ * This function maps to "DELETE FROM table"
+ *
+ * @access public
+ * @param string the table name
+ * @return string
+ */
+ function _truncate($table)
+ {
+ return $this->_delete($table);
}
// --------------------------------------------------------------------
@@ -416,13 +522,28 @@ class CI_DB_odbc_driver extends CI_DB {
* @access public
* @param string the table name
* @param array the where clause
+ * @param string the limit clause
* @return string
*/
- function _delete($table, $where, $limit = FALSE)
+ function _delete($table, $where = array(), $like = array(), $limit = FALSE)
{
+ $conditions = '';
+
+ if (count($where) > 0 || 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 ".$this->_escape_table($table)." WHERE ".implode(" ", $where).$limit;
+ return "DELETE FROM ".$table.$conditions.$limit;
}
// --------------------------------------------------------------------
diff --git a/system/database/drivers/odbc/odbc_forge.php b/system/database/drivers/odbc/odbc_forge.php
new file mode 100644
index 000000000..1d79344c1
--- /dev/null
+++ b/system/database/drivers/odbc/odbc_forge.php
@@ -0,0 +1,236 @@
+<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package CodeIgniter
+ * @author Rick Ellis
+ * @copyright Copyright (c) 2006, EllisLab, Inc.
+ * @license http://www.codeigniter.com/user_guide/license.html
+ * @link http://www.codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * ODBC Forge Class
+ *
+ * @category Database
+ * @author Rick Ellis
+ * @link http://www.codeigniter.com/database/
+ */
+class CI_DB_odbc_forge extends CI_DB_forge {
+
+ /**
+ * Create database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _create_database()
+ {
+ // ODBC has no "create database" command since it's
+ // designed to connect to an existing database
+ if ($this->db->db_debug)
+ {
+ return $this->db->display_error('db_unsuported_feature');
+ }
+ return FALSE;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _drop_database($name)
+ {
+ // ODBC has no "drop database" command since it's
+ // designed to connect to an existing database
+ if ($this->db->db_debug)
+ {
+ return $this->db->display_error('db_unsuported_feature');
+ }
+ return FALSE;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Create Table
+ *
+ * @access private
+ * @param string the table name
+ * @param array the fields
+ * @param mixed primary key(s)
+ * @param mixed key(s)
+ * @param boolean should 'IF NOT EXISTS' be added to the SQL
+ * @return bool
+ */
+ function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
+ {
+ $sql = 'CREATE TABLE ';
+
+ if ($if_not_exists === TRUE)
+ {
+ $sql .= 'IF NOT EXISTS ';
+ }
+
+ $sql .= $this->db->_escape_table($table)." (";
+ $current_field_count = 0;
+
+ foreach ($fields as $field=>$attributes)
+ {
+ // Numeric field names aren't allowed in databases, so if the key is
+ // numeric, we know it was assigned by PHP and the developer manually
+ // entered the field information, so we'll simply add it to the list
+ if (is_numeric($field))
+ {
+ $sql .= "\n\t$attributes";
+ }
+ else
+ {
+ $attributes = array_change_key_case($attributes, CASE_UPPER);
+
+ $sql .= "\n\t".$this->db->_protect_identifiers($field);
+
+ $sql .= ' '.$attributes['TYPE'];
+
+ if (array_key_exists('CONSTRAINT', $attributes))
+ {
+ $sql .= '('.$attributes['CONSTRAINT'].')';
+ }
+
+ if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
+ {
+ $sql .= ' UNSIGNED';
+ }
+
+ if (array_key_exists('DEFAULT', $attributes))
+ {
+ $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
+ }
+
+ if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
+ {
+ $sql .= ' NULL';
+ }
+ else
+ {
+ $sql .= ' NOT NULL';
+ }
+
+ if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
+ {
+ $sql .= ' AUTO_INCREMENT';
+ }
+ }
+
+ // don't add a comma on the end of the last field
+ if (++$current_field_count < count($fields))
+ {
+ $sql .= ',';
+ }
+ }
+
+ if (count($primary_keys) > 0)
+ {
+ $primary_keys = $this->db->_protect_identifiers($primary_keys);
+ $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
+ }
+
+ if (is_array($keys) && count($keys) > 0)
+ {
+ $keys = $this->db->_protect_identifiers($keys);
+ foreach ($keys as $key)
+ {
+ $sql .= ",\n\tFOREIGN KEY ($key)";
+ }
+ }
+
+ $sql .= "\n)";
+
+ return $sql;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop Table
+ *
+ * @access private
+ * @return bool
+ */
+ function _drop_table($table)
+ {
+ // Not a supported ODBC feature
+ if ($this->db->db_debug)
+ {
+ return $this->db->display_error('db_unsuported_feature');
+ }
+ return FALSE;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Alter table query
+ *
+ * Generates a platform-specific query so that a table can be altered
+ * Called by add_column(), drop_column(), and column_alter(),
+ *
+ * @access private
+ * @param string the ALTER type (ADD, DROP, CHANGE)
+ * @param string the column name
+ * @param string the table name
+ * @param string the column definition
+ * @param string the default value
+ * @param boolean should 'NOT NULL' be added
+ * @param string the field after which we should add the new field
+ * @return object
+ */
+ function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
+ {
+ $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
+
+ // DROP has everything it needs now.
+ if ($alter_type == 'DROP')
+ {
+ return $sql;
+ }
+
+ $sql .= " $column_definition";
+
+ if ($default_value != '')
+ {
+ $sql .= " DEFAULT \"$default_value\"";
+ }
+
+ if ($null === NULL)
+ {
+ $sql .= ' NULL';
+ }
+ else
+ {
+ $sql .= ' NOT NULL';
+ }
+
+ if ($after_field != '')
+ {
+ $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
+ }
+
+ return $sql;
+
+ }
+
+}
+?> \ No newline at end of file
diff --git a/system/database/drivers/odbc/odbc_result.php b/system/database/drivers/odbc/odbc_result.php
index e909fd743..53c317356 100644
--- a/system/database/drivers/odbc/odbc_result.php
+++ b/system/database/drivers/odbc/odbc_result.php
@@ -192,7 +192,6 @@ class CI_DB_odbc_result extends CI_DB_result {
* @access private
* @return object
*/
-
function _odbc_fetch_object(& $odbc_result) {
$rs = array();
$rs_obj = false;
@@ -215,7 +214,6 @@ class CI_DB_odbc_result extends CI_DB_result {
* @access private
* @return array
*/
-
function _odbc_fetch_array(& $odbc_result) {
$rs = array();
$rs_assoc = false;
diff --git a/system/database/drivers/odbc/odbc_utility.php b/system/database/drivers/odbc/odbc_utility.php
index 97e950fc1..d64d15af5 100644
--- a/system/database/drivers/odbc/odbc_utility.php
+++ b/system/database/drivers/odbc/odbc_utility.php
@@ -24,18 +24,15 @@
*/
class CI_DB_odbc_utility extends CI_DB_utility {
-
/**
- * Create database
+ * List databases
*
* @access private
- * @param string the database name
* @return bool
*/
- function _create_database()
+ function _list_databases()
{
- // ODBC has no "create database" command since it's
- // designed to connect to an existing database
+ // Not sure if ODBC lets you list all databases...
if ($this->db->db_debug)
{
return $this->db->display_error('db_unsuported_feature');
@@ -46,16 +43,17 @@ class CI_DB_odbc_utility extends CI_DB_utility {
// --------------------------------------------------------------------
/**
- * Drop database
+ * Optimize table query
+ *
+ * Generates a platform-specific query so that a table can be optimized
*
* @access private
- * @param string the database name
- * @return bool
+ * @param string the table name
+ * @return object
*/
- function _drop_database($name)
+ function _optimize_table($table)
{
- // ODBC has no "drop database" command since it's
- // designed to connect to an existing database
+ // Not a supported ODBC feature
if ($this->db->db_debug)
{
return $this->db->display_error('db_unsuported_feature');
@@ -66,14 +64,17 @@ class CI_DB_odbc_utility extends CI_DB_utility {
// --------------------------------------------------------------------
/**
- * List databases
+ * Repair table query
+ *
+ * Generates a platform-specific query so that a table can be repaired
*
* @access private
- * @return bool
+ * @param string the table name
+ * @return object
*/
- function _list_databases()
+ function _repair_table($table)
{
- // Not sure if ODBC lets you list all databases...
+ // Not a supported ODBC feature
if ($this->db->db_debug)
{
return $this->db->display_error('db_unsuported_feature');
@@ -84,35 +85,37 @@ class CI_DB_odbc_utility extends CI_DB_utility {
// --------------------------------------------------------------------
/**
- * Drop Table
+ * ODBC Export
*
* @access private
- * @return bool
+ * @param array Preferences
+ * @return mixed
*/
- function _drop_table($table)
+ function _backup($params = array())
{
- // Not a supported ODBC feature
- if ($this->db->db_debug)
- {
- return $this->db->display_error('db_unsuported_feature');
- }
- return FALSE;
+ // Currently unsupported
+ return $this->db->display_error('db_unsuported_feature');
}
-
- // --------------------------------------------------------------------
-
+
/**
- * Optimize table query
*
- * Generates a platform-specific query so that a table can be optimized
+ * The functions below have been deprecated as of 1.6, and are only here for backwards
+ * compatibility. They now reside in dbforge(). The use of dbutils for database manipulation
+ * is STRONGLY discouraged in favour if using dbforge.
+ *
+ */
+
+ /**
+ * Create database
*
* @access private
- * @param string the table name
- * @return object
+ * @param string the database name
+ * @return bool
*/
- function _optimize_table($table)
+ function _create_database()
{
- // Not a supported ODBC feature
+ // ODBC has no "create database" command since it's
+ // designed to connect to an existing database
if ($this->db->db_debug)
{
return $this->db->display_error('db_unsuported_feature');
@@ -123,39 +126,21 @@ class CI_DB_odbc_utility extends CI_DB_utility {
// --------------------------------------------------------------------
/**
- * Repair table query
- *
- * Generates a platform-specific query so that a table can be repaired
+ * Drop database
*
* @access private
- * @param string the table name
- * @return object
+ * @param string the database name
+ * @return bool
*/
- function _repair_table($table)
+ function _drop_database($name)
{
- // Not a supported ODBC feature
+ // ODBC has no "drop database" command since it's
+ // designed to connect to an existing database
if ($this->db->db_debug)
{
return $this->db->display_error('db_unsuported_feature');
}
return FALSE;
}
-
- // --------------------------------------------------------------------
-
- /**
- * ODBC Export
- *
- * @access private
- * @param array Preferences
- * @return mixed
- */
- function _backup($params = array())
- {
- // Currently unsupported
- return $this->db->display_error('db_unsuported_feature');
- }
-
}
-
?> \ No newline at end of file
diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php
index 88f08b2d7..63a72f5d3 100644
--- a/system/database/drivers/postgre/postgre_driver.php
+++ b/system/database/drivers/postgre/postgre_driver.php
@@ -35,7 +35,7 @@ class CI_DB_postgre_driver extends CI_DB {
* database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $_count_string = "SELECT COUNT(*) AS numrows ";
+ var $_count_string = "SELECT COUNT(*) AS ";
var $_random_keyword = ' RANDOM()'; // database specific random keyword
/**
@@ -83,6 +83,22 @@ class CI_DB_postgre_driver extends CI_DB {
// --------------------------------------------------------------------
/**
+ * Set client character set
+ *
+ * @access public
+ * @param string
+ * @param string
+ * @return resource
+ */
+ function db_set_charset($charset, $collation)
+ {
+ // TODO - add support if needed
+ return TRUE;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Version number query string
*
* @access public
@@ -305,11 +321,19 @@ class CI_DB_postgre_driver extends CI_DB {
* Generates a platform-specific query string so that the table names can be fetched
*
* @access private
+ * @param boolean
* @return string
*/
- function _list_tables()
+ function _list_tables($prefix_limit = FALSE)
{
- return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
+ $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
+
+ if ($prefix_limit !== FALSE AND $this->dbprefix != '')
+ {
+ $sql .= " AND table_name LIKE '".$this->dbprefix."%'";
+ }
+
+ return $sql;
}
// --------------------------------------------------------------------
@@ -395,6 +419,58 @@ class CI_DB_postgre_driver extends CI_DB {
// --------------------------------------------------------------------
/**
+ * Protect Identifiers
+ *
+ * This function adds backticks if appropriate based on db type
+ *
+ * @access private
+ * @param mixed the item to escape
+ * @param boolean only affect the first word
+ * @return mixed the item with backticks
+ */
+ function _protect_identifiers($item, $first_word_only = FALSE)
+ {
+ if (is_array($item))
+ {
+ $escaped_array = array();
+
+ foreach($item as $k=>$v)
+ {
+ $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
+ }
+
+ return $escaped_array;
+ }
+
+ // This function may get "item1 item2" as a string, and so
+ // we may need ""item1" "item2"" and not ""item1 item2""
+ if (strpos($item, ' ') !== FALSE)
+ {
+ // This function may get "field >= 1", and need it to return ""field" >= 1"
+ if ($first_word_only === TRUE)
+ {
+ return '"'.preg_replace('/ /', '" ', $item, 1);
+ }
+
+ $item = preg_replace('/(^|\s|\()([\w\d\-\_]+?)(\s|\)|$)/iS', '$1"$2"$3', $item);
+ }
+
+ $exceptions = array('AS', '/', '-', '%', '+', '*');
+
+ foreach ($exceptions as $exception)
+ {
+ if (stristr($item, " "{$exception}" ") !== FALSE)
+ {
+ $item = preg_replace('/ "('.preg_quote($exception).')" /i', ' $1 ', $item);
+ }
+ }
+
+ return $item;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Insert statement
*
* Generates a platform-specific insert string from the supplied data
@@ -421,9 +497,11 @@ class CI_DB_postgre_driver extends CI_DB {
* @param string the table name
* @param array the update data
* @param array the where clause
+ * @param array the orderby clause
+ * @param array the limit clause
* @return string
*/
- function _update($table, $values, $where, $limit = FALSE)
+ function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
{
foreach($values as $key => $val)
{
@@ -431,8 +509,29 @@ class CI_DB_postgre_driver extends CI_DB {
}
$limit = (!$limit) ? '' : ' LIMIT '.$limit;
+
+ $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
- return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$limit;
+ return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$orderby.$limit;
+ }
+
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Truncate statement
+ *
+ * Generates a platform-specific truncate string from the supplied data
+ * If the database does not support the truncate() command
+ * This function maps to "DELETE FROM table"
+ *
+ * @access public
+ * @param string the table name
+ * @return string
+ */
+ function _truncate($table)
+ {
+ return "TRUNCATE ".$this->_escape_table($table);
}
// --------------------------------------------------------------------
@@ -445,17 +544,31 @@ class CI_DB_postgre_driver extends CI_DB {
* @access public
* @param string the table name
* @param array the where clause
+ * @param string the limit clause
* @return string
*/
- function _delete($table, $where, $limit = FALSE)
+ function _delete($table, $where = array(), $like = array(), $limit = FALSE)
{
+ $conditions = '';
+
+ if (count($where) > 0 || 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 ".$this->_escape_table($table)." WHERE ".implode(" ", $where).$limit;
+ return "DELETE FROM ".$table.$conditions.$limit;
}
// --------------------------------------------------------------------
-
/**
* Limit string
*
diff --git a/system/database/drivers/postgre/postgre_forge.php b/system/database/drivers/postgre/postgre_forge.php
new file mode 100644
index 000000000..34bbe88b6
--- /dev/null
+++ b/system/database/drivers/postgre/postgre_forge.php
@@ -0,0 +1,219 @@
+<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package CodeIgniter
+ * @author Rick Ellis
+ * @copyright Copyright (c) 2006, EllisLab, Inc.
+ * @license http://www.codeigniter.com/user_guide/license.html
+ * @link http://www.codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * Postgre Forge Class
+ *
+ * @category Database
+ * @author Rick Ellis
+ * @link http://www.codeigniter.com/user_guide/database/
+ */
+class CI_DB_postgre_forge extends CI_DB_forge {
+
+ /**
+ * Create database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _create_database($name)
+ {
+ return "CREATE DATABASE ".$name;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _drop_database($name)
+ {
+ return "DROP DATABASE ".$name;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Create Table
+ *
+ * @access private
+ * @param string the table name
+ * @param array the fields
+ * @param mixed primary key(s)
+ * @param mixed key(s)
+ * @param boolean should 'IF NOT EXISTS' be added to the SQL
+ * @return bool
+ */
+ function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
+ {
+ $sql = 'CREATE TABLE ';
+
+ if ($if_not_exists === TRUE)
+ {
+ $sql .= 'IF NOT EXISTS ';
+ }
+
+ $sql .= $this->db->_escape_table($table)." (";
+ $current_field_count = 0;
+
+ foreach ($fields as $field=>$attributes)
+ {
+ // Numeric field names aren't allowed in databases, so if the key is
+ // numeric, we know it was assigned by PHP and the developer manually
+ // entered the field information, so we'll simply add it to the list
+ if (is_numeric($field))
+ {
+ $sql .= "\n\t$attributes";
+ }
+ else
+ {
+ $attributes = array_change_key_case($attributes, CASE_UPPER);
+
+ $sql .= "\n\t".$this->db->_protect_identifiers($field);
+
+ $sql .= ' '.$attributes['TYPE'];
+
+ if (array_key_exists('CONSTRAINT', $attributes))
+ {
+ $sql .= '('.$attributes['CONSTRAINT'].')';
+ }
+
+ if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
+ {
+ $sql .= ' UNSIGNED';
+ }
+
+ if (array_key_exists('DEFAULT', $attributes))
+ {
+ $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
+ }
+
+ if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
+ {
+ $sql .= ' NULL';
+ }
+ else
+ {
+ $sql .= ' NOT NULL';
+ }
+
+ if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
+ {
+ $sql .= ' AUTO_INCREMENT';
+ }
+ }
+
+ // don't add a comma on the end of the last field
+ if (++$current_field_count < count($fields))
+ {
+ $sql .= ',';
+ }
+ }
+
+ if (count($primary_keys) > 0)
+ {
+ $primary_keys = $this->db->_protect_identifiers($primary_keys);
+ $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
+ }
+
+ if (is_array($keys) && count($keys) > 0)
+ {
+ $keys = $this->db->_protect_identifiers($keys);
+ foreach ($keys as $key)
+ {
+ $sql .= ",\n\tFOREIGN KEY ($key)";
+ }
+ }
+
+ $sql .= "\n);";
+
+ return $sql;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop Table
+ *
+ * @access private
+ * @return bool
+ */
+ function _drop_table($table)
+ {
+ return "DROP TABLE ".$this->db->_escape_table($table)." CASCADE";
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Alter table query
+ *
+ * Generates a platform-specific query so that a table can be altered
+ * Called by add_column(), drop_column(), and column_alter(),
+ *
+ * @access private
+ * @param string the ALTER type (ADD, DROP, CHANGE)
+ * @param string the column name
+ * @param string the table name
+ * @param string the column definition
+ * @param string the default value
+ * @param boolean should 'NOT NULL' be added
+ * @param string the field after which we should add the new field
+ * @return object
+ */
+ function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
+ {
+ $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
+
+ // DROP has everything it needs now.
+ if ($alter_type == 'DROP')
+ {
+ return $sql;
+ }
+
+ $sql .= " $column_definition";
+
+ if ($default_value != '')
+ {
+ $sql .= " DEFAULT \"$default_value\"";
+ }
+
+ if ($null === NULL)
+ {
+ $sql .= ' NULL';
+ }
+ else
+ {
+ $sql .= ' NOT NULL';
+ }
+
+ if ($after_field != '')
+ {
+ $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
+ }
+
+ return $sql;
+
+ }
+
+}
+?> \ No newline at end of file
diff --git a/system/database/drivers/postgre/postgre_utility.php b/system/database/drivers/postgre/postgre_utility.php
index fa5960e58..a706d9505 100644
--- a/system/database/drivers/postgre/postgre_utility.php
+++ b/system/database/drivers/postgre/postgre_utility.php
@@ -24,35 +24,6 @@
*/
class CI_DB_postgre_utility extends CI_DB_utility {
-
- /**
- * Create database
- *
- * @access private
- * @param string the database name
- * @return bool
- */
- function _create_database($name)
- {
- return "CREATE DATABASE ".$name;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Drop database
- *
- * @access private
- * @param string the database name
- * @return bool
- */
- function _drop_database($name)
- {
- return "DROP DATABASE ".$name;
- }
-
- // --------------------------------------------------------------------
-
/**
* List databases
*
@@ -67,19 +38,6 @@ class CI_DB_postgre_utility extends CI_DB_utility {
// --------------------------------------------------------------------
/**
- * Drop Table
- *
- * @access private
- * @return bool
- */
- function _drop_table($table)
- {
- return "DROP TABLE ".$this->db->_escape_table($table)." CASCADE";
- }
-
- // --------------------------------------------------------------------
-
- /**
* Optimize table query
*
* Is table optimization supported in Postgre?
@@ -124,6 +82,41 @@ class CI_DB_postgre_utility extends CI_DB_utility {
return $this->db->display_error('db_unsuported_feature');
}
+ /**
+ *
+ * The functions below have been deprecated as of 1.6, and are only here for backwards
+ * compatibility. They now reside in dbforge(). The use of dbutils for database manipulation
+ * is STRONGLY discouraged in favour if using dbforge.
+ *
+ */
+
+ /**
+ * Create database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _create_database($name)
+ {
+ return "CREATE DATABASE ".$name;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _drop_database($name)
+ {
+ return "DROP DATABASE ".$name;
+ }
+
+
}
?> \ No newline at end of file
diff --git a/system/database/drivers/sqlite/sqlite_driver.php b/system/database/drivers/sqlite/sqlite_driver.php
index 6189b1ff0..2d0ca157c 100644
--- a/system/database/drivers/sqlite/sqlite_driver.php
+++ b/system/database/drivers/sqlite/sqlite_driver.php
@@ -37,7 +37,7 @@ class CI_DB_sqlite_driver extends CI_DB {
* database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $_count_string = "SELECT COUNT(*) AS numrows ";
+ var $_count_string = "SELECT COUNT(*) AS ";
var $_random_keyword = ' Random()'; // database specific random keyword
/**
@@ -104,6 +104,22 @@ class CI_DB_sqlite_driver extends CI_DB {
// --------------------------------------------------------------------
/**
+ * Set client character set
+ *
+ * @access public
+ * @param string
+ * @param string
+ * @return resource
+ */
+ function db_set_charset($charset, $collation)
+ {
+ // TODO - add support if needed
+ return TRUE;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Version number query string
*
* @access public
@@ -299,11 +315,18 @@ class CI_DB_sqlite_driver extends CI_DB {
* Generates a platform-specific query string so that the table names can be fetched
*
* @access private
+ * @param boolean
* @return string
*/
- function _list_tables()
+ function _list_tables($prefix_limit = FALSE)
{
- return "SELECT name from sqlite_master WHERE type='table'";
+ $sql = "SELECT name from sqlite_master WHERE type='table'";
+
+ if ($prefix_limit !== FALSE AND $this->dbprefix != '')
+ {
+ $sql .= " AND 'name' LIKE '".$this->dbprefix."%'";
+ }
+ return $sql;
}
// --------------------------------------------------------------------
@@ -390,6 +413,58 @@ class CI_DB_sqlite_driver extends CI_DB {
// --------------------------------------------------------------------
/**
+ * Protect Identifiers
+ *
+ * This function adds backticks if appropriate based on db type
+ *
+ * @access private
+ * @param mixed the item to escape
+ * @param boolean only affect the first word
+ * @return mixed the item with backticks
+ */
+ function _protect_identifiers($item, $first_word_only = FALSE)
+ {
+ if (is_array($item))
+ {
+ $escaped_array = array();
+
+ foreach($item as $k=>$v)
+ {
+ $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
+ }
+
+ return $escaped_array;
+ }
+
+ // This function may get "item1 item2" as a string, and so
+ // we may need "`item1` `item2`" and not "`item1 item2`"
+ if (strpos($item, ' ') !== FALSE)
+ {
+ // This function may get "field >= 1", and need it to return "`field` >= 1"
+ if ($first_word_only === TRUE)
+ {
+ return '`'.preg_replace('/ /', '` ', $item, 1);
+ }
+
+ $item = preg_replace('/(^|\s|\()([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
+ }
+
+ $exceptions = array('AS', '/', '-', '%', '+', '*');
+
+ foreach ($exceptions as $exception)
+ {
+ if (stristr($item, " `{$exception}` ") !== FALSE)
+ {
+ $item = preg_replace('/ `('.preg_quote($exception).')` /i', ' $1 ', $item);
+ }
+ }
+
+ return $item;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Insert statement
*
* Generates a platform-specific insert string from the supplied data
@@ -416,9 +491,11 @@ class CI_DB_sqlite_driver extends CI_DB {
* @param string the table name
* @param array the update data
* @param array the where clause
+ * @param array the orderby clause
+ * @param array the limit clause
* @return string
*/
- function _update($table, $values, $where, $limit = FALSE)
+ function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
{
foreach($values as $key => $val)
{
@@ -426,8 +503,29 @@ class CI_DB_sqlite_driver extends CI_DB {
}
$limit = (!$limit) ? '' : ' LIMIT '.$limit;
+
+ $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
+
+ return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$orderby.$limit;
+ }
+
- return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$limit;
+ // --------------------------------------------------------------------
+
+ /**
+ * Truncate statement
+ *
+ * Generates a platform-specific truncate string from the supplied data
+ * If the database does not support the truncate() command
+ * This function maps to "DELETE FROM table"
+ *
+ * @access public
+ * @param string the table name
+ * @return string
+ */
+ function _truncate($table)
+ {
+ return $this->_delete($table);
}
// --------------------------------------------------------------------
@@ -440,15 +538,30 @@ class CI_DB_sqlite_driver extends CI_DB {
* @access public
* @param string the table name
* @param array the where clause
+ * @param string the limit clause
* @return string
*/
- function _delete($table, $where, $limit = FALSE)
+ function _delete($table, $where = array(), $like = array(), $limit = FALSE)
{
+ $conditions = '';
+
+ if (count($where) > 0 || 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 ".$this->_escape_table($table)." WHERE ".implode(" ", $where).$limit;
+ return "DELETE FROM ".$table.$conditions.$limit;
}
-
+
// --------------------------------------------------------------------
/**
diff --git a/system/database/drivers/sqlite/sqlite_forge.php b/system/database/drivers/sqlite/sqlite_forge.php
new file mode 100644
index 000000000..54b65987d
--- /dev/null
+++ b/system/database/drivers/sqlite/sqlite_forge.php
@@ -0,0 +1,233 @@
+<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package CodeIgniter
+ * @author Rick Ellis
+ * @copyright Copyright (c) 2006, EllisLab, Inc.
+ * @license http://www.codeigniter.com/user_guide/license.html
+ * @link http://www.codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * SQLite Forge Class
+ *
+ * @category Database
+ * @author Rick Ellis
+ * @link http://www.codeigniter.com/user_guide/database/
+ */
+class CI_DB_sqlite_forge extends CI_DB_forge {
+
+ /**
+ * Create database
+ *
+ * @access public
+ * @param string the database name
+ * @return bool
+ */
+ function _create_database()
+ {
+ // In SQLite, a database is created when you connect to the database.
+ // We'll return TRUE so that an error isn't generated
+ return TRUE;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _drop_database($name)
+ {
+ if ( ! @file_exists($this->db->database) OR ! @unlink($this->db->database))
+ {
+ if ($this->db->db_debug)
+ {
+ return $this->db->display_error('db_unable_to_drop');
+ }
+ return FALSE;
+ }
+ return TRUE;
+ }
+ // --------------------------------------------------------------------
+
+ /**
+ * Create Table
+ *
+ * @access private
+ * @param string the table name
+ * @param array the fields
+ * @param mixed primary key(s)
+ * @param mixed key(s)
+ * @param boolean should 'IF NOT EXISTS' be added to the SQL
+ * @return bool
+ */
+ function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
+ {
+ $sql = 'CREATE TABLE ';
+
+ if ($if_not_exists === TRUE)
+ {
+ $sql .= 'IF NOT EXISTS ';
+ }
+
+ $sql .= $this->db->_escape_table($table)." (";
+ $current_field_count = 0;
+
+ foreach ($fields as $field=>$attributes)
+ {
+ // Numeric field names aren't allowed in databases, so if the key is
+ // numeric, we know it was assigned by PHP and the developer manually
+ // entered the field information, so we'll simply add it to the list
+ if (is_numeric($field))
+ {
+ $sql .= "\n\t$attributes";
+ }
+ else
+ {
+ $attributes = array_change_key_case($attributes, CASE_UPPER);
+
+ $sql .= "\n\t".$this->db->_protect_identifiers($field);
+
+ $sql .= ' '.$attributes['TYPE'];
+
+ if (array_key_exists('CONSTRAINT', $attributes))
+ {
+ $sql .= '('.$attributes['CONSTRAINT'].')';
+ }
+
+ if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
+ {
+ $sql .= ' UNSIGNED';
+ }
+
+ if (array_key_exists('DEFAULT', $attributes))
+ {
+ $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
+ }
+
+ if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
+ {
+ $sql .= ' NULL';
+ }
+ else
+ {
+ $sql .= ' NOT NULL';
+ }
+
+ if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
+ {
+ $sql .= ' AUTO_INCREMENT';
+ }
+ }
+
+ // don't add a comma on the end of the last field
+ if (++$current_field_count < count($fields))
+ {
+ $sql .= ',';
+ }
+ }
+
+ if (count($primary_keys) > 0)
+ {
+ $primary_keys = $this->db->_protect_identifiers($primary_keys);
+ $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
+ }
+
+ if (count($keys) > 0)
+ {
+ $keys = $this->db->_protect_identifiers($keys);
+ $sql .= ",\n\tUNIQUE (" . implode(', ', $keys) . ")";
+ }
+
+ $sql .= "\n)";
+
+ return $sql;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop Table
+ *
+ * Unsupported feature in SQLite
+ *
+ * @access private
+ * @return bool
+ */
+ function _drop_table($table)
+ {
+ if ($this->db->db_debug)
+ {
+ return $this->db->display_error('db_unsuported_feature');
+ }
+ return array();
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Alter table query
+ *
+ * Generates a platform-specific query so that a table can be altered
+ * Called by add_column(), drop_column(), and column_alter(),
+ *
+ * @access private
+ * @param string the ALTER type (ADD, DROP, CHANGE)
+ * @param string the column name
+ * @param string the table name
+ * @param string the column definition
+ * @param string the default value
+ * @param boolean should 'NOT NULL' be added
+ * @param string the field after which we should add the new field
+ * @return object
+ */
+ function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
+ {
+ $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
+
+ // DROP has everything it needs now.
+ if ($alter_type == 'DROP')
+ {
+ // SQLite does not support dropping columns
+ // http://www.sqlite.org/omitted.html
+ // http://www.sqlite.org/faq.html#q11
+ return FALSE;
+ }
+
+ $sql .= " $column_definition";
+
+ if ($default_value != '')
+ {
+ $sql .= " DEFAULT \"$default_value\"";
+ }
+
+ if ($null === NULL)
+ {
+ $sql .= ' NULL';
+ }
+ else
+ {
+ $sql .= ' NOT NULL';
+ }
+
+ if ($after_field != '')
+ {
+ $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
+ }
+
+ return $sql;
+
+ }
+}
+?> \ No newline at end of file
diff --git a/system/database/drivers/sqlite/sqlite_result.php b/system/database/drivers/sqlite/sqlite_result.php
index caa167813..0939c87b8 100644
--- a/system/database/drivers/sqlite/sqlite_result.php
+++ b/system/database/drivers/sqlite/sqlite_result.php
@@ -167,7 +167,14 @@ class CI_DB_sqlite_result extends CI_DB_result {
}
else
{
- return $this->_fetch_assoc();
+ $arr = sqlite_fetch_array($this->result_id, SQLITE_ASSOC);
+ if (is_array($arr))
+ {
+ $obj = (object) $arr;
+ return $obj;
+ } else {
+ return NULL;
+ }
}
}
diff --git a/system/database/drivers/sqlite/sqlite_utility.php b/system/database/drivers/sqlite/sqlite_utility.php
index 85e74d05f..1c78c54b7 100644
--- a/system/database/drivers/sqlite/sqlite_utility.php
+++ b/system/database/drivers/sqlite/sqlite_utility.php
@@ -24,45 +24,6 @@
*/
class CI_DB_sqlite_utility extends CI_DB_utility {
-
- /**
- * Create database
- *
- * @access public
- * @param string the database name
- * @return bool
- */
- function _create_database()
- {
- // In SQLite, a database is created when you connect to the database.
- // We'll return TRUE so that an error isn't generated
- return TRUE;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Drop database
- *
- * @access private
- * @param string the database name
- * @return bool
- */
- function _drop_database($name)
- {
- if ( ! @file_exists($this->db->database) OR ! @unlink($this->db->database))
- {
- if ($this->db->db_debug)
- {
- return $this->db->display_error('db_unable_to_drop');
- }
- return FALSE;
- }
- return TRUE;
- }
-
- // --------------------------------------------------------------------
-
/**
* List databases
*
@@ -86,25 +47,6 @@ class CI_DB_sqlite_utility extends CI_DB_utility {
// --------------------------------------------------------------------
/**
- * Drop Table
- *
- * Unsupported feature in SQLite
- *
- * @access private
- * @return bool
- */
- function _drop_table($table)
- {
- if ($this->db->db_debug)
- {
- return $this->db->display_error('db_unsuported_feature');
- }
- return array();
- }
-
- // --------------------------------------------------------------------
-
- /**
* Optimize table query
*
* Is optimization even supported in SQLite?
@@ -149,6 +91,49 @@ class CI_DB_sqlite_utility extends CI_DB_utility {
return $this->db->display_error('db_unsuported_feature');
}
-}
+ /**
+ *
+ * The functions below have been deprecated as of 1.6, and are only here for backwards
+ * compatibility. They now reside in dbforge(). The use of dbutils for database manipulation
+ * is STRONGLY discouraged in favour if using dbforge.
+ *
+ */
+
+ /**
+ * Create database
+ *
+ * @access public
+ * @param string the database name
+ * @return bool
+ */
+ function _create_database()
+ {
+ // In SQLite, a database is created when you connect to the database.
+ // We'll return TRUE so that an error isn't generated
+ return TRUE;
+ }
+ // --------------------------------------------------------------------
+
+ /**
+ * Drop database
+ *
+ * @access private
+ * @param string the database name
+ * @return bool
+ */
+ function _drop_database($name)
+ {
+ if ( ! @file_exists($this->db->database) OR ! @unlink($this->db->database))
+ {
+ if ($this->db->db_debug)
+ {
+ return $this->db->display_error('db_unable_to_drop');
+ }
+ return FALSE;
+ }
+ return TRUE;
+ }
+
+}
?> \ No newline at end of file