summaryrefslogtreecommitdiffstats
path: root/system/database
diff options
context:
space:
mode:
Diffstat (limited to 'system/database')
-rwxr-xr-xsystem/database/DB.php12
-rwxr-xr-xsystem/database/DB_active_rec.php300
-rwxr-xr-xsystem/database/DB_driver.php6
-rwxr-xr-xsystem/database/DB_result.php97
-rwxr-xr-xsystem/database/drivers/mysql/mysql_driver.php17
-rwxr-xr-xsystem/database/drivers/mysql/mysql_forge.php8
-rwxr-xr-xsystem/database/drivers/mysqli/mysqli_driver.php17
-rwxr-xr-xsystem/database/drivers/mysqli/mysqli_forge.php8
-rwxr-xr-xsystem/database/drivers/postgre/postgre_driver.php18
-rw-r--r--system/database/drivers/sqlsrv/index.html10
-rw-r--r--system/database/drivers/sqlsrv/sqlsrv_driver.php598
-rw-r--r--system/database/drivers/sqlsrv/sqlsrv_forge.php248
-rw-r--r--system/database/drivers/sqlsrv/sqlsrv_result.php169
-rw-r--r--system/database/drivers/sqlsrv/sqlsrv_utility.php88
14 files changed, 1369 insertions, 227 deletions
diff --git a/system/database/DB.php b/system/database/DB.php
index 8bf1ba8ba..33207d885 100755
--- a/system/database/DB.php
+++ b/system/database/DB.php
@@ -28,11 +28,11 @@ function &DB($params = '', $active_record_override = NULL)
if (is_string($params) AND strpos($params, '://') === FALSE)
{
// Is the config file in the environment folder?
- if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database'.EXT))
+ if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database.php'))
{
- if ( ! file_exists($file_path = APPPATH.'config/database'.EXT))
+ if ( ! file_exists($file_path = APPPATH.'config/database.php'))
{
- show_error('The configuration file database'.EXT.' does not exist.');
+ show_error('The configuration file database.php does not exist.');
}
}
@@ -116,11 +116,11 @@ function &DB($params = '', $active_record_override = NULL)
$active_record = $active_record_override;
}
- require_once(BASEPATH.'database/DB_driver'.EXT);
+ require_once(BASEPATH.'database/DB_driver.php');
if ( ! isset($active_record) OR $active_record == TRUE)
{
- require_once(BASEPATH.'database/DB_active_rec'.EXT);
+ require_once(BASEPATH.'database/DB_active_rec.php');
if ( ! class_exists('CI_DB'))
{
@@ -135,7 +135,7 @@ function &DB($params = '', $active_record_override = NULL)
}
}
- require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);
+ require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php');
// Instantiate the DB adapter
$driver = 'CI_DB_'.$params['dbdriver'].'_driver';
diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php
index db8471364..2af3553ed 100755
--- a/system/database/DB_active_rec.php
+++ b/system/database/DB_active_rec.php
@@ -58,7 +58,9 @@ class CI_DB_active_record extends CI_DB_driver {
var $ar_cache_having = array();
var $ar_cache_orderby = array();
var $ar_cache_set = array();
-
+
+ var $ar_no_escape = array();
+ var $ar_cache_no_escape = array();
// --------------------------------------------------------------------
@@ -67,18 +69,11 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Generates the SELECT portion of the query
*
- * @access public
* @param string
* @return object
*/
- function select($select = '*', $escape = NULL)
+ public function select($select = '*', $escape = NULL)
{
- // Set the global value if this was sepecified
- if (is_bool($escape))
- {
- $this->_protect_identifiers = $escape;
- }
-
if (is_string($select))
{
$select = explode(',', $select);
@@ -91,11 +86,13 @@ class CI_DB_active_record extends CI_DB_driver {
if ($val != '')
{
$this->ar_select[] = $val;
+ $this->ar_no_escape[] = $escape;
if ($this->ar_caching === TRUE)
{
$this->ar_cache_select[] = $val;
$this->ar_cache_exists[] = 'select';
+ $this->ar_cache_no_escape[] = $escape;
}
}
}
@@ -109,12 +106,11 @@ class CI_DB_active_record extends CI_DB_driver {
*
* 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 = '')
+ public function select_max($select = '', $alias = '')
{
return $this->_max_min_avg_sum($select, $alias, 'MAX');
}
@@ -126,12 +122,11 @@ class CI_DB_active_record extends CI_DB_driver {
*
* 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 = '')
+ public function select_min($select = '', $alias = '')
{
return $this->_max_min_avg_sum($select, $alias, 'MIN');
}
@@ -143,12 +138,11 @@ class CI_DB_active_record extends CI_DB_driver {
*
* 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 = '')
+ public function select_avg($select = '', $alias = '')
{
return $this->_max_min_avg_sum($select, $alias, 'AVG');
}
@@ -160,12 +154,11 @@ class CI_DB_active_record extends CI_DB_driver {
*
* 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 = '')
+ public function select_sum($select = '', $alias = '')
{
return $this->_max_min_avg_sum($select, $alias, 'SUM');
}
@@ -180,12 +173,11 @@ class CI_DB_active_record extends CI_DB_driver {
* select_avg()
* select_sum()
*
- * @access public
* @param string the field
* @param string an alias
* @return object
*/
- function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
+ protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
{
if ( ! is_string($select) OR $select == '')
{
@@ -222,11 +214,10 @@ class CI_DB_active_record extends CI_DB_driver {
/**
* Determines the alias name based on the table
*
- * @access private
* @param string
* @return string
*/
- function _create_alias_from_table($item)
+ protected function _create_alias_from_table($item)
{
if (strpos($item, '.') !== FALSE)
{
@@ -243,11 +234,10 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Sets a flag which tells the query string compiler to add DISTINCT
*
- * @access public
* @param bool
* @return object
*/
- function distinct($val = TRUE)
+ public function distinct($val = TRUE)
{
$this->ar_distinct = (is_bool($val)) ? $val : TRUE;
return $this;
@@ -260,11 +250,10 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Generates the FROM portion of the query
*
- * @access public
* @param mixed can be a string or array
* @return object
*/
- function from($from)
+ public function from($from)
{
foreach ((array)$from as $val)
{
@@ -313,13 +302,12 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Generates the JOIN portion of the query
*
- * @access public
* @param string
* @param string the join condition
* @param string the type of join
* @return object
*/
- function join($table, $cond, $type = '')
+ public function join($table, $cond, $type = '')
{
if ($type != '')
{
@@ -369,12 +357,11 @@ class CI_DB_active_record extends CI_DB_driver {
* Generates the WHERE portion of the query. Separates
* multiple calls with AND
*
- * @access public
* @param mixed
* @param mixed
* @return object
*/
- function where($key, $value = NULL, $escape = TRUE)
+ public function where($key, $value = NULL, $escape = TRUE)
{
return $this->_where($key, $value, 'AND ', $escape);
}
@@ -387,12 +374,11 @@ class CI_DB_active_record extends CI_DB_driver {
* Generates the WHERE portion of the query. Separates
* multiple calls with OR
*
- * @access public
* @param mixed
* @param mixed
* @return object
*/
- function or_where($key, $value = NULL, $escape = TRUE)
+ public function or_where($key, $value = NULL, $escape = TRUE)
{
return $this->_where($key, $value, 'OR ', $escape);
}
@@ -402,15 +388,14 @@ class CI_DB_active_record extends CI_DB_driver {
/**
* Where
*
- * Called by where() or orwhere()
+ * Called by where() or or_where()
*
- * @access private
* @param mixed
* @param mixed
* @param string
* @return object
*/
- function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
+ protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
{
if ( ! is_array($key))
{
@@ -441,10 +426,10 @@ class CI_DB_active_record extends CI_DB_driver {
$v = ' '.$this->escape($v);
}
-
+
if ( ! $this->_has_operator($k))
{
- $k .= ' =';
+ $k .= ' = ';
}
}
else
@@ -473,12 +458,11 @@ class CI_DB_active_record extends CI_DB_driver {
* Generates a WHERE field IN ('item', 'item') SQL query joined with
* AND if appropriate
*
- * @access public
* @param string The field to search
* @param array The values searched on
* @return object
*/
- function where_in($key = NULL, $values = NULL)
+ public function where_in($key = NULL, $values = NULL)
{
return $this->_where_in($key, $values);
}
@@ -491,12 +475,11 @@ class CI_DB_active_record extends CI_DB_driver {
* Generates a WHERE field IN ('item', 'item') SQL query joined with
* OR if appropriate
*
- * @access public
* @param string The field to search
* @param array The values searched on
* @return object
*/
- function or_where_in($key = NULL, $values = NULL)
+ public function or_where_in($key = NULL, $values = NULL)
{
return $this->_where_in($key, $values, FALSE, 'OR ');
}
@@ -509,12 +492,11 @@ class CI_DB_active_record extends CI_DB_driver {
* Generates a WHERE field NOT IN ('item', 'item') SQL query joined
* with AND if appropriate
*
- * @access public
* @param string The field to search
* @param array The values searched on
* @return object
*/
- function where_not_in($key = NULL, $values = NULL)
+ public function where_not_in($key = NULL, $values = NULL)
{
return $this->_where_in($key, $values, TRUE);
}
@@ -527,12 +509,11 @@ class CI_DB_active_record extends CI_DB_driver {
* Generates a WHERE field NOT IN ('item', 'item') SQL query joined
* with OR if appropriate
*
- * @access public
* @param string The field to search
* @param array The values searched on
* @return object
*/
- function or_where_not_in($key = NULL, $values = NULL)
+ public function or_where_not_in($key = NULL, $values = NULL)
{
return $this->_where_in($key, $values, TRUE, 'OR ');
}
@@ -544,14 +525,13 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Called by where_in, where_in_or, where_not_in, where_not_in_or
*
- * @access public
* @param string The field to search
* @param array The values searched on
* @param boolean If the statement would be IN or NOT IN
* @param string
* @return object
*/
- function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
+ protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
{
if ($key === NULL OR $values === NULL)
{
@@ -594,12 +574,11 @@ class CI_DB_active_record extends CI_DB_driver {
* Generates a %LIKE% portion of the query. Separates
* multiple calls with AND
*
- * @access public
* @param mixed
* @param mixed
* @return object
*/
- function like($field, $match = '', $side = 'both')
+ public function like($field, $match = '', $side = 'both')
{
return $this->_like($field, $match, 'AND ', $side);
}
@@ -612,12 +591,11 @@ class CI_DB_active_record extends CI_DB_driver {
* Generates a NOT LIKE portion of the query. Separates
* multiple calls with AND
*
- * @access public
* @param mixed
* @param mixed
* @return object
*/
- function not_like($field, $match = '', $side = 'both')
+ public function not_like($field, $match = '', $side = 'both')
{
return $this->_like($field, $match, 'AND ', $side, 'NOT');
}
@@ -630,12 +608,11 @@ class CI_DB_active_record extends CI_DB_driver {
* Generates a %LIKE% portion of the query. Separates
* multiple calls with OR
*
- * @access public
* @param mixed
* @param mixed
* @return object
*/
- function or_like($field, $match = '', $side = 'both')
+ public function or_like($field, $match = '', $side = 'both')
{
return $this->_like($field, $match, 'OR ', $side);
}
@@ -648,12 +625,11 @@ class CI_DB_active_record extends CI_DB_driver {
* Generates a NOT LIKE portion of the query. Separates
* multiple calls with OR
*
- * @access public
* @param mixed
* @param mixed
* @return object
*/
- function or_not_like($field, $match = '', $side = 'both')
+ public function or_not_like($field, $match = '', $side = 'both')
{
return $this->_like($field, $match, 'OR ', $side, 'NOT');
}
@@ -665,13 +641,12 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Called by like() or orlike()
*
- * @access private
* @param mixed
* @param mixed
* @param string
* @return object
*/
- function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
+ protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
{
if ( ! is_array($field))
{
@@ -721,11 +696,10 @@ class CI_DB_active_record extends CI_DB_driver {
/**
* GROUP BY
*
- * @access public
* @param string
* @return object
*/
- function group_by($by)
+ public function group_by($by)
{
if (is_string($by))
{
@@ -757,12 +731,11 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Separates multiple calls with AND
*
- * @access public
* @param string
* @param string
* @return object
*/
- function having($key, $value = '', $escape = TRUE)
+ public function having($key, $value = '', $escape = TRUE)
{
return $this->_having($key, $value, 'AND ', $escape);
}
@@ -774,12 +747,11 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Separates multiple calls with OR
*
- * @access public
* @param string
* @param string
* @return object
*/
- function or_having($key, $value = '', $escape = TRUE)
+ public function or_having($key, $value = '', $escape = TRUE)
{
return $this->_having($key, $value, 'OR ', $escape);
}
@@ -791,12 +763,11 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Called by having() or or_having()
*
- * @access private
* @param string
* @param string
* @return object
*/
- function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
+ protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
{
if ( ! is_array($key))
{
@@ -819,7 +790,7 @@ class CI_DB_active_record extends CI_DB_driver {
if ($v != '')
{
- $v = ' '.$this->escape_str($v);
+ $v = ' '.$this->escape($v);
}
$this->ar_having[] = $prefix.$k.$v;
@@ -838,12 +809,11 @@ class CI_DB_active_record extends CI_DB_driver {
/**
* Sets the ORDER BY value
*
- * @access public
* @param string
* @param string direction: asc or desc
* @return object
*/
- function order_by($orderby, $direction = '')
+ public function order_by($orderby, $direction = '')
{
if (strtolower($direction) == 'random')
{
@@ -894,12 +864,11 @@ class CI_DB_active_record extends CI_DB_driver {
/**
* Sets the LIMIT value
*
- * @access public
* @param integer the limit value
* @param integer the offset value
* @return object
*/
- function limit($value, $offset = '')
+ public function limit($value, $offset = '')
{
$this->ar_limit = $value;
@@ -916,11 +885,10 @@ class CI_DB_active_record extends CI_DB_driver {
/**
* Sets the OFFSET value
*
- * @access public
* @param integer the offset value
* @return object
*/
- function offset($offset)
+ public function offset($offset)
{
$this->ar_offset = $offset;
return $this;
@@ -931,13 +899,12 @@ class CI_DB_active_record extends CI_DB_driver {
/**
* The "set" function. Allows key/value pairs to be set for inserting or updating
*
- * @access public
* @param mixed
* @param string
* @param boolean
* @return object
*/
- function set($key, $value = '', $escape = TRUE)
+ public function set($key, $value = '', $escape = TRUE)
{
$key = $this->_object_to_array($key);
@@ -969,13 +936,12 @@ class CI_DB_active_record extends CI_DB_driver {
* Compiles the select statement based on the other functions called
* and runs the query
*
- * @access public
* @param string the table
* @param string the limit clause
* @param string the offset clause
* @return object
*/
- function get($table = '', $limit = null, $offset = null)
+ public function get($table = '', $limit = null, $offset = null)
{
if ($table != '')
{
@@ -1001,11 +967,10 @@ class CI_DB_active_record extends CI_DB_driver {
* Generates a platform-specific query string that counts all records
* returned by an Active Record query.
*
- * @access public
* @param string
* @return string
*/
- function count_all_results($table = '')
+ public function count_all_results($table = '')
{
if ($table != '')
{
@@ -1034,13 +999,12 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Allows the where clause, limit and offset to be added directly
*
- * @access public
* @param string the where clause
* @param string the limit clause
* @param string the offset clause
* @return object
*/
- function get_where($table = '', $where = null, $limit = null, $offset = null)
+ public function get_where($table = '', $where = null, $limit = null, $offset = null)
{
if ($table != '')
{
@@ -1071,12 +1035,11 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Compiles batch insert strings and runs the queries
*
- * @access public
* @param string the table to retrieve the results from
* @param array an associative array of insert values
* @return object
*/
- function insert_batch($table = '', $set = NULL)
+ public function insert_batch($table = '', $set = NULL)
{
if ( ! is_null($set))
{
@@ -1129,14 +1092,12 @@ class CI_DB_active_record extends CI_DB_driver {
/**
* The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
*
- * @access public
* @param mixed
* @param string
* @param boolean
* @return object
*/
-
- function set_insert_batch($key, $value = '', $escape = TRUE)
+ public function set_insert_batch($key, $value = '', $escape = TRUE)
{
$key = $this->_object_to_array_batch($key);
@@ -1191,8 +1152,7 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Compiles an insert string and runs the query
*
- * @access public
- * @param string the table to retrieve the results from
+ * @param string the table to insert data into
* @param array an associative array of insert values
* @return object
*/
@@ -1232,7 +1192,18 @@ class CI_DB_active_record extends CI_DB_driver {
return $this->query($sql);
}
- function replace($table = '', $set = NULL)
+ // --------------------------------------------------------------------
+
+ /**
+ * Replace
+ *
+ * Compiles an replace into string and runs the query
+ *
+ * @param string the table to replace data into
+ * @param array an associative array of insert values
+ * @return object
+ */
+ public function replace($table = '', $set = NULL)
{
if ( ! is_null($set))
{
@@ -1275,13 +1246,12 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Compiles an update string and runs the query
*
- * @access public
* @param string the table to retrieve the results from
* @param array an associative array of update values
* @param mixed the where clause
* @return object
*/
- function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
+ public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
{
// Combine any cached components with the current statements
$this->_merge_cache();
@@ -1338,13 +1308,12 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Compiles an update string and runs the query
*
- * @access public
* @param string the table to retrieve the results from
* @param array an associative array of update values
* @param string the where key
* @return object
*/
- function update_batch($table = '', $set = NULL, $index = NULL)
+ public function update_batch($table = '', $set = NULL, $index = NULL)
{
// Combine any cached components with the current statements
$this->_merge_cache();
@@ -1404,14 +1373,12 @@ class CI_DB_active_record extends CI_DB_driver {
/**
* The "set_update_batch" function. Allows key/value pairs to be set for batch updating
*
- * @access public
* @param array
* @param string
* @param boolean
* @return object
*/
-
- function set_update_batch($key, $index = '', $escape = TRUE)
+ public function set_update_batch($key, $index = '', $escape = TRUE)
{
$key = $this->_object_to_array_batch($key);
@@ -1464,11 +1431,10 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Compiles a delete string and runs "DELETE FROM table"
*
- * @access public
* @param string the table to empty
* @return object
*/
- function empty_table($table = '')
+ public function empty_table($table = '')
{
if ($table == '')
{
@@ -1504,11 +1470,10 @@ class CI_DB_active_record extends CI_DB_driver {
* 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 = '')
+ public function truncate($table = '')
{
if ($table == '')
{
@@ -1542,14 +1507,13 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Compiles a delete string and runs the query
*
- * @access public
* @param mixed the table(s) to delete from. String or array
* @param mixed the where clause
* @param mixed the limit clause
* @param boolean
* @return object
*/
- function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
+ public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
{
// Combine any cached components with the current statements
$this->_merge_cache();
@@ -1619,11 +1583,10 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Prepends a database prefix if one exists in configuration
*
- * @access public
* @param string the table
* @return string
*/
- function dbprefix($table = '')
+ public function dbprefix($table = '')
{
if ($table == '')
{
@@ -1636,15 +1599,29 @@ class CI_DB_active_record extends CI_DB_driver {
// --------------------------------------------------------------------
/**
+ * Set DB Prefix
+ *
+ * Set's the DB Prefix to something new without needing to reconnect
+ *
+ * @param string the prefix
+ * @return string
+ */
+ public function set_dbprefix($prefix = '')
+ {
+ return $this->dbprefix = $prefix;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Track Aliases
*
* Used to track SQL statements written with aliased tables.
*
- * @access private
* @param string The table to inspect
* @return string
*/
- function _track_aliases($table)
+ protected function _track_aliases($table)
{
if (is_array($table))
{
@@ -1687,10 +1664,9 @@ class CI_DB_active_record extends CI_DB_driver {
* Generates a query string based on which functions were used.
* Should not be called directly. The get() function calls it.
*
- * @access private
* @return string
*/
- function _compile_select($select_override = FALSE)
+ protected function _compile_select($select_override = FALSE)
{
// Combine any cached components with the current statements
$this->_merge_cache();
@@ -1718,7 +1694,8 @@ class CI_DB_active_record extends CI_DB_driver {
// is because until the user calls the from() function we don't know if there are aliases
foreach ($this->ar_select as $key => $val)
{
- $this->ar_select[$key] = $this->_protect_identifiers($val);
+ $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL;
+ $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape);
}
$sql .= implode(', ', $this->ar_select);
@@ -1753,9 +1730,7 @@ class CI_DB_active_record extends CI_DB_driver {
if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
{
- $sql .= "\n";
-
- $sql .= "WHERE ";
+ $sql .= "\nWHERE ";
}
$sql .= implode("\n", $this->ar_where);
@@ -1830,11 +1805,10 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Takes an object as input and converts the class variables to array key/vals
*
- * @access public
* @param object
* @return array
*/
- function _object_to_array($object)
+ public function _object_to_array($object)
{
if ( ! is_object($object))
{
@@ -1861,11 +1835,10 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Takes an object as input and converts the class variables to array key/vals
*
- * @access public
* @param object
* @return array
*/
- function _object_to_array_batch($object)
+ public function _object_to_array_batch($object)
{
if ( ! is_object($object))
{
@@ -1901,10 +1874,9 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Starts AR caching
*
- * @access public
* @return void
*/
- function start_cache()
+ public function start_cache()
{
$this->ar_caching = TRUE;
}
@@ -1916,10 +1888,9 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Stops AR caching
*
- * @access public
* @return void
*/
- function stop_cache()
+ public function stop_cache()
{
$this->ar_caching = FALSE;
}
@@ -1934,22 +1905,21 @@ class CI_DB_active_record extends CI_DB_driver {
* @access public
* @return void
*/
- function flush_cache()
+ public function flush_cache()
{
- $this->_reset_run(
- array(
- 'ar_cache_select' => array(),
- 'ar_cache_from' => array(),
- 'ar_cache_join' => array(),
- 'ar_cache_where' => array(),
- 'ar_cache_like' => array(),
- 'ar_cache_groupby' => array(),
- 'ar_cache_having' => array(),
- 'ar_cache_orderby' => array(),
- 'ar_cache_set' => array(),
- 'ar_cache_exists' => array()
- )
- );
+ $this->_reset_run(array(
+ 'ar_cache_select' => array(),
+ 'ar_cache_from' => array(),
+ 'ar_cache_join' => array(),
+ 'ar_cache_where' => array(),
+ 'ar_cache_like' => array(),
+ 'ar_cache_groupby' => array(),
+ 'ar_cache_having' => array(),
+ 'ar_cache_orderby' => array(),
+ 'ar_cache_set' => array(),
+ 'ar_cache_exists' => array(),
+ 'ar_cache_no_escape' => array()
+ ));
}
// --------------------------------------------------------------------
@@ -1960,10 +1930,9 @@ class CI_DB_active_record extends CI_DB_driver {
* When called, this function merges any cached AR arrays with
* locally called ones.
*
- * @access private
* @return void
*/
- function _merge_cache()
+ protected function _merge_cache()
{
if (count($this->ar_cache_exists) == 0)
{
@@ -1989,6 +1958,8 @@ class CI_DB_active_record extends CI_DB_driver {
{
$this->_track_aliases($this->ar_from);
}
+
+ $this->ar_no_escape = $this->ar_cache_no_escape;
}
// --------------------------------------------------------------------
@@ -1996,11 +1967,10 @@ class CI_DB_active_record extends CI_DB_driver {
/**
* Resets the active record values. Called by the get() function
*
- * @access private
* @param array An array of fields to reset
* @return void
*/
- function _reset_run($ar_reset_items)
+ protected function _reset_run($ar_reset_items)
{
foreach ($ar_reset_items as $item => $default_value)
{
@@ -2016,27 +1986,27 @@ class CI_DB_active_record extends CI_DB_driver {
/**
* Resets the active record values. Called by the get() function
*
- * @access private
* @return void
*/
- function _reset_select()
+ protected function _reset_select()
{
$ar_reset_items = array(
- 'ar_select' => array(),
- 'ar_from' => array(),
- 'ar_join' => array(),
- 'ar_where' => array(),
- 'ar_like' => array(),
- 'ar_groupby' => array(),
- 'ar_having' => array(),
- 'ar_orderby' => array(),
- 'ar_wherein' => array(),
- 'ar_aliased_tables' => array(),
- 'ar_distinct' => FALSE,
- 'ar_limit' => FALSE,
- 'ar_offset' => FALSE,
- 'ar_order' => FALSE,
- );
+ 'ar_select' => array(),
+ 'ar_from' => array(),
+ 'ar_join' => array(),
+ 'ar_where' => array(),
+ 'ar_like' => array(),
+ 'ar_groupby' => array(),
+ 'ar_having' => array(),
+ 'ar_orderby' => array(),
+ 'ar_wherein' => array(),
+ 'ar_aliased_tables' => array(),
+ 'ar_no_escape' => array(),
+ 'ar_distinct' => FALSE,
+ 'ar_limit' => FALSE,
+ 'ar_offset' => FALSE,
+ 'ar_order' => FALSE,
+ );
$this->_reset_run($ar_reset_items);
}
@@ -2048,25 +2018,23 @@ class CI_DB_active_record extends CI_DB_driver {
*
* Called by the insert() update() insert_batch() update_batch() and delete() functions
*
- * @access private
* @return void
*/
- function _reset_write()
+ protected function _reset_write()
{
$ar_reset_items = array(
- 'ar_set' => array(),
- 'ar_from' => array(),
- 'ar_where' => array(),
- 'ar_like' => array(),
- 'ar_orderby' => array(),
- 'ar_keys' => array(),
- 'ar_limit' => FALSE,
- 'ar_order' => FALSE
- );
+ 'ar_set' => array(),
+ 'ar_from' => array(),
+ 'ar_where' => array(),
+ 'ar_like' => array(),
+ 'ar_orderby' => array(),
+ 'ar_keys' => array(),
+ 'ar_limit' => FALSE,
+ 'ar_order' => FALSE
+ );
$this->_reset_run($ar_reset_items);
}
-
}
/* End of file DB_active_rec.php */
diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php
index e7a9de475..10e8ed0c0 100755
--- a/system/database/DB_driver.php
+++ b/system/database/DB_driver.php
@@ -424,8 +424,8 @@ class CI_DB_driver {
if ( ! class_exists($driver))
{
- include_once(BASEPATH.'database/DB_result'.EXT);
- include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT);
+ include_once(BASEPATH.'database/DB_result.php');
+ include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
}
return $driver;
@@ -1115,7 +1115,7 @@ class CI_DB_driver {
if ( ! class_exists('CI_DB_Cache'))
{
- if ( ! @include(BASEPATH.'database/DB_cache'.EXT))
+ if ( ! @include(BASEPATH.'database/DB_cache.php'))
{
return $this->cache_off();
}
diff --git a/system/database/DB_result.php b/system/database/DB_result.php
index 76e1d6abb..0c4e78105 100755
--- a/system/database/DB_result.php
+++ b/system/database/DB_result.php
@@ -28,14 +28,14 @@
*/
class CI_DB_result {
- var $conn_id = NULL;
- var $result_id = NULL;
- var $result_array = array();
- var $result_object = array();
- var $custom_result_object = array();
- var $current_row = 0;
- var $num_rows = 0;
- var $row_data = NULL;
+ var $conn_id = NULL;
+ var $result_id = NULL;
+ var $result_array = array();
+ var $result_object = array();
+ var $custom_result_object = array();
+ var $current_row = 0;
+ var $num_rows = 0;
+ var $row_data = NULL;
/**
@@ -47,47 +47,52 @@ class CI_DB_result {
*/
function result($type = 'object')
{
- if ($type == 'array') return $this->result_array();
- else if ($type == 'object') return $this->result_object();
- else return $this->custom_result_object($type);
+ if ($type == 'array') return $this->result_array();
+ else if ($type == 'object') return $this->result_object();
+ else return $this->custom_result_object($type);
}
// --------------------------------------------------------------------
- /**
- * Custom query result.
- *
- * @param class_name A string that represents the type of object you want back
- * @return array of objects
- */
- function custom_result_object($class_name)
- {
- if (array_key_exists($class_name, $this->custom_result_object))
- {
- return $this->custom_result_object[$class_name];
- }
-
- if ($this->result_id === FALSE OR $this->num_rows() == 0)
- {
- return array();
- }
-
- // add the data to the object
- $this->_data_seek(0);
- $result_object = array();
+ /**
+ * Custom query result.
+ *
+ * @param class_name A string that represents the type of object you want back
+ * @return array of objects
+ */
+ function custom_result_object($class_name)
+ {
+ if (array_key_exists($class_name, $this->custom_result_object))
+ {
+ return $this->custom_result_object[$class_name];
+ }
+
+ if ($this->result_id === FALSE OR $this->num_rows() == 0)
+ {
+ return array();
+ }
+
+ // add the data to the object
+ $this->_data_seek(0);
+ $result_object = array();
+
while ($row = $this->_fetch_object())
- {
- $object = new $class_name();
- foreach ($row as $key => $value)
- {
- $object->$key = $value;
- }
+ {
+ $object = new $class_name();
+
+ foreach ($row as $key => $value)
+ {
+ $object->$key = $value;
+ }
+
$result_object[] = $object;
}
- // return the array
- return $this->custom_result_object[$class_name] = $result_object;
- }
+ // return the array
+ return $this->custom_result_object[$class_name] = $result_object;
+ }
+
+ // --------------------------------------------------------------------
/**
* Query result. "object" version.
@@ -180,9 +185,9 @@ class CI_DB_result {
$n = 0;
}
- if ($type == 'object') return $this->row_object($n);
- else if ($type == 'array') return $this->row_array($n);
- else return $this->custom_row_object($n, $type);
+ if ($type == 'object') return $this->row_object($n);
+ else if ($type == 'array') return $this->row_array($n);
+ else return $this->custom_row_object($n, $type);
}
// --------------------------------------------------------------------
@@ -219,7 +224,7 @@ class CI_DB_result {
// --------------------------------------------------------------------
- /**
+ /**
* Returns a single result row - custom object version
*
* @access public
@@ -242,7 +247,7 @@ class CI_DB_result {
return $result[$this->current_row];
}
- /**
+ /**
* Returns a single result row - object version
*
* @access public
diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php
index 4ff9b0a11..b7d547cc0 100755
--- a/system/database/drivers/mysql/mysql_driver.php
+++ b/system/database/drivers/mysql/mysql_driver.php
@@ -132,7 +132,22 @@ class CI_DB_mysql_driver extends CI_DB {
*/
function db_set_charset($charset, $collation)
{
- return @mysql_query("SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'", $this->conn_id);
+ static $use_set_names;
+
+ if ( ! isset($use_set_names))
+ {
+ // mysql_set_charset() requires PHP >= 5.2.3 and MySQL >= 5.0.7, use SET NAMES as fallback
+ $use_set_names = (version_compare(PHP_VERSION, '5.2.3', '>=') && version_compare(mysql_get_server_info(), '5.0.7', '>=')) ? FALSE : TRUE;
+ }
+
+ if ($use_set_names)
+ {
+ return @mysql_query("SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'", $this->conn_id);
+ }
+ else
+ {
+ return @mysql_set_charset($charset, $this->conn_id);
+ }
}
// --------------------------------------------------------------------
diff --git a/system/database/drivers/mysql/mysql_forge.php b/system/database/drivers/mysql/mysql_forge.php
index 529ec980d..c1cae136c 100755
--- a/system/database/drivers/mysql/mysql_forge.php
+++ b/system/database/drivers/mysql/mysql_forge.php
@@ -119,9 +119,13 @@ class CI_DB_mysql_forge extends CI_DB_forge {
$sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
}
- if (array_key_exists('NULL', $attributes))
+ if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
{
- $sql .= ($attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL';
+ $sql .= ' NULL';
+ }
+ else
+ {
+ $sql .= ' NOT NULL';
}
if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php
index ccdabce1a..b8586c21d 100755
--- a/system/database/drivers/mysqli/mysqli_driver.php
+++ b/system/database/drivers/mysqli/mysqli_driver.php
@@ -132,7 +132,22 @@ class CI_DB_mysqli_driver extends CI_DB {
*/
function _db_set_charset($charset, $collation)
{
- return @mysqli_query($this->conn_id, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'");
+ static $use_set_names;
+
+ if ( ! isset($use_set_names))
+ {
+ // mysqli_set_charset() requires MySQL >= 5.0.7, use SET NAMES as fallback
+ $use_set_names = (version_compare(mysqli_get_server_info($this->conn_id), '5.0.7', '>=')) ? FALSE : TRUE;
+ }
+
+ if ($use_set_names)
+ {
+ return @mysqli_query($this->conn_id, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'");
+ }
+ else
+ {
+ return @mysqli_set_charset($this->conn_id, $charset);
+ }
}
// --------------------------------------------------------------------
diff --git a/system/database/drivers/mysqli/mysqli_forge.php b/system/database/drivers/mysqli/mysqli_forge.php
index d5097335e..260549457 100755
--- a/system/database/drivers/mysqli/mysqli_forge.php
+++ b/system/database/drivers/mysqli/mysqli_forge.php
@@ -104,9 +104,13 @@ class CI_DB_mysqli_forge extends CI_DB_forge {
$sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
}
- if (array_key_exists('NULL', $attributes))
+ if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
{
- $sql .= ($attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL';
+ $sql .= ' NULL';
+ }
+ else
+ {
+ $sql .= ' NOT NULL';
}
if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php
index 47ff36246..140396885 100755
--- a/system/database/drivers/postgre/postgre_driver.php
+++ b/system/database/drivers/postgre/postgre_driver.php
@@ -554,6 +554,24 @@ class CI_DB_postgre_driver extends CI_DB {
// --------------------------------------------------------------------
/**
+ * Insert_batch statement
+ *
+ * Generates a platform-specific insert string from the supplied data
+ *
+ * @access public
+ * @param string the table name
+ * @param array the insert keys
+ * @param array the insert values
+ * @return string
+ */
+ function _insert_batch($table, $keys, $values)
+ {
+ return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Update statement
*
* Generates a platform-specific update string from the supplied data
diff --git a/system/database/drivers/sqlsrv/index.html b/system/database/drivers/sqlsrv/index.html
new file mode 100644
index 000000000..c942a79ce
--- /dev/null
+++ b/system/database/drivers/sqlsrv/index.html
@@ -0,0 +1,10 @@
+<html>
+<head>
+ <title>403 Forbidden</title>
+</head>
+<body>
+
+<p>Directory access is forbidden.</p>
+
+</body>
+</html> \ No newline at end of file
diff --git a/system/database/drivers/sqlsrv/sqlsrv_driver.php b/system/database/drivers/sqlsrv/sqlsrv_driver.php
new file mode 100644
index 000000000..1d32792ce
--- /dev/null
+++ b/system/database/drivers/sqlsrv/sqlsrv_driver.php
@@ -0,0 +1,598 @@
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 5.1.6 or newer
+ *
+ * @package CodeIgniter
+ * @author ExpressionEngine Dev Team
+ * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
+ * @license http://codeigniter.com/user_guide/license.html
+ * @link http://codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * SQLSRV Database Adapter Class
+ *
+ * Note: _DB is an extender class that the app controller
+ * creates dynamically based on whether the active record
+ * class is being used or not.
+ *
+ * @package CodeIgniter
+ * @subpackage Drivers
+ * @category Database
+ * @author ExpressionEngine Dev Team
+ * @link http://codeigniter.com/user_guide/database/
+ */
+class CI_DB_sqlsrv_driver extends CI_DB {
+
+ var $dbdriver = 'sqlsrv';
+
+ // The character used for escaping
+ var $_escape_char = '';
+
+ // clause and character used for LIKE escape sequences
+ var $_like_escape_str = " ESCAPE '%s' ";
+ var $_like_escape_chr = '!';
+
+ /**
+ * The syntax to count rows is slightly different across different
+ * 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 ";
+ var $_random_keyword = ' ASC'; // not currently supported
+
+ /**
+ * Non-persistent database connection
+ *
+ * @access private called by the base class
+ * @return resource
+ */
+ function db_connect($pooling = false)
+ {
+ // Check for a UTF-8 charset being passed as CI's default 'utf8'.
+ $character_set = (0 === strcasecmp('utf8', $this->char_set)) ? 'UTF-8' : $this->char_set;
+
+ $connection = array(
+ 'UID' => empty($this->username) ? '' : $this->username,
+ 'PWD' => empty($this->password) ? '' : $this->password,
+ 'Database' => $this->database,
+ 'ConnectionPooling' => $pooling ? 1 : 0,
+ 'CharacterSet' => $character_set,
+ 'ReturnDatesAsStrings' => 1
+ );
+
+ // If the username and password are both empty, assume this is a
+ // 'Windows Authentication Mode' connection.
+ if(empty($connection['UID']) && empty($connection['PWD'])) {
+ unset($connection['UID'], $connection['PWD']);
+ }
+
+ return sqlsrv_connect($this->hostname, $connection);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Persistent database connection
+ *
+ * @access private called by the base class
+ * @return resource
+ */
+ function db_pconnect()
+ {
+ $this->db_connect(TRUE);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Reconnect
+ *
+ * Keep / reestablish the db connection if no queries have been
+ * sent for a length of time exceeding the server's idle timeout
+ *
+ * @access public
+ * @return void
+ */
+ function reconnect()
+ {
+ // not implemented in MSSQL
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Select the database
+ *
+ * @access private called by the base class
+ * @return resource
+ */
+ function db_select()
+ {
+ return $this->_execute('USE ' . $this->database);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * 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
+ * @param string an SQL query
+ * @return resource
+ */
+ function _execute($sql)
+ {
+ $sql = $this->_prep_query($sql);
+ return sqlsrv_query($this->conn_id, $sql, null, array(
+ 'Scrollable' => SQLSRV_CURSOR_STATIC,
+ 'SendStreamParamsAtExec' => true
+ ));
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Prep the query
+ *
+ * If needed, each database adapter can prep the query string
+ *
+ * @access private called by execute()
+ * @param string an SQL query
+ * @return string
+ */
+ function _prep_query($sql)
+ {
+ return $sql;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Begin Transaction
+ *
+ * @access public
+ * @return bool
+ */
+ function trans_begin($test_mode = FALSE)
+ {
+ if ( ! $this->trans_enabled)
+ {
+ return TRUE;
+ }
+
+ // When transactions are nested we only begin/commit/rollback the outermost ones
+ if ($this->_trans_depth > 0)
+ {
+ return TRUE;
+ }
+
+ // Reset the transaction failure flag.
+ // If the $test_mode flag is set to TRUE transactions will be rolled back
+ // even if the queries produce a successful result.
+ $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
+
+ return sqlsrv_begin_transaction($this->conn_id);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Commit Transaction
+ *
+ * @access public
+ * @return bool
+ */
+ function trans_commit()
+ {
+ if ( ! $this->trans_enabled)
+ {
+ return TRUE;
+ }
+
+ // When transactions are nested we only begin/commit/rollback the outermost ones
+ if ($this->_trans_depth > 0)
+ {
+ return TRUE;
+ }
+
+ return sqlsrv_commit($this->conn_id);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Rollback Transaction
+ *
+ * @access public
+ * @return bool
+ */
+ function trans_rollback()
+ {
+ if ( ! $this->trans_enabled)
+ {
+ return TRUE;
+ }
+
+ // When transactions are nested we only begin/commit/rollback the outermost ones
+ if ($this->_trans_depth > 0)
+ {
+ return TRUE;
+ }
+
+ return sqlsrv_rollback($this->conn_id);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Escape String
+ *
+ * @access public
+ * @param string
+ * @param bool whether or not the string will be used in a LIKE condition
+ * @return string
+ */
+ function escape_str($str, $like = FALSE)
+ {
+ // Escape single quotes
+ return str_replace("'", "''", $str);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Affected Rows
+ *
+ * @access public
+ * @return integer
+ */
+ function affected_rows()
+ {
+ return @sqlrv_rows_affected($this->conn_id);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Insert ID
+ *
+ * Returns the last id created in the Identity column.
+ *
+ * @access public
+ * @return integer
+ */
+ function insert_id()
+ {
+ return $this->query('select @@IDENTITY as insert_id')->row('insert_id');
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Parse major version
+ *
+ * Grabs the major version number from the
+ * database server version string passed in.
+ *
+ * @access private
+ * @param string $version
+ * @return int16 major version number
+ */
+ function _parse_major_version($version)
+ {
+ preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
+ return $ver_info[1]; // return the major version b/c that's all we're interested in.
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Version number query string
+ *
+ * @access public
+ * @return string
+ */
+ function _version()
+ {
+ $info = sqlsrv_server_info($this->conn_id);
+ return sprintf("select '%s' as ver", $info['SQLServerVersion']);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * "Count All" query
+ *
+ * Generates a platform-specific query string that counts all records in
+ * the specified database
+ *
+ * @access public
+ * @param string
+ * @return string
+ */
+ function count_all($table = '')
+ {
+ if ($table == '')
+ return '0';
+
+ $query = $this->query("SELECT COUNT(*) AS numrows FROM " . $this->dbprefix . $table);
+
+ if ($query->num_rows() == 0)
+ return '0';
+
+ $row = $query->row();
+ return $row->numrows;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * List table query
+ *
+ * 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)
+ {
+ return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * List column query
+ *
+ * Generates a platform-specific query string so that the column names can be fetched
+ *
+ * @access private
+ * @param string the table name
+ * @return string
+ */
+ function _list_columns($table = '')
+ {
+ return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'";
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Field data query
+ *
+ * Generates a platform-specific query so that the column data can be retrieved
+ *
+ * @access public
+ * @param string the table name
+ * @return object
+ */
+ function _field_data($table)
+ {
+ return "SELECT TOP 1 * FROM " . $this->_escape_table($table);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * The error message string
+ *
+ * @access private
+ * @return string
+ */
+ function _error_message()
+ {
+ $error = array_shift(sqlsrv_errors());
+ return !empty($error['message']) ? $error['message'] : null;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * The error message number
+ *
+ * @access private
+ * @return integer
+ */
+ function _error_number()
+ {
+ $error = array_shift(sqlsrv_errors());
+ return isset($error['SQLSTATE']) ? $error['SQLSTATE'] : null;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Escape Table Name
+ *
+ * This function adds backticks if the table name has a period
+ * in it. Some DBs will get cranky unless periods are escaped
+ *
+ * @access private
+ * @param string the table name
+ * @return string
+ */
+ function _escape_table($table)
+ {
+ return $table;
+ }
+
+
+ /**
+ * Escape the SQL Identifiers
+ *
+ * This function escapes column and table names
+ *
+ * @access private
+ * @param string
+ * @return string
+ */
+ function _escape_identifiers($item)
+ {
+ return $item;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * From Tables
+ *
+ * This function implicitly groups FROM tables so there is no confusion
+ * about operator precedence in harmony with SQL standards
+ *
+ * @access public
+ * @param type
+ * @return type
+ */
+ function _from_tables($tables)
+ {
+ if ( ! is_array($tables))
+ {
+ $tables = array($tables);
+ }
+
+ return implode(', ', $tables);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Insert statement
+ *
+ * Generates a platform-specific insert string from the supplied data
+ *
+ * @access public
+ * @param string the table name
+ * @param array the insert keys
+ * @param array the insert values
+ * @return string
+ */
+ function _insert($table, $keys, $values)
+ {
+ return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Update statement
+ *
+ * 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
+ * @param array the orderby clause
+ * @param array the limit clause
+ * @return string
+ */
+ function _update($table, $values, $where)
+ {
+ foreach($values as $key => $val)
+ {
+ $valstr[] = $key." = ".$val;
+ }
+
+ return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * 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;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Delete statement
+ *
+ * Generates a platform-specific delete string from the supplied data
+ *
+ * @access public
+ * @param string the table name
+ * @param array the where clause
+ * @param string the limit clause
+ * @return string
+ */
+ function _delete($table, $where)
+ {
+ return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Limit string
+ *
+ * Generates a platform-specific LIMIT clause
+ *
+ * @access public
+ * @param string the sql query string
+ * @param integer the number of rows to limit the query to
+ * @param integer the offset value
+ * @return string
+ */
+ function _limit($sql, $limit, $offset)
+ {
+ $i = $limit + $offset;
+
+ return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Close DB Connection
+ *
+ * @access public
+ * @param resource
+ * @return void
+ */
+ function _close($conn_id)
+ {
+ @sqlsrv_close($conn_id);
+ }
+
+}
+
+
+
+/* End of file mssql_driver.php */
+/* Location: ./system/database/drivers/mssql/mssql_driver.php */ \ No newline at end of file
diff --git a/system/database/drivers/sqlsrv/sqlsrv_forge.php b/system/database/drivers/sqlsrv/sqlsrv_forge.php
new file mode 100644
index 000000000..cc88ec5ca
--- /dev/null
+++ b/system/database/drivers/sqlsrv/sqlsrv_forge.php
@@ -0,0 +1,248 @@
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 5.1.6 or newer
+ *
+ * @package CodeIgniter
+ * @author ExpressionEngine Dev Team
+ * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
+ * @license http://codeigniter.com/user_guide/license.html
+ * @link http://codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * SQLSRV Forge Class
+ *
+ * @category Database
+ * @author ExpressionEngine Dev Team
+ * @link http://codeigniter.com/user_guide/database/
+ */
+class CI_DB_sqlsrv_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_identifiers($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_identifiers($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)
+ {
+ foreach ($keys as $key)
+ {
+ if (is_array($key))
+ {
+ $key = $this->db->_protect_identifiers($key);
+ }
+ else
+ {
+ $key = array($this->db->_protect_identifiers($key));
+ }
+
+ $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $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;
+
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Rename a table
+ *
+ * Generates a platform-specific query so that a table can be renamed
+ *
+ * @access private
+ * @param string the old table name
+ * @param string the new table name
+ * @return string
+ */
+ function _rename_table($table_name, $new_table_name)
+ {
+ // I think this syntax will work, but can find little documentation on renaming tables in MSSQL
+ $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
+ return $sql;
+ }
+
+}
+
+/* End of file mssql_forge.php */
+/* Location: ./system/database/drivers/mssql/mssql_forge.php */ \ No newline at end of file
diff --git a/system/database/drivers/sqlsrv/sqlsrv_result.php b/system/database/drivers/sqlsrv/sqlsrv_result.php
new file mode 100644
index 000000000..bf0abd1c6
--- /dev/null
+++ b/system/database/drivers/sqlsrv/sqlsrv_result.php
@@ -0,0 +1,169 @@
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 5.1.6 or newer
+ *
+ * @package CodeIgniter
+ * @author ExpressionEngine Dev Team
+ * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
+ * @license http://codeigniter.com/user_guide/license.html
+ * @link http://codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * SQLSRV Result Class
+ *
+ * This class extends the parent result class: CI_DB_result
+ *
+ * @category Database
+ * @author ExpressionEngine Dev Team
+ * @link http://codeigniter.com/user_guide/database/
+ */
+class CI_DB_sqlsrv_result extends CI_DB_result {
+
+ /**
+ * Number of rows in the result set
+ *
+ * @access public
+ * @return integer
+ */
+ function num_rows()
+ {
+ return @sqlsrv_num_rows($this->result_id);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Number of fields in the result set
+ *
+ * @access public
+ * @return integer
+ */
+ function num_fields()
+ {
+ return @sqlsrv_num_fields($this->result_id);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Fetch Field Names
+ *
+ * Generates an array of column names
+ *
+ * @access public
+ * @return array
+ */
+ function list_fields()
+ {
+ $field_names = array();
+ foreach(sqlsrv_field_metadata($this->result_id) as $offset => $field)
+ {
+ $field_names[] = $field['Name'];
+ }
+
+ return $field_names;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Field data
+ *
+ * Generates an array of objects containing field meta-data
+ *
+ * @access public
+ * @return array
+ */
+ function field_data()
+ {
+ $retval = array();
+ foreach(sqlsrv_field_metadata($this->result_id) as $offset => $field)
+ {
+ $F = new stdClass();
+ $F->name = $field['Name'];
+ $F->type = $field['Type'];
+ $F->max_length = $field['Size'];
+ $F->primary_key = 0;
+ $F->default = '';
+
+ $retval[] = $F;
+ }
+
+ return $retval;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Free the result
+ *
+ * @return null
+ */
+ function free_result()
+ {
+ if (is_resource($this->result_id))
+ {
+ sqlsrv_free_stmt($this->result_id);
+ $this->result_id = FALSE;
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Data Seek
+ *
+ * Moves the internal pointer to the desired offset. We call
+ * this internally before fetching results to make sure the
+ * result set starts at zero
+ *
+ * @access private
+ * @return array
+ */
+ function _data_seek($n = 0)
+ {
+ // Not implemented
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Result - associative array
+ *
+ * Returns the result set as an array
+ *
+ * @access private
+ * @return array
+ */
+ function _fetch_assoc()
+ {
+ return sqlsrv_fetch_array($this->result_id, SQLSRV_FETCH_ASSOC);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Result - object
+ *
+ * Returns the result set as an object
+ *
+ * @access private
+ * @return object
+ */
+ function _fetch_object()
+ {
+ return sqlsrv_fetch_object($this->result_id);
+ }
+
+}
+
+
+/* End of file mssql_result.php */
+/* Location: ./system/database/drivers/mssql/mssql_result.php */ \ No newline at end of file
diff --git a/system/database/drivers/sqlsrv/sqlsrv_utility.php b/system/database/drivers/sqlsrv/sqlsrv_utility.php
new file mode 100644
index 000000000..13a1850c4
--- /dev/null
+++ b/system/database/drivers/sqlsrv/sqlsrv_utility.php
@@ -0,0 +1,88 @@
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 5.1.6 or newer
+ *
+ * @package CodeIgniter
+ * @author ExpressionEngine Dev Team
+ * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
+ * @license http://codeigniter.com/user_guide/license.html
+ * @link http://codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * SQLSRV Utility Class
+ *
+ * @category Database
+ * @author ExpressionEngine Dev Team
+ * @link http://codeigniter.com/user_guide/database/
+ */
+class CI_DB_sqlsrv_utility extends CI_DB_utility {
+
+ /**
+ * List databases
+ *
+ * @access private
+ * @return bool
+ */
+ function _list_databases()
+ {
+ return "EXEC sp_helpdb"; // Can also be: EXEC sp_databases
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Optimize table query
+ *
+ * Generates a platform-specific query so that a table can be optimized
+ *
+ * @access private
+ * @param string the table name
+ * @return object
+ */
+ function _optimize_table($table)
+ {
+ return FALSE; // Is this supported in MS SQL?
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Repair table query
+ *
+ * Generates a platform-specific query so that a table can be repaired
+ *
+ * @access private
+ * @param string the table name
+ * @return object
+ */
+ function _repair_table($table)
+ {
+ return FALSE; // Is this supported in MS SQL?
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * MSSQL Export
+ *
+ * @access private
+ * @param array Preferences
+ * @return mixed
+ */
+ function _backup($params = array())
+ {
+ // Currently unsupported
+ return $this->db->display_error('db_unsuported_feature');
+ }
+
+}
+
+/* End of file mssql_utility.php */
+/* Location: ./system/database/drivers/mssql/mssql_utility.php */ \ No newline at end of file