From 39b622db9bda38282a32bb45623da63efe685729 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Wed, 16 Jan 2008 21:10:09 +0000 Subject: Many new Active Record functions, and another whack of stuff --- system/database/drivers/odbc/odbc_driver.php | 137 ++++++++++++++- system/database/drivers/odbc/odbc_forge.php | 236 ++++++++++++++++++++++++++ system/database/drivers/odbc/odbc_result.php | 2 - system/database/drivers/odbc/odbc_utility.php | 101 +++++------ 4 files changed, 408 insertions(+), 68 deletions(-) create mode 100644 system/database/drivers/odbc/odbc_forge.php (limited to 'system/database/drivers/odbc') 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 @@ -78,6 +84,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 * @@ -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; } // -------------------------------------------------------------------- @@ -365,6 +396,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 * @@ -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 @@ +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 -- cgit v1.2.3-24-g4f1b