From 7f55d6133b70346a428ae481d1fe57bf4d4d2320 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 26 Jan 2012 13:44:28 +0200 Subject: Improve the CUBRID database driver --- system/database/drivers/cubrid/cubrid_driver.php | 284 ++++++++-------------- system/database/drivers/cubrid/cubrid_forge.php | 136 ++++------- system/database/drivers/cubrid/cubrid_result.php | 68 ++---- system/database/drivers/cubrid/cubrid_utility.php | 35 +-- 4 files changed, 183 insertions(+), 340 deletions(-) diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index cde719eae..1cfaf3e42 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -1,13 +1,13 @@ -port == '') { - $this->port = self::DEFAULT_PORT; + // Default CUBRID broker port + $this->port = 33000; } + } + /** + * Non-persistent database connection + * + * @return resource + */ + public function db_connect() + { $conn = cubrid_connect($this->hostname, $this->port, $this->database, $this->username, $this->password); if ($conn) @@ -112,7 +109,7 @@ class CI_DB_cubrid_driver extends CI_DB { * @access private called by the base class * @return resource */ - function db_pconnect() + public function db_pconnect() { return $this->db_connect(); } @@ -125,10 +122,9 @@ class CI_DB_cubrid_driver extends CI_DB { * 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() + public function reconnect() { if (cubrid_ping($this->conn_id) === FALSE) { @@ -141,10 +137,9 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Select the database * - * @access private called by the base class * @return resource */ - function db_select() + public function db_select() { // In CUBRID there is no need to select a database as the database // is chosen at the connection time. @@ -158,12 +153,11 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Set client character set * - * @access public * @param string * @param string - * @return resource + * @return bool */ - function db_set_charset($charset, $collation) + public function db_set_charset($charset, $collation) { // In CUBRID, there is no need to set charset or collation. // This is why returning true will allow the application continue @@ -176,16 +170,10 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Version number query string * - * @access public * @return string */ - function _version() + protected function _version() { - // To obtain the CUBRID Server version, no need to run the SQL query. - // CUBRID PHP API provides a function to determin this value. - // This is why we also need to add 'cubrid' value to the list of - // $driver_version_exceptions array in DB_driver class in - // version() function. return cubrid_get_server_info($this->conn_id); } @@ -194,11 +182,10 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Execute the query * - * @access private called by the base class * @param string an SQL query * @return resource */ - function _execute($sql) + protected function _execute($sql) { $sql = $this->_prep_query($sql); return @cubrid_query($sql, $this->conn_id); @@ -211,11 +198,10 @@ class CI_DB_cubrid_driver extends CI_DB { * * 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) + protected function _prep_query($sql) { // No need to prepare return $sql; @@ -226,18 +212,12 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Begin Transaction * - * @access public * @return bool */ - function trans_begin($test_mode = FALSE) + public 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) + if ( ! $this->trans_enabled OR $this->_trans_depth > 0) { return TRUE; } @@ -245,7 +225,7 @@ class CI_DB_cubrid_driver extends CI_DB { // 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; + $this->_trans_failure = ($test_mode === TRUE); if (cubrid_get_autocommit($this->conn_id)) { @@ -260,18 +240,12 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Commit Transaction * - * @access public * @return bool */ - function trans_commit() + public 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) + if ( ! $this->trans_enabled OR $this->_trans_depth > 0) { return TRUE; } @@ -291,18 +265,12 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Rollback Transaction * - * @access public * @return bool */ - function trans_rollback() + public 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) + if ( ! $this->trans_enabled OR $this->_trans_depth > 0) { return TRUE; } @@ -322,12 +290,11 @@ class CI_DB_cubrid_driver extends CI_DB { /** * 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) + public function escape_str($str, $like = FALSE) { if (is_array($str)) { @@ -339,7 +306,7 @@ class CI_DB_cubrid_driver extends CI_DB { return $str; } - if (function_exists('cubrid_real_escape_string') AND is_resource($this->conn_id)) + if (function_exists('cubrid_real_escape_string') && is_resource($this->conn_id)) { $str = cubrid_real_escape_string($str, $this->conn_id); } @@ -351,7 +318,7 @@ class CI_DB_cubrid_driver extends CI_DB { // escape LIKE condition wildcards if ($like === TRUE) { - $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str); + return str_replace(array('%', '_'), array('\\%', '\\_'), $str); } return $str; @@ -362,10 +329,9 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Affected Rows * - * @access public - * @return integer + * @return int */ - function affected_rows() + public function affected_rows() { return @cubrid_affected_rows($this->conn_id); } @@ -375,10 +341,9 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Insert ID * - * @access public - * @return integer + * @return int */ - function insert_id() + public function insert_id() { return @cubrid_insert_id($this->conn_id); } @@ -391,27 +356,24 @@ class CI_DB_cubrid_driver extends CI_DB { * Generates a platform-specific query string that counts all records in * the specified table * - * @access public * @param string - * @return string + * @return int */ - function count_all($table = '') + public function count_all($table = '') { if ($table == '') { return 0; } - - $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE)); - + $query = $this->query($this->_count_string.$this->_protect_identifiers('numrows').' FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE)); if ($query->num_rows() == 0) { return 0; } - $row = $query->row(); + $query = $query->row(); $this->_reset_select(); - return (int) $row->numrows; + return (int) $query->numrows; } // -------------------------------------------------------------------- @@ -421,17 +383,16 @@ class CI_DB_cubrid_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) + protected function _list_tables($prefix_limit = FALSE) { - $sql = "SHOW TABLES"; + $sql = 'SHOW TABLES'; - if ($prefix_limit !== FALSE AND $this->dbprefix != '') + if ($prefix_limit !== FALSE && $this->dbprefix != '') { - $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'"; + return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'"; } return $sql; @@ -444,13 +405,12 @@ class CI_DB_cubrid_driver extends CI_DB { * * Generates a platform-specific query string so that the column names can be fetched * - * @access public * @param string the table name * @return string */ - function _list_columns($table = '') + protected function _list_columns($table = '') { - return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE); + return 'SHOW COLUMNS FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE); } // -------------------------------------------------------------------- @@ -460,13 +420,12 @@ class CI_DB_cubrid_driver extends CI_DB { * * Generates a platform-specific query so that the column data can be retrieved * - * @access public * @param string the table name - * @return object + * @return string */ - function _field_data($table) + protected function _field_data($table) { - return "SELECT * FROM ".$table." LIMIT 1"; + return 'SELECT * FROM '.$table.' LIMIT 1'; } // -------------------------------------------------------------------- @@ -474,10 +433,9 @@ class CI_DB_cubrid_driver extends CI_DB { /** * The error message string * - * @access private * @return string */ - function _error_message() + protected function _error_message() { return cubrid_error($this->conn_id); } @@ -487,10 +445,9 @@ class CI_DB_cubrid_driver extends CI_DB { /** * The error message number * - * @access private - * @return integer + * @return int */ - function _error_number() + protected function _error_number() { return cubrid_errno($this->conn_id); } @@ -502,11 +459,10 @@ class CI_DB_cubrid_driver extends CI_DB { * * This function escapes column and table names * - * @access private * @param string * @return string */ - function _escape_identifiers($item) + public function _escape_identifiers($item) { if ($this->_escape_char == '') { @@ -517,24 +473,20 @@ class CI_DB_cubrid_driver extends CI_DB { { if (strpos($item, '.'.$id) !== FALSE) { - $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); + $item = str_replace('.', $this->_escape_char.'.', $item); // remove duplicates if the user already included the escape - return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); + return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item); } } if (strpos($item, '.') !== FALSE) { - $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; - } - else - { - $str = $this->_escape_char.$item.$this->_escape_char; + $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item); } // remove duplicates if the user already included the escape - return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); + return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char); } // -------------------------------------------------------------------- @@ -545,11 +497,10 @@ class CI_DB_cubrid_driver extends CI_DB { * 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 + * @param string the table name + * @return string */ - function _from_tables($tables) + protected function _from_tables($tables) { if ( ! is_array($tables)) { @@ -566,15 +517,14 @@ class CI_DB_cubrid_driver extends CI_DB { * * 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) + protected function _insert($table, $keys, $values) { - return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")"; + return 'INSERT INTO '.$table.' ("'.implode('", "', $keys).'") VALUES ('.implode(', ', $values).')'; } // -------------------------------------------------------------------- @@ -585,15 +535,14 @@ class CI_DB_cubrid_driver extends CI_DB { * * Generates a platform-specific replace 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 _replace($table, $keys, $values) + protected function _replace($table, $keys, $values) { - return "REPLACE INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")"; + return 'REPLACE INTO '.$table.' ("'.implode('", "', $keys).'") VALUES ('.implode(', ', $values).')'; } // -------------------------------------------------------------------- @@ -603,26 +552,23 @@ class CI_DB_cubrid_driver extends CI_DB { * * 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) + protected function _insert_batch($table, $keys, $values) { - return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES ".implode(', ', $values); + return 'INSERT INTO '.$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 @@ -630,24 +576,17 @@ class CI_DB_cubrid_driver extends CI_DB { * @param array the limit clause * @return string */ - function _update($table, $values, $where, $orderby = array(), $limit = FALSE) + protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) { foreach ($values as $key => $val) { $valstr[] = sprintf('"%s" = %s', $key, $val); } - $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; - - $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; - - $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); - - $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; - - $sql .= $orderby.$limit; - - return $sql; + return 'UPDATE '.$table.' SET '.implode(', ', $valstr) + .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '') + .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '') + .( ! $limit ? '' : ' LIMIT '.$limit); } // -------------------------------------------------------------------- @@ -658,16 +597,15 @@ class CI_DB_cubrid_driver extends CI_DB { * * Generates a platform-specific batch 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 */ - function _update_batch($table, $values, $index, $where = NULL) + protected function _update_batch($table, $values, $index, $where = NULL) { $ids = array(); - $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : ''; + $where = ($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : ''; foreach ($values as $key => $val) { @@ -682,30 +620,23 @@ class CI_DB_cubrid_driver extends CI_DB { } } - $sql = "UPDATE ".$table." SET "; + $sql = 'UPDATE '.$table.' SET '; $cases = ''; foreach ($final as $k => $v) { - $cases .= $k.' = CASE '."\n"; - foreach ($v as $row) - { - $cases .= $row."\n"; - } - - $cases .= 'ELSE '.$k.' END, '; + $cases .= $k." = CASE \n" + .implode("\n", $v) + .'ELSE '.$k.' END, '; } - $sql .= substr($cases, 0, -2); - - $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')'; - - return $sql; + return $sql.substr($cases, 0, -2) + .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '') + .$index.' IN ('.implode(',', $ids).')'; } // -------------------------------------------------------------------- - /** * Truncate statement * @@ -713,13 +644,12 @@ class CI_DB_cubrid_driver extends CI_DB { * 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) + protected function _truncate($table) { - return "TRUNCATE ".$table; + return 'TRUNCATE '.$table; } // -------------------------------------------------------------------- @@ -729,31 +659,27 @@ class CI_DB_cubrid_driver extends CI_DB { * * 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 = array(), $like = array(), $limit = FALSE) + protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) { $conditions = ''; if (count($where) > 0 OR count($like) > 0) { - $conditions = "\nWHERE "; - $conditions .= implode("\n", $this->ar_where); + $conditions = "\nWHERE ".implode("\n", $this->ar_where); if (count($where) > 0 && count($like) > 0) { - $conditions .= " AND "; + $conditions .= ' AND '; } $conditions .= implode("\n", $like); } - $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; - - return "DELETE FROM ".$table.$conditions.$limit; + return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit); } // -------------------------------------------------------------------- @@ -763,24 +689,14 @@ class CI_DB_cubrid_driver extends CI_DB { * * 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 + * @param int the number of rows to limit the query to + * @param int the offset value * @return string */ - function _limit($sql, $limit, $offset) + protected function _limit($sql, $limit, $offset) { - if ($offset == 0) - { - $offset = ''; - } - else - { - $offset .= ", "; - } - - return $sql."LIMIT ".$offset.$limit; + return $sql.'LIMIT '.($offset == 0 ? '' : ', ').$limit; } // -------------------------------------------------------------------- @@ -788,17 +704,15 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Close DB Connection * - * @access public * @param resource * @return void */ - function _close($conn_id) + protected function _close($conn_id) { @cubrid_close($conn_id); } } - /* End of file cubrid_driver.php */ -/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */ \ No newline at end of file +/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */ diff --git a/system/database/drivers/cubrid/cubrid_forge.php b/system/database/drivers/cubrid/cubrid_forge.php index 85e740057..ce5aa2098 100644 --- a/system/database/drivers/cubrid/cubrid_forge.php +++ b/system/database/drivers/cubrid/cubrid_forge.php @@ -1,13 +1,13 @@ -$attributes) + 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"; + $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']).' '; - } + $sql .= "\n\t\"".$this->db->protect_identifiers($field).'"' + .(array_key_exists('NAME', $attributes) ? ' '.$this->db->protect_identifiers($attributes['NAME']).' ' : ''); if (array_key_exists('TYPE', $attributes)) { @@ -113,9 +104,8 @@ class CI_DB_cubrid_forge extends CI_DB_forge { case 'numeric': $sql .= '('.implode(',', $attributes['CONSTRAINT']).')'; break; - case 'enum': // As of version 8.4.0 CUBRID does not support - // enum data type. - break; + case 'enum': // As of version 8.4.0 CUBRID does not support enum data type + break; case 'set': $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")'; break; @@ -125,36 +115,19 @@ class CI_DB_cubrid_forge extends CI_DB_forge { } } + /* As of version 8.4.0 CUBRID does not support UNSIGNED INTEGER data type. + * Will be supported in the next release as a part of MySQL Compatibility. + * if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE) { - //$sql .= ' UNSIGNED'; - // As of version 8.4.0 CUBRID does not support UNSIGNED INTEGER data type. - // Will be supported in the next release as a part of MySQL Compatibility. - } - - 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'; + $sql .= ' UNSIGNED'; } + */ - if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE) - { - $sql .= ' AUTO_INCREMENT'; - } - - if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE) - { - $sql .= ' UNIQUE'; - } + $sql .= (array_key_exists('DEFAULT', $attributes) ? " DEFAULT '".$attributes['DEFAULT']."'" : '') + .((array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL') + .((array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE) ? ' AUTO_INCREMENT' : '') + .((array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE) ? ' UNIQUE' : ''); } // don't add a comma on the end of the last field @@ -172,7 +145,6 @@ class CI_DB_cubrid_forge extends CI_DB_forge { /** * Create Table * - * @access private * @param string the table name * @param mixed the fields * @param mixed primary key(s) @@ -180,28 +152,24 @@ class CI_DB_cubrid_forge extends CI_DB_forge { * @param boolean should 'IF NOT EXISTS' be added to the SQL * @return bool */ - function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) + public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) { $sql = 'CREATE TABLE '; + /* As of version 8.4.0 CUBRID does not support this SQL syntax. if ($if_not_exists === TRUE) { - //$sql .= 'IF NOT EXISTS '; - // As of version 8.4.0 CUBRID does not support this SQL syntax. + $sql .= 'IF NOT EXISTS '; } + */ - $sql .= $this->db->_escape_identifiers($table)." ("; - - $sql .= $this->_process_fields($fields); + $sql .= $this->db->_escape_identifiers($table).' ('.$this->_process_fields($fields); // If there is a PK defined if (count($primary_keys) > 0) { - $key_name = "pk_" . $table . "_" . - $this->db->_protect_identifiers(implode('_', $primary_keys)); - - $primary_keys = $this->db->_protect_identifiers($primary_keys); - $sql .= ",\n\tCONSTRAINT " . $key_name . " PRIMARY KEY(" . implode(', ', $primary_keys) . ")"; + $key_name = 'pk_'.$table.'_'.$this->db->protect_identifiers(implode('_', $primary_keys)); + $sql .= ",\n\tCONSTRAINT ".$key_name.' PRIMARY KEY('.implode(', ', $this->db->protect_identifiers($primary_keys)).')'; } if (is_array($keys) && count($keys) > 0) @@ -210,22 +178,20 @@ class CI_DB_cubrid_forge extends CI_DB_forge { { if (is_array($key)) { - $key_name = $this->db->_protect_identifiers(implode('_', $key)); - $key = $this->db->_protect_identifiers($key); + $key_name = $this->db->protect_identifiers(implode('_', $key)); + $key = $this->db->protect_identifiers($key); } else { - $key_name = $this->db->_protect_identifiers($key); + $key_name = $this->db->protect_identifiers($key); $key = array($key_name); } - - $sql .= ",\n\tKEY \"{$key_name}\" (" . implode(', ', $key) . ")"; + + $sql .= ",\n\tKEY ".$key_name.' ('.implode(', ', $key).')'; } } - $sql .= "\n);"; - - return $sql; + return $sql."\n);"; } // -------------------------------------------------------------------- @@ -233,12 +199,11 @@ class CI_DB_cubrid_forge extends CI_DB_forge { /** * Drop Table * - * @access private * @return string */ - function _drop_table($table) + public function _drop_table($table) { - return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table); + return 'DROP TABLE IF EXISTS '.$this->db->_escape_identifiers($table); } // -------------------------------------------------------------------- @@ -249,31 +214,24 @@ class CI_DB_cubrid_forge extends CI_DB_forge { * 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 + * @return string */ - function _alter_table($alter_type, $table, $fields, $after_field = '') + public function _alter_table($alter_type, $table, $fields, $after_field = '') { - $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type "; + $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 != '') + if ($alter_type === 'DROP') { - $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field); + return $sql.$this->db->protect_identifiers($fields); } - return $sql; + return $sql.$this->_process_fields($fields) + .($after_field != '' ? ' AFTER '.$this->db->protect_identifiers($after_field) : ''); } // -------------------------------------------------------------------- @@ -283,18 +241,16 @@ class CI_DB_cubrid_forge extends CI_DB_forge { * * 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) + public function _rename_table($table_name, $new_table_name) { - $sql = 'RENAME TABLE '.$this->db->_protect_identifiers($table_name)." AS ".$this->db->_protect_identifiers($new_table_name); - return $sql; + return 'RENAME TABLE '.$this->db->protect_identifiers($table_name).' AS '.$this->db->protect_identifiers($new_table_name); } } /* End of file cubrid_forge.php */ -/* Location: ./system/database/drivers/cubrid/cubrid_forge.php */ \ No newline at end of file +/* Location: ./system/database/drivers/cubrid/cubrid_forge.php */ diff --git a/system/database/drivers/cubrid/cubrid_result.php b/system/database/drivers/cubrid/cubrid_result.php index 4c0fede10..7a72cdde4 100644 --- a/system/database/drivers/cubrid/cubrid_result.php +++ b/system/database/drivers/cubrid/cubrid_result.php @@ -1,13 +1,13 @@ -result_id); } @@ -54,10 +51,9 @@ class CI_DB_cubrid_result extends CI_DB_result { /** * Number of fields in the result set * - * @access public - * @return integer + * @return int */ - function num_fields() + public function num_fields() { return @cubrid_num_fields($this->result_id); } @@ -69,10 +65,9 @@ class CI_DB_cubrid_result extends CI_DB_result { * * Generates an array of column names * - * @access public * @return array */ - function list_fields() + public function list_fields() { return cubrid_column_names($this->result_id); } @@ -84,21 +79,18 @@ class CI_DB_cubrid_result extends CI_DB_result { * * Generates an array of objects containing field meta-data * - * @access public * @return array */ - function field_data() + public function field_data() { - $retval = array(); - - $tablePrimaryKeys = array(); + $retval = $tablePrimaryKeys = array(); while ($field = cubrid_fetch_field($this->result_id)) { - $F = new stdClass(); - $F->name = $field->name; - $F->type = $field->type; - $F->default = $field->def; + $F = new stdClass(); + $F->name = $field->name; + $F->type = $field->type; + $F->default = $field->def; $F->max_length = $field->max_length; // At this moment primary_key property is not returned when @@ -115,19 +107,18 @@ class CI_DB_cubrid_result extends CI_DB_result { // The query will search for exact single columns, thus // compound PK is not supported. $res = cubrid_query($this->conn_id, - "SELECT COUNT(*) FROM db_index WHERE class_name = '" . $field->table . - "' AND is_primary_key = 'YES' AND index_name = 'pk_" . - $field->table . "_" . $field->name . "'" - ); + "SELECT COUNT(*) FROM db_index WHERE class_name = '".$field->table + ."' AND is_primary_key = 'YES' AND index_name = 'pk_".$field->table.'_'.$field->name."'" + ); if ($res) { $row = cubrid_fetch_array($res, CUBRID_NUM); - $F->primary_key = ($row[0] > 0 ? 1 : null); + $F->primary_key = ($row[0] > 0 ? 1 : NULL); } else { - $F->primary_key = null; + $F->primary_key = NULL; } if (is_resource($res)) @@ -147,13 +138,12 @@ class CI_DB_cubrid_result extends CI_DB_result { /** * Free the result * - * @return null + * @return void */ - function free_result() + public function free_result() { - if(is_resource($this->result_id) || - get_resource_type($this->result_id) == "Unknown" && - preg_match('/Resource id #/', strval($this->result_id))) + if (is_resource($this->result_id) OR + (get_resource_type($this->result_id) === 'Unknown' && preg_match('/Resource id #/', strval($this->result_id)))) { cubrid_close_request($this->result_id); $this->result_id = FALSE; @@ -169,10 +159,9 @@ class CI_DB_cubrid_result extends CI_DB_result { * this internally before fetching results to make sure the * result set starts at zero * - * @access private * @return array */ - function _data_seek($n = 0) + protected function _data_seek($n = 0) { return cubrid_data_seek($this->result_id, $n); } @@ -184,10 +173,9 @@ class CI_DB_cubrid_result extends CI_DB_result { * * Returns the result set as an array * - * @access private * @return array */ - function _fetch_assoc() + protected function _fetch_assoc() { return cubrid_fetch_assoc($this->result_id); } @@ -199,16 +187,14 @@ class CI_DB_cubrid_result extends CI_DB_result { * * Returns the result set as an object * - * @access private * @return object */ - function _fetch_object() + protected function _fetch_object() { return cubrid_fetch_object($this->result_id); } } - /* End of file cubrid_result.php */ -/* Location: ./system/database/drivers/cubrid/cubrid_result.php */ \ No newline at end of file +/* Location: ./system/database/drivers/cubrid/cubrid_result.php */ diff --git a/system/database/drivers/cubrid/cubrid_utility.php b/system/database/drivers/cubrid/cubrid_utility.php index 750c0d8dd..dbfbe5f6b 100644 --- a/system/database/drivers/cubrid/cubrid_utility.php +++ b/system/database/drivers/cubrid/cubrid_utility.php @@ -1,13 +1,13 @@ -conn_id) - { - return "SELECT '" . $this->database . "'"; - } - else - { - return FALSE; - } + return "SELECT '".$this->database."'"; } // -------------------------------------------------------------------- @@ -66,12 +56,11 @@ class CI_DB_cubrid_utility extends CI_DB_utility { * * Generates a platform-specific query so that a table can be optimized * - * @access private * @param string the table name - * @return object + * @return bool * @link http://www.cubrid.org/manual/840/en/Optimize%20Database */ - function _optimize_table($table) + public function _optimize_table($table) { // No SQL based support in CUBRID as of version 8.4.0. Database or // table optimization can be performed using CUBRID Manager @@ -86,12 +75,11 @@ class CI_DB_cubrid_utility extends CI_DB_utility { * * Generates a platform-specific query so that a table can be repaired * - * @access private * @param string the table name - * @return object + * @return bool * @link http://www.cubrid.org/manual/840/en/Checking%20Database%20Consistency */ - function _repair_table($table) + public function _repair_table($table) { // Not supported in CUBRID as of version 8.4.0. Database or // table consistency can be checked using CUBRID Manager @@ -103,11 +91,10 @@ class CI_DB_cubrid_utility extends CI_DB_utility { /** * CUBRID Export * - * @access private * @param array Preferences * @return mixed */ - function _backup($params = array()) + public function _backup($params = array()) { // No SQL based support in CUBRID as of version 8.4.0. Database or // table backup can be performed using CUBRID Manager @@ -117,4 +104,4 @@ class CI_DB_cubrid_utility extends CI_DB_utility { } /* End of file cubrid_utility.php */ -/* Location: ./system/database/drivers/cubrid/cubrid_utility.php */ \ No newline at end of file +/* Location: ./system/database/drivers/cubrid/cubrid_utility.php */ -- cgit v1.2.3-24-g4f1b From 69a6093da35640482a8394ca66686e045304b0e3 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 26 Jan 2012 14:04:06 +0200 Subject: Replace array_key_exists() with isset() and ! empty() --- system/database/drivers/cubrid/cubrid_forge.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/system/database/drivers/cubrid/cubrid_forge.php b/system/database/drivers/cubrid/cubrid_forge.php index ce5aa2098..d277ca394 100644 --- a/system/database/drivers/cubrid/cubrid_forge.php +++ b/system/database/drivers/cubrid/cubrid_forge.php @@ -89,13 +89,13 @@ class CI_DB_cubrid_forge extends CI_DB_forge { $attributes = array_change_key_case($attributes, CASE_UPPER); $sql .= "\n\t\"".$this->db->protect_identifiers($field).'"' - .(array_key_exists('NAME', $attributes) ? ' '.$this->db->protect_identifiers($attributes['NAME']).' ' : ''); + .(isset($attributes['NAME']) ? ' '.$this->db->protect_identifiers($attributes['NAME']).' ' : ''); - if (array_key_exists('TYPE', $attributes)) + if ( ! empty($attributes['TYPE'])) { $sql .= ' '.$attributes['TYPE']; - if (array_key_exists('CONSTRAINT', $attributes)) + if (isset($attributes['CONSTRAINT'])) { switch ($attributes['TYPE']) { @@ -118,16 +118,16 @@ class CI_DB_cubrid_forge extends CI_DB_forge { /* As of version 8.4.0 CUBRID does not support UNSIGNED INTEGER data type. * Will be supported in the next release as a part of MySQL Compatibility. * - if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE) + if (isset($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) { $sql .= ' UNSIGNED'; } */ - $sql .= (array_key_exists('DEFAULT', $attributes) ? " DEFAULT '".$attributes['DEFAULT']."'" : '') - .((array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL') - .((array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE) ? ' AUTO_INCREMENT' : '') - .((array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE) ? ' UNIQUE' : ''); + $sql .= (isset($attributes['DEFAULT']) ? " DEFAULT '".$attributes['DEFAULT']."'" : '') + .((isset($attributes['NULL']) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL') + .((isset($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) ? ' AUTO_INCREMENT' : '') + .((isset($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) ? ' UNIQUE' : ''); } // don't add a comma on the end of the last field -- cgit v1.2.3-24-g4f1b From c6953f4077fe3ee394abb37eaa0575527bd013cc Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 26 Jan 2012 14:43:16 +0200 Subject: Fix _limit() --- system/database/drivers/cubrid/cubrid_driver.php | 2 +- system/database/drivers/cubrid/cubrid_forge.php | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index 1cfaf3e42..b89746b7d 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -696,7 +696,7 @@ class CI_DB_cubrid_driver extends CI_DB { */ protected function _limit($sql, $limit, $offset) { - return $sql.'LIMIT '.($offset == 0 ? '' : ', ').$limit; + return $sql.'LIMIT '.($offset == 0 ? '' : $offset.', ').$limit; } // -------------------------------------------------------------------- diff --git a/system/database/drivers/cubrid/cubrid_forge.php b/system/database/drivers/cubrid/cubrid_forge.php index d277ca394..4b1c2839a 100644 --- a/system/database/drivers/cubrid/cubrid_forge.php +++ b/system/database/drivers/cubrid/cubrid_forge.php @@ -89,13 +89,13 @@ class CI_DB_cubrid_forge extends CI_DB_forge { $attributes = array_change_key_case($attributes, CASE_UPPER); $sql .= "\n\t\"".$this->db->protect_identifiers($field).'"' - .(isset($attributes['NAME']) ? ' '.$this->db->protect_identifiers($attributes['NAME']).' ' : ''); + .( ! empty($attributes['NAME']) ? ' '.$this->db->protect_identifiers($attributes['NAME']).' ' : ''); if ( ! empty($attributes['TYPE'])) { $sql .= ' '.$attributes['TYPE']; - if (isset($attributes['CONSTRAINT'])) + if ( ! empty($attributes['CONSTRAINT'])) { switch ($attributes['TYPE']) { @@ -125,9 +125,9 @@ class CI_DB_cubrid_forge extends CI_DB_forge { */ $sql .= (isset($attributes['DEFAULT']) ? " DEFAULT '".$attributes['DEFAULT']."'" : '') - .((isset($attributes['NULL']) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL') - .((isset($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) ? ' AUTO_INCREMENT' : '') - .((isset($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) ? ' UNIQUE' : ''); + .(( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL') + .(( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) ? ' AUTO_INCREMENT' : '') + .(( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) ? ' UNIQUE' : ''); } // don't add a comma on the end of the last field -- cgit v1.2.3-24-g4f1b From 214583f1ba4c026141e595dac5868d5074095a7e Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 26 Jan 2012 16:39:09 +0200 Subject: Improve the PostgreSQL database driver --- system/database/drivers/postgre/postgre_driver.php | 357 ++++++++++----------- system/database/drivers/postgre/postgre_forge.php | 136 +++----- system/database/drivers/postgre/postgre_result.php | 60 ++-- .../database/drivers/postgre/postgre_utility.php | 33 +- 4 files changed, 257 insertions(+), 329 deletions(-) diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index 42329bded..8b8f33371 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -1,13 +1,13 @@ - 'host', - 'port' => 'port', - 'database' => 'dbname', - 'username' => 'user', - 'password' => 'password' - ); - - $connect_string = ""; + 'hostname' => 'host', + 'port' => 'port', + 'database' => 'dbname', + 'username' => 'user', + 'password' => 'password' + ); + foreach ($components as $key => $val) { if (isset($this->$key) && $this->$key != '') { - $connect_string .= " $val=".$this->$key; + $this->_pg_dsn .= $val.'='.$this->$key.' '; } } - return trim($connect_string); + + if (strlen($this->_pg_dsn) > 0) + { + $this->_pg_dsn = rtrim($this->_pg_dsn); + } } // -------------------------------------------------------------------- @@ -90,12 +97,17 @@ class CI_DB_postgre_driver extends CI_DB { /** * Non-persistent database connection * - * @access private called by the base class * @return resource */ - function db_connect() + public function db_connect() { - return @pg_connect($this->_connect_string()); + $ret = @pg_connect($this->_pg_dsn); + if (is_resource($ret) && $this->char_set != '') + { + pg_set_client_encoding($ret, $this->char_set); + } + + return $ret; } // -------------------------------------------------------------------- @@ -103,12 +115,17 @@ class CI_DB_postgre_driver extends CI_DB { /** * Persistent database connection * - * @access private called by the base class * @return resource */ - function db_pconnect() + public function db_pconnect() { - return @pg_pconnect($this->_connect_string()); + $ret = @pg_pconnect($this->_pg_dsn); + if (is_resource($ret) && $this->char_set != '') + { + pg_set_client_encoding($ret, $this->char_set); + } + + return $ret; } // -------------------------------------------------------------------- @@ -119,10 +136,9 @@ class CI_DB_postgre_driver extends CI_DB { * 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() + public function reconnect() { if (pg_ping($this->conn_id) === FALSE) { @@ -135,10 +151,9 @@ class CI_DB_postgre_driver extends CI_DB { /** * Select the database * - * @access private called by the base class * @return resource */ - function db_select() + public function db_select() { // Not needed for Postgre so we'll return TRUE return TRUE; @@ -149,15 +164,35 @@ class CI_DB_postgre_driver extends CI_DB { /** * Set client character set * - * @access public * @param string * @param string - * @return resource + * @return bool */ - function db_set_charset($charset, $collation) + public function db_set_charset($charset, $collation) { - // @todo - add support if needed - return TRUE; + return (pg_set_client_encoding($this->conn_id, $charset) === 0); + } + + // -------------------------------------------------------------------- + + /** + * Database version + * + * This method overrides the one from the base class, as if PHP was + * compiled with PostgreSQL version >= 7.4 libraries, we have a native + * function to get the server's version. + * + * @return string + */ + + public function version() + { + $version = pg_version($this->conn_id); + + /* If we don't have the server version string - fall back to + * the base driver class' method + */ + return isset($version['server']) ? $version['server'] : parent::version(); } // -------------------------------------------------------------------- @@ -165,12 +200,11 @@ class CI_DB_postgre_driver extends CI_DB { /** * Version number query string * - * @access public * @return string */ - function _version() + protected function _version() { - return "SELECT version() AS ver"; + return 'SELECT version() AS ver'; } // -------------------------------------------------------------------- @@ -178,14 +212,12 @@ class CI_DB_postgre_driver extends CI_DB { /** * Execute the query * - * @access private called by the base class * @param string an SQL query * @return resource */ - function _execute($sql) + protected function _execute($sql) { - $sql = $this->_prep_query($sql); - return @pg_query($this->conn_id, $sql); + return @pg_query($this->conn_id, $this->_prep_query($sql)); } // -------------------------------------------------------------------- @@ -195,11 +227,10 @@ class CI_DB_postgre_driver extends CI_DB { * * 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) + protected function _prep_query($sql) { return $sql; } @@ -209,18 +240,13 @@ class CI_DB_postgre_driver extends CI_DB { /** * Begin Transaction * - * @access public + * @param bool * @return bool */ - function trans_begin($test_mode = FALSE) + public 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) + if ( ! $this->trans_enabled OR $this->_trans_depth > 0) { return TRUE; } @@ -228,9 +254,9 @@ class CI_DB_postgre_driver extends CI_DB { // 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; + $this->_trans_failure = ($test_mode === TRUE); - return @pg_exec($this->conn_id, "begin"); + return (bool) @pg_query($this->conn_id, 'BEGIN'); } // -------------------------------------------------------------------- @@ -238,23 +264,17 @@ class CI_DB_postgre_driver extends CI_DB { /** * Commit Transaction * - * @access public * @return bool */ - function trans_commit() + public 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) + if ( ! $this->trans_enabled OR $this->_trans_depth > 0) { return TRUE; } - return @pg_exec($this->conn_id, "commit"); + return (bool) @pg_query($this->conn_id, 'COMMIT'); } // -------------------------------------------------------------------- @@ -262,23 +282,17 @@ class CI_DB_postgre_driver extends CI_DB { /** * Rollback Transaction * - * @access public * @return bool */ - function trans_rollback() + public 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) + if ( ! $this->trans_enabled OR $this->_trans_depth > 0) { return TRUE; } - return @pg_exec($this->conn_id, "rollback"); + return (bool) @pg_query($this->conn_id, 'ROLLBACK'); } // -------------------------------------------------------------------- @@ -286,12 +300,11 @@ class CI_DB_postgre_driver extends CI_DB { /** * 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) + public function escape_str($str, $like = FALSE) { if (is_array($str)) { @@ -308,9 +321,9 @@ class CI_DB_postgre_driver extends CI_DB { // escape LIKE condition wildcards if ($like === TRUE) { - $str = str_replace( array('%', '_', $this->_like_escape_chr), - array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr), - $str); + return str_replace(array('%', '_', $this->_like_escape_chr), + array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr), + $str); } return $str; @@ -321,10 +334,9 @@ class CI_DB_postgre_driver extends CI_DB { /** * Affected Rows * - * @access public - * @return integer + * @return int */ - function affected_rows() + public function affected_rows() { return @pg_affected_rows($this->result_id); } @@ -334,40 +346,45 @@ class CI_DB_postgre_driver extends CI_DB { /** * Insert ID * - * @access public - * @return integer + * @return int */ - function insert_id() + public function insert_id() { - $v = $this->_version(); - $v = $v['server']; + $v = pg_version($this->conn_id); + $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4 - $table = func_num_args() > 0 ? func_get_arg(0) : NULL; - $column = func_num_args() > 1 ? func_get_arg(1) : NULL; + $table = (func_num_args() > 0) ? func_get_arg(0) : NULL; + $column = (func_num_args() > 1) ? func_get_arg(1) : NULL; if ($table == NULL && $v >= '8.1') { - $sql='SELECT LASTVAL() as ins_id'; - } - elseif ($table != NULL && $column != NULL && $v >= '8.0') - { - $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column); - $query = $this->query($sql); - $row = $query->row(); - $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq); + $sql = 'SELECT LASTVAL() AS ins_id'; } elseif ($table != NULL) { - // seq_name passed in table parameter - $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table); + if ($column != NULL && $v >= '8.0') + { + $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq"; + $query = $this->query($sql); + $query = $query->row(); + $seq = $query->seq; + } + else + { + // seq_name passed in table parameter + $seq = $table; + } + + $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id"; } else { return pg_last_oid($this->result_id); } + $query = $this->query($sql); - $row = $query->row(); - return $row->ins_id; + $query = $query->row(); + return (int) $query->ins_id; } // -------------------------------------------------------------------- @@ -378,27 +395,25 @@ class CI_DB_postgre_driver extends CI_DB { * Generates a platform-specific query string that counts all records in * the specified database * - * @access public * @param string * @return string */ - function count_all($table = '') + public function count_all($table = '') { if ($table == '') { return 0; } - $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE)); - + $query = $this->query($this->_count_string.$this->_protect_identifiers('numrows').' FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE)); if ($query->num_rows() == 0) { return 0; } - $row = $query->row(); + $query = $query->row(); $this->_reset_select(); - return (int) $row->numrows; + return (int) $query->numrows; } // -------------------------------------------------------------------- @@ -408,17 +423,16 @@ 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 + * @param bool * @return string */ - function _list_tables($prefix_limit = FALSE) + protected function _list_tables($prefix_limit = FALSE) { $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'"; - if ($prefix_limit !== FALSE AND $this->dbprefix != '') + if ($prefix_limit !== FALSE && $this->dbprefix != '') { - $sql .= " AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr); + return $sql." AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr); } return $sql; @@ -431,13 +445,12 @@ class CI_DB_postgre_driver extends CI_DB { * * Generates a platform-specific query string so that the column names can be fetched * - * @access public * @param string the table name * @return string */ - function _list_columns($table = '') + protected function _list_columns($table = '') { - return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$table."'"; + return "SELECT column_name FROM information_schema.columns WHERE table_name = '".$table."'"; } // -------------------------------------------------------------------- @@ -447,13 +460,12 @@ class CI_DB_postgre_driver extends CI_DB { * * Generates a platform-specific query so that the column data can be retrieved * - * @access public * @param string the table name - * @return object + * @return string */ - function _field_data($table) + protected function _field_data($table) { - return "SELECT * FROM ".$table." LIMIT 1"; + return 'SELECT * FROM '.$table.' LIMIT 1'; } // -------------------------------------------------------------------- @@ -461,10 +473,9 @@ class CI_DB_postgre_driver extends CI_DB { /** * The error message string * - * @access private * @return string */ - function _error_message() + protected function _error_message() { return pg_last_error($this->conn_id); } @@ -474,11 +485,11 @@ class CI_DB_postgre_driver extends CI_DB { /** * The error message number * - * @access private - * @return integer + * @return string */ - function _error_number() + protected function _error_number() { + // Not supported in Postgre return ''; } @@ -489,11 +500,10 @@ class CI_DB_postgre_driver extends CI_DB { * * This function escapes column and table names * - * @access private * @param string * @return string */ - function _escape_identifiers($item) + public function _escape_identifiers($item) { if ($this->_escape_char == '') { @@ -504,24 +514,20 @@ class CI_DB_postgre_driver extends CI_DB { { if (strpos($item, '.'.$id) !== FALSE) { - $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); + $item = str_replace('.', $this->_escape_char.'.', $item); // remove duplicates if the user already included the escape - return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); + return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item); } } if (strpos($item, '.') !== FALSE) { - $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; - } - else - { - $str = $this->_escape_char.$item.$this->_escape_char; + $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item); } // remove duplicates if the user already included the escape - return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); + return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char); } // -------------------------------------------------------------------- @@ -532,11 +538,10 @@ class CI_DB_postgre_driver extends CI_DB { * 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 + * @param string table name + * @return string */ - function _from_tables($tables) + protected function _from_tables($tables) { if ( ! is_array($tables)) { @@ -553,15 +558,14 @@ class CI_DB_postgre_driver extends CI_DB { * * 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) + protected function _insert($table, $keys, $values) { - return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; + return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; } // -------------------------------------------------------------------- @@ -571,15 +575,14 @@ class CI_DB_postgre_driver extends CI_DB { * * 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) + protected function _insert_batch($table, $keys, $values) { - return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values); + return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values); } // -------------------------------------------------------------------- @@ -589,7 +592,6 @@ class CI_DB_postgre_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 @@ -597,24 +599,17 @@ class CI_DB_postgre_driver extends CI_DB { * @param array the limit clause * @return string */ - function _update($table, $values, $where, $orderby = array(), $limit = FALSE) + protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) { foreach ($values as $key => $val) { - $valstr[] = $key." = ".$val; + $valstr[] = $key.' = '.$val; } - $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; - - $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; - - $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); - - $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; - - $sql .= $orderby.$limit; - - return $sql; + return 'UPDATE '.$table.' SET '.implode(', ', $valstr) + .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '') + .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '') + .( ! $limit ? '' : ' LIMIT '.$limit); } // -------------------------------------------------------------------- @@ -626,13 +621,12 @@ class CI_DB_postgre_driver extends CI_DB { * 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) + protected function _truncate($table) { - return "TRUNCATE ".$table; + return 'TRUNCATE '.$table; } // -------------------------------------------------------------------- @@ -642,31 +636,27 @@ class CI_DB_postgre_driver extends CI_DB { * * 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 = array(), $like = array(), $limit = FALSE) + protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) { $conditions = ''; if (count($where) > 0 OR count($like) > 0) { - $conditions = "\nWHERE "; - $conditions .= implode("\n", $this->ar_where); + $conditions = "\nWHERE ".implode("\n", $this->ar_where); if (count($where) > 0 && count($like) > 0) { - $conditions .= " AND "; + $conditions .= ' AND '; } $conditions .= implode("\n", $like); } - $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; - - return "DELETE FROM ".$table.$conditions.$limit; + return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit); } // -------------------------------------------------------------------- @@ -675,22 +665,14 @@ class CI_DB_postgre_driver extends CI_DB { * * 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 + * @param int the number of rows to limit the query to + * @param int the offset value * @return string */ - function _limit($sql, $limit, $offset) + protected function _limit($sql, $limit, $offset) { - $sql .= "LIMIT ".$limit; - - if ($offset > 0) - { - $sql .= " OFFSET ".$offset; - } - - return $sql; + return $sql.' LIMIT '.$limit.($offset == 0 ? '' : ' OFFSET '.$offset); } // -------------------------------------------------------------------- @@ -698,18 +680,15 @@ class CI_DB_postgre_driver extends CI_DB { /** * Close DB Connection * - * @access public * @param resource * @return void */ - function _close($conn_id) + protected function _close($conn_id) { @pg_close($conn_id); } - } - /* End of file postgre_driver.php */ -/* Location: ./system/database/drivers/postgre/postgre_driver.php */ \ No newline at end of file +/* Location: ./system/database/drivers/postgre/postgre_driver.php */ diff --git a/system/database/drivers/postgre/postgre_forge.php b/system/database/drivers/postgre/postgre_forge.php index 756fd347a..5af834b8d 100644 --- a/system/database/drivers/postgre/postgre_forge.php +++ b/system/database/drivers/postgre/postgre_forge.php @@ -1,13 +1,13 @@ -$attributes) + 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"; + $sql .= "\n\t".$attributes; } else { - $attributes = array_change_key_case($attributes, CASE_UPPER); - - $sql .= "\n\t".$this->db->_protect_identifiers($field); + $sql .= "\n\t".$this->db->protect_identifiers($field); - $is_unsigned = (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE); + $attributes = array_change_key_case($attributes, CASE_UPPER); + $is_unsigned = ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE); // Convert datatypes to be PostgreSQL-compatible switch (strtoupper($attributes['TYPE'])) @@ -122,44 +117,24 @@ class CI_DB_postgre_forge extends CI_DB_forge { case 'BLOB': $attributes['TYPE'] = 'BYTEA'; break; + default: + break; } // If this is an auto-incrementing primary key, use the serial data type instead - if (in_array($field, $primary_keys) && array_key_exists('AUTO_INCREMENT', $attributes) - && $attributes['AUTO_INCREMENT'] === TRUE) - { - $sql .= ' SERIAL'; - } - else - { - $sql .= ' '.$attributes['TYPE']; - } + $sql .= (in_array($field, $primary_keys) && ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) + ? ' SERIAL' : ' '.$attributes['TYPE']; // Modified to prevent constraints with integer data types - if (array_key_exists('CONSTRAINT', $attributes) && strpos($attributes['TYPE'], 'INT') === false) + if ( ! empty($attributes['CONSTRAINT']) && strpos($attributes['TYPE'], 'INT') === FALSE) { $sql .= '('.$attributes['CONSTRAINT'].')'; } - 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'; - } - - // Added new attribute to create unqite fields. Also works with MySQL - if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE) - { - $sql .= ' UNIQUE'; - } + $sql .= (isset($attributes['DEFAULT']) ? " DEFAULT '".$attributes['DEFAULT']."'" : '') + .(( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL') + // Added new attribute to create unqite fields. Also works with MySQL + .(( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) ? ' UNIQUE' : ''); } // don't add a comma on the end of the last field @@ -168,7 +143,7 @@ class CI_DB_postgre_forge extends CI_DB_forge { $sql .= ','; } } - + return $sql; } @@ -177,15 +152,14 @@ class CI_DB_postgre_forge extends CI_DB_forge { /** * 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 + * @param bool should 'IF NOT EXISTS' be added to the SQL * @return bool */ - function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) + public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) { $sql = 'CREATE TABLE '; @@ -198,18 +172,17 @@ class CI_DB_postgre_forge extends CI_DB_forge { } } - $sql .= $this->db->_escape_identifiers($table)." ("; - $sql .= $this->_process_fields($fields, $primary_keys); + $sql .= $this->db->_escape_identifiers($table).' ('.$this->_process_fields($fields, $primary_keys); if (count($primary_keys) > 0) { - // Something seems to break when passing an array to _protect_identifiers() + // Something seems to break when passing an array to protect_identifiers() foreach ($primary_keys as $index => $key) { - $primary_keys[$index] = $this->db->_protect_identifiers($key); + $primary_keys[$index] = $this->db->protect_identifiers($key); } - $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")"; + $sql .= ",\n\tPRIMARY KEY (".implode(', ', $primary_keys).')'; } $sql .= "\n);"; @@ -220,16 +193,16 @@ class CI_DB_postgre_forge extends CI_DB_forge { { if (is_array($key)) { - $key = $this->db->_protect_identifiers($key); + $key = $this->db->protect_identifiers($key); } else { - $key = array($this->db->_protect_identifiers($key)); + $key = array($this->db->protect_identifiers($key)); } foreach ($key as $field) { - $sql .= "CREATE INDEX " . $table . "_" . str_replace(array('"', "'"), '', $field) . "_index ON $table ($field); "; + $sql .= 'CREATE INDEX '.$table.'_'.str_replace(array('"', "'"), '', $field).'_index ON '.$table.' ('.$field.'); '; } } } @@ -241,10 +214,13 @@ class CI_DB_postgre_forge extends CI_DB_forge { /** * Drop Table + * + * @param string table name + * @return string */ - function _drop_table($table) + public function _drop_table($table) { - return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table)." CASCADE"; + return 'DROP TABLE IF EXISTS '.$this->db->_escape_identifiers($table).' CASCADE'; } // -------------------------------------------------------------------- @@ -255,34 +231,27 @@ class CI_DB_postgre_forge extends CI_DB_forge { * 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 bool should 'NOT NULL' be added * @param string the field after which we should add the new field - * @return object + * @return string */ - function _alter_table($alter_type, $table, $fields, $after_field = '') + public function _alter_table($alter_type, $table, $fields, $after_field = '') { - $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type "; + $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '; // DROP has everything it needs now. - if ($alter_type == 'DROP') + if ($alter_type === 'DROP') { - return $sql.$this->db->_protect_identifiers($fields); + 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; + return $sql.$this->_process_fields($fields) + .($after_field != '' ? ' AFTER '.$this->db->protect_identifiers($after_field) : ''); } // -------------------------------------------------------------------- @@ -292,17 +261,16 @@ class CI_DB_postgre_forge extends CI_DB_forge { * * 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) + public function _rename_table($table_name, $new_table_name) { - $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name); - return $sql; + return 'ALTER TABLE '.$this->db->protect_identifiers($table_name).' RENAME TO '.$this->db->protect_identifiers($new_table_name); } + } /* End of file postgre_forge.php */ -/* Location: ./system/database/drivers/postgre/postgre_forge.php */ \ No newline at end of file +/* Location: ./system/database/drivers/postgre/postgre_forge.php */ diff --git a/system/database/drivers/postgre/postgre_result.php b/system/database/drivers/postgre/postgre_result.php index 9161bf955..2cfa997e7 100644 --- a/system/database/drivers/postgre/postgre_result.php +++ b/system/database/drivers/postgre/postgre_result.php @@ -1,13 +1,13 @@ -result_id); } @@ -54,10 +51,9 @@ class CI_DB_postgre_result extends CI_DB_result { /** * Number of fields in the result set * - * @access public - * @return integer + * @return int */ - function num_fields() + public function num_fields() { return @pg_num_fields($this->result_id); } @@ -69,13 +65,12 @@ class CI_DB_postgre_result extends CI_DB_result { * * Generates an array of column names * - * @access public * @return array */ - function list_fields() + public function list_fields() { $field_names = array(); - for ($i = 0; $i < $this->num_fields(); $i++) + for ($i = 0, $c = $this->num_fields(); $i < $c; $i++) { $field_names[] = pg_field_name($this->result_id, $i); } @@ -90,22 +85,19 @@ class CI_DB_postgre_result extends CI_DB_result { * * Generates an array of objects containing field meta-data * - * @access public * @return array */ - function field_data() + public function field_data() { $retval = array(); - for ($i = 0; $i < $this->num_fields(); $i++) + for ($i = 0, $c = $this->num_fields(); $i < $c; $i++) { - $F = new stdClass(); - $F->name = pg_field_name($this->result_id, $i); - $F->type = pg_field_type($this->result_id, $i); - $F->max_length = pg_field_size($this->result_id, $i); - $F->primary_key = 0; - $F->default = ''; - - $retval[] = $F; + $retval[$i] = new stdClass(); + $retval[$i]->name = pg_field_name($this->result_id, $i); + $retval[$i]->type = pg_field_type($this->result_id, $i); + $retval[$i]->max_length = pg_field_size($this->result_id, $i); + $retval[$i]->primary_key = 0; + $retval[$i]->default = ''; } return $retval; @@ -116,9 +108,9 @@ class CI_DB_postgre_result extends CI_DB_result { /** * Free the result * - * @return null + * @return void */ - function free_result() + public function free_result() { if (is_resource($this->result_id)) { @@ -132,14 +124,13 @@ class CI_DB_postgre_result extends CI_DB_result { /** * Data Seek * - * Moves the internal pointer to the desired offset. We call + * 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) + protected function _data_seek($n = 0) { return pg_result_seek($this->result_id, $n); } @@ -151,10 +142,9 @@ class CI_DB_postgre_result extends CI_DB_result { * * Returns the result set as an array * - * @access private * @return array */ - function _fetch_assoc() + protected function _fetch_assoc() { return pg_fetch_assoc($this->result_id); } @@ -166,16 +156,14 @@ class CI_DB_postgre_result extends CI_DB_result { * * Returns the result set as an object * - * @access private * @return object */ - function _fetch_object() + protected function _fetch_object() { return pg_fetch_object($this->result_id); } } - /* End of file postgre_result.php */ -/* Location: ./system/database/drivers/postgre/postgre_result.php */ \ No newline at end of file +/* Location: ./system/database/drivers/postgre/postgre_result.php */ diff --git a/system/database/drivers/postgre/postgre_utility.php b/system/database/drivers/postgre/postgre_utility.php index dffd8c549..c175a382b 100644 --- a/system/database/drivers/postgre/postgre_utility.php +++ b/system/database/drivers/postgre/postgre_utility.php @@ -1,13 +1,13 @@ -db->display_error('db_unsuported_feature'); } } - /* End of file postgre_utility.php */ -/* Location: ./system/database/drivers/postgre/postgre_utility.php */ \ No newline at end of file +/* Location: ./system/database/drivers/postgre/postgre_utility.php */ -- cgit v1.2.3-24-g4f1b From 0e5a17f6c736397c1e9581071cb852b5291fa97e Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 26 Jan 2012 16:49:03 +0200 Subject: Update the changelog --- user_guide_src/source/changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 48011f208..0e5b21395 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -47,6 +47,7 @@ Release Date: Not Released get_compiled_insert(), get_compiled_update(), get_compiled_delete(). - Taking care of LIKE condition when used with MySQL UPDATE statement. - Adding $escape parameter to the order_by function, this enables ordering by custom fields. + - The PostgreSQL database driver now uses pg_version() for server version checking, when appropriate. - Libraries -- cgit v1.2.3-24-g4f1b From ca7e5f1eb15408b9d71e55d9a0e54b96f5ddc6aa Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 26 Jan 2012 17:58:28 +0200 Subject: Call parent::__construct() in our constructor --- system/database/drivers/cubrid/cubrid_driver.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index b89746b7d..d1fc5f0fa 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -57,8 +57,10 @@ class CI_DB_cubrid_driver extends CI_DB { protected $_count_string = 'SELECT COUNT(*) AS '; protected $_random_keyword = ' RAND()'; // database specific random keyword - public function __construct() + public function __construct($params) { + parent::__construct($params); + // If no port is defined by the user, use the default value if ($this->port == '') { -- cgit v1.2.3-24-g4f1b From c25c3d3d61ff38a348224252c1b3c9041e50ac32 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 27 Jan 2012 11:35:30 +0200 Subject: Remove an empty line :) --- system/database/drivers/postgre/postgre_driver.php | 1 - 1 file changed, 1 deletion(-) diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index 8b8f33371..4e35cd6d7 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -184,7 +184,6 @@ class CI_DB_postgre_driver extends CI_DB { * * @return string */ - public function version() { $version = pg_version($this->conn_id); -- cgit v1.2.3-24-g4f1b From 8d5b24a8c55dc1ae7721e10de094c4aba2ca7eae Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 27 Jan 2012 14:37:38 +0200 Subject: Fix issue #128 --- system/core/Lang.php | 23 ++++++++++++----------- user_guide_src/source/changelog.rst | 1 + 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/system/core/Lang.php b/system/core/Lang.php index c40a6856e..d68c04812 100755 --- a/system/core/Lang.php +++ b/system/core/Lang.php @@ -25,8 +25,6 @@ * @filesource */ -// ------------------------------------------------------------------------ - /** * Language Class * @@ -74,22 +72,20 @@ class CI_Lang { if ($add_suffix == TRUE) { - $langfile = str_replace('_lang.', '', $langfile).'_lang'; + $langfile = str_replace('_lang', '', $langfile).'_lang'; } $langfile .= '.php'; - if (in_array($langfile, $this->is_loaded, TRUE)) + if ($idiom == '') { - return; + $config =& get_config(); + $idiom = ( ! empty($config['language'])) ? $config['language'] : 'english'; } - $config =& get_config(); - - if ($idiom == '') + if ($return == FALSE && isset($this->is_loaded[$langfile]) && $this->is_loaded[$langfile] === $idiom) { - $deft_lang = ( ! isset($config['language'])) ? 'english' : $config['language']; - $idiom = ($deft_lang == '') ? 'english' : $deft_lang; + return; } // Determine where the language file is and load it @@ -121,6 +117,11 @@ class CI_Lang { if ( ! isset($lang) OR ! is_array($lang)) { log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile); + + if ($return == TRUE) + { + return array(); + } return; } @@ -129,7 +130,7 @@ class CI_Lang { return $lang; } - $this->is_loaded[] = $langfile; + $this->is_loaded[$langfile] = $idiom; $this->language = array_merge($this->language, $lang); unset($lang); diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 48011f208..71143b99b 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -97,6 +97,7 @@ Bug fixes for 3.0 - In Pagination library, when use_page_numbers=TRUE previous link and page 1 link do not have the same url - Fixed a bug (#561) - Errors in :doc:`XML-RPC Library ` were not properly escaped. - Fixed a bug (#904) - ``CI_Loader::initialize()`` caused a PHP Fatal error to be triggered if error level E_STRICT is used. +- Fixed a bug (#128) - :doc:`Language Library ` did not correctly keep track of loaded language files. Version 2.1.0 ============= -- cgit v1.2.3-24-g4f1b From 65571d9d9684573887dc4a481b44f33b13584059 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 27 Jan 2012 14:51:58 +0200 Subject: Remove an unnecessary unset() --- system/core/Lang.php | 1 - 1 file changed, 1 deletion(-) diff --git a/system/core/Lang.php b/system/core/Lang.php index d68c04812..711ccab70 100755 --- a/system/core/Lang.php +++ b/system/core/Lang.php @@ -132,7 +132,6 @@ class CI_Lang { $this->is_loaded[$langfile] = $idiom; $this->language = array_merge($this->language, $lang); - unset($lang); log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile); return TRUE; -- cgit v1.2.3-24-g4f1b From 7719a037c4648be714ebe577aa38acba012f7d90 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 27 Jan 2012 21:05:55 +0200 Subject: Switch _process_fields() from private to protected --- system/database/drivers/cubrid/cubrid_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/database/drivers/cubrid/cubrid_forge.php b/system/database/drivers/cubrid/cubrid_forge.php index 4b1c2839a..8423180e9 100644 --- a/system/database/drivers/cubrid/cubrid_forge.php +++ b/system/database/drivers/cubrid/cubrid_forge.php @@ -70,7 +70,7 @@ class CI_DB_cubrid_forge extends CI_DB_forge { * @param mixed the fields * @return string */ - private function _process_fields($fields) + protected function _process_fields($fields) { $current_field_count = 0; $sql = ''; -- cgit v1.2.3-24-g4f1b From e90b6844701a8d7240cb2ac595c2d9d0d211d765 Mon Sep 17 00:00:00 2001 From: Kwaan Online <> Date: Tue, 31 Jan 2012 10:15:30 +0000 Subject: - Updated timezone selete list to match function output. - Updated timezone reference to match function output. - Note some reference locations have been abridged so they fit the table format. A note has been added to this effect. --- user_guide_src/source/helpers/date_helper.rst | 152 ++++++++++++++++---------- 1 file changed, 92 insertions(+), 60 deletions(-) diff --git a/user_guide_src/source/helpers/date_helper.rst b/user_guide_src/source/helpers/date_helper.rst index ad06dd628..bd5a3aa18 100644 --- a/user_guide_src/source/helpers/date_helper.rst +++ b/user_guide_src/source/helpers/date_helper.rst @@ -327,36 +327,46 @@ Generates a pull-down menu of timezones, like this one:
@@ -393,66 +403,88 @@ Timezone Reference The following table indicates each timezone and its location. +Note some of the location lists have been abridged for clarity and formatting. + +------------+----------------------------------------------------------------+ | Time Zone | Location | +============+================================================================+ -| UM12 | (UTC - 12:00) Enitwetok, Kwajalien | +| UM12 | (UTC - 12:00) Baker/Howland Island | ++------------+----------------------------------------------------------------+ +| UM11 | (UTC - 11:00) Samoa Time Zone, Niue | ++------------+----------------------------------------------------------------+ +| UM10 | (UTC - 10:00) Hawaii-Aleutian Standard Time, Cook Islands | ++------------+----------------------------------------------------------------+ +| UM95 | (UTC - 09:30) Marquesas Islands | ++------------+----------------------------------------------------------------+ +| UM9 | (UTC - 09:00) Alaska Standard Time, Gambier Islands | ++------------+----------------------------------------------------------------+ +| UM8 | (UTC - 08:00) Pacific Standard Time, Clipperton Island | ++------------+----------------------------------------------------------------+ +| UM7 | (UTC - 11:00) Mountain Standard Time | ++------------+----------------------------------------------------------------+ +| UM6 | (UTC - 06:00) Central Standard Time | ++------------+----------------------------------------------------------------+ +| UM5 | (UTC - 05:00) Eastern Standard Time, Western Caribbean | ++------------+----------------------------------------------------------------+ +| UM45 | (UTC - 04:30) Venezuelan Standard Time | ++------------+----------------------------------------------------------------+ +| UM4 | (UTC - 04:00) Atlantic Standard Time, Eastern Caribbean | +------------+----------------------------------------------------------------+ -| UM11 | (UTC - 11:00) Nome, Midway Island, Samoa | +| UM35 | (UTC - 03:30) Newfoundland Standard Time | +------------+----------------------------------------------------------------+ -| UM10 | (UTC - 10:00) Hawaii | +| UM3 | (UTC - 03:00) Argentina, Brazil, French Guiana, Uruguay | +------------+----------------------------------------------------------------+ -| UM9 | (UTC - 9:00) Alaska | +| UM2 | (UTC - 02:00) South Georgia/South Sandwich Islands | +------------+----------------------------------------------------------------+ -| UM8 | (UTC - 8:00) Pacific Time | +| UM1 | (UTC -1:00) Azores, Cape Verde Islands | +------------+----------------------------------------------------------------+ -| UM7 | (UTC - 7:00) Mountain Time | +| UTC | (UTC) Greenwich Mean Time, Western European Time | +------------+----------------------------------------------------------------+ -| UM6 | (UTC - 6:00) Central Time, Mexico City | +| UP1 | (UTC +1:00) Central European Time, West Africa Time | +------------+----------------------------------------------------------------+ -| UM5 | (UTC - 5:00) Eastern Time, Bogota, Lima, Quito | +| UP2 | (UTC +2:00) Central Africa Time, Eastern European Time | +------------+----------------------------------------------------------------+ -| UM4 | (UTC - 4:00) Atlantic Time, Caracas, La Paz | +| UP3 | (UTC +3:00) Moscow Time, East Africa Time | +------------+----------------------------------------------------------------+ -| UM25 | (UTC - 3:30) Newfoundland | +| UP35 | (UTC +3:30) Iran Standard Time | +------------+----------------------------------------------------------------+ -| UM3 | (UTC - 3:00) Brazil, Buenos Aires, Georgetown, Falkland Is. | +| UP4 | (UTC +4:00) Azerbaijan Standard Time, Samara Time | +------------+----------------------------------------------------------------+ -| UM2 | (UTC - 2:00) Mid-Atlantic, Ascention Is., St Helena | +| UP45 | (UTC +4:30) Afghanistan | +------------+----------------------------------------------------------------+ -| UM1 | (UTC - 1:00) Azores, Cape Verde Islands | +| UP5 | (UTC +5:00) Pakistan Standard Time, Yekaterinburg Time | +------------+----------------------------------------------------------------+ -| UTC | (UTC) Casablanca, Dublin, Edinburgh, London, Lisbon, Monrovia | +| UP55 | (UTC +5:30) Indian Standard Time, Sri Lanka Time | +------------+----------------------------------------------------------------+ -| UP1 | (UTC + 1:00) Berlin, Brussels, Copenhagen, Madrid, Paris, Rome | +| UP575 | (UTC +5:45) Nepal Time | +------------+----------------------------------------------------------------+ -| UP2 | (UTC + 2:00) Kaliningrad, South Africa, Warsaw | +| UP6 | (UTC +6:00) Bangladesh Standard Time, Bhutan Time, Omsk Time | +------------+----------------------------------------------------------------+ -| UP3 | (UTC + 3:00) Baghdad, Riyadh, Moscow, Nairobi | +| UP65 | (UTC +6:30) Cocos Islands, Myanmar | +------------+----------------------------------------------------------------+ -| UP25 | (UTC + 3:30) Tehran | +| UP7 | (UTC +7:00) Krasnoyarsk Time, Cambodia, Laos, Thailand, Vietnam| +------------+----------------------------------------------------------------+ -| UP4 | (UTC + 4:00) Adu Dhabi, Baku, Muscat, Tbilisi | +| UP8 | (UTC +8:00) Australian Western Standard Time, Beijing Time | +------------+----------------------------------------------------------------+ -| UP35 | (UTC + 4:30) Kabul | +| UP875 | (UTC +8:45) Australian Central Western Standard Time | +------------+----------------------------------------------------------------+ -| UP5 | (UTC + 5:00) Islamabad, Karachi, Tashkent | +| UP9 | (UTC +9:00) Japan Standard Time, Korea Standard Time, Yakutsk | +------------+----------------------------------------------------------------+ -| UP45 | (UTC + 5:30) Bombay, Calcutta, Madras, New Delhi | +| UP95 | (UTC +9:30) Australian Central Standard Time | +------------+----------------------------------------------------------------+ -| UP6 | (UTC + 6:00) Almaty, Colomba, Dhaka | +| UP10 | (UTC +10:00) Australian Eastern Standard Time, Vladivostok Time| +------------+----------------------------------------------------------------+ -| UP7 | (UTC + 7:00) Bangkok, Hanoi, Jakarta | +| UP105 | (UTC +10:30) Lord Howe Island | +------------+----------------------------------------------------------------+ -| UP8 | (UTC + 8:00) Beijing, Hong Kong, Perth, Singapore, Taipei | +| UP11 | (UTC +11:00) Magadan Time, Solomon Islands, Vanuatu | +------------+----------------------------------------------------------------+ -| UP9 | (UTC + 9:00) Osaka, Sapporo, Seoul, Tokyo, Yakutsk | +| UP115 | (UTC +11:30) Norfolk Island | +------------+----------------------------------------------------------------+ -| UP85 | (UTC + 9:30) Adelaide, Darwin | +| UP12 | (UTC +12:00) Fiji, Gilbert Islands, Kamchatka, New Zealand | +------------+----------------------------------------------------------------+ -| UP10 | (UTC + 10:00) Melbourne, Papua New Guinea, Sydney, Vladivostok | +| UP1275 | (UTC +12:45) Chatham Islands Standard Time | +------------+----------------------------------------------------------------+ -| UP11 | (UTC + 11:00) Magadan, New Caledonia, Solomon Islands | +| UP13 | (UTC +13:00) Phoenix Islands Time, Tonga | +------------+----------------------------------------------------------------+ -| UP12 | (UTC + 12:00) Auckland, Wellington, Fiji, Marshall Island | +| UP14 | (UTC +14:00) Line Islands | +------------+----------------------------------------------------------------+ -- cgit v1.2.3-24-g4f1b From a64c61a77b0e1fb23d2063d4f54fef64c93e7e36 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 12 Feb 2012 22:26:46 +0200 Subject: Improve DSN string support --- system/database/drivers/postgre/postgre_driver.php | 63 +++++++++++++++------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index 4779799a4..89c0cd0c9 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -56,9 +56,6 @@ class CI_DB_postgre_driver extends CI_DB { protected $_count_string = 'SELECT COUNT(*) AS '; protected $_random_keyword = ' RANDOM()'; // database specific random keyword - // Postgre-specific properties - protected $_pg_dsn; - /** * Constructor * @@ -70,26 +67,54 @@ class CI_DB_postgre_driver extends CI_DB { { parent::__construct($params); - $components = array( - 'hostname' => 'host', - 'port' => 'port', - 'database' => 'dbname', - 'username' => 'user', - 'password' => 'password' - ); + if ( ! empty($this->dsn)) + { + return; + } - foreach ($components as $key => $val) + $this->dsn === '' OR $this->dsn = ''; + + if (strpos($this->hostname, '/') !== FALSE) { - if (isset($this->$key) && $this->$key != '') - { - $this->_pg_dsn .= $val.'='.$this->$key.' '; - } + // If UNIX sockets are used, we shouldn't set a port + $this->port = ''; } - if (strlen($this->_pg_dsn) > 0) + $this->hostname === '' OR $this->dsn = 'host='.$this->hostname; + + if ( ! empty($this->port) && ctype_digit($this->port)) { - $this->_pg_dsn = rtrim($this->_pg_dsn); + $this->dsn .= 'host='.$this->port.' '; } + + if ($this->username !== '') + { + $this->dsn .= 'username='.$this->username.' '; + + /* An empty password is valid! + * + * $db['password'] = NULL must be done in order to ignore it. + */ + $this->password === NULL OR $this->dsn .= "password='".$this->password."' "; + } + + $this->database === '' OR $this->dsn .= 'dbname='.$this->database.' '; + + /* We don't have these options as elements in our standard configuration + * array, but they might be set by parse_url() if the configuration was + * provided via string. Example: + * + * postgre://username:password@localhost:5432/database?connect_timeout=5&sslmode=1 + */ + foreach (array('connect_timeout', 'options', 'sslmode', 'service') as $key) + { + if (isset($this->$key) && is_string($this->key) && $this->key !== '') + { + $this->dsn .= $key."='".$this->key."' "; + } + } + + $this->dsn = rtrim($this->dsn); } // -------------------------------------------------------------------- @@ -101,7 +126,7 @@ class CI_DB_postgre_driver extends CI_DB { */ public function db_connect() { - $ret = @pg_connect($this->_pg_dsn); + $ret = @pg_connect($this->dsn); if (is_resource($ret) && $this->char_set != '') { pg_set_client_encoding($ret, $this->char_set); @@ -119,7 +144,7 @@ class CI_DB_postgre_driver extends CI_DB { */ public function db_pconnect() { - $ret = @pg_pconnect($this->_pg_dsn); + $ret = @pg_pconnect($this->dsn); if (is_resource($ret) && $this->char_set != '') { pg_set_client_encoding($ret, $this->char_set); -- cgit v1.2.3-24-g4f1b From 2854724143db31f3bb0821c168314de51480bd43 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 12 Feb 2012 22:30:14 +0200 Subject: Use protect_identifiers() instead of _escape_identifiers() in CI_DB_postgre_forge --- system/database/drivers/postgre/postgre_forge.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/database/drivers/postgre/postgre_forge.php b/system/database/drivers/postgre/postgre_forge.php index f9c79bf29..6030adf38 100644 --- a/system/database/drivers/postgre/postgre_forge.php +++ b/system/database/drivers/postgre/postgre_forge.php @@ -172,7 +172,7 @@ class CI_DB_postgre_forge extends CI_DB_forge { } } - $sql .= $this->db->_escape_identifiers($table).' ('.$this->_process_fields($fields, $primary_keys); + $sql .= $this->db->protect_identifiers($table).' ('.$this->_process_fields($fields, $primary_keys); if (count($primary_keys) > 0) { @@ -220,7 +220,7 @@ class CI_DB_postgre_forge extends CI_DB_forge { */ public function _drop_table($table) { - return 'DROP TABLE IF EXISTS '.$this->db->_escape_identifiers($table).' CASCADE'; + return 'DROP TABLE IF EXISTS '.$this->db->protect_identifiers($table).' CASCADE'; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 2e430a3f3b160641c36037fbaf92457e0b3bb51e Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 12 Feb 2012 23:37:58 +0200 Subject: Added CUBRID DSN string & persistent connections support --- system/database/drivers/cubrid/cubrid_driver.php | 104 +++++++++++++---------- system/database/drivers/cubrid/cubrid_forge.php | 4 +- 2 files changed, 60 insertions(+), 48 deletions(-) diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index d1fc5f0fa..f7dec72de 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -57,15 +57,21 @@ class CI_DB_cubrid_driver extends CI_DB { protected $_count_string = 'SELECT COUNT(*) AS '; protected $_random_keyword = ' RAND()'; // database specific random keyword + // CUBRID-specific properties + public $auto_commit = TRUE; + public function __construct($params) { parent::__construct($params); - // If no port is defined by the user, use the default value - if ($this->port == '') + if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches)) { - // Default CUBRID broker port - $this->port = 33000; + preg_match('/autocommit=on/', $matches[2], $matches) OR $this->auto_commit = FALSE; + } + else + { + // If no port is defined by the user, use the default value + $this->port == '' OR $this->port = 33000; } } @@ -76,44 +82,62 @@ class CI_DB_cubrid_driver extends CI_DB { */ public function db_connect() { - $conn = cubrid_connect($this->hostname, $this->port, $this->database, $this->username, $this->password); - - if ($conn) - { - // Check if a user wants to run queries in dry, i.e. run the - // queries but not commit them. - if (isset($this->auto_commit) && ! $this->auto_commit) - { - cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_FALSE); - } - else - { - cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_TRUE); - $this->auto_commit = TRUE; - } - } - - return $conn; + return $this->_cubrid_connect(); } // -------------------------------------------------------------------- /** * Persistent database connection + * * In CUBRID persistent DB connection is supported natively in CUBRID * engine which can be configured in the CUBRID Broker configuration * file by setting the CCI_PCONNECT parameter to ON. In that case, all * connections established between the client application and the - * server will become persistent. This is calling the same - * @cubrid_connect function will establish persisten connection - * considering that the CCI_PCONNECT is ON. + * server will become persistent. * - * @access private called by the base class * @return resource */ public function db_pconnect() { - return $this->db_connect(); + return $this->_cubrid_connect(TRUE); + } + + // -------------------------------------------------------------------- + + /** + * CUBRID connection + * + * A CUBRID-specific method to create a connection to the database. + * Except for determining if a persistent connection should be used, + * the rest of the logic is the same for db_connect() and db_pconnect(). + * + * @param bool + * @return resource + */ + protected function _cubrid_connect($persistent = FALSE) + { + if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches)) + { + $conn_id = ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '') + ? cubrid_connect_with_url($this->dsn, $this->username, $this->password) + : cubrid_connect_with_url($this->dsn); + } + else + { + $_temp = ($persistent !== TRUE) ? 'cubrid_connect' : 'cubrid_pconnect'; + $conn_id = ($this->username !== '') + ? $_temp($this->hostname, $this->port, $this->database, $this->username, $this->password) + : $_temp($this->hostname, $this->port, $this->database); + } + + if ($conn_id) + { + $_temp = ($this->auto_commit) ? CUBRID_AUTOCOMMIT_TRUE : CUBRID_AUTOCOMMIT_FALSE; + cubrid_set_autocommit($conn_id, $_temp); + } + + return $conn_id; } // -------------------------------------------------------------------- @@ -161,9 +185,7 @@ class CI_DB_cubrid_driver extends CI_DB { */ public function db_set_charset($charset, $collation) { - // In CUBRID, there is no need to set charset or collation. - // This is why returning true will allow the application continue - // its normal process. + // Not supported in CUBRID return TRUE; } @@ -189,8 +211,7 @@ class CI_DB_cubrid_driver extends CI_DB { */ protected function _execute($sql) { - $sql = $this->_prep_query($sql); - return @cubrid_query($sql, $this->conn_id); + return @cubrid_query($this->_prep_query($sql), $this->conn_id); } // -------------------------------------------------------------------- @@ -205,7 +226,6 @@ class CI_DB_cubrid_driver extends CI_DB { */ protected function _prep_query($sql) { - // No need to prepare return $sql; } @@ -385,7 +405,7 @@ class CI_DB_cubrid_driver extends CI_DB { * * Generates a platform-specific query string so that the table names can be fetched * - * @param boolean + * @param bool * @return string */ protected function _list_tables($prefix_limit = FALSE) @@ -607,8 +627,6 @@ class CI_DB_cubrid_driver extends CI_DB { protected function _update_batch($table, $values, $index, $where = NULL) { $ids = array(); - $where = ($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : ''; - foreach ($values as $key => $val) { $ids[] = $val[$index]; @@ -622,9 +640,7 @@ class CI_DB_cubrid_driver extends CI_DB { } } - $sql = 'UPDATE '.$table.' SET '; $cases = ''; - foreach ($final as $k => $v) { $cases .= $k." = CASE \n" @@ -632,7 +648,7 @@ class CI_DB_cubrid_driver extends CI_DB { .'ELSE '.$k.' END, '; } - return $sql.substr($cases, 0, -2) + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2) .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '') .$index.' IN ('.implode(',', $ids).')'; } @@ -672,13 +688,9 @@ class CI_DB_cubrid_driver extends CI_DB { if (count($where) > 0 OR count($like) > 0) { - $conditions = "\nWHERE ".implode("\n", $this->ar_where); - - if (count($where) > 0 && count($like) > 0) - { - $conditions .= ' AND '; - } - $conditions .= implode("\n", $like); + $conditions = "\nWHERE ".implode("\n", $where) + .((count($where) > 0 && count($like) > 0) ? ' AND ' : '') + .implode("\n", $like); } return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit); diff --git a/system/database/drivers/cubrid/cubrid_forge.php b/system/database/drivers/cubrid/cubrid_forge.php index 8423180e9..11143a05f 100644 --- a/system/database/drivers/cubrid/cubrid_forge.php +++ b/system/database/drivers/cubrid/cubrid_forge.php @@ -163,7 +163,7 @@ class CI_DB_cubrid_forge extends CI_DB_forge { } */ - $sql .= $this->db->_escape_identifiers($table).' ('.$this->_process_fields($fields); + $sql .= $this->db->protect_identifiers($table).' ('.$this->_process_fields($fields); // If there is a PK defined if (count($primary_keys) > 0) @@ -203,7 +203,7 @@ class CI_DB_cubrid_forge extends CI_DB_forge { */ public function _drop_table($table) { - return 'DROP TABLE IF EXISTS '.$this->db->_escape_identifiers($table); + return 'DROP TABLE IF EXISTS '.$this->db->protect_identifiers($table); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 6ae2852d0e5b112baa302995d1448f1147a7bf0e Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 12 Feb 2012 23:41:46 +0200 Subject: Update the changelog --- user_guide_src/source/changelog.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 649616e8d..31b99ee5f 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -51,6 +51,8 @@ Release Date: Not Released - MySQLi driver now supports persistent connections when running on PHP >= 5.3. - Added dsn if the group connections in the config use PDO or any driver which need DSN. - Improved PDO database support. + - Added DSN string support for CUBRID. + - Added persistent connections support for CUBRID. - Libraries -- cgit v1.2.3-24-g4f1b From 36cd531e9874d32a272088838055bdd56045c41b Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 13 Feb 2012 01:40:55 +0200 Subject: Fix auto_commit initialization --- system/database/drivers/cubrid/cubrid_driver.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index f7dec72de..a0d874a8c 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -66,7 +66,10 @@ class CI_DB_cubrid_driver extends CI_DB { if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches)) { - preg_match('/autocommit=on/', $matches[2], $matches) OR $this->auto_commit = FALSE; + if (preg_match('/autocommit=off/', $matches[2], $matches)) + { + $this->auto_commit = FALSE; + } } else { -- cgit v1.2.3-24-g4f1b From fe4b4e9d5f5c19da77c8e675f33f030766019ac3 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 13 Feb 2012 05:40:25 +0200 Subject: Fix pconnect with DSN --- system/database/drivers/cubrid/cubrid_driver.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index a0d874a8c..8d5344af9 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -122,9 +122,10 @@ class CI_DB_cubrid_driver extends CI_DB { { if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches)) { + $_temp = ($persistent !== TRUE) ? 'cubrid_connect_with_url' : 'cubrid_pconnect_with_url'; $conn_id = ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '') - ? cubrid_connect_with_url($this->dsn, $this->username, $this->password) - : cubrid_connect_with_url($this->dsn); + ? $_temp($this->dsn, $this->username, $this->password) + : $_temp($this->dsn); } else { -- cgit v1.2.3-24-g4f1b From cac407d6399e9325454b48418cf3fce873ce1b14 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 14 Feb 2012 11:45:36 +0200 Subject: Replace a preg_match() with stripos() --- system/database/drivers/cubrid/cubrid_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index 8d5344af9..147e463ce 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -66,7 +66,7 @@ class CI_DB_cubrid_driver extends CI_DB { if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches)) { - if (preg_match('/autocommit=off/', $matches[2], $matches)) + if (stripos($matches[2], 'autocommit=off') !== FALSE) { $this->auto_commit = FALSE; } -- cgit v1.2.3-24-g4f1b From 394a3f171c72176d3e31a2562e604a26a703f957 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 15 Feb 2012 14:35:11 +0200 Subject: Add improvements from @CUBRID's implementation --- system/database/drivers/cubrid/cubrid_driver.php | 33 +++++++------- system/database/drivers/cubrid/cubrid_forge.php | 26 ++++++----- system/database/drivers/cubrid/cubrid_result.php | 54 +++++------------------ system/database/drivers/cubrid/cubrid_utility.php | 11 ++--- user_guide_src/source/changelog.rst | 2 + 5 files changed, 48 insertions(+), 78 deletions(-) diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index 147e463ce..aa8e997ce 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -43,7 +43,7 @@ class CI_DB_cubrid_driver extends CI_DB { public $dbdriver = 'cubrid'; // The character used for escaping - no need in CUBRID - protected $_escape_char = ''; + protected $_escape_char = '`'; // clause and character used for LIKE escape sequences - not used in CUBRID protected $_like_escape_str = ''; @@ -135,12 +135,6 @@ class CI_DB_cubrid_driver extends CI_DB { : $_temp($this->hostname, $this->port, $this->database); } - if ($conn_id) - { - $_temp = ($this->auto_commit) ? CUBRID_AUTOCOMMIT_TRUE : CUBRID_AUTOCOMMIT_FALSE; - cubrid_set_autocommit($conn_id, $_temp); - } - return $conn_id; } @@ -332,7 +326,9 @@ class CI_DB_cubrid_driver extends CI_DB { return $str; } - if (function_exists('cubrid_real_escape_string') && is_resource($this->conn_id)) + if (function_exists('cubrid_real_escape_string') && + (is_resource($this->conn_id) + OR (get_resource_type($this->conn_id) === 'Unknown' && preg_match('/Resource id #/', strval($this->conn_id))))) { $str = cubrid_real_escape_string($str, $this->conn_id); } @@ -359,7 +355,7 @@ class CI_DB_cubrid_driver extends CI_DB { */ public function affected_rows() { - return @cubrid_affected_rows($this->conn_id); + return @cubrid_affected_rows(); } // -------------------------------------------------------------------- @@ -550,7 +546,7 @@ class CI_DB_cubrid_driver extends CI_DB { */ protected function _insert($table, $keys, $values) { - return 'INSERT INTO '.$table.' ("'.implode('", "', $keys).'") VALUES ('.implode(', ', $values).')'; + return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; } // -------------------------------------------------------------------- @@ -568,7 +564,7 @@ class CI_DB_cubrid_driver extends CI_DB { */ protected function _replace($table, $keys, $values) { - return 'REPLACE INTO '.$table.' ("'.implode('", "', $keys).'") VALUES ('.implode(', ', $values).')'; + return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; } // -------------------------------------------------------------------- @@ -585,7 +581,7 @@ class CI_DB_cubrid_driver extends CI_DB { */ protected function _insert_batch($table, $keys, $values) { - return 'INSERT INTO '.$table.' ("'.implode('", "', $keys).'") VALUES '.implode(', ', $values); + return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values); } // -------------------------------------------------------------------- @@ -602,15 +598,20 @@ class CI_DB_cubrid_driver extends CI_DB { * @param array the limit clause * @return string */ - protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) + protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) { foreach ($values as $key => $val) { - $valstr[] = sprintf('"%s" = %s', $key, $val); + $valstr[] = $key.' = '.$val; + } + + $where = ($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : ''; + if (count($like) > 0) + { + $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); } - return 'UPDATE '.$table.' SET '.implode(', ', $valstr) - .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '') + return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '') .( ! $limit ? '' : ' LIMIT '.$limit); } diff --git a/system/database/drivers/cubrid/cubrid_forge.php b/system/database/drivers/cubrid/cubrid_forge.php index 11143a05f..5f7985705 100644 --- a/system/database/drivers/cubrid/cubrid_forge.php +++ b/system/database/drivers/cubrid/cubrid_forge.php @@ -21,7 +21,7 @@ * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com - * @since Version 1.0 + * @since Version 3.0 * @filesource */ @@ -42,8 +42,8 @@ class CI_DB_cubrid_forge extends CI_DB_forge { */ public function _create_database($name) { - // CUBRID does not allow to create a database in SQL. The GUI tools - // have to be used for this purpose. + // CUBRID does not allow to create a database in SQL. GUI or + // command line tools have to be used for this purpose. return FALSE; } @@ -57,8 +57,8 @@ class CI_DB_cubrid_forge extends CI_DB_forge { */ public function _drop_database($name) { - // CUBRID does not allow to drop a database in SQL. The GUI tools - // have to be used for this purpose. + // CUBRID does not allow to drop a database in SQL. GUI or + // command line tools have to be used for this purpose. return FALSE; } @@ -88,7 +88,7 @@ class CI_DB_cubrid_forge extends CI_DB_forge { { $attributes = array_change_key_case($attributes, CASE_UPPER); - $sql .= "\n\t\"".$this->db->protect_identifiers($field).'"' + $sql .= "\n\t".$this->db->protect_identifiers($field) .( ! empty($attributes['NAME']) ? ' '.$this->db->protect_identifiers($attributes['NAME']).' ' : ''); if ( ! empty($attributes['TYPE'])) @@ -104,7 +104,9 @@ class CI_DB_cubrid_forge extends CI_DB_forge { case 'numeric': $sql .= '('.implode(',', $attributes['CONSTRAINT']).')'; break; - case 'enum': // As of version 8.4.0 CUBRID does not support enum data type + case 'enum': + // Will be supported in the future as part a part of + // MySQL compatibility features. break; case 'set': $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")'; @@ -115,7 +117,7 @@ class CI_DB_cubrid_forge extends CI_DB_forge { } } - /* As of version 8.4.0 CUBRID does not support UNSIGNED INTEGER data type. + /* As of version 8.4.1 CUBRID does not support UNSIGNED INTEGER data type. * Will be supported in the next release as a part of MySQL Compatibility. * if (isset($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) @@ -156,7 +158,7 @@ class CI_DB_cubrid_forge extends CI_DB_forge { { $sql = 'CREATE TABLE '; - /* As of version 8.4.0 CUBRID does not support this SQL syntax. + /* As of version 8.4.1 CUBRID does not support this SQL syntax. if ($if_not_exists === TRUE) { $sql .= 'IF NOT EXISTS '; @@ -168,7 +170,7 @@ class CI_DB_cubrid_forge extends CI_DB_forge { // If there is a PK defined if (count($primary_keys) > 0) { - $key_name = 'pk_'.$table.'_'.$this->db->protect_identifiers(implode('_', $primary_keys)); + $key_name = $this->db->protect_identifiers('pk_'.$table.'_'.implode('_', $primary_keys)); $sql .= ",\n\tCONSTRAINT ".$key_name.' PRIMARY KEY('.implode(', ', $this->db->protect_identifiers($primary_keys)).')'; } @@ -178,12 +180,12 @@ class CI_DB_cubrid_forge extends CI_DB_forge { { if (is_array($key)) { - $key_name = $this->db->protect_identifiers(implode('_', $key)); + $key_name = $this->db->protect_identifiers('idx_'.$table.implode('_', $key)); $key = $this->db->protect_identifiers($key); } else { - $key_name = $this->db->protect_identifiers($key); + $key_name = $this->db->protect_identifiers('idx_'.$table.$key); $key = array($key_name); } diff --git a/system/database/drivers/cubrid/cubrid_result.php b/system/database/drivers/cubrid/cubrid_result.php index 7a72cdde4..3351b3430 100644 --- a/system/database/drivers/cubrid/cubrid_result.php +++ b/system/database/drivers/cubrid/cubrid_result.php @@ -83,51 +83,21 @@ class CI_DB_cubrid_result extends CI_DB_result { */ public function field_data() { - $retval = $tablePrimaryKeys = array(); + $retval = array(); + $i = 0; while ($field = cubrid_fetch_field($this->result_id)) { - $F = new stdClass(); - $F->name = $field->name; - $F->type = $field->type; - $F->default = $field->def; - $F->max_length = $field->max_length; - - // At this moment primary_key property is not returned when - // cubrid_fetch_field is called. The following code will - // provide a patch for it. primary_key property will be added - // in the next release. - - // TODO: later version of CUBRID will provide primary_key - // property. - // When PK is defined in CUBRID, an index is automatically - // created in the db_index system table in the form of - // pk_tblname_fieldname. So the following will count how many - // columns are there which satisfy this format. - // The query will search for exact single columns, thus - // compound PK is not supported. - $res = cubrid_query($this->conn_id, - "SELECT COUNT(*) FROM db_index WHERE class_name = '".$field->table - ."' AND is_primary_key = 'YES' AND index_name = 'pk_".$field->table.'_'.$field->name."'" - ); - - if ($res) - { - $row = cubrid_fetch_array($res, CUBRID_NUM); - $F->primary_key = ($row[0] > 0 ? 1 : NULL); - } - else - { - $F->primary_key = NULL; - } - - if (is_resource($res)) - { - cubrid_close_request($res); - $this->result_id = FALSE; - } - - $retval[] = $F; + $retval[$i] = new stdClass(); + $retval[$i]->name = $field->name; + // CUBRID returns type as e.g. varchar(100), + // so we need to remove all digits and brackets. + $retval[$i]->type = preg_replace('/[\d()]/', '', $field->type); + $retval[$i]->default = $field->def; + // Use CUBRID's native API to obtain column's max_length, + // otherwise $field->max_length has incorrect info + $retval[$i]->max_length = cubrid_field_len($this->result_id, $i); + $retval[$i++]->primary_key = $field->primary_key; } return $retval; diff --git a/system/database/drivers/cubrid/cubrid_utility.php b/system/database/drivers/cubrid/cubrid_utility.php index dbfbe5f6b..ab9056ffb 100644 --- a/system/database/drivers/cubrid/cubrid_utility.php +++ b/system/database/drivers/cubrid/cubrid_utility.php @@ -21,7 +21,7 @@ * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com - * @since Version 1.0 + * @since Version 3.0 * @filesource */ @@ -39,14 +39,9 @@ class CI_DB_cubrid_utility extends CI_DB_utility { * * @return array */ - public function _list_databases() + public function list_databases() { - // CUBRID does not allow to see the list of all databases on the - // server. It is the way its architecture is designed. Every - // database is independent and isolated. - // For this reason we can return only the name of the currect - // connected database. - return "SELECT '".$this->database."'"; + return $this->data_cache['db_names'] = cubrid_list_dbs($this->db->conn_id); } // -------------------------------------------------------------------- diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 164c39a95..233fed678 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -53,6 +53,7 @@ Release Date: Not Released - Improved PDO database support. - Added DSN string support for CUBRID. - Added persistent connections support for CUBRID. + - CUBRID driver's utility class now overrides list_databases() in order to use cubrid_list_dbs(). - Libraries @@ -107,6 +108,7 @@ Bug fixes for 3.0 - Fixed a hosting edge case where an empty $_SERVER['HTTPS'] variable would evaluate to 'on' - Fixed a bug (#154) - ``CI_Session::sess_update()`` caused the session to be destroyed on pages where multiple AJAX requests were executed at once. - Fixed a possible bug in ``CI_Input::is_ajax_request()`` where some clients might not send the X-Requested-With HTTP header value exactly as 'XmlHttpRequest'. +- Fixed a bug in CUBRID's affected_rows() where a connection resource was passed to cubrid_affected_rows() instead of a result. Version 2.1.0 ============= -- cgit v1.2.3-24-g4f1b From cf63120a60da9485ab1f94bc58b5d62e6b05a0fd Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 16 Feb 2012 11:36:28 +0200 Subject: A change in the class descriptions --- system/database/drivers/cubrid/cubrid_driver.php | 2 +- system/database/drivers/cubrid/cubrid_forge.php | 2 +- system/database/drivers/cubrid/cubrid_result.php | 2 +- system/database/drivers/cubrid/cubrid_utility.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index aa8e997ce..1dc7a454b 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -21,7 +21,7 @@ * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com - * @since Version 2.0.2 + * @since Version 2.1 * @filesource */ diff --git a/system/database/drivers/cubrid/cubrid_forge.php b/system/database/drivers/cubrid/cubrid_forge.php index 5f7985705..048a0597b 100644 --- a/system/database/drivers/cubrid/cubrid_forge.php +++ b/system/database/drivers/cubrid/cubrid_forge.php @@ -21,7 +21,7 @@ * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com - * @since Version 3.0 + * @since Version 2.1 * @filesource */ diff --git a/system/database/drivers/cubrid/cubrid_result.php b/system/database/drivers/cubrid/cubrid_result.php index 3351b3430..453954754 100644 --- a/system/database/drivers/cubrid/cubrid_result.php +++ b/system/database/drivers/cubrid/cubrid_result.php @@ -21,7 +21,7 @@ * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com - * @since Version 2.0.2 + * @since Version 2.1 * @filesource */ diff --git a/system/database/drivers/cubrid/cubrid_utility.php b/system/database/drivers/cubrid/cubrid_utility.php index ab9056ffb..9819abd08 100644 --- a/system/database/drivers/cubrid/cubrid_utility.php +++ b/system/database/drivers/cubrid/cubrid_utility.php @@ -21,7 +21,7 @@ * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com - * @since Version 3.0 + * @since Version 2.1 * @filesource */ -- cgit v1.2.3-24-g4f1b From 7efad20597ef7e06f8cf837a9f40918d2d3f2727 Mon Sep 17 00:00:00 2001 From: Jamie Rumbelow Date: Sun, 19 Feb 2012 12:37:00 +0000 Subject: Renaming Active Record to Query Builder across the system --- application/config/database.php | 8 +- system/core/Loader.php | 10 +- system/database/DB.php | 18 +- system/database/DB_active_rec.php | 2217 -------------------- system/database/DB_driver.php | 4 +- system/database/DB_query_builder.php | 2217 ++++++++++++++++++++ system/database/drivers/cubrid/cubrid_driver.php | 4 +- system/database/drivers/mssql/mssql_driver.php | 4 +- system/database/drivers/mysql/mysql_driver.php | 4 +- system/database/drivers/mysqli/mysqli_driver.php | 4 +- system/database/drivers/oci8/oci8_driver.php | 4 +- system/database/drivers/odbc/odbc_driver.php | 4 +- system/database/drivers/pdo/pdo_driver.php | 4 +- system/database/drivers/postgre/postgre_driver.php | 4 +- system/database/drivers/sqlite/sqlite_driver.php | 4 +- system/database/drivers/sqlsrv/sqlsrv_driver.php | 2 +- user_guide_src/source/database/active_record.rst | 1008 --------- user_guide_src/source/database/configuration.rst | 12 +- user_guide_src/source/database/connecting.rst | 2 +- user_guide_src/source/database/examples.rst | 8 +- user_guide_src/source/database/index.rst | 4 +- user_guide_src/source/database/queries.rst | 2 +- user_guide_src/source/database/query_builder.rst | 1008 +++++++++ user_guide_src/source/general/models.rst | 2 +- user_guide_src/source/helpers/directory_helper.rst | 2 +- user_guide_src/source/overview/features.rst | 2 +- .../source/tutorial/create_news_items.rst | 2 +- user_guide_src/source/tutorial/index.rst | 2 +- user_guide_src/source/tutorial/news_section.rst | 4 +- 29 files changed, 3285 insertions(+), 3285 deletions(-) delete mode 100644 system/database/DB_active_rec.php create mode 100644 system/database/DB_query_builder.php delete mode 100644 user_guide_src/source/database/active_record.rst create mode 100644 user_guide_src/source/database/query_builder.rst diff --git a/application/config/database.php b/application/config/database.php index bd68db1d8..b1cad9025 100644 --- a/application/config/database.php +++ b/application/config/database.php @@ -45,7 +45,7 @@ | ['dbdriver'] The database type. e.g.: mysql. Currently supported: mysql, mysqli, pdo, postgre, odbc, mssql, sqlite, oci8 | ['dbprefix'] You can add an optional prefix, which will be added -| to the table name when using the Active Record class +| to the table name when using the Query Builder class | ['pconnect'] TRUE/FALSE - Whether to use a persistent connection | ['db_debug'] TRUE/FALSE - Whether database errors should be displayed. | ['cache_on'] TRUE/FALSE - Enables/disables query caching @@ -68,12 +68,12 @@ | The $active_group variable lets you choose which connection group to | make active. By default there is only one group (the 'default' group). | -| The $active_record variables lets you determine whether or not to load -| the active record class +| The $query_builder variables lets you determine whether or not to load +| the query builder class */ $active_group = 'default'; -$active_record = TRUE; +$query_builder = TRUE; $db['default']['dsn'] = ''; $db['default']['hostname'] = 'localhost'; diff --git a/system/core/Loader.php b/system/core/Loader.php index 12daaa928..750e8cdd6 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -315,16 +315,16 @@ class CI_Loader { * * @param string the DB credentials * @param bool whether to return the DB object - * @param bool whether to enable active record (this allows us to override the config setting) + * @param bool whether to enable query builder (this allows us to override the config setting) * @return object */ - public function database($params = '', $return = FALSE, $active_record = NULL) + public function database($params = '', $return = FALSE, $query_builder = NULL) { // Grab the super object $CI =& get_instance(); // Do we even need to load the database class? - if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db)) + if (class_exists('CI_DB') AND $return == FALSE AND $query_builder == NULL AND isset($CI->db) AND is_object($CI->db)) { return FALSE; } @@ -333,7 +333,7 @@ class CI_Loader { if ($return === TRUE) { - return DB($params, $active_record); + return DB($params, $query_builder); } // Initialize the db variable. Needed to prevent @@ -341,7 +341,7 @@ class CI_Loader { $CI->db = ''; // Load the DB class - $CI->db =& DB($params, $active_record); + $CI->db =& DB($params, $query_builder); } // -------------------------------------------------------------------- diff --git a/system/database/DB.php b/system/database/DB.php index d06ffb40e..9ce6103ca 100755 --- a/system/database/DB.php +++ b/system/database/DB.php @@ -32,9 +32,9 @@ * @author EllisLab Dev Team * @link http://codeigniter.com/user_guide/database/ * @param string - * @param bool Determines if active record should be used or not + * @param bool Determines if query builder should be used or not */ -function &DB($params = '', $active_record_override = NULL) +function &DB($params = '', $query_builder_override = NULL) { // Load the DB config file if a DSN string wasn't passed if (is_string($params) AND strpos($params, '://') === FALSE) @@ -115,22 +115,22 @@ function &DB($params = '', $active_record_override = NULL) show_error('You have not selected a database type to connect to.'); } - // Load the DB classes. Note: Since the active record class is optional + // Load the DB classes. Note: Since the query builder class is optional // we need to dynamically create a class that extends proper parent class - // based on whether we're using the active record class or not. - if ($active_record_override !== NULL) + // based on whether we're using the query builder class or not. + if ($query_builder_override !== NULL) { - $active_record = $active_record_override; + $query_builder = $query_builder_override; } require_once(BASEPATH.'database/DB_driver.php'); - if ( ! isset($active_record) OR $active_record == TRUE) + if ( ! isset($query_builder) OR $query_builder == TRUE) { - require_once(BASEPATH.'database/DB_active_rec.php'); + require_once(BASEPATH.'database/DB_query_builder.php'); if ( ! class_exists('CI_DB')) { - class CI_DB extends CI_DB_active_record { } + class CI_DB extends CI_DB_query_builder { } } } elseif ( ! class_exists('CI_DB')) diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php deleted file mode 100644 index 424735157..000000000 --- a/system/database/DB_active_rec.php +++ /dev/null @@ -1,2217 +0,0 @@ -ar_select[] = $val; - $this->ar_no_escape[] = $escape; - - if ($this->ar_caching === TRUE) - { - $this->ar_cache_select[] = $val; - $this->ar_cache_exists[] = 'select'; - $this->ar_cache_no_escape[] = $escape; - } - } - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Select Max - * - * Generates a SELECT MAX(field) portion of a query - * - * @param string the field - * @param string an alias - * @return object - */ - public function select_max($select = '', $alias = '') - { - return $this->_max_min_avg_sum($select, $alias, 'MAX'); - } - - // -------------------------------------------------------------------- - - /** - * Select Min - * - * Generates a SELECT MIN(field) portion of a query - * - * @param string the field - * @param string an alias - * @return object - */ - public function select_min($select = '', $alias = '') - { - return $this->_max_min_avg_sum($select, $alias, 'MIN'); - } - - // -------------------------------------------------------------------- - - /** - * Select Average - * - * Generates a SELECT AVG(field) portion of a query - * - * @param string the field - * @param string an alias - * @return object - */ - public function select_avg($select = '', $alias = '') - { - return $this->_max_min_avg_sum($select, $alias, 'AVG'); - } - - // -------------------------------------------------------------------- - - /** - * Select Sum - * - * Generates a SELECT SUM(field) portion of a query - * - * @param string the field - * @param string an alias - * @return object - */ - public function select_sum($select = '', $alias = '') - { - return $this->_max_min_avg_sum($select, $alias, 'SUM'); - } - - // -------------------------------------------------------------------- - - /** - * Processing Function for the four functions above: - * - * select_max() - * select_min() - * select_avg() - * select_sum() - * - * @param string the field - * @param string an alias - * @return object - */ - protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX') - { - if ( ! is_string($select) OR $select == '') - { - $this->display_error('db_invalid_query'); - } - - $type = strtoupper($type); - - if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM'))) - { - show_error('Invalid function type: '.$type); - } - - if ($alias == '') - { - $alias = $this->_create_alias_from_table(trim($select)); - } - - $sql = $this->_protect_identifiers($type.'('.trim($select).')').' AS '.$this->_protect_identifiers(trim($alias)); - $this->ar_select[] = $sql; - - if ($this->ar_caching === TRUE) - { - $this->ar_cache_select[] = $sql; - $this->ar_cache_exists[] = 'select'; - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Determines the alias name based on the table - * - * @param string - * @return string - */ - protected function _create_alias_from_table($item) - { - if (strpos($item, '.') !== FALSE) - { - return end(explode('.', $item)); - } - - return $item; - } - - // -------------------------------------------------------------------- - - /** - * DISTINCT - * - * Sets a flag which tells the query string compiler to add DISTINCT - * - * @param bool - * @return object - */ - public function distinct($val = TRUE) - { - $this->ar_distinct = (is_bool($val)) ? $val : TRUE; - return $this; - } - - // -------------------------------------------------------------------- - - /** - * From - * - * Generates the FROM portion of the query - * - * @param mixed can be a string or array - * @return object - */ - public function from($from) - { - foreach ((array)$from as $val) - { - if (strpos($val, ',') !== FALSE) - { - foreach (explode(',', $val) as $v) - { - $v = trim($v); - $this->_track_aliases($v); - $v = $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE); - - if ($this->ar_caching === TRUE) - { - $this->ar_cache_from[] = $v; - $this->ar_cache_exists[] = 'from'; - } - } - } - else - { - $val = trim($val); - - // Extract any aliases that might exist. We use this information - // in the _protect_identifiers to know whether to add a table prefix - $this->_track_aliases($val); - $this->ar_from[] = $val = $this->_protect_identifiers($val, TRUE, NULL, FALSE); - - if ($this->ar_caching === TRUE) - { - $this->ar_cache_from[] = $val; - $this->ar_cache_exists[] = 'from'; - } - } - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Join - * - * Generates the JOIN portion of the query - * - * @param string - * @param string the join condition - * @param string the type of join - * @return object - */ - public function join($table, $cond, $type = '') - { - if ($type != '') - { - $type = strtoupper(trim($type)); - - if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'))) - { - $type = ''; - } - else - { - $type .= ' '; - } - } - - // Extract any aliases that might exist. We use this information - // in the _protect_identifiers to know whether to add a table prefix - $this->_track_aliases($table); - - // Strip apart the condition and protect the identifiers - if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match)) - { - $cond = $this->_protect_identifiers($match[1]).$match[2].$this->_protect_identifiers($match[3]); - } - - // Assemble the JOIN statement - $this->ar_join[] = $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond; - - if ($this->ar_caching === TRUE) - { - $this->ar_cache_join[] = $join; - $this->ar_cache_exists[] = 'join'; - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Where - * - * Generates the WHERE portion of the query. Separates - * multiple calls with AND - * - * @param mixed - * @param mixed - * @return object - */ - public function where($key, $value = NULL, $escape = TRUE) - { - return $this->_where($key, $value, 'AND ', $escape); - } - - // -------------------------------------------------------------------- - - /** - * OR Where - * - * Generates the WHERE portion of the query. Separates - * multiple calls with OR - * - * @param mixed - * @param mixed - * @return object - */ - public function or_where($key, $value = NULL, $escape = TRUE) - { - return $this->_where($key, $value, 'OR ', $escape); - } - - // -------------------------------------------------------------------- - - /** - * Where - * - * Called by where() or or_where() - * - * @param mixed - * @param mixed - * @param string - * @return object - */ - protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL) - { - $type = $this->_group_get_type($type); - - if ( ! is_array($key)) - { - $key = array($key => $value); - } - - // If the escape value was not set will will base it on the global setting - if ( ! is_bool($escape)) - { - $escape = $this->_protect_identifiers; - } - - foreach ($key as $k => $v) - { - $prefix = (count($this->ar_where) === 0 AND count($this->ar_cache_where) === 0) ? '' : $type; - - if (is_null($v) && ! $this->_has_operator($k)) - { - // value appears not to have been set, assign the test to IS NULL - $k .= ' IS NULL'; - } - - if ( ! is_null($v)) - { - if ($escape === TRUE) - { - $k = $this->_protect_identifiers($k, FALSE, $escape); - $v = ' '.$this->escape($v); - } - - if ( ! $this->_has_operator($k)) - { - $k .= ' = '; - } - } - else - { - $k = $this->_protect_identifiers($k, FALSE, $escape); - } - - $this->ar_where[] = $prefix.$k.$v; - if ($this->ar_caching === TRUE) - { - $this->ar_cache_where[] = $prefix.$k.$v; - $this->ar_cache_exists[] = 'where'; - } - - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Where_in - * - * Generates a WHERE field IN ('item', 'item') SQL query joined with - * AND if appropriate - * - * @param string The field to search - * @param array The values searched on - * @return object - */ - public function where_in($key = NULL, $values = NULL) - { - return $this->_where_in($key, $values); - } - - // -------------------------------------------------------------------- - - /** - * Where_in_or - * - * Generates a WHERE field IN ('item', 'item') SQL query joined with - * OR if appropriate - * - * @param string The field to search - * @param array The values searched on - * @return object - */ - public function or_where_in($key = NULL, $values = NULL) - { - return $this->_where_in($key, $values, FALSE, 'OR '); - } - - // -------------------------------------------------------------------- - - /** - * Where_not_in - * - * Generates a WHERE field NOT IN ('item', 'item') SQL query joined - * with AND if appropriate - * - * @param string The field to search - * @param array The values searched on - * @return object - */ - public function where_not_in($key = NULL, $values = NULL) - { - return $this->_where_in($key, $values, TRUE); - } - - // -------------------------------------------------------------------- - - /** - * Where_not_in_or - * - * Generates a WHERE field NOT IN ('item', 'item') SQL query joined - * with OR if appropriate - * - * @param string The field to search - * @param array The values searched on - * @return object - */ - public function or_where_not_in($key = NULL, $values = NULL) - { - return $this->_where_in($key, $values, TRUE, 'OR '); - } - - // -------------------------------------------------------------------- - - /** - * Where_in - * - * Called by where_in, where_in_or, where_not_in, where_not_in_or - * - * @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 - */ - protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ') - { - if ($key === NULL OR $values === NULL) - { - return; - } - - $type = $this->_group_get_type($type); - - if ( ! is_array($values)) - { - $values = array($values); - } - - $not = ($not) ? ' NOT' : ''; - - foreach ($values as $value) - { - $this->ar_wherein[] = $this->escape($value); - } - - $prefix = (count($this->ar_where) === 0) ? '' : $type; - $this->ar_where[] = $where_in = $prefix.$this->_protect_identifiers($key).$not.' IN ('.implode(', ', $this->ar_wherein).') '; - - if ($this->ar_caching === TRUE) - { - $this->ar_cache_where[] = $where_in; - $this->ar_cache_exists[] = 'where'; - } - - // reset the array for multiple calls - $this->ar_wherein = array(); - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Like - * - * Generates a %LIKE% portion of the query. Separates - * multiple calls with AND - * - * @param mixed - * @param mixed - * @return object - */ - public function like($field, $match = '', $side = 'both') - { - return $this->_like($field, $match, 'AND ', $side); - } - - // -------------------------------------------------------------------- - - /** - * Not Like - * - * Generates a NOT LIKE portion of the query. Separates - * multiple calls with AND - * - * @param mixed - * @param mixed - * @return object - */ - public function not_like($field, $match = '', $side = 'both') - { - return $this->_like($field, $match, 'AND ', $side, 'NOT'); - } - - // -------------------------------------------------------------------- - - /** - * OR Like - * - * Generates a %LIKE% portion of the query. Separates - * multiple calls with OR - * - * @param mixed - * @param mixed - * @return object - */ - public function or_like($field, $match = '', $side = 'both') - { - return $this->_like($field, $match, 'OR ', $side); - } - - // -------------------------------------------------------------------- - - /** - * OR Not Like - * - * Generates a NOT LIKE portion of the query. Separates - * multiple calls with OR - * - * @param mixed - * @param mixed - * @return object - */ - public function or_not_like($field, $match = '', $side = 'both') - { - return $this->_like($field, $match, 'OR ', $side, 'NOT'); - } - - // -------------------------------------------------------------------- - - /** - * Like - * - * Called by like() or orlike() - * - * @param mixed - * @param mixed - * @param string - * @return object - */ - protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '') - { - $type = $this->_group_get_type($type); - - if ( ! is_array($field)) - { - $field = array($field => $match); - } - - foreach ($field as $k => $v) - { - $k = $this->_protect_identifiers($k); - $prefix = (count($this->ar_like) === 0) ? '' : $type; - $v = $this->escape_like_str($v); - - if ($side === 'none') - { - $like_statement = $prefix." $k $not LIKE '{$v}'"; - } - elseif ($side === 'before') - { - $like_statement = $prefix." $k $not LIKE '%{$v}'"; - } - elseif ($side === 'after') - { - $like_statement = $prefix." $k $not LIKE '{$v}%'"; - } - else - { - $like_statement = $prefix." $k $not LIKE '%{$v}%'"; - } - - // some platforms require an escape sequence definition for LIKE wildcards - if ($this->_like_escape_str != '') - { - $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr); - } - - $this->ar_like[] = $like_statement; - if ($this->ar_caching === TRUE) - { - $this->ar_cache_like[] = $like_statement; - $this->ar_cache_exists[] = 'like'; - } - - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Starts a query group. - * - * @param string (Internal use only) - * @param string (Internal use only) - * @return object - */ - public function group_start($not = '', $type = 'AND ') - { - $type = $this->_group_get_type($type); - $this->ar_where_group_started = TRUE; - $prefix = (count($this->ar_where) === 0 AND count($this->ar_cache_where) === 0) ? '' : $type; - $this->ar_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->ar_where_group_count).' ('; - - if ($this->ar_caching) - { - $this->ar_cache_where[] = $value; - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Starts a query group, but ORs the group - * - * @return object - */ - public function or_group_start() - { - return $this->group_start('', 'OR '); - } - - // -------------------------------------------------------------------- - - /** - * Starts a query group, but NOTs the group - * - * @return object - */ - public function not_group_start() - { - return $this->group_start('NOT ', 'AND '); - } - - // -------------------------------------------------------------------- - - /** - * Starts a query group, but OR NOTs the group - * - * @return object - */ - public function or_not_group_start() - { - return $this->group_start('NOT ', 'OR '); - } - - // -------------------------------------------------------------------- - - /** - * Ends a query group - * - * @return object - */ - public function group_end() - { - $this->ar_where_group_started = FALSE; - $this->ar_where[] = $value = str_repeat(' ', $this->ar_where_group_count--) . ')'; - - if ($this->ar_caching) - { - $this->ar_cache_where[] = $value; - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Group_get_type - * - * Called by group_start(), _like(), _where() and _where_in() - * - * @param string - * @return string - */ - protected function _group_get_type($type) - { - if ($this->ar_where_group_started) - { - $type = ''; - $this->ar_where_group_started = FALSE; - } - - return $type; - } - - // -------------------------------------------------------------------- - - /** - * GROUP BY - * - * @param string - * @return object - */ - public function group_by($by) - { - if (is_string($by)) - { - $by = explode(',', $by); - } - - foreach ($by as $val) - { - $val = trim($val); - - if ($val != '') - { - $this->ar_groupby[] = $val = $this->_protect_identifiers($val); - - if ($this->ar_caching === TRUE) - { - $this->ar_cache_groupby[] = $val; - $this->ar_cache_exists[] = 'groupby'; - } - } - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Sets the HAVING value - * - * Separates multiple calls with AND - * - * @param string - * @param string - * @return object - */ - public function having($key, $value = '', $escape = TRUE) - { - return $this->_having($key, $value, 'AND ', $escape); - } - - // -------------------------------------------------------------------- - - /** - * Sets the OR HAVING value - * - * Separates multiple calls with OR - * - * @param string - * @param string - * @return object - */ - public function or_having($key, $value = '', $escape = TRUE) - { - return $this->_having($key, $value, 'OR ', $escape); - } - - // -------------------------------------------------------------------- - - /** - * Sets the HAVING values - * - * Called by having() or or_having() - * - * @param string - * @param string - * @return object - */ - protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE) - { - if ( ! is_array($key)) - { - $key = array($key => $value); - } - - foreach ($key as $k => $v) - { - $prefix = (count($this->ar_having) === 0) ? '' : $type; - - if ($escape === TRUE) - { - $k = $this->_protect_identifiers($k); - } - - if ( ! $this->_has_operator($k)) - { - $k .= ' = '; - } - - if ($v != '') - { - $v = ' '.$this->escape($v); - } - - $this->ar_having[] = $prefix.$k.$v; - if ($this->ar_caching === TRUE) - { - $this->ar_cache_having[] = $prefix.$k.$v; - $this->ar_cache_exists[] = 'having'; - } - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Sets the ORDER BY value - * - * @param string - * @param string direction: asc or desc - * @param bool enable field name escaping - * @return object - */ - public function order_by($orderby, $direction = '', $escape = TRUE) - { - if (strtolower($direction) === 'random') - { - $orderby = ''; // Random results want or don't need a field name - $direction = $this->_random_keyword; - } - elseif (trim($direction) != '') - { - $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC'; - } - - - if ((strpos($orderby, ',') !== FALSE) && $escape === TRUE) - { - $temp = array(); - foreach (explode(',', $orderby) as $part) - { - $part = trim($part); - if ( ! in_array($part, $this->ar_aliased_tables)) - { - $part = $this->_protect_identifiers(trim($part)); - } - - $temp[] = $part; - } - - $orderby = implode(', ', $temp); - } - elseif ($direction != $this->_random_keyword) - { - if ($escape === TRUE) - { - $orderby = $this->_protect_identifiers($orderby); - } - } - - $this->ar_orderby[] = $orderby_statement = $orderby.$direction; - - if ($this->ar_caching === TRUE) - { - $this->ar_cache_orderby[] = $orderby_statement; - $this->ar_cache_exists[] = 'orderby'; - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Sets the LIMIT value - * - * @param integer the limit value - * @param integer the offset value - * @return object - */ - public function limit($value, $offset = NULL) - { - $this->ar_limit = (int) $value; - - if ( ! is_null($offset)) - { - $this->ar_offset = (int) $offset; - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Sets the OFFSET value - * - * @param integer the offset value - * @return object - */ - public function offset($offset) - { - $this->ar_offset = (int) $offset; - return $this; - } - - // -------------------------------------------------------------------- - - /** - * The "set" function. Allows key/value pairs to be set for inserting or updating - * - * @param mixed - * @param string - * @param boolean - * @return object - */ - public function set($key, $value = '', $escape = TRUE) - { - $key = $this->_object_to_array($key); - - if ( ! is_array($key)) - { - $key = array($key => $value); - } - - foreach ($key as $k => $v) - { - if ($escape === FALSE) - { - $this->ar_set[$this->_protect_identifiers($k)] = $v; - } - else - { - $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v); - } - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Get SELECT query string - * - * Compiles a SELECT query string and returns the sql. - * - * @access public - * @param string the table name to select from (optional) - * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone - * @return string - */ - public function get_compiled_select($table = '', $reset = TRUE) - { - if ($table != '') - { - $this->_track_aliases($table); - $this->from($table); - } - - $select = $this->_compile_select(); - - if ($reset === TRUE) - { - $this->_reset_select(); - } - - return $select; - } - - // -------------------------------------------------------------------- - - /** - * Get - * - * Compiles the select statement based on the other functions called - * and runs the query - * - * @param string the table - * @param string the limit clause - * @param string the offset clause - * @return object - */ - public function get($table = '', $limit = null, $offset = null) - { - if ($table != '') - { - $this->_track_aliases($table); - $this->from($table); - } - - if ( ! is_null($limit)) - { - $this->limit($limit, $offset); - } - - $result = $this->query($this->_compile_select()); - $this->_reset_select(); - return $result; - } - - /** - * "Count All Results" query - * - * Generates a platform-specific query string that counts all records - * returned by an Active Record query. - * - * @param string - * @return string - */ - public function count_all_results($table = '') - { - if ($table != '') - { - $this->_track_aliases($table); - $this->from($table); - } - - $result = $this->query($this->_compile_select($this->_count_string.$this->_protect_identifiers('numrows'))); - $this->_reset_select(); - - if ($result->num_rows() === 0) - { - return 0; - } - - $row = $result->row(); - return (int) $row->numrows; - } - // -------------------------------------------------------------------- - - /** - * Get_Where - * - * Allows the where clause, limit and offset to be added directly - * - * @param string the where clause - * @param string the limit clause - * @param string the offset clause - * @return object - */ - public function get_where($table = '', $where = null, $limit = null, $offset = null) - { - if ($table != '') - { - $this->from($table); - } - - if ( ! is_null($where)) - { - $this->where($where); - } - - if ( ! is_null($limit)) - { - $this->limit($limit, $offset); - } - - $result = $this->query($this->_compile_select()); - $this->_reset_select(); - return $result; - } - - // -------------------------------------------------------------------- - - /** - * Insert_Batch - * - * Compiles batch insert strings and runs the queries - * - * @param string the table to retrieve the results from - * @param array an associative array of insert values - * @return object - */ - public function insert_batch($table = '', $set = NULL) - { - if ( ! is_null($set)) - { - $this->set_insert_batch($set); - } - - if (count($this->ar_set) === 0) - { - if ($this->db_debug) - { - // No valid data array. Folds in cases where keys and values did not match up - return $this->display_error('db_must_use_set'); - } - return FALSE; - } - - if ($table == '') - { - if ( ! isset($this->ar_from[0])) - { - return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; - } - - $table = $this->ar_from[0]; - } - - // Batch this baby - for ($i = 0, $total = count($this->ar_set); $i < $total; $i += 100) - { - $this->query($this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100))); - } - - $this->_reset_write(); - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts - * - * @param mixed - * @param string - * @param boolean - * @return object - */ - public function set_insert_batch($key, $value = '', $escape = TRUE) - { - $key = $this->_object_to_array_batch($key); - - if ( ! is_array($key)) - { - $key = array($key => $value); - } - - $keys = array_keys(current($key)); - sort($keys); - - foreach ($key as $row) - { - if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0) - { - // batch function above returns an error on an empty array - $this->ar_set[] = array(); - return; - } - - ksort($row); // puts $row in the same order as our keys - - if ($escape === FALSE) - { - $this->ar_set[] = '('.implode(',', $row).')'; - } - else - { - $clean = array(); - foreach ($row as $value) - { - $clean[] = $this->escape($value); - } - - $this->ar_set[] = '('.implode(',', $clean).')'; - } - } - - foreach ($keys as $k) - { - $this->ar_keys[] = $this->_protect_identifiers($k); - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Get INSERT query string - * - * Compiles an insert query and returns the sql - * - * @access public - * @param string the table to insert into - * @param boolean TRUE: reset AR values; FALSE: leave AR values alone - * @return string - */ - public function get_compiled_insert($table = '', $reset = TRUE) - { - if ($this->_validate_insert($table) === FALSE) - { - return FALSE; - } - - $sql = $this->_insert( - $this->_protect_identifiers( - $this->ar_from[0], TRUE, NULL, FALSE - ), - array_keys($this->ar_set), - array_values($this->ar_set) - ); - - if ($reset === TRUE) - { - $this->_reset_write(); - } - - return $sql; - } - - // -------------------------------------------------------------------- - - /** - * Insert - * - * Compiles an insert string and runs the query - * - * @access public - * @param string the table to insert data into - * @param array an associative array of insert values - * @return object - */ - public function insert($table = '', $set = NULL) - { - if ( ! is_null($set)) - { - $this->set($set); - } - - if ($this->_validate_insert($table) === FALSE) - { - return FALSE; - } - - $sql = $this->_insert( - $this->_protect_identifiers( - $this->ar_from[0], TRUE, NULL, FALSE - ), - array_keys($this->ar_set), - array_values($this->ar_set) - ); - - $this->_reset_write(); - return $this->query($sql); - } - - // -------------------------------------------------------------------- - - /** - * Validate Insert - * - * This method is used by both insert() and get_compiled_insert() to - * validate that the there data is actually being set and that table - * has been chosen to be inserted into. - * - * @access public - * @param string the table to insert data into - * @return string - */ - protected function _validate_insert($table = '') - { - if (count($this->ar_set) === 0) - { - return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; - } - - if ($table == '') - { - if ( ! isset($this->ar_from[0])) - { - return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; - } - } - else - { - $this->ar_from[0] = $table; - } - - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * 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)) - { - $this->set($set); - } - - if (count($this->ar_set) === 0) - { - return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; - } - - if ($table == '') - { - if ( ! isset($this->ar_from[0])) - { - return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; - } - - $table = $this->ar_from[0]; - } - - $sql = $this->_replace($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set)); - $this->_reset_write(); - return $this->query($sql); - } - - // -------------------------------------------------------------------- - - /** - * Get UPDATE query string - * - * Compiles an update query and returns the sql - * - * @access public - * @param string the table to update - * @param boolean TRUE: reset AR values; FALSE: leave AR values alone - * @return string - */ - public function get_compiled_update($table = '', $reset = TRUE) - { - // Combine any cached components with the current statements - $this->_merge_cache(); - - if ($this->_validate_update($table) === FALSE) - { - return FALSE; - } - - $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit); - - if ($reset === TRUE) - { - $this->_reset_write(); - } - - return $sql; - } - - // -------------------------------------------------------------------- - - /** - * Update - * - * Compiles an update string and runs the query - * - * @param string the table to retrieve the results from - * @param array an associative array of update values - * @param mixed the where clause - * @return object - */ - public function update($table = '', $set = NULL, $where = NULL, $limit = NULL) - { - // Combine any cached components with the current statements - $this->_merge_cache(); - - if ( ! is_null($set)) - { - $this->set($set); - } - - if ($this->_validate_update($table) === FALSE) - { - return FALSE; - } - - if ($where != NULL) - { - $this->where($where); - } - - if ($limit != NULL) - { - $this->limit($limit); - } - - $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit, $this->ar_like); - $this->_reset_write(); - return $this->query($sql); - } - - // -------------------------------------------------------------------- - - /** - * Validate Update - * - * This method is used by both update() and get_compiled_update() to - * validate that data is actually being set and that a table has been - * chosen to be update. - * - * @access public - * @param string the table to update data on - * @return bool - */ - protected function _validate_update($table = '') - { - if (count($this->ar_set) == 0) - { - return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; - } - - if ($table == '') - { - if ( ! isset($this->ar_from[0])) - { - return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; - } - } - else - { - $this->ar_from[0] = $table; - } - - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Update_Batch - * - * Compiles an update string and runs the query - * - * @param string the table to retrieve the results from - * @param array an associative array of update values - * @param string the where key - * @return bool - */ - public function update_batch($table = '', $set = NULL, $index = NULL) - { - // Combine any cached components with the current statements - $this->_merge_cache(); - - if (is_null($index)) - { - return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE; - } - - if ( ! is_null($set)) - { - $this->set_update_batch($set, $index); - } - - if (count($this->ar_set) === 0) - { - return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; - } - - if ($table == '') - { - if ( ! isset($this->ar_from[0])) - { - return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; - } - - $table = $this->ar_from[0]; - } - - // Batch this baby - for ($i = 0, $total = count($this->ar_set); $i < $total; $i += 100) - { - $this->query($this->_update_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->ar_set, $i, 100), $this->_protect_identifiers($index), $this->ar_where)); - } - - $this->_reset_write(); - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * The "set_update_batch" function. Allows key/value pairs to be set for batch updating - * - * @param array - * @param string - * @param boolean - * @return object - */ - public function set_update_batch($key, $index = '', $escape = TRUE) - { - $key = $this->_object_to_array_batch($key); - - if ( ! is_array($key)) - { - // @todo error - } - - foreach ($key as $k => $v) - { - $index_set = FALSE; - $clean = array(); - foreach ($v as $k2 => $v2) - { - if ($k2 == $index) - { - $index_set = TRUE; - } - else - { - $not[] = $k.'-'.$v; - } - - $clean[$this->_protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2); - } - - if ($index_set == FALSE) - { - return $this->display_error('db_batch_missing_index'); - } - - $this->ar_set[] = $clean; - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Empty Table - * - * Compiles a delete string and runs "DELETE FROM table" - * - * @param string the table to empty - * @return object - */ - public function empty_table($table = '') - { - if ($table == '') - { - if ( ! isset($this->ar_from[0])) - { - return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; - } - - $table = $this->ar_from[0]; - } - else - { - $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); - } - - $sql = $this->_delete($table); - $this->_reset_write(); - return $this->query($sql); - } - - // -------------------------------------------------------------------- - - /** - * 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" - * - * @param string the table to truncate - * @return object - */ - public function truncate($table = '') - { - if ($table == '') - { - if ( ! isset($this->ar_from[0])) - { - return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; - } - - $table = $this->ar_from[0]; - } - else - { - $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); - } - - $sql = $this->_truncate($table); - $this->_reset_write(); - return $this->query($sql); - } - - // -------------------------------------------------------------------- - - /** - * Get DELETE query string - * - * Compiles a delete query string and returns the sql - * - * @access public - * @param string the table to delete from - * @param boolean TRUE: reset AR values; FALSE: leave AR values alone - * @return string - */ - public function get_compiled_delete($table = '', $reset = TRUE) - { - $this->return_delete_sql = TRUE; - $sql = $this->delete($table, '', NULL, $reset); - $this->return_delete_sql = FALSE; - return $sql; - } - - // -------------------------------------------------------------------- - - /** - * Delete - * - * Compiles a delete string and runs the query - * - * @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 - */ - public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE) - { - // Combine any cached components with the current statements - $this->_merge_cache(); - - if ($table == '') - { - if ( ! isset($this->ar_from[0])) - { - return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; - } - - $table = $this->ar_from[0]; - } - elseif (is_array($table)) - { - foreach ($table as $single_table) - { - $this->delete($single_table, $where, $limit, FALSE); - } - - $this->_reset_write(); - return; - } - else - { - $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); - } - - if ($where != '') - { - $this->where($where); - } - - if ($limit != NULL) - { - $this->limit($limit); - } - - if (count($this->ar_where) === 0 && count($this->ar_wherein) === 0 && count($this->ar_like) === 0) - { - return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE; - } - - $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit); - if ($reset_data) - { - $this->_reset_write(); - } - - return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql); - } - - // -------------------------------------------------------------------- - - /** - * DB Prefix - * - * Prepends a database prefix if one exists in configuration - * - * @param string the table - * @return string - */ - public function dbprefix($table = '') - { - if ($table == '') - { - $this->display_error('db_table_name_required'); - } - - return $this->dbprefix.$table; - } - - // -------------------------------------------------------------------- - - /** - * 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. - * - * @param string The table to inspect - * @return string - */ - protected function _track_aliases($table) - { - if (is_array($table)) - { - foreach ($table as $t) - { - $this->_track_aliases($t); - } - return; - } - - // Does the string contain a comma? If so, we need to separate - // the string into discreet statements - if (strpos($table, ',') !== FALSE) - { - return $this->_track_aliases(explode(',', $table)); - } - - // if a table alias is used we can recognize it by a space - if (strpos($table, ' ') !== FALSE) - { - // if the alias is written with the AS keyword, remove it - $table = preg_replace('/ AS /i', ' ', $table); - - // Grab the alias - $table = trim(strrchr($table, ' ')); - - // Store the alias, if it doesn't already exist - if ( ! in_array($table, $this->ar_aliased_tables)) - { - $this->ar_aliased_tables[] = $table; - } - } - } - - // -------------------------------------------------------------------- - - /** - * Compile the SELECT statement - * - * Generates a query string based on which functions were used. - * Should not be called directly. The get() function calls it. - * - * @return string - */ - protected function _compile_select($select_override = FALSE) - { - // Combine any cached components with the current statements - $this->_merge_cache(); - - // Write the "select" portion of the query - if ($select_override !== FALSE) - { - $sql = $select_override; - } - else - { - $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT '; - - if (count($this->ar_select) === 0) - { - $sql .= '*'; - } - else - { - // Cycle through the "select" portion of the query and prep each column name. - // The reason we protect identifiers here rather then in the select() function - // is because until the user calls the from() function we don't know if there are aliases - foreach ($this->ar_select as $key => $val) - { - $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL; - $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape); - } - - $sql .= implode(', ', $this->ar_select); - } - } - - // Write the "FROM" portion of the query - if (count($this->ar_from) > 0) - { - $sql .= "\nFROM ".$this->_from_tables($this->ar_from); - } - - // Write the "JOIN" portion of the query - if (count($this->ar_join) > 0) - { - $sql .= "\n".implode("\n", $this->ar_join); - } - - // Write the "WHERE" portion of the query - if (count($this->ar_where) > 0 OR count($this->ar_like) > 0) - { - $sql .= "\nWHERE "; - } - - $sql .= implode("\n", $this->ar_where); - - // Write the "LIKE" portion of the query - if (count($this->ar_like) > 0) - { - if (count($this->ar_where) > 0) - { - $sql .= "\nAND "; - } - - $sql .= implode("\n", $this->ar_like); - } - - // Write the "GROUP BY" portion of the query - if (count($this->ar_groupby) > 0) - { - $sql .= "\nGROUP BY ".implode(', ', $this->ar_groupby); - } - - // Write the "HAVING" portion of the query - if (count($this->ar_having) > 0) - { - $sql .= "\nHAVING ".implode("\n", $this->ar_having); - } - - // Write the "ORDER BY" portion of the query - if (count($this->ar_orderby) > 0) - { - $sql .= "\nORDER BY ".implode(', ', $this->ar_orderby); - if ($this->ar_order !== FALSE) - { - $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC'; - } - } - - // Write the "LIMIT" portion of the query - if (is_numeric($this->ar_limit)) - { - return $this->_limit($sql."\n", $this->ar_limit, $this->ar_offset); - } - - return $sql; - } - - // -------------------------------------------------------------------- - - /** - * Object to Array - * - * Takes an object as input and converts the class variables to array key/vals - * - * @param object - * @return array - */ - public function _object_to_array($object) - { - if ( ! is_object($object)) - { - return $object; - } - - $array = array(); - foreach (get_object_vars($object) as $key => $val) - { - // There are some built in keys we need to ignore for this conversion - if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name') - { - $array[$key] = $val; - } - } - - return $array; - } - - // -------------------------------------------------------------------- - - /** - * Object to Array - * - * Takes an object as input and converts the class variables to array key/vals - * - * @param object - * @return array - */ - public function _object_to_array_batch($object) - { - if ( ! is_object($object)) - { - return $object; - } - - $array = array(); - $out = get_object_vars($object); - $fields = array_keys($out); - - foreach ($fields as $val) - { - // There are some built in keys we need to ignore for this conversion - if ($val !== '_parent_name') - { - $i = 0; - foreach ($out[$val] as $data) - { - $array[$i++][$val] = $data; - } - } - } - - return $array; - } - - // -------------------------------------------------------------------- - - /** - * Start Cache - * - * Starts AR caching - * - * @return void - */ - public function start_cache() - { - $this->ar_caching = TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Stop Cache - * - * Stops AR caching - * - * @return void - */ - public function stop_cache() - { - $this->ar_caching = FALSE; - } - - // -------------------------------------------------------------------- - - /** - * Flush Cache - * - * Empties the AR cache - * - * @access public - * @return void - */ - public function flush_cache() - { - $this->_reset_run(array( - 'ar_cache_select' => array(), - 'ar_cache_from' => array(), - 'ar_cache_join' => array(), - 'ar_cache_where' => array(), - 'ar_cache_like' => array(), - 'ar_cache_groupby' => array(), - 'ar_cache_having' => array(), - 'ar_cache_orderby' => array(), - 'ar_cache_set' => array(), - 'ar_cache_exists' => array(), - 'ar_cache_no_escape' => array() - )); - } - - // -------------------------------------------------------------------- - - /** - * Merge Cache - * - * When called, this function merges any cached AR arrays with - * locally called ones. - * - * @return void - */ - protected function _merge_cache() - { - if (count($this->ar_cache_exists) === 0) - { - return; - } - - foreach ($this->ar_cache_exists as $val) - { - $ar_variable = 'ar_'.$val; - $ar_cache_var = 'ar_cache_'.$val; - - if (count($this->$ar_cache_var) === 0) - { - continue; - } - - $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable)); - } - - // If we are "protecting identifiers" we need to examine the "from" - // portion of the query to determine if there are any aliases - if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0) - { - $this->_track_aliases($this->ar_from); - } - - $this->ar_no_escape = $this->ar_cache_no_escape; - } - - // -------------------------------------------------------------------- - - /** - * Reset Active Record values. - * - * Publicly-visible method to reset the AR values. - * - * @return void - */ - public function reset_query() - { - $this->_reset_select(); - $this->_reset_write(); - } - - // -------------------------------------------------------------------- - - /** - * Resets the active record values. Called by the get() function - * - * @param array An array of fields to reset - * @return void - */ - protected function _reset_run($ar_reset_items) - { - foreach ($ar_reset_items as $item => $default_value) - { - if ( ! in_array($item, $this->ar_store_array)) - { - $this->$item = $default_value; - } - } - } - - // -------------------------------------------------------------------- - - /** - * Resets the active record values. Called by the get() function - * - * @return void - */ - protected function _reset_select() - { - $this->_reset_run(array( - 'ar_select' => array(), - 'ar_from' => array(), - 'ar_join' => array(), - 'ar_where' => array(), - 'ar_like' => array(), - 'ar_groupby' => array(), - 'ar_having' => array(), - 'ar_orderby' => array(), - 'ar_wherein' => array(), - 'ar_aliased_tables' => array(), - 'ar_no_escape' => array(), - 'ar_distinct' => FALSE, - 'ar_limit' => FALSE, - 'ar_offset' => FALSE, - 'ar_order' => FALSE - ) - ); - } - - // -------------------------------------------------------------------- - - /** - * Resets the active record "write" values. - * - * Called by the insert() update() insert_batch() update_batch() and delete() functions - * - * @return void - */ - protected function _reset_write() - { - $this->_reset_run(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 - ) - ); - } - -} - -/* End of file DB_active_rec.php */ -/* Location: ./system/database/DB_active_rec.php */ diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index b829bbe46..339226cbb 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1270,7 +1270,7 @@ class CI_DB_driver { /** * Protect Identifiers * - * This function is used extensively by the Active Record class, and by + * This function is used extensively by the Query Builder class, and by * a couple functions in this class. * It takes a column or table name (optionally with an alias) and inserts * the table prefix onto it. Some logic is necessary in order to deal with @@ -1344,7 +1344,7 @@ class CI_DB_driver { // Does the first segment of the exploded item match // one of the aliases previously identified? If so, // we have nothing more to do other than escape the item - if (in_array($parts[0], $this->ar_aliased_tables)) + if (in_array($parts[0], $this->qb_aliased_tables)) { if ($protect_identifiers === TRUE) { diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php new file mode 100644 index 000000000..b6ad0f3d8 --- /dev/null +++ b/system/database/DB_query_builder.php @@ -0,0 +1,2217 @@ +qb_select[] = $val; + $this->qb_no_escape[] = $escape; + + if ($this->qb_caching === TRUE) + { + $this->qb_cache_select[] = $val; + $this->qb_cache_exists[] = 'select'; + $this->qb_cache_no_escape[] = $escape; + } + } + } + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Select Max + * + * Generates a SELECT MAX(field) portion of a query + * + * @param string the field + * @param string an alias + * @return object + */ + public function select_max($select = '', $alias = '') + { + return $this->_max_min_avg_sum($select, $alias, 'MAX'); + } + + // -------------------------------------------------------------------- + + /** + * Select Min + * + * Generates a SELECT MIN(field) portion of a query + * + * @param string the field + * @param string an alias + * @return object + */ + public function select_min($select = '', $alias = '') + { + return $this->_max_min_avg_sum($select, $alias, 'MIN'); + } + + // -------------------------------------------------------------------- + + /** + * Select Average + * + * Generates a SELECT AVG(field) portion of a query + * + * @param string the field + * @param string an alias + * @return object + */ + public function select_avg($select = '', $alias = '') + { + return $this->_max_min_avg_sum($select, $alias, 'AVG'); + } + + // -------------------------------------------------------------------- + + /** + * Select Sum + * + * Generates a SELECT SUM(field) portion of a query + * + * @param string the field + * @param string an alias + * @return object + */ + public function select_sum($select = '', $alias = '') + { + return $this->_max_min_avg_sum($select, $alias, 'SUM'); + } + + // -------------------------------------------------------------------- + + /** + * Processing Function for the four functions above: + * + * select_max() + * select_min() + * select_avg() + * select_sum() + * + * @param string the field + * @param string an alias + * @return object + */ + protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX') + { + if ( ! is_string($select) OR $select == '') + { + $this->display_error('db_invalid_query'); + } + + $type = strtoupper($type); + + if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM'))) + { + show_error('Invalid function type: '.$type); + } + + if ($alias == '') + { + $alias = $this->_create_alias_from_table(trim($select)); + } + + $sql = $this->_protect_identifiers($type.'('.trim($select).')').' AS '.$this->_protect_identifiers(trim($alias)); + $this->qb_select[] = $sql; + + if ($this->qb_caching === TRUE) + { + $this->qb_cache_select[] = $sql; + $this->qb_cache_exists[] = 'select'; + } + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Determines the alias name based on the table + * + * @param string + * @return string + */ + protected function _create_alias_from_table($item) + { + if (strpos($item, '.') !== FALSE) + { + return end(explode('.', $item)); + } + + return $item; + } + + // -------------------------------------------------------------------- + + /** + * DISTINCT + * + * Sets a flag which tells the query string compiler to add DISTINCT + * + * @param bool + * @return object + */ + public function distinct($val = TRUE) + { + $this->qb_distinct = (is_bool($val)) ? $val : TRUE; + return $this; + } + + // -------------------------------------------------------------------- + + /** + * From + * + * Generates the FROM portion of the query + * + * @param mixed can be a string or array + * @return object + */ + public function from($from) + { + foreach ((array)$from as $val) + { + if (strpos($val, ',') !== FALSE) + { + foreach (explode(',', $val) as $v) + { + $v = trim($v); + $this->_track_aliases($v); + $v = $this->qb_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE); + + if ($this->qb_caching === TRUE) + { + $this->qb_cache_from[] = $v; + $this->qb_cache_exists[] = 'from'; + } + } + } + else + { + $val = trim($val); + + // Extract any aliases that might exist. We use this information + // in the _protect_identifiers to know whether to add a table prefix + $this->_track_aliases($val); + $this->qb_from[] = $val = $this->_protect_identifiers($val, TRUE, NULL, FALSE); + + if ($this->qb_caching === TRUE) + { + $this->qb_cache_from[] = $val; + $this->qb_cache_exists[] = 'from'; + } + } + } + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Join + * + * Generates the JOIN portion of the query + * + * @param string + * @param string the join condition + * @param string the type of join + * @return object + */ + public function join($table, $cond, $type = '') + { + if ($type != '') + { + $type = strtoupper(trim($type)); + + if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'))) + { + $type = ''; + } + else + { + $type .= ' '; + } + } + + // Extract any aliases that might exist. We use this information + // in the _protect_identifiers to know whether to add a table prefix + $this->_track_aliases($table); + + // Strip apart the condition and protect the identifiers + if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match)) + { + $cond = $this->_protect_identifiers($match[1]).$match[2].$this->_protect_identifiers($match[3]); + } + + // Assemble the JOIN statement + $this->qb_join[] = $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond; + + if ($this->qb_caching === TRUE) + { + $this->qb_cache_join[] = $join; + $this->qb_cache_exists[] = 'join'; + } + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Where + * + * Generates the WHERE portion of the query. Separates + * multiple calls with AND + * + * @param mixed + * @param mixed + * @return object + */ + public function where($key, $value = NULL, $escape = TRUE) + { + return $this->_where($key, $value, 'AND ', $escape); + } + + // -------------------------------------------------------------------- + + /** + * OR Where + * + * Generates the WHERE portion of the query. Separates + * multiple calls with OR + * + * @param mixed + * @param mixed + * @return object + */ + public function or_where($key, $value = NULL, $escape = TRUE) + { + return $this->_where($key, $value, 'OR ', $escape); + } + + // -------------------------------------------------------------------- + + /** + * Where + * + * Called by where() or or_where() + * + * @param mixed + * @param mixed + * @param string + * @return object + */ + protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL) + { + $type = $this->_group_get_type($type); + + if ( ! is_array($key)) + { + $key = array($key => $value); + } + + // If the escape value was not set will will base it on the global setting + if ( ! is_bool($escape)) + { + $escape = $this->_protect_identifiers; + } + + foreach ($key as $k => $v) + { + $prefix = (count($this->qb_where) === 0 AND count($this->qb_cache_where) === 0) ? '' : $type; + + if (is_null($v) && ! $this->_has_operator($k)) + { + // value appears not to have been set, assign the test to IS NULL + $k .= ' IS NULL'; + } + + if ( ! is_null($v)) + { + if ($escape === TRUE) + { + $k = $this->_protect_identifiers($k, FALSE, $escape); + $v = ' '.$this->escape($v); + } + + if ( ! $this->_has_operator($k)) + { + $k .= ' = '; + } + } + else + { + $k = $this->_protect_identifiers($k, FALSE, $escape); + } + + $this->qb_where[] = $prefix.$k.$v; + if ($this->qb_caching === TRUE) + { + $this->qb_cache_where[] = $prefix.$k.$v; + $this->qb_cache_exists[] = 'where'; + } + + } + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Where_in + * + * Generates a WHERE field IN ('item', 'item') SQL query joined with + * AND if appropriate + * + * @param string The field to search + * @param array The values searched on + * @return object + */ + public function where_in($key = NULL, $values = NULL) + { + return $this->_where_in($key, $values); + } + + // -------------------------------------------------------------------- + + /** + * Where_in_or + * + * Generates a WHERE field IN ('item', 'item') SQL query joined with + * OR if appropriate + * + * @param string The field to search + * @param array The values searched on + * @return object + */ + public function or_where_in($key = NULL, $values = NULL) + { + return $this->_where_in($key, $values, FALSE, 'OR '); + } + + // -------------------------------------------------------------------- + + /** + * Where_not_in + * + * Generates a WHERE field NOT IN ('item', 'item') SQL query joined + * with AND if appropriate + * + * @param string The field to search + * @param array The values searched on + * @return object + */ + public function where_not_in($key = NULL, $values = NULL) + { + return $this->_where_in($key, $values, TRUE); + } + + // -------------------------------------------------------------------- + + /** + * Where_not_in_or + * + * Generates a WHERE field NOT IN ('item', 'item') SQL query joined + * with OR if appropriate + * + * @param string The field to search + * @param array The values searched on + * @return object + */ + public function or_where_not_in($key = NULL, $values = NULL) + { + return $this->_where_in($key, $values, TRUE, 'OR '); + } + + // -------------------------------------------------------------------- + + /** + * Where_in + * + * Called by where_in, where_in_or, where_not_in, where_not_in_or + * + * @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 + */ + protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ') + { + if ($key === NULL OR $values === NULL) + { + return; + } + + $type = $this->_group_get_type($type); + + if ( ! is_array($values)) + { + $values = array($values); + } + + $not = ($not) ? ' NOT' : ''; + + foreach ($values as $value) + { + $this->qb_wherein[] = $this->escape($value); + } + + $prefix = (count($this->qb_where) === 0) ? '' : $type; + $this->qb_where[] = $where_in = $prefix.$this->_protect_identifiers($key).$not.' IN ('.implode(', ', $this->qb_wherein).') '; + + if ($this->qb_caching === TRUE) + { + $this->qb_cache_where[] = $where_in; + $this->qb_cache_exists[] = 'where'; + } + + // reset the array for multiple calls + $this->qb_wherein = array(); + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Like + * + * Generates a %LIKE% portion of the query. Separates + * multiple calls with AND + * + * @param mixed + * @param mixed + * @return object + */ + public function like($field, $match = '', $side = 'both') + { + return $this->_like($field, $match, 'AND ', $side); + } + + // -------------------------------------------------------------------- + + /** + * Not Like + * + * Generates a NOT LIKE portion of the query. Separates + * multiple calls with AND + * + * @param mixed + * @param mixed + * @return object + */ + public function not_like($field, $match = '', $side = 'both') + { + return $this->_like($field, $match, 'AND ', $side, 'NOT'); + } + + // -------------------------------------------------------------------- + + /** + * OR Like + * + * Generates a %LIKE% portion of the query. Separates + * multiple calls with OR + * + * @param mixed + * @param mixed + * @return object + */ + public function or_like($field, $match = '', $side = 'both') + { + return $this->_like($field, $match, 'OR ', $side); + } + + // -------------------------------------------------------------------- + + /** + * OR Not Like + * + * Generates a NOT LIKE portion of the query. Separates + * multiple calls with OR + * + * @param mixed + * @param mixed + * @return object + */ + public function or_not_like($field, $match = '', $side = 'both') + { + return $this->_like($field, $match, 'OR ', $side, 'NOT'); + } + + // -------------------------------------------------------------------- + + /** + * Like + * + * Called by like() or orlike() + * + * @param mixed + * @param mixed + * @param string + * @return object + */ + protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '') + { + $type = $this->_group_get_type($type); + + if ( ! is_array($field)) + { + $field = array($field => $match); + } + + foreach ($field as $k => $v) + { + $k = $this->_protect_identifiers($k); + $prefix = (count($this->qb_like) === 0) ? '' : $type; + $v = $this->escape_like_str($v); + + if ($side === 'none') + { + $like_statement = $prefix." $k $not LIKE '{$v}'"; + } + elseif ($side === 'before') + { + $like_statement = $prefix." $k $not LIKE '%{$v}'"; + } + elseif ($side === 'after') + { + $like_statement = $prefix." $k $not LIKE '{$v}%'"; + } + else + { + $like_statement = $prefix." $k $not LIKE '%{$v}%'"; + } + + // some platforms require an escape sequence definition for LIKE wildcards + if ($this->_like_escape_str != '') + { + $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr); + } + + $this->qb_like[] = $like_statement; + if ($this->qb_caching === TRUE) + { + $this->qb_cache_like[] = $like_statement; + $this->qb_cache_exists[] = 'like'; + } + + } + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Starts a query group. + * + * @param string (Internal use only) + * @param string (Internal use only) + * @return object + */ + public function group_start($not = '', $type = 'AND ') + { + $type = $this->_group_get_type($type); + $this->qb_where_group_started = TRUE; + $prefix = (count($this->qb_where) === 0 AND count($this->qb_cache_where) === 0) ? '' : $type; + $this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' ('; + + if ($this->qb_caching) + { + $this->qb_cache_where[] = $value; + } + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Starts a query group, but ORs the group + * + * @return object + */ + public function or_group_start() + { + return $this->group_start('', 'OR '); + } + + // -------------------------------------------------------------------- + + /** + * Starts a query group, but NOTs the group + * + * @return object + */ + public function not_group_start() + { + return $this->group_start('NOT ', 'AND '); + } + + // -------------------------------------------------------------------- + + /** + * Starts a query group, but OR NOTs the group + * + * @return object + */ + public function or_not_group_start() + { + return $this->group_start('NOT ', 'OR '); + } + + // -------------------------------------------------------------------- + + /** + * Ends a query group + * + * @return object + */ + public function group_end() + { + $this->qb_where_group_started = FALSE; + $this->qb_where[] = $value = str_repeat(' ', $this->qb_where_group_count--) . ')'; + + if ($this->qb_caching) + { + $this->qb_cache_where[] = $value; + } + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Group_get_type + * + * Called by group_start(), _like(), _where() and _where_in() + * + * @param string + * @return string + */ + protected function _group_get_type($type) + { + if ($this->qb_where_group_started) + { + $type = ''; + $this->qb_where_group_started = FALSE; + } + + return $type; + } + + // -------------------------------------------------------------------- + + /** + * GROUP BY + * + * @param string + * @return object + */ + public function group_by($by) + { + if (is_string($by)) + { + $by = explode(',', $by); + } + + foreach ($by as $val) + { + $val = trim($val); + + if ($val != '') + { + $this->qb_groupby[] = $val = $this->_protect_identifiers($val); + + if ($this->qb_caching === TRUE) + { + $this->qb_cache_groupby[] = $val; + $this->qb_cache_exists[] = 'groupby'; + } + } + } + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Sets the HAVING value + * + * Separates multiple calls with AND + * + * @param string + * @param string + * @return object + */ + public function having($key, $value = '', $escape = TRUE) + { + return $this->_having($key, $value, 'AND ', $escape); + } + + // -------------------------------------------------------------------- + + /** + * Sets the OR HAVING value + * + * Separates multiple calls with OR + * + * @param string + * @param string + * @return object + */ + public function or_having($key, $value = '', $escape = TRUE) + { + return $this->_having($key, $value, 'OR ', $escape); + } + + // -------------------------------------------------------------------- + + /** + * Sets the HAVING values + * + * Called by having() or or_having() + * + * @param string + * @param string + * @return object + */ + protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE) + { + if ( ! is_array($key)) + { + $key = array($key => $value); + } + + foreach ($key as $k => $v) + { + $prefix = (count($this->qb_having) === 0) ? '' : $type; + + if ($escape === TRUE) + { + $k = $this->_protect_identifiers($k); + } + + if ( ! $this->_has_operator($k)) + { + $k .= ' = '; + } + + if ($v != '') + { + $v = ' '.$this->escape($v); + } + + $this->qb_having[] = $prefix.$k.$v; + if ($this->qb_caching === TRUE) + { + $this->qb_cache_having[] = $prefix.$k.$v; + $this->qb_cache_exists[] = 'having'; + } + } + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Sets the ORDER BY value + * + * @param string + * @param string direction: asc or desc + * @param bool enable field name escaping + * @return object + */ + public function order_by($orderby, $direction = '', $escape = TRUE) + { + if (strtolower($direction) === 'random') + { + $orderby = ''; // Random results want or don't need a field name + $direction = $this->_random_keyword; + } + elseif (trim($direction) != '') + { + $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC'; + } + + + if ((strpos($orderby, ',') !== FALSE) && $escape === TRUE) + { + $temp = array(); + foreach (explode(',', $orderby) as $part) + { + $part = trim($part); + if ( ! in_array($part, $this->qb_aliased_tables)) + { + $part = $this->_protect_identifiers(trim($part)); + } + + $temp[] = $part; + } + + $orderby = implode(', ', $temp); + } + elseif ($direction != $this->_random_keyword) + { + if ($escape === TRUE) + { + $orderby = $this->_protect_identifiers($orderby); + } + } + + $this->qb_orderby[] = $orderby_statement = $orderby.$direction; + + if ($this->qb_caching === TRUE) + { + $this->qb_cache_orderby[] = $orderby_statement; + $this->qb_cache_exists[] = 'orderby'; + } + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Sets the LIMIT value + * + * @param integer the limit value + * @param integer the offset value + * @return object + */ + public function limit($value, $offset = NULL) + { + $this->qb_limit = (int) $value; + + if ( ! is_null($offset)) + { + $this->qb_offset = (int) $offset; + } + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Sets the OFFSET value + * + * @param integer the offset value + * @return object + */ + public function offset($offset) + { + $this->qb_offset = (int) $offset; + return $this; + } + + // -------------------------------------------------------------------- + + /** + * The "set" function. Allows key/value pairs to be set for inserting or updating + * + * @param mixed + * @param string + * @param boolean + * @return object + */ + public function set($key, $value = '', $escape = TRUE) + { + $key = $this->_object_to_array($key); + + if ( ! is_array($key)) + { + $key = array($key => $value); + } + + foreach ($key as $k => $v) + { + if ($escape === FALSE) + { + $this->qb_set[$this->_protect_identifiers($k)] = $v; + } + else + { + $this->qb_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v); + } + } + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Get SELECT query string + * + * Compiles a SELECT query string and returns the sql. + * + * @access public + * @param string the table name to select from (optional) + * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone + * @return string + */ + public function get_compiled_select($table = '', $reset = TRUE) + { + if ($table != '') + { + $this->_track_aliases($table); + $this->from($table); + } + + $select = $this->_compile_select(); + + if ($reset === TRUE) + { + $this->_reset_select(); + } + + return $select; + } + + // -------------------------------------------------------------------- + + /** + * Get + * + * Compiles the select statement based on the other functions called + * and runs the query + * + * @param string the table + * @param string the limit clause + * @param string the offset clause + * @return object + */ + public function get($table = '', $limit = null, $offset = null) + { + if ($table != '') + { + $this->_track_aliases($table); + $this->from($table); + } + + if ( ! is_null($limit)) + { + $this->limit($limit, $offset); + } + + $result = $this->query($this->_compile_select()); + $this->_reset_select(); + return $result; + } + + /** + * "Count All Results" query + * + * Generates a platform-specific query string that counts all records + * returned by an Query Builder query. + * + * @param string + * @return string + */ + public function count_all_results($table = '') + { + if ($table != '') + { + $this->_track_aliases($table); + $this->from($table); + } + + $result = $this->query($this->_compile_select($this->_count_string.$this->_protect_identifiers('numrows'))); + $this->_reset_select(); + + if ($result->num_rows() === 0) + { + return 0; + } + + $row = $result->row(); + return (int) $row->numrows; + } + // -------------------------------------------------------------------- + + /** + * Get_Where + * + * Allows the where clause, limit and offset to be added directly + * + * @param string the where clause + * @param string the limit clause + * @param string the offset clause + * @return object + */ + public function get_where($table = '', $where = null, $limit = null, $offset = null) + { + if ($table != '') + { + $this->from($table); + } + + if ( ! is_null($where)) + { + $this->where($where); + } + + if ( ! is_null($limit)) + { + $this->limit($limit, $offset); + } + + $result = $this->query($this->_compile_select()); + $this->_reset_select(); + return $result; + } + + // -------------------------------------------------------------------- + + /** + * Insert_Batch + * + * Compiles batch insert strings and runs the queries + * + * @param string the table to retrieve the results from + * @param array an associative array of insert values + * @return object + */ + public function insert_batch($table = '', $set = NULL) + { + if ( ! is_null($set)) + { + $this->set_insert_batch($set); + } + + if (count($this->qb_set) === 0) + { + if ($this->db_debug) + { + // No valid data array. Folds in cases where keys and values did not match up + return $this->display_error('db_must_use_set'); + } + return FALSE; + } + + if ($table == '') + { + if ( ! isset($this->qb_from[0])) + { + return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; + } + + $table = $this->qb_from[0]; + } + + // Batch this baby + for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100) + { + $this->query($this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100))); + } + + $this->_reset_write(); + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts + * + * @param mixed + * @param string + * @param boolean + * @return object + */ + public function set_insert_batch($key, $value = '', $escape = TRUE) + { + $key = $this->_object_to_array_batch($key); + + if ( ! is_array($key)) + { + $key = array($key => $value); + } + + $keys = array_keys(current($key)); + sort($keys); + + foreach ($key as $row) + { + if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0) + { + // batch function above returns an error on an empty array + $this->qb_set[] = array(); + return; + } + + ksort($row); // puts $row in the same order as our keys + + if ($escape === FALSE) + { + $this->qb_set[] = '('.implode(',', $row).')'; + } + else + { + $clean = array(); + foreach ($row as $value) + { + $clean[] = $this->escape($value); + } + + $this->qb_set[] = '('.implode(',', $clean).')'; + } + } + + foreach ($keys as $k) + { + $this->qb_keys[] = $this->_protect_identifiers($k); + } + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Get INSERT query string + * + * Compiles an insert query and returns the sql + * + * @access public + * @param string the table to insert into + * @param boolean TRUE: reset AR values; FALSE: leave AR values alone + * @return string + */ + public function get_compiled_insert($table = '', $reset = TRUE) + { + if ($this->_validate_insert($table) === FALSE) + { + return FALSE; + } + + $sql = $this->_insert( + $this->_protect_identifiers( + $this->qb_from[0], TRUE, NULL, FALSE + ), + array_keys($this->qb_set), + array_values($this->qb_set) + ); + + if ($reset === TRUE) + { + $this->_reset_write(); + } + + return $sql; + } + + // -------------------------------------------------------------------- + + /** + * Insert + * + * Compiles an insert string and runs the query + * + * @access public + * @param string the table to insert data into + * @param array an associative array of insert values + * @return object + */ + public function insert($table = '', $set = NULL) + { + if ( ! is_null($set)) + { + $this->set($set); + } + + if ($this->_validate_insert($table) === FALSE) + { + return FALSE; + } + + $sql = $this->_insert( + $this->_protect_identifiers( + $this->qb_from[0], TRUE, NULL, FALSE + ), + array_keys($this->qb_set), + array_values($this->qb_set) + ); + + $this->_reset_write(); + return $this->query($sql); + } + + // -------------------------------------------------------------------- + + /** + * Validate Insert + * + * This method is used by both insert() and get_compiled_insert() to + * validate that the there data is actually being set and that table + * has been chosen to be inserted into. + * + * @access public + * @param string the table to insert data into + * @return string + */ + protected function _validate_insert($table = '') + { + if (count($this->qb_set) === 0) + { + return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; + } + + if ($table == '') + { + if ( ! isset($this->qb_from[0])) + { + return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; + } + } + else + { + $this->qb_from[0] = $table; + } + + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * 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)) + { + $this->set($set); + } + + if (count($this->qb_set) === 0) + { + return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; + } + + if ($table == '') + { + if ( ! isset($this->qb_from[0])) + { + return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; + } + + $table = $this->qb_from[0]; + } + + $sql = $this->_replace($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->qb_set), array_values($this->qb_set)); + $this->_reset_write(); + return $this->query($sql); + } + + // -------------------------------------------------------------------- + + /** + * Get UPDATE query string + * + * Compiles an update query and returns the sql + * + * @access public + * @param string the table to update + * @param boolean TRUE: reset AR values; FALSE: leave AR values alone + * @return string + */ + public function get_compiled_update($table = '', $reset = TRUE) + { + // Combine any cached components with the current statements + $this->_merge_cache(); + + if ($this->_validate_update($table) === FALSE) + { + return FALSE; + } + + $sql = $this->_update($this->_protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set, $this->qb_where, $this->qb_orderby, $this->qb_limit); + + if ($reset === TRUE) + { + $this->_reset_write(); + } + + return $sql; + } + + // -------------------------------------------------------------------- + + /** + * Update + * + * Compiles an update string and runs the query + * + * @param string the table to retrieve the results from + * @param array an associative array of update values + * @param mixed the where clause + * @return object + */ + public function update($table = '', $set = NULL, $where = NULL, $limit = NULL) + { + // Combine any cached components with the current statements + $this->_merge_cache(); + + if ( ! is_null($set)) + { + $this->set($set); + } + + if ($this->_validate_update($table) === FALSE) + { + return FALSE; + } + + if ($where != NULL) + { + $this->where($where); + } + + if ($limit != NULL) + { + $this->limit($limit); + } + + $sql = $this->_update($this->_protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set, $this->qb_where, $this->qb_orderby, $this->qb_limit, $this->qb_like); + $this->_reset_write(); + return $this->query($sql); + } + + // -------------------------------------------------------------------- + + /** + * Validate Update + * + * This method is used by both update() and get_compiled_update() to + * validate that data is actually being set and that a table has been + * chosen to be update. + * + * @access public + * @param string the table to update data on + * @return bool + */ + protected function _validate_update($table = '') + { + if (count($this->qb_set) == 0) + { + return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; + } + + if ($table == '') + { + if ( ! isset($this->qb_from[0])) + { + return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; + } + } + else + { + $this->qb_from[0] = $table; + } + + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Update_Batch + * + * Compiles an update string and runs the query + * + * @param string the table to retrieve the results from + * @param array an associative array of update values + * @param string the where key + * @return bool + */ + public function update_batch($table = '', $set = NULL, $index = NULL) + { + // Combine any cached components with the current statements + $this->_merge_cache(); + + if (is_null($index)) + { + return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE; + } + + if ( ! is_null($set)) + { + $this->set_update_batch($set, $index); + } + + if (count($this->qb_set) === 0) + { + return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; + } + + if ($table == '') + { + if ( ! isset($this->qb_from[0])) + { + return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; + } + + $table = $this->qb_from[0]; + } + + // Batch this baby + for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100) + { + $this->query($this->_update_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, 100), $this->_protect_identifiers($index), $this->qb_where)); + } + + $this->_reset_write(); + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * The "set_update_batch" function. Allows key/value pairs to be set for batch updating + * + * @param array + * @param string + * @param boolean + * @return object + */ + public function set_update_batch($key, $index = '', $escape = TRUE) + { + $key = $this->_object_to_array_batch($key); + + if ( ! is_array($key)) + { + // @todo error + } + + foreach ($key as $k => $v) + { + $index_set = FALSE; + $clean = array(); + foreach ($v as $k2 => $v2) + { + if ($k2 == $index) + { + $index_set = TRUE; + } + else + { + $not[] = $k.'-'.$v; + } + + $clean[$this->_protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2); + } + + if ($index_set == FALSE) + { + return $this->display_error('db_batch_missing_index'); + } + + $this->qb_set[] = $clean; + } + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Empty Table + * + * Compiles a delete string and runs "DELETE FROM table" + * + * @param string the table to empty + * @return object + */ + public function empty_table($table = '') + { + if ($table == '') + { + if ( ! isset($this->qb_from[0])) + { + return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; + } + + $table = $this->qb_from[0]; + } + else + { + $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); + } + + $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" + * + * @param string the table to truncate + * @return object + */ + public function truncate($table = '') + { + if ($table == '') + { + if ( ! isset($this->qb_from[0])) + { + return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; + } + + $table = $this->qb_from[0]; + } + else + { + $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); + } + + $sql = $this->_truncate($table); + $this->_reset_write(); + return $this->query($sql); + } + + // -------------------------------------------------------------------- + + /** + * Get DELETE query string + * + * Compiles a delete query string and returns the sql + * + * @access public + * @param string the table to delete from + * @param boolean TRUE: reset AR values; FALSE: leave AR values alone + * @return string + */ + public function get_compiled_delete($table = '', $reset = TRUE) + { + $this->return_delete_sql = TRUE; + $sql = $this->delete($table, '', NULL, $reset); + $this->return_delete_sql = FALSE; + return $sql; + } + + // -------------------------------------------------------------------- + + /** + * Delete + * + * Compiles a delete string and runs the query + * + * @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 + */ + public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE) + { + // Combine any cached components with the current statements + $this->_merge_cache(); + + if ($table == '') + { + if ( ! isset($this->qb_from[0])) + { + return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; + } + + $table = $this->qb_from[0]; + } + elseif (is_array($table)) + { + foreach ($table as $single_table) + { + $this->delete($single_table, $where, $limit, FALSE); + } + + $this->_reset_write(); + return; + } + else + { + $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); + } + + if ($where != '') + { + $this->where($where); + } + + if ($limit != NULL) + { + $this->limit($limit); + } + + if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0) + { + return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE; + } + + $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit); + if ($reset_data) + { + $this->_reset_write(); + } + + return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql); + } + + // -------------------------------------------------------------------- + + /** + * DB Prefix + * + * Prepends a database prefix if one exists in configuration + * + * @param string the table + * @return string + */ + public function dbprefix($table = '') + { + if ($table == '') + { + $this->display_error('db_table_name_required'); + } + + return $this->dbprefix.$table; + } + + // -------------------------------------------------------------------- + + /** + * 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. + * + * @param string The table to inspect + * @return string + */ + protected function _track_aliases($table) + { + if (is_array($table)) + { + foreach ($table as $t) + { + $this->_track_aliases($t); + } + return; + } + + // Does the string contain a comma? If so, we need to separate + // the string into discreet statements + if (strpos($table, ',') !== FALSE) + { + return $this->_track_aliases(explode(',', $table)); + } + + // if a table alias is used we can recognize it by a space + if (strpos($table, ' ') !== FALSE) + { + // if the alias is written with the AS keyword, remove it + $table = preg_replace('/ AS /i', ' ', $table); + + // Grab the alias + $table = trim(strrchr($table, ' ')); + + // Store the alias, if it doesn't already exist + if ( ! in_array($table, $this->qb_aliased_tables)) + { + $this->qb_aliased_tables[] = $table; + } + } + } + + // -------------------------------------------------------------------- + + /** + * Compile the SELECT statement + * + * Generates a query string based on which functions were used. + * Should not be called directly. The get() function calls it. + * + * @return string + */ + protected function _compile_select($select_override = FALSE) + { + // Combine any cached components with the current statements + $this->_merge_cache(); + + // Write the "select" portion of the query + if ($select_override !== FALSE) + { + $sql = $select_override; + } + else + { + $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT '; + + if (count($this->qb_select) === 0) + { + $sql .= '*'; + } + else + { + // Cycle through the "select" portion of the query and prep each column name. + // The reason we protect identifiers here rather then in the select() function + // is because until the user calls the from() function we don't know if there are aliases + foreach ($this->qb_select as $key => $val) + { + $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL; + $this->qb_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape); + } + + $sql .= implode(', ', $this->qb_select); + } + } + + // Write the "FROM" portion of the query + if (count($this->qb_from) > 0) + { + $sql .= "\nFROM ".$this->_from_tables($this->qb_from); + } + + // Write the "JOIN" portion of the query + if (count($this->qb_join) > 0) + { + $sql .= "\n".implode("\n", $this->qb_join); + } + + // Write the "WHERE" portion of the query + if (count($this->qb_where) > 0 OR count($this->qb_like) > 0) + { + $sql .= "\nWHERE "; + } + + $sql .= implode("\n", $this->qb_where); + + // Write the "LIKE" portion of the query + if (count($this->qb_like) > 0) + { + if (count($this->qb_where) > 0) + { + $sql .= "\nAND "; + } + + $sql .= implode("\n", $this->qb_like); + } + + // Write the "GROUP BY" portion of the query + if (count($this->qb_groupby) > 0) + { + $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby); + } + + // Write the "HAVING" portion of the query + if (count($this->qb_having) > 0) + { + $sql .= "\nHAVING ".implode("\n", $this->qb_having); + } + + // Write the "ORDER BY" portion of the query + if (count($this->qb_orderby) > 0) + { + $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby); + if ($this->qb_order !== FALSE) + { + $sql .= ($this->qb_order == 'desc') ? ' DESC' : ' ASC'; + } + } + + // Write the "LIMIT" portion of the query + if (is_numeric($this->qb_limit)) + { + return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset); + } + + return $sql; + } + + // -------------------------------------------------------------------- + + /** + * Object to Array + * + * Takes an object as input and converts the class variables to array key/vals + * + * @param object + * @return array + */ + public function _object_to_array($object) + { + if ( ! is_object($object)) + { + return $object; + } + + $array = array(); + foreach (get_object_vars($object) as $key => $val) + { + // There are some built in keys we need to ignore for this conversion + if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name') + { + $array[$key] = $val; + } + } + + return $array; + } + + // -------------------------------------------------------------------- + + /** + * Object to Array + * + * Takes an object as input and converts the class variables to array key/vals + * + * @param object + * @return array + */ + public function _object_to_array_batch($object) + { + if ( ! is_object($object)) + { + return $object; + } + + $array = array(); + $out = get_object_vars($object); + $fields = array_keys($out); + + foreach ($fields as $val) + { + // There are some built in keys we need to ignore for this conversion + if ($val !== '_parent_name') + { + $i = 0; + foreach ($out[$val] as $data) + { + $array[$i++][$val] = $data; + } + } + } + + return $array; + } + + // -------------------------------------------------------------------- + + /** + * Start Cache + * + * Starts AR caching + * + * @return void + */ + public function start_cache() + { + $this->qb_caching = TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Stop Cache + * + * Stops AR caching + * + * @return void + */ + public function stop_cache() + { + $this->qb_caching = FALSE; + } + + // -------------------------------------------------------------------- + + /** + * Flush Cache + * + * Empties the AR cache + * + * @access public + * @return void + */ + public function flush_cache() + { + $this->_reset_run(array( + 'ar_cache_select' => array(), + 'ar_cache_from' => array(), + 'ar_cache_join' => array(), + 'ar_cache_where' => array(), + 'ar_cache_like' => array(), + 'ar_cache_groupby' => array(), + 'ar_cache_having' => array(), + 'ar_cache_orderby' => array(), + 'ar_cache_set' => array(), + 'ar_cache_exists' => array(), + 'ar_cache_no_escape' => array() + )); + } + + // -------------------------------------------------------------------- + + /** + * Merge Cache + * + * When called, this function merges any cached AR arrays with + * locally called ones. + * + * @return void + */ + protected function _merge_cache() + { + if (count($this->qb_cache_exists) === 0) + { + return; + } + + foreach ($this->qb_cache_exists as $val) + { + $qb_variable = 'ar_'.$val; + $qb_cache_var = 'ar_cache_'.$val; + + if (count($this->$qb_cache_var) === 0) + { + continue; + } + + $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable)); + } + + // If we are "protecting identifiers" we need to examine the "from" + // portion of the query to determine if there are any aliases + if ($this->_protect_identifiers === TRUE AND count($this->qb_cache_from) > 0) + { + $this->_track_aliases($this->qb_from); + } + + $this->qb_no_escape = $this->qb_cache_no_escape; + } + + // -------------------------------------------------------------------- + + /** + * Reset Query Builder values. + * + * Publicly-visible method to reset the AR values. + * + * @return void + */ + public function reset_query() + { + $this->_reset_select(); + $this->_reset_write(); + } + + // -------------------------------------------------------------------- + + /** + * Resets the query builder values. Called by the get() function + * + * @param array An array of fields to reset + * @return void + */ + protected function _reset_run($qb_reset_items) + { + foreach ($qb_reset_items as $item => $default_value) + { + if ( ! in_array($item, $this->qb_store_array)) + { + $this->$item = $default_value; + } + } + } + + // -------------------------------------------------------------------- + + /** + * Resets the query builder values. Called by the get() function + * + * @return void + */ + protected function _reset_select() + { + $this->_reset_run(array( + 'ar_select' => array(), + 'ar_from' => array(), + 'ar_join' => array(), + 'ar_where' => array(), + 'ar_like' => array(), + 'ar_groupby' => array(), + 'ar_having' => array(), + 'ar_orderby' => array(), + 'ar_wherein' => array(), + 'ar_aliased_tables' => array(), + 'ar_no_escape' => array(), + 'ar_distinct' => FALSE, + 'ar_limit' => FALSE, + 'ar_offset' => FALSE, + 'ar_order' => FALSE + ) + ); + } + + // -------------------------------------------------------------------- + + /** + * Resets the query builder "write" values. + * + * Called by the insert() update() insert_batch() update_batch() and delete() functions + * + * @return void + */ + protected function _reset_write() + { + $this->_reset_run(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 + ) + ); + } + +} + +/* End of file DB_query_builder.php */ +/* Location: ./system/database/DB_query_builder.php */ diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index cde719eae..ccabb3b4b 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -31,7 +31,7 @@ * CUBRID Database Adapter Class * * Note: _DB is an extender class that the app controller - * creates dynamically based on whether the active record + * creates dynamically based on whether the query builder * class is being used or not. * * @package CodeIgniter @@ -742,7 +742,7 @@ class CI_DB_cubrid_driver extends CI_DB { if (count($where) > 0 OR count($like) > 0) { $conditions = "\nWHERE "; - $conditions .= implode("\n", $this->ar_where); + $conditions .= implode("\n", $this->qb_where); if (count($where) > 0 && count($like) > 0) { diff --git a/system/database/drivers/mssql/mssql_driver.php b/system/database/drivers/mssql/mssql_driver.php index 2a1098932..b809241b4 100644 --- a/system/database/drivers/mssql/mssql_driver.php +++ b/system/database/drivers/mssql/mssql_driver.php @@ -31,7 +31,7 @@ * MS SQL Database Adapter Class * * Note: _DB is an extender class that the app controller - * creates dynamically based on whether the active record + * creates dynamically based on whether the query builder * class is being used or not. * * @package CodeIgniter @@ -623,7 +623,7 @@ class CI_DB_mssql_driver extends CI_DB { if (count($where) > 0 OR count($like) > 0) { $conditions = "\nWHERE "; - $conditions .= implode("\n", $this->ar_where); + $conditions .= implode("\n", $this->qb_where); if (count($where) > 0 && count($like) > 0) { diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php index 067710ff0..9b5e331b8 100644 --- a/system/database/drivers/mysql/mysql_driver.php +++ b/system/database/drivers/mysql/mysql_driver.php @@ -29,7 +29,7 @@ * MySQL Database Adapter Class * * Note: _DB is an extender class that the app controller - * creates dynamically based on whether the active record + * creates dynamically based on whether the query builder * class is being used or not. * * @package CodeIgniter @@ -646,7 +646,7 @@ class CI_DB_mysql_driver extends CI_DB { if (count($where) > 0 OR count($like) > 0) { - $conditions = "\nWHERE ".implode("\n", $this->ar_where); + $conditions = "\nWHERE ".implode("\n", $this->qb_where); if (count($where) > 0 && count($like) > 0) { diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php index a79b2a4ad..f2d679f43 100644 --- a/system/database/drivers/mysqli/mysqli_driver.php +++ b/system/database/drivers/mysqli/mysqli_driver.php @@ -29,7 +29,7 @@ * MySQLi Database Adapter Class * * Note: _DB is an extender class that the app controller - * creates dynamically based on whether the active record + * creates dynamically based on whether the query builder * class is being used or not. * * @package CodeIgniter @@ -641,7 +641,7 @@ class CI_DB_mysqli_driver extends CI_DB { $conditions = ''; if (count($where) > 0 OR count($like) > 0) { - $conditions = "\nWHERE ".implode("\n", $this->ar_where); + $conditions = "\nWHERE ".implode("\n", $this->qb_where); if (count($where) > 0 && count($like) > 0) { diff --git a/system/database/drivers/oci8/oci8_driver.php b/system/database/drivers/oci8/oci8_driver.php index c6621901b..5fbc0a0a0 100644 --- a/system/database/drivers/oci8/oci8_driver.php +++ b/system/database/drivers/oci8/oci8_driver.php @@ -31,7 +31,7 @@ * oci8 Database Adapter Class * * Note: _DB is an extender class that the app controller - * creates dynamically based on whether the active record + * creates dynamically based on whether the query builder * class is being used or not. * * @package CodeIgniter @@ -754,7 +754,7 @@ class CI_DB_oci8_driver extends CI_DB { if (count($where) > 0 OR count($like) > 0) { $conditions = "\nWHERE "; - $conditions .= implode("\n", $this->ar_where); + $conditions .= implode("\n", $this->qb_where); if (count($where) > 0 && count($like) > 0) { diff --git a/system/database/drivers/odbc/odbc_driver.php b/system/database/drivers/odbc/odbc_driver.php index 6ba39f0cd..695d0080a 100644 --- a/system/database/drivers/odbc/odbc_driver.php +++ b/system/database/drivers/odbc/odbc_driver.php @@ -31,7 +31,7 @@ * ODBC Database Adapter Class * * Note: _DB is an extender class that the app controller - * creates dynamically based on whether the active record + * creates dynamically based on whether the query builder * class is being used or not. * * @package CodeIgniter @@ -593,7 +593,7 @@ class CI_DB_odbc_driver extends CI_DB { if (count($where) > 0 OR count($like) > 0) { $conditions = "\nWHERE "; - $conditions .= implode("\n", $this->ar_where); + $conditions .= implode("\n", $this->qb_where); if (count($where) > 0 && count($like) > 0) { diff --git a/system/database/drivers/pdo/pdo_driver.php b/system/database/drivers/pdo/pdo_driver.php index de2b0abeb..77d9adf78 100644 --- a/system/database/drivers/pdo/pdo_driver.php +++ b/system/database/drivers/pdo/pdo_driver.php @@ -31,7 +31,7 @@ * PDO Database Adapter Class * * Note: _DB is an extender class that the app controller - * creates dynamically based on whether the active record + * creates dynamically based on whether the query builder * class is being used or not. * * @package CodeIgniter @@ -888,7 +888,7 @@ class CI_DB_pdo_driver extends CI_DB { if (count($where) > 0 OR count($like) > 0) { $conditions = "\nWHERE "; - $conditions .= implode("\n", $this->ar_where); + $conditions .= implode("\n", $this->qb_where); if (count($where) > 0 && count($like) > 0) { diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index 42329bded..3a172bc9e 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -31,7 +31,7 @@ * Postgre Database Adapter Class * * Note: _DB is an extender class that the app controller - * creates dynamically based on whether the active record + * creates dynamically based on whether the query builder * class is being used or not. * * @package CodeIgniter @@ -655,7 +655,7 @@ class CI_DB_postgre_driver extends CI_DB { if (count($where) > 0 OR count($like) > 0) { $conditions = "\nWHERE "; - $conditions .= implode("\n", $this->ar_where); + $conditions .= implode("\n", $this->qb_where); if (count($where) > 0 && count($like) > 0) { diff --git a/system/database/drivers/sqlite/sqlite_driver.php b/system/database/drivers/sqlite/sqlite_driver.php index 28c3caecd..3a7b7c673 100644 --- a/system/database/drivers/sqlite/sqlite_driver.php +++ b/system/database/drivers/sqlite/sqlite_driver.php @@ -33,7 +33,7 @@ * SQLite Database Adapter Class * * Note: _DB is an extender class that the app controller - * creates dynamically based on whether the active record + * creates dynamically based on whether the query builder * class is being used or not. * * @package CodeIgniter @@ -607,7 +607,7 @@ class CI_DB_sqlite_driver extends CI_DB { if (count($where) > 0 OR count($like) > 0) { $conditions = "\nWHERE "; - $conditions .= implode("\n", $this->ar_where); + $conditions .= implode("\n", $this->qb_where); if (count($where) > 0 && count($like) > 0) { diff --git a/system/database/drivers/sqlsrv/sqlsrv_driver.php b/system/database/drivers/sqlsrv/sqlsrv_driver.php index 6fd52ef70..a21009ad3 100644 --- a/system/database/drivers/sqlsrv/sqlsrv_driver.php +++ b/system/database/drivers/sqlsrv/sqlsrv_driver.php @@ -31,7 +31,7 @@ * SQLSRV Database Adapter Class * * Note: _DB is an extender class that the app controller - * creates dynamically based on whether the active record + * creates dynamically based on whether the query builder * class is being used or not. * * @package CodeIgniter diff --git a/user_guide_src/source/database/active_record.rst b/user_guide_src/source/database/active_record.rst deleted file mode 100644 index c04e67d2a..000000000 --- a/user_guide_src/source/database/active_record.rst +++ /dev/null @@ -1,1008 +0,0 @@ -################### -Active Record Class -################### - -CodeIgniter uses a modified version of the Active Record Database -Pattern. This pattern allows information to be retrieved, inserted, and -updated in your database with minimal scripting. In some cases only one -or two lines of code are necessary to perform a database action. -CodeIgniter does not require that each database table be its own class -file. It instead provides a more simplified interface. - -Beyond simplicity, a major benefit to using the Active Record features -is that it allows you to create database independent applications, since -the query syntax is generated by each database adapter. It also allows -for safer queries, since the values are escaped automatically by the -system. - -.. note:: If you intend to write your own queries you can disable this - class in your database config file, allowing the core database library - and adapter to utilize fewer resources. - -.. contents:: Page Contents - -************** -Selecting Data -************** - -The following functions allow you to build SQL **SELECT** statements. - -$this->db->get() -================ - -Runs the selection query and returns the result. Can be used by itself -to retrieve all records from a table:: - - $query = $this->db->get('mytable'); // Produces: SELECT * FROM mytable - -The second and third parameters enable you to set a limit and offset -clause:: - - $query = $this->db->get('mytable', 10, 20); - // Produces: SELECT * FROM mytable LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax) - -You'll notice that the above function is assigned to a variable named -$query, which can be used to show the results:: - - $query = $this->db->get('mytable'); - - foreach ($query->result() as $row) - { - echo $row->title; - } - -Please visit the :doc:`result functions ` page for a full -discussion regarding result generation. - -$this->db->get_compiled_select() -================================ - -Compiles the selection query just like `$this->db->get()`_ but does not *run* -the query. This method simply returns the SQL query as a string. - -Example:: - - $sql = $this->db->get_compiled_select('mytable'); - echo $sql; - - // Produces string: SELECT * FROM mytable - -The second parameter enables you to set whether or not the active record query -will be reset (by default it will be—just like `$this->db->get()`):: - - echo $this->db->limit(10,20)->get_compiled_select('mytable', FALSE); - // Produces string: SELECT * FROM mytable LIMIT 20, 10 - // (in MySQL. Other databases have slightly different syntax) - - echo $this->db->select('title, content, date')->get_compiled_select(); - - // Produces string: SELECT title, content, date FROM mytable - -The key thing to notice in the above example is that the second query did not -utilize `$this->db->from()`_ and did not pass a table name into the first -parameter. The reason for this outcome is because the query has not been -executed using `$this->db->get()`_ which resets values or reset directly -using `$this->db->reset_query()`_. - - -$this->db->get_where() -====================== - -Identical to the above function except that it permits you to add a -"where" clause in the second parameter, instead of using the db->where() -function:: - - $query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset); - -Please read the about the where function below for more information. - -.. note:: get_where() was formerly known as getwhere(), which has been removed - -$this->db->select() -=================== - -Permits you to write the SELECT portion of your query:: - - $this->db->select('title, content, date'); - $query = $this->db->get('mytable'); // Produces: SELECT title, content, date FROM mytable - - -.. note:: If you are selecting all (\*) from a table you do not need to - use this function. When omitted, CodeIgniter assumes you wish to SELECT * - -$this->db->select() accepts an optional second parameter. If you set it -to FALSE, CodeIgniter will not try to protect your field or table names -with backticks. This is useful if you need a compound select statement. - -:: - - $this->db->select('(SELECT SUM(payments.amount) FROM payments WHERE payments.invoice_id=4') AS amount_paid', FALSE); - $query = $this->db->get('mytable'); - - -$this->db->select_max() -======================= - -Writes a "SELECT MAX(field)" portion for your query. You can optionally -include a second parameter to rename the resulting field. - -:: - - $this->db->select_max('age'); - $query = $this->db->get('members'); // Produces: SELECT MAX(age) as age FROM members - - $this->db->select_max('age', 'member_age'); - $query = $this->db->get('members'); // Produces: SELECT MAX(age) as member_age FROM members - - -$this->db->select_min() -======================= - -Writes a "SELECT MIN(field)" portion for your query. As with -select_max(), You can optionally include a second parameter to rename -the resulting field. - -:: - - $this->db->select_min('age'); - $query = $this->db->get('members'); // Produces: SELECT MIN(age) as age FROM members - - -$this->db->select_avg() -======================= - -Writes a "SELECT AVG(field)" portion for your query. As with -select_max(), You can optionally include a second parameter to rename -the resulting field. - -:: - - $this->db->select_avg('age'); - $query = $this->db->get('members'); // Produces: SELECT AVG(age) as age FROM members - - -$this->db->select_sum() -======================= - -Writes a "SELECT SUM(field)" portion for your query. As with -select_max(), You can optionally include a second parameter to rename -the resulting field. - -:: - - $this->db->select_sum('age'); - $query = $this->db->get('members'); // Produces: SELECT SUM(age) as age FROM members - - -$this->db->from() -================= - -Permits you to write the FROM portion of your query:: - - $this->db->select('title, content, date'); - $this->db->from('mytable'); - $query = $this->db->get(); // Produces: SELECT title, content, date FROM mytable - -.. note:: As shown earlier, the FROM portion of your query can be specified - in the $this->db->get() function, so use whichever method you prefer. - -$this->db->join() -================= - -Permits you to write the JOIN portion of your query:: - - $this->db->select('*'); - $this->db->from('blogs'); - $this->db->join('comments', 'comments.id = blogs.id'); - $query = $this->db->get(); - - // Produces: - // SELECT * FROM blogs JOIN comments ON comments.id = blogs.id - -Multiple function calls can be made if you need several joins in one -query. - -If you need a specific type of JOIN you can specify it via the third -parameter of the function. Options are: left, right, outer, inner, left -outer, and right outer. - -:: - - $this->db->join('comments', 'comments.id = blogs.id', 'left'); - // Produces: LEFT JOIN comments ON comments.id = blogs.id - -$this->db->where() -================== - -This function enables you to set **WHERE** clauses using one of four -methods: - -.. note:: All values passed to this function are escaped automatically, - producing safer queries. - -#. **Simple key/value method:** - - :: - - $this->db->where('name', $name); // Produces: WHERE name = 'Joe' - - Notice that the equal sign is added for you. - - If you use multiple function calls they will be chained together with - AND between them: - - :: - - $this->db->where('name', $name); - $this->db->where('title', $title); - $this->db->where('status', $status); - // WHERE name = 'Joe' AND title = 'boss' AND status = 'active' - -#. **Custom key/value method:** - You can include an operator in the first parameter in order to - control the comparison: - - :: - - $this->db->where('name !=', $name); - $this->db->where('id <', $id); // Produces: WHERE name != 'Joe' AND id < 45 - -#. **Associative array method:** - - :: - - $array = array('name' => $name, 'title' => $title, 'status' => $status); - $this->db->where($array); - // Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active' - - You can include your own operators using this method as well: - - :: - - $array = array('name !=' => $name, 'id <' => $id, 'date >' => $date); - $this->db->where($array); - -#. **Custom string:** - You can write your own clauses manually:: - - $where = "name='Joe' AND status='boss' OR status='active'"; - $this->db->where($where); - - -$this->db->where() accepts an optional third parameter. If you set it to -FALSE, CodeIgniter will not try to protect your field or table names -with backticks. - -:: - - $this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE); - - -$this->db->or_where() -===================== - -This function is identical to the one above, except that multiple -instances are joined by OR:: - - $this->db->where('name !=', $name); - $this->db->or_where('id >', $id); // Produces: WHERE name != 'Joe' OR id > 50 - -.. note:: or_where() was formerly known as orwhere(), which has been - removed. - -$this->db->where_in() -===================== - -Generates a WHERE field IN ('item', 'item') SQL query joined with AND if -appropriate - -:: - - $names = array('Frank', 'Todd', 'James'); - $this->db->where_in('username', $names); - // Produces: WHERE username IN ('Frank', 'Todd', 'James') - - -$this->db->or_where_in() -======================== - -Generates a WHERE field IN ('item', 'item') SQL query joined with OR if -appropriate - -:: - - $names = array('Frank', 'Todd', 'James'); - $this->db->or_where_in('username', $names); - // Produces: OR username IN ('Frank', 'Todd', 'James') - - -$this->db->where_not_in() -========================= - -Generates a WHERE field NOT IN ('item', 'item') SQL query joined with -AND if appropriate - -:: - - $names = array('Frank', 'Todd', 'James'); - $this->db->where_not_in('username', $names); - // Produces: WHERE username NOT IN ('Frank', 'Todd', 'James') - - -$this->db->or_where_not_in() -============================ - -Generates a WHERE field NOT IN ('item', 'item') SQL query joined with OR -if appropriate - -:: - - $names = array('Frank', 'Todd', 'James'); - $this->db->or_where_not_in('username', $names); - // Produces: OR username NOT IN ('Frank', 'Todd', 'James') - - -$this->db->like() -================= - -This function enables you to generate **LIKE** clauses, useful for doing -searches. - -.. note:: All values passed to this function are escaped automatically. - -#. **Simple key/value method:** - - :: - - $this->db->like('title', 'match'); // Produces: WHERE title LIKE '%match%' - - If you use multiple function calls they will be chained together with - AND between them:: - - $this->db->like('title', 'match'); - $this->db->like('body', 'match'); - // WHERE title LIKE '%match%' AND body LIKE '%match% - - If you want to control where the wildcard (%) is placed, you can use - an optional third argument. Your options are 'before', 'after' and - 'both' (which is the default). - - :: - - $this->db->like('title', 'match', 'before'); // Produces: WHERE title LIKE '%match' - $this->db->like('title', 'match', 'after'); // Produces: WHERE title LIKE 'match%' - $this->db->like('title', 'match', 'both'); // Produces: WHERE title LIKE '%match%' - -#. **Associative array method:** - - :: - - $array = array('title' => $match, 'page1' => $match, 'page2' => $match); - $this->db->like($array); - // WHERE title LIKE '%match%' AND page1 LIKE '%match%' AND page2 LIKE '%match%' - - -$this->db->or_like() -==================== - -This function is identical to the one above, except that multiple -instances are joined by OR:: - - $this->db->like('title', 'match'); $this->db->or_like('body', $match); - // WHERE title LIKE '%match%' OR body LIKE '%match%' - -.. note:: or_like() was formerly known as orlike(), which has been removed. - -$this->db->not_like() -===================== - -This function is identical to **like()**, except that it generates NOT -LIKE statements:: - - $this->db->not_like('title', 'match'); // WHERE title NOT LIKE '%match% - -$this->db->or_not_like() -======================== - -This function is identical to **not_like()**, except that multiple -instances are joined by OR:: - - $this->db->like('title', 'match'); - $this->db->or_not_like('body', 'match'); - // WHERE title LIKE '%match% OR body NOT LIKE '%match%' - -$this->db->group_by() -===================== - -Permits you to write the GROUP BY portion of your query:: - - $this->db->group_by("title"); // Produces: GROUP BY title - -You can also pass an array of multiple values as well:: - - $this->db->group_by(array("title", "date")); // Produces: GROUP BY title, date - -.. note:: group_by() was formerly known as groupby(), which has been - removed. - -$this->db->distinct() -===================== - -Adds the "DISTINCT" keyword to a query - -:: - - $this->db->distinct(); - $this->db->get('table'); // Produces: SELECT DISTINCT * FROM table - - -$this->db->having() -=================== - -Permits you to write the HAVING portion of your query. There are 2 -possible syntaxes, 1 argument or 2:: - - $this->db->having('user_id = 45'); // Produces: HAVING user_id = 45 - $this->db->having('user_id', 45); // Produces: HAVING user_id = 45 - -You can also pass an array of multiple values as well:: - - $this->db->having(array('title =' => 'My Title', 'id <' => $id)); - // Produces: HAVING title = 'My Title', id < 45 - - -If you are using a database that CodeIgniter escapes queries for, you -can prevent escaping content by passing an optional third argument, and -setting it to FALSE. - -:: - - $this->db->having('user_id', 45); // Produces: HAVING `user_id` = 45 in some databases such as MySQL - $this->db->having('user_id', 45, FALSE); // Produces: HAVING user_id = 45 - - -$this->db->or_having() -====================== - -Identical to having(), only separates multiple clauses with "OR". - -$this->db->order_by() -===================== - -Lets you set an ORDER BY clause. The first parameter contains the name -of the column you would like to order by. The second parameter lets you -set the direction of the result. Options are asc or desc, or random. - -:: - - $this->db->order_by("title", "desc"); // Produces: ORDER BY title DESC - -You can also pass your own string in the first parameter:: - - $this->db->order_by('title desc, name asc'); // Produces: ORDER BY title DESC, name ASC - -Or multiple function calls can be made if you need multiple fields. - -:: - - $this->db->order_by("title", "desc"); - $this->db->order_by("name", "asc"); // Produces: ORDER BY title DESC, name ASC - - -.. note:: order_by() was formerly known as orderby(), which has been - removed. - -.. note:: random ordering is not currently supported in Oracle or MSSQL - drivers. These will default to 'ASC'. - -$this->db->limit() -================== - -Lets you limit the number of rows you would like returned by the query:: - - $this->db->limit(10); // Produces: LIMIT 10 - -The second parameter lets you set a result offset. - -:: - - $this->db->limit(10, 20); // Produces: LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax) - -$this->db->count_all_results() -============================== - -Permits you to determine the number of rows in a particular Active -Record query. Queries will accept Active Record restrictors such as -where(), or_where(), like(), or_like(), etc. Example:: - - echo $this->db->count_all_results('my_table'); // Produces an integer, like 25 - $this->db->like('title', 'match'); - $this->db->from('my_table'); - echo $this->db->count_all_results(); // Produces an integer, like 17 - -$this->db->count_all() -====================== - -Permits you to determine the number of rows in a particular table. -Submit the table name in the first parameter. Example:: - - echo $this->db->count_all('my_table'); // Produces an integer, like 25 - -************** -Query grouping -************** - -Query grouping allows you to create groups of WHERE clauses by enclosing them in parentheses. This will allow -you to create queries with complex WHERE clauses. Nested groups are supported. Example: - - $this->db->select('*')->from('my_table') - ->group_start() - ->where('a', 'a') - ->or_group_start() - ->where('b', 'b') - ->where('c', 'c') - ->group_end() - ->group_end() - ->where('d', 'd') - ->get(); - - // Generates: - // SELECT * FROM (`my_table`) WHERE ( `a` = 'a' OR ( `b` = 'b' AND `c` = 'c' ) ) AND `d` = 'd' - -.. note:: groups need to be balanced, make sure every group_start() is matched by a group_end(). - -$this->db->group_start() -======================== - -Starts a new group by adding an opening parenthesis to the WHERE clause of the query. - -$this->db->or_group_start() -=========================== - -Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with 'OR'. - -$this->db->not_group_start() -============================ - -Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with 'NOT'. - -$this->db->or_not_group_start() -=============================== - -Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with 'OR NOT'. - -$this->db->group_end() -====================== - -Ends the current group by adding an closing parenthesis to the WHERE clause of the query. - -************** -Inserting Data -************** - -$this->db->insert() -=================== - -Generates an insert string based on the data you supply, and runs the -query. You can either pass an **array** or an **object** to the -function. Here is an example using an array:: - - $data = array( - 'title' => 'My title', - 'name' => 'My Name', - 'date' => 'My date' - ); - - $this->db->insert('mytable', $data); - // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date') - -The first parameter will contain the table name, the second is an -associative array of values. - -Here is an example using an object:: - - /* - class Myclass { - var $title = 'My Title'; - var $content = 'My Content'; - var $date = 'My Date'; - } - */ - - $object = new Myclass; - $this->db->insert('mytable', $object); - // Produces: INSERT INTO mytable (title, content, date) VALUES ('My Title', 'My Content', 'My Date') - -The first parameter will contain the table name, the second is an -object. - -.. note:: All values are escaped automatically producing safer queries. - -$this->db->get_compiled_insert() -================================ -Compiles the insertion query just like `$this->db->insert()`_ but does not -*run* the query. This method simply returns the SQL query as a string. - -Example:: - - $data = array( - 'title' => 'My title', - 'name' => 'My Name', - 'date' => 'My date' - ); - - $sql = $this->db->set($data)->get_compiled_insert('mytable'); - echo $sql; - - // Produces string: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date') - -The second parameter enables you to set whether or not the active record query -will be reset (by default it will be--just like `$this->db->insert()`_):: - - echo $this->db->set('title', 'My Title')->get_compiled_insert('mytable', FALSE); - - // Produces string: INSERT INTO mytable (title) VALUES ('My Title') - - echo $this->db->set('content', 'My Content')->get_compiled_insert(); - - // Produces string: INSERT INTO mytable (title, content) VALUES ('My Title', 'My Content') - -The key thing to notice in the above example is that the second query did not -utlize `$this->db->from()`_ nor did it pass a table name into the first -parameter. The reason this worked is because the query has not been executed -using `$this->db->insert()`_ which resets values or reset directly using -`$this->db->reset_query()`_. - -$this->db->insert_batch() -========================= - -Generates an insert string based on the data you supply, and runs the -query. You can either pass an **array** or an **object** to the -function. Here is an example using an array:: - - $data = array( - array( - 'title' => 'My title', - 'name' => 'My Name', - 'date' => 'My date' - ), - array( - 'title' => 'Another title', - 'name' => 'Another Name', - 'date' => 'Another date' - ) - ); - - $this->db->insert_batch('mytable', $data); - // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date') - -The first parameter will contain the table name, the second is an -associative array of values. - -.. note:: All values are escaped automatically producing safer queries. - -$this->db->set() -================ - -This function enables you to set values for inserts or updates. - -**It can be used instead of passing a data array directly to the insert -or update functions:** - -:: - - $this->db->set('name', $name); - $this->db->insert('mytable'); // Produces: INSERT INTO mytable (name) VALUES ('{$name}') - -If you use multiple function called they will be assembled properly -based on whether you are doing an insert or an update:: - - $this->db->set('name', $name); - $this->db->set('title', $title); - $this->db->set('status', $status); - $this->db->insert('mytable'); - -**set()** will also accept an optional third parameter ($escape), that -will prevent data from being escaped if set to FALSE. To illustrate the -difference, here is set() used both with and without the escape -parameter. - -:: - - $this->db->set('field', 'field+1', FALSE); - $this->db->insert('mytable'); // gives INSERT INTO mytable (field) VALUES (field+1) - $this->db->set('field', 'field+1'); - $this->db->insert('mytable'); // gives INSERT INTO mytable (field) VALUES ('field+1') - - -You can also pass an associative array to this function:: - - $array = array( - 'name' => $name, - 'title' => $title, - 'status' => $status - ); - - $this->db->set($array); - $this->db->insert('mytable'); - -Or an object:: - - /* - class Myclass { - var $title = 'My Title'; - var $content = 'My Content'; - var $date = 'My Date'; - } - */ - - $object = new Myclass; - $this->db->set($object); - $this->db->insert('mytable'); - - -************* -Updating Data -************* - -$this->db->update() -=================== - -Generates an update string and runs the query based on the data you -supply. You can pass an **array** or an **object** to the function. Here -is an example using an array:: - - $data = array( - 'title' => $title, - 'name' => $name, - 'date' => $date - ); - - $this->db->where('id', $id); - $this->db->update('mytable', $data); - // Produces: // UPDATE mytable // SET title = '{$title}', name = '{$name}', date = '{$date}' // WHERE id = $id - -Or you can supply an object:: - - /* - class Myclass { - var $title = 'My Title'; - var $content = 'My Content'; - var $date = 'My Date'; - } - */ - - $object = new Myclass; - $this->db->where('id', $id); - $this->db->update('mytable', $object); - // Produces: // UPDATE mytable // SET title = '{$title}', name = '{$name}', date = '{$date}' // WHERE id = $id - -.. note:: All values are escaped automatically producing safer queries. - -You'll notice the use of the $this->db->where() function, enabling you -to set the WHERE clause. You can optionally pass this information -directly into the update function as a string:: - - $this->db->update('mytable', $data, "id = 4"); - -Or as an array:: - - $this->db->update('mytable', $data, array('id' => $id)); - -You may also use the $this->db->set() function described above when -performing updates. - -$this->db->update_batch() -========================= - -Generates an update string based on the data you supply, and runs the query. -You can either pass an **array** or an **object** to the function. -Here is an example using an array:: - - $data = array( - array( - 'title' => 'My title' , - 'name' => 'My Name 2' , - 'date' => 'My date 2' - ), - array( - 'title' => 'Another title' , - 'name' => 'Another Name 2' , - 'date' => 'Another date 2' - ) - ); - - $this->db->update_batch('mytable', $data, 'title'); - - // Produces: - // UPDATE `mytable` SET `name` = CASE - // WHEN `title` = 'My title' THEN 'My Name 2' - // WHEN `title` = 'Another title' THEN 'Another Name 2' - // ELSE `name` END, - // `date` = CASE - // WHEN `title` = 'My title' THEN 'My date 2' - // WHEN `title` = 'Another title' THEN 'Another date 2' - // ELSE `date` END - // WHERE `title` IN ('My title','Another title') - -The first parameter will contain the table name, the second is an associative -array of values, the third parameter is the where key. - -.. note:: All values are escaped automatically producing safer queries. - -$this->db->get_compiled_update() -================================ - -This works exactly the same way as ``$this->db->get_compiled_insert()`` except -that it produces an UPDATE SQL string instead of an INSERT SQL string. - -For more information view documentation for `$this->db->get_compiled_insert()`_. - - -************* -Deleting Data -************* - -$this->db->delete() -=================== - -Generates a delete SQL string and runs the query. - -:: - - $this->db->delete('mytable', array('id' => $id)); // Produces: // DELETE FROM mytable // WHERE id = $id - -The first parameter is the table name, the second is the where clause. -You can also use the where() or or_where() functions instead of passing -the data to the second parameter of the function:: - - $this->db->where('id', $id); - $this->db->delete('mytable'); - - // Produces: - // DELETE FROM mytable - // WHERE id = $id - - -An array of table names can be passed into delete() if you would like to -delete data from more than 1 table. - -:: - - $tables = array('table1', 'table2', 'table3'); - $this->db->where('id', '5'); - $this->db->delete($tables); - - -If you want to delete all data from a table, you can use the truncate() -function, or empty_table(). - -$this->db->empty_table() -======================== - -Generates a delete SQL string and runs the -query.:: - - $this->db->empty_table('mytable'); // Produces: DELETE FROM mytable - - -$this->db->truncate() -===================== - -Generates a truncate SQL string and runs the query. - -:: - - $this->db->from('mytable'); - $this->db->truncate(); - - // or - - $this->db->truncate('mytable'); - - // Produce: - // TRUNCATE mytable - -.. note:: If the TRUNCATE command isn't available, truncate() will - execute as "DELETE FROM table". - -$this->db->get_compiled_delete() -================================ -This works exactly the same way as ``$this->db->get_compiled_insert()`` except -that it produces a DELETE SQL string instead of an INSERT SQL string. - -For more information view documentation for `$this->db->get_compiled_insert()`_. - -*************** -Method Chaining -*************** - -Method chaining allows you to simplify your syntax by connecting -multiple functions. Consider this example:: - - $query = $this->db->select('title') - ->where('id', $id) - ->limit(10, 20) - ->get('mytable'); - -.. _ar-caching: - -********************* -Active Record Caching -********************* - -While not "true" caching, Active Record enables you to save (or "cache") -certain parts of your queries for reuse at a later point in your -script's execution. Normally, when an Active Record call is completed, -all stored information is reset for the next call. With caching, you can -prevent this reset, and reuse information easily. - -Cached calls are cumulative. If you make 2 cached select() calls, and -then 2 uncached select() calls, this will result in 4 select() calls. -There are three Caching functions available: - -$this->db->start_cache() -======================== - -This function must be called to begin caching. All Active Record queries -of the correct type (see below for supported queries) are stored for -later use. - -$this->db->stop_cache() -======================= - -This function can be called to stop caching. - -$this->db->flush_cache() -======================== - -This function deletes all items from the Active Record cache. - -Here's a usage example:: - - $this->db->start_cache(); - $this->db->select('field1'); - $this->db->stop_cache(); - $this->db->get('tablename'); - //Generates: SELECT `field1` FROM (`tablename`) - - $this->db->select('field2'); - $this->db->get('tablename'); - //Generates: SELECT `field1`, `field2` FROM (`tablename`) - - $this->db->flush_cache(); - $this->db->select('field2'); - $this->db->get('tablename'); - //Generates: SELECT `field2` FROM (`tablename`) - - -.. note:: The following statements can be cached: select, from, join, - where, like, group_by, having, order_by, set - - -$this->db->reset_query() -======================== - -Resetting Active Record allows you to start fresh with your query without -executing it first using a method like $this->db->get() or $this->db->insert(). -Just like the methods that execute a query, this will *not* reset items you've -cached using `Active Record Caching`_. - -This is useful in situations where you are using Active Record to generate SQL -(ex. ``$this->db->get_compiled_select()``) but then choose to, for instance, -run the query:: - - // Note that the second parameter of the get_compiled_select method is FALSE - $sql = $this->db->select(array('field1','field2')) - ->where('field3',5) - ->get_compiled_select('mytable', FALSE); - - // ... - // Do something crazy with the SQL code... like add it to a cron script for - // later execution or something... - // ... - - $data = $this->db->get()->result_array(); - - // Would execute and return an array of results of the following query: - // SELECT field1, field1 from mytable where field3 = 5; diff --git a/user_guide_src/source/database/configuration.rst b/user_guide_src/source/database/configuration.rst index 040e7e33f..953bbfbcb 100644 --- a/user_guide_src/source/database/configuration.rst +++ b/user_guide_src/source/database/configuration.rst @@ -106,18 +106,18 @@ Note: The name "test" is arbitrary. It can be anything you want. By default we've used the word "default" for the primary connection, but it too can be renamed to something more relevant to your project. -Active Record +Query Builder ------------- -The :doc:`Active Record Class ` is globally enabled or -disabled by setting the $active_record variable in the database +The :doc:`Query Builder Class ` is globally enabled or +disabled by setting the $query_builder variable in the database configuration file to TRUE/FALSE (boolean). If you are not using the -active record class, setting it to FALSE will utilize fewer resources +query builder class, setting it to FALSE will utilize fewer resources when the database classes are initialized. :: - $active_record = TRUE; + $query_builder = TRUE; .. note:: that some CodeIgniter classes such as Sessions require Active Records be enabled to access certain functionality. @@ -134,7 +134,7 @@ Explanation of Values: **database** The name of the database you want to connect to. **dbdriver** The database type. ie: mysql, postgres, odbc, etc. Must be specified in lower case. **dbprefix** An optional table prefix which will added to the table name when running :doc: - `Active Record ` queries. This permits multiple CodeIgniter installations + `Query Builder ` queries. This permits multiple CodeIgniter installations to share one database. **pconnect** TRUE/FALSE (boolean) - Whether to use a persistent connection. **db_debug** TRUE/FALSE (boolean) - Whether database errors should be displayed. diff --git a/user_guide_src/source/database/connecting.rst b/user_guide_src/source/database/connecting.rst index a834cc0f7..4db038e1e 100644 --- a/user_guide_src/source/database/connecting.rst +++ b/user_guide_src/source/database/connecting.rst @@ -36,7 +36,7 @@ Available Parameters string. #. TRUE/FALSE (boolean). Whether to return the connection ID (see Connecting to Multiple Databases below). -#. TRUE/FALSE (boolean). Whether to enable the Active Record class. Set +#. TRUE/FALSE (boolean). Whether to enable the Query Builder class. Set to TRUE by default. Manually Connecting to a Database diff --git a/user_guide_src/source/database/examples.rst b/user_guide_src/source/database/examples.rst index d1cd48837..8b3cc4701 100644 --- a/user_guide_src/source/database/examples.rst +++ b/user_guide_src/source/database/examples.rst @@ -104,10 +104,10 @@ Standard Insert $this->db->query($sql); echo $this->db->affected_rows(); -Active Record Query +Query Builder Query =================== -The :doc:`Active Record Pattern ` gives you a simplified +The :doc:`Query Builder Pattern ` gives you a simplified means of retrieving data:: $query = $this->db->get('table_name'); @@ -118,10 +118,10 @@ means of retrieving data:: } The above get() function retrieves all the results from the supplied -table. The :doc:`Active Record ` class contains a full +table. The :doc:`Query Builder ` class contains a full compliment of functions for working with data. -Active Record Insert +Query Builder Insert ==================== :: diff --git a/user_guide_src/source/database/index.rst b/user_guide_src/source/database/index.rst index ab12b7cb7..7ccb8fb00 100644 --- a/user_guide_src/source/database/index.rst +++ b/user_guide_src/source/database/index.rst @@ -3,7 +3,7 @@ The Database Class ################## CodeIgniter comes with a full-featured and very fast abstracted database -class that supports both traditional structures and Active Record +class that supports both traditional structures and Query Builder patterns. The database functions offer clear, simple syntax. .. toctree:: @@ -15,7 +15,7 @@ patterns. The database functions offer clear, simple syntax. Running Queries Generating Query Results Query Helper Functions - Active Record Class + Query Builder Class Transactions Table MetaData Field MetaData diff --git a/user_guide_src/source/database/queries.rst b/user_guide_src/source/database/queries.rst index 971d5d61d..2ba3d3f4a 100644 --- a/user_guide_src/source/database/queries.rst +++ b/user_guide_src/source/database/queries.rst @@ -50,7 +50,7 @@ Protecting identifiers ********************** In many databases it is advisable to protect table and field names - for -example with backticks in MySQL. **Active Record queries are +example with backticks in MySQL. **Query Builder queries are automatically protected**, however if you need to manually protect an identifier you can use:: diff --git a/user_guide_src/source/database/query_builder.rst b/user_guide_src/source/database/query_builder.rst new file mode 100644 index 000000000..f55d8e2f7 --- /dev/null +++ b/user_guide_src/source/database/query_builder.rst @@ -0,0 +1,1008 @@ +################### +Query Builder Class +################### + +CodeIgniter gives you access to a Query Builder class. This pattern +allows information to be retrieved, inserted, and updated in your +database with minimal scripting. In some cases only one or two lines +of code are necessary to perform a database action. +CodeIgniter does not require that each database table be its own class +file. It instead provides a more simplified interface. + +Beyond simplicity, a major benefit to using the Query Builder features +is that it allows you to create database independent applications, since +the query syntax is generated by each database adapter. It also allows +for safer queries, since the values are escaped automatically by the +system. + +.. note:: If you intend to write your own queries you can disable this + class in your database config file, allowing the core database library + and adapter to utilize fewer resources. + +.. contents:: Page Contents + +************** +Selecting Data +************** + +The following functions allow you to build SQL **SELECT** statements. + +$this->db->get() +================ + +Runs the selection query and returns the result. Can be used by itself +to retrieve all records from a table:: + + $query = $this->db->get('mytable'); // Produces: SELECT * FROM mytable + +The second and third parameters enable you to set a limit and offset +clause:: + + $query = $this->db->get('mytable', 10, 20); + // Produces: SELECT * FROM mytable LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax) + +You'll notice that the above function is assigned to a variable named +$query, which can be used to show the results:: + + $query = $this->db->get('mytable'); + + foreach ($query->result() as $row) + { + echo $row->title; + } + +Please visit the :doc:`result functions ` page for a full +discussion regarding result generation. + +$this->db->get_compiled_select() +================================ + +Compiles the selection query just like `$this->db->get()`_ but does not *run* +the query. This method simply returns the SQL query as a string. + +Example:: + + $sql = $this->db->get_compiled_select('mytable'); + echo $sql; + + // Produces string: SELECT * FROM mytable + +The second parameter enables you to set whether or not the query builder query +will be reset (by default it will be—just like `$this->db->get()`):: + + echo $this->db->limit(10,20)->get_compiled_select('mytable', FALSE); + // Produces string: SELECT * FROM mytable LIMIT 20, 10 + // (in MySQL. Other databases have slightly different syntax) + + echo $this->db->select('title, content, date')->get_compiled_select(); + + // Produces string: SELECT title, content, date FROM mytable + +The key thing to notice in the above example is that the second query did not +utilize `$this->db->from()`_ and did not pass a table name into the first +parameter. The reason for this outcome is because the query has not been +executed using `$this->db->get()`_ which resets values or reset directly +using `$this->db->reset_query()`_. + + +$this->db->get_where() +====================== + +Identical to the above function except that it permits you to add a +"where" clause in the second parameter, instead of using the db->where() +function:: + + $query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset); + +Please read the about the where function below for more information. + +.. note:: get_where() was formerly known as getwhere(), which has been removed + +$this->db->select() +=================== + +Permits you to write the SELECT portion of your query:: + + $this->db->select('title, content, date'); + $query = $this->db->get('mytable'); // Produces: SELECT title, content, date FROM mytable + + +.. note:: If you are selecting all (\*) from a table you do not need to + use this function. When omitted, CodeIgniter assumes you wish to SELECT * + +$this->db->select() accepts an optional second parameter. If you set it +to FALSE, CodeIgniter will not try to protect your field or table names +with backticks. This is useful if you need a compound select statement. + +:: + + $this->db->select('(SELECT SUM(payments.amount) FROM payments WHERE payments.invoice_id=4') AS amount_paid', FALSE); + $query = $this->db->get('mytable'); + + +$this->db->select_max() +======================= + +Writes a "SELECT MAX(field)" portion for your query. You can optionally +include a second parameter to rename the resulting field. + +:: + + $this->db->select_max('age'); + $query = $this->db->get('members'); // Produces: SELECT MAX(age) as age FROM members + + $this->db->select_max('age', 'member_age'); + $query = $this->db->get('members'); // Produces: SELECT MAX(age) as member_age FROM members + + +$this->db->select_min() +======================= + +Writes a "SELECT MIN(field)" portion for your query. As with +select_max(), You can optionally include a second parameter to rename +the resulting field. + +:: + + $this->db->select_min('age'); + $query = $this->db->get('members'); // Produces: SELECT MIN(age) as age FROM members + + +$this->db->select_avg() +======================= + +Writes a "SELECT AVG(field)" portion for your query. As with +select_max(), You can optionally include a second parameter to rename +the resulting field. + +:: + + $this->db->select_avg('age'); + $query = $this->db->get('members'); // Produces: SELECT AVG(age) as age FROM members + + +$this->db->select_sum() +======================= + +Writes a "SELECT SUM(field)" portion for your query. As with +select_max(), You can optionally include a second parameter to rename +the resulting field. + +:: + + $this->db->select_sum('age'); + $query = $this->db->get('members'); // Produces: SELECT SUM(age) as age FROM members + + +$this->db->from() +================= + +Permits you to write the FROM portion of your query:: + + $this->db->select('title, content, date'); + $this->db->from('mytable'); + $query = $this->db->get(); // Produces: SELECT title, content, date FROM mytable + +.. note:: As shown earlier, the FROM portion of your query can be specified + in the $this->db->get() function, so use whichever method you prefer. + +$this->db->join() +================= + +Permits you to write the JOIN portion of your query:: + + $this->db->select('*'); + $this->db->from('blogs'); + $this->db->join('comments', 'comments.id = blogs.id'); + $query = $this->db->get(); + + // Produces: + // SELECT * FROM blogs JOIN comments ON comments.id = blogs.id + +Multiple function calls can be made if you need several joins in one +query. + +If you need a specific type of JOIN you can specify it via the third +parameter of the function. Options are: left, right, outer, inner, left +outer, and right outer. + +:: + + $this->db->join('comments', 'comments.id = blogs.id', 'left'); + // Produces: LEFT JOIN comments ON comments.id = blogs.id + +$this->db->where() +================== + +This function enables you to set **WHERE** clauses using one of four +methods: + +.. note:: All values passed to this function are escaped automatically, + producing safer queries. + +#. **Simple key/value method:** + + :: + + $this->db->where('name', $name); // Produces: WHERE name = 'Joe' + + Notice that the equal sign is added for you. + + If you use multiple function calls they will be chained together with + AND between them: + + :: + + $this->db->where('name', $name); + $this->db->where('title', $title); + $this->db->where('status', $status); + // WHERE name = 'Joe' AND title = 'boss' AND status = 'active' + +#. **Custom key/value method:** + You can include an operator in the first parameter in order to + control the comparison: + + :: + + $this->db->where('name !=', $name); + $this->db->where('id <', $id); // Produces: WHERE name != 'Joe' AND id < 45 + +#. **Associative array method:** + + :: + + $array = array('name' => $name, 'title' => $title, 'status' => $status); + $this->db->where($array); + // Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active' + + You can include your own operators using this method as well: + + :: + + $array = array('name !=' => $name, 'id <' => $id, 'date >' => $date); + $this->db->where($array); + +#. **Custom string:** + You can write your own clauses manually:: + + $where = "name='Joe' AND status='boss' OR status='active'"; + $this->db->where($where); + + +$this->db->where() accepts an optional third parameter. If you set it to +FALSE, CodeIgniter will not try to protect your field or table names +with backticks. + +:: + + $this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE); + + +$this->db->or_where() +===================== + +This function is identical to the one above, except that multiple +instances are joined by OR:: + + $this->db->where('name !=', $name); + $this->db->or_where('id >', $id); // Produces: WHERE name != 'Joe' OR id > 50 + +.. note:: or_where() was formerly known as orwhere(), which has been + removed. + +$this->db->where_in() +===================== + +Generates a WHERE field IN ('item', 'item') SQL query joined with AND if +appropriate + +:: + + $names = array('Frank', 'Todd', 'James'); + $this->db->where_in('username', $names); + // Produces: WHERE username IN ('Frank', 'Todd', 'James') + + +$this->db->or_where_in() +======================== + +Generates a WHERE field IN ('item', 'item') SQL query joined with OR if +appropriate + +:: + + $names = array('Frank', 'Todd', 'James'); + $this->db->or_where_in('username', $names); + // Produces: OR username IN ('Frank', 'Todd', 'James') + + +$this->db->where_not_in() +========================= + +Generates a WHERE field NOT IN ('item', 'item') SQL query joined with +AND if appropriate + +:: + + $names = array('Frank', 'Todd', 'James'); + $this->db->where_not_in('username', $names); + // Produces: WHERE username NOT IN ('Frank', 'Todd', 'James') + + +$this->db->or_where_not_in() +============================ + +Generates a WHERE field NOT IN ('item', 'item') SQL query joined with OR +if appropriate + +:: + + $names = array('Frank', 'Todd', 'James'); + $this->db->or_where_not_in('username', $names); + // Produces: OR username NOT IN ('Frank', 'Todd', 'James') + + +$this->db->like() +================= + +This function enables you to generate **LIKE** clauses, useful for doing +searches. + +.. note:: All values passed to this function are escaped automatically. + +#. **Simple key/value method:** + + :: + + $this->db->like('title', 'match'); // Produces: WHERE title LIKE '%match%' + + If you use multiple function calls they will be chained together with + AND between them:: + + $this->db->like('title', 'match'); + $this->db->like('body', 'match'); + // WHERE title LIKE '%match%' AND body LIKE '%match% + + If you want to control where the wildcard (%) is placed, you can use + an optional third argument. Your options are 'before', 'after' and + 'both' (which is the default). + + :: + + $this->db->like('title', 'match', 'before'); // Produces: WHERE title LIKE '%match' + $this->db->like('title', 'match', 'after'); // Produces: WHERE title LIKE 'match%' + $this->db->like('title', 'match', 'both'); // Produces: WHERE title LIKE '%match%' + +#. **Associative array method:** + + :: + + $array = array('title' => $match, 'page1' => $match, 'page2' => $match); + $this->db->like($array); + // WHERE title LIKE '%match%' AND page1 LIKE '%match%' AND page2 LIKE '%match%' + + +$this->db->or_like() +==================== + +This function is identical to the one above, except that multiple +instances are joined by OR:: + + $this->db->like('title', 'match'); $this->db->or_like('body', $match); + // WHERE title LIKE '%match%' OR body LIKE '%match%' + +.. note:: or_like() was formerly known as orlike(), which has been removed. + +$this->db->not_like() +===================== + +This function is identical to **like()**, except that it generates NOT +LIKE statements:: + + $this->db->not_like('title', 'match'); // WHERE title NOT LIKE '%match% + +$this->db->or_not_like() +======================== + +This function is identical to **not_like()**, except that multiple +instances are joined by OR:: + + $this->db->like('title', 'match'); + $this->db->or_not_like('body', 'match'); + // WHERE title LIKE '%match% OR body NOT LIKE '%match%' + +$this->db->group_by() +===================== + +Permits you to write the GROUP BY portion of your query:: + + $this->db->group_by("title"); // Produces: GROUP BY title + +You can also pass an array of multiple values as well:: + + $this->db->group_by(array("title", "date")); // Produces: GROUP BY title, date + +.. note:: group_by() was formerly known as groupby(), which has been + removed. + +$this->db->distinct() +===================== + +Adds the "DISTINCT" keyword to a query + +:: + + $this->db->distinct(); + $this->db->get('table'); // Produces: SELECT DISTINCT * FROM table + + +$this->db->having() +=================== + +Permits you to write the HAVING portion of your query. There are 2 +possible syntaxes, 1 argument or 2:: + + $this->db->having('user_id = 45'); // Produces: HAVING user_id = 45 + $this->db->having('user_id', 45); // Produces: HAVING user_id = 45 + +You can also pass an array of multiple values as well:: + + $this->db->having(array('title =' => 'My Title', 'id <' => $id)); + // Produces: HAVING title = 'My Title', id < 45 + + +If you are using a database that CodeIgniter escapes queries for, you +can prevent escaping content by passing an optional third argument, and +setting it to FALSE. + +:: + + $this->db->having('user_id', 45); // Produces: HAVING `user_id` = 45 in some databases such as MySQL + $this->db->having('user_id', 45, FALSE); // Produces: HAVING user_id = 45 + + +$this->db->or_having() +====================== + +Identical to having(), only separates multiple clauses with "OR". + +$this->db->order_by() +===================== + +Lets you set an ORDER BY clause. The first parameter contains the name +of the column you would like to order by. The second parameter lets you +set the direction of the result. Options are asc or desc, or random. + +:: + + $this->db->order_by("title", "desc"); // Produces: ORDER BY title DESC + +You can also pass your own string in the first parameter:: + + $this->db->order_by('title desc, name asc'); // Produces: ORDER BY title DESC, name ASC + +Or multiple function calls can be made if you need multiple fields. + +:: + + $this->db->order_by("title", "desc"); + $this->db->order_by("name", "asc"); // Produces: ORDER BY title DESC, name ASC + + +.. note:: order_by() was formerly known as orderby(), which has been + removed. + +.. note:: random ordering is not currently supported in Oracle or MSSQL + drivers. These will default to 'ASC'. + +$this->db->limit() +================== + +Lets you limit the number of rows you would like returned by the query:: + + $this->db->limit(10); // Produces: LIMIT 10 + +The second parameter lets you set a result offset. + +:: + + $this->db->limit(10, 20); // Produces: LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax) + +$this->db->count_all_results() +============================== + +Permits you to determine the number of rows in a particular Active +Record query. Queries will accept Query Builder restrictors such as +where(), or_where(), like(), or_like(), etc. Example:: + + echo $this->db->count_all_results('my_table'); // Produces an integer, like 25 + $this->db->like('title', 'match'); + $this->db->from('my_table'); + echo $this->db->count_all_results(); // Produces an integer, like 17 + +$this->db->count_all() +====================== + +Permits you to determine the number of rows in a particular table. +Submit the table name in the first parameter. Example:: + + echo $this->db->count_all('my_table'); // Produces an integer, like 25 + +************** +Query grouping +************** + +Query grouping allows you to create groups of WHERE clauses by enclosing them in parentheses. This will allow +you to create queries with complex WHERE clauses. Nested groups are supported. Example: + + $this->db->select('*')->from('my_table') + ->group_start() + ->where('a', 'a') + ->or_group_start() + ->where('b', 'b') + ->where('c', 'c') + ->group_end() + ->group_end() + ->where('d', 'd') + ->get(); + + // Generates: + // SELECT * FROM (`my_table`) WHERE ( `a` = 'a' OR ( `b` = 'b' AND `c` = 'c' ) ) AND `d` = 'd' + +.. note:: groups need to be balanced, make sure every group_start() is matched by a group_end(). + +$this->db->group_start() +======================== + +Starts a new group by adding an opening parenthesis to the WHERE clause of the query. + +$this->db->or_group_start() +=========================== + +Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with 'OR'. + +$this->db->not_group_start() +============================ + +Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with 'NOT'. + +$this->db->or_not_group_start() +=============================== + +Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with 'OR NOT'. + +$this->db->group_end() +====================== + +Ends the current group by adding an closing parenthesis to the WHERE clause of the query. + +************** +Inserting Data +************** + +$this->db->insert() +=================== + +Generates an insert string based on the data you supply, and runs the +query. You can either pass an **array** or an **object** to the +function. Here is an example using an array:: + + $data = array( + 'title' => 'My title', + 'name' => 'My Name', + 'date' => 'My date' + ); + + $this->db->insert('mytable', $data); + // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date') + +The first parameter will contain the table name, the second is an +associative array of values. + +Here is an example using an object:: + + /* + class Myclass { + var $title = 'My Title'; + var $content = 'My Content'; + var $date = 'My Date'; + } + */ + + $object = new Myclass; + $this->db->insert('mytable', $object); + // Produces: INSERT INTO mytable (title, content, date) VALUES ('My Title', 'My Content', 'My Date') + +The first parameter will contain the table name, the second is an +object. + +.. note:: All values are escaped automatically producing safer queries. + +$this->db->get_compiled_insert() +================================ +Compiles the insertion query just like `$this->db->insert()`_ but does not +*run* the query. This method simply returns the SQL query as a string. + +Example:: + + $data = array( + 'title' => 'My title', + 'name' => 'My Name', + 'date' => 'My date' + ); + + $sql = $this->db->set($data)->get_compiled_insert('mytable'); + echo $sql; + + // Produces string: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date') + +The second parameter enables you to set whether or not the query builder query +will be reset (by default it will be--just like `$this->db->insert()`_):: + + echo $this->db->set('title', 'My Title')->get_compiled_insert('mytable', FALSE); + + // Produces string: INSERT INTO mytable (title) VALUES ('My Title') + + echo $this->db->set('content', 'My Content')->get_compiled_insert(); + + // Produces string: INSERT INTO mytable (title, content) VALUES ('My Title', 'My Content') + +The key thing to notice in the above example is that the second query did not +utlize `$this->db->from()`_ nor did it pass a table name into the first +parameter. The reason this worked is because the query has not been executed +using `$this->db->insert()`_ which resets values or reset directly using +`$this->db->reset_query()`_. + +$this->db->insert_batch() +========================= + +Generates an insert string based on the data you supply, and runs the +query. You can either pass an **array** or an **object** to the +function. Here is an example using an array:: + + $data = array( + array( + 'title' => 'My title', + 'name' => 'My Name', + 'date' => 'My date' + ), + array( + 'title' => 'Another title', + 'name' => 'Another Name', + 'date' => 'Another date' + ) + ); + + $this->db->insert_batch('mytable', $data); + // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date') + +The first parameter will contain the table name, the second is an +associative array of values. + +.. note:: All values are escaped automatically producing safer queries. + +$this->db->set() +================ + +This function enables you to set values for inserts or updates. + +**It can be used instead of passing a data array directly to the insert +or update functions:** + +:: + + $this->db->set('name', $name); + $this->db->insert('mytable'); // Produces: INSERT INTO mytable (name) VALUES ('{$name}') + +If you use multiple function called they will be assembled properly +based on whether you are doing an insert or an update:: + + $this->db->set('name', $name); + $this->db->set('title', $title); + $this->db->set('status', $status); + $this->db->insert('mytable'); + +**set()** will also accept an optional third parameter ($escape), that +will prevent data from being escaped if set to FALSE. To illustrate the +difference, here is set() used both with and without the escape +parameter. + +:: + + $this->db->set('field', 'field+1', FALSE); + $this->db->insert('mytable'); // gives INSERT INTO mytable (field) VALUES (field+1) + $this->db->set('field', 'field+1'); + $this->db->insert('mytable'); // gives INSERT INTO mytable (field) VALUES ('field+1') + + +You can also pass an associative array to this function:: + + $array = array( + 'name' => $name, + 'title' => $title, + 'status' => $status + ); + + $this->db->set($array); + $this->db->insert('mytable'); + +Or an object:: + + /* + class Myclass { + var $title = 'My Title'; + var $content = 'My Content'; + var $date = 'My Date'; + } + */ + + $object = new Myclass; + $this->db->set($object); + $this->db->insert('mytable'); + + +************* +Updating Data +************* + +$this->db->update() +=================== + +Generates an update string and runs the query based on the data you +supply. You can pass an **array** or an **object** to the function. Here +is an example using an array:: + + $data = array( + 'title' => $title, + 'name' => $name, + 'date' => $date + ); + + $this->db->where('id', $id); + $this->db->update('mytable', $data); + // Produces: // UPDATE mytable // SET title = '{$title}', name = '{$name}', date = '{$date}' // WHERE id = $id + +Or you can supply an object:: + + /* + class Myclass { + var $title = 'My Title'; + var $content = 'My Content'; + var $date = 'My Date'; + } + */ + + $object = new Myclass; + $this->db->where('id', $id); + $this->db->update('mytable', $object); + // Produces: // UPDATE mytable // SET title = '{$title}', name = '{$name}', date = '{$date}' // WHERE id = $id + +.. note:: All values are escaped automatically producing safer queries. + +You'll notice the use of the $this->db->where() function, enabling you +to set the WHERE clause. You can optionally pass this information +directly into the update function as a string:: + + $this->db->update('mytable', $data, "id = 4"); + +Or as an array:: + + $this->db->update('mytable', $data, array('id' => $id)); + +You may also use the $this->db->set() function described above when +performing updates. + +$this->db->update_batch() +========================= + +Generates an update string based on the data you supply, and runs the query. +You can either pass an **array** or an **object** to the function. +Here is an example using an array:: + + $data = array( + array( + 'title' => 'My title' , + 'name' => 'My Name 2' , + 'date' => 'My date 2' + ), + array( + 'title' => 'Another title' , + 'name' => 'Another Name 2' , + 'date' => 'Another date 2' + ) + ); + + $this->db->update_batch('mytable', $data, 'title'); + + // Produces: + // UPDATE `mytable` SET `name` = CASE + // WHEN `title` = 'My title' THEN 'My Name 2' + // WHEN `title` = 'Another title' THEN 'Another Name 2' + // ELSE `name` END, + // `date` = CASE + // WHEN `title` = 'My title' THEN 'My date 2' + // WHEN `title` = 'Another title' THEN 'Another date 2' + // ELSE `date` END + // WHERE `title` IN ('My title','Another title') + +The first parameter will contain the table name, the second is an associative +array of values, the third parameter is the where key. + +.. note:: All values are escaped automatically producing safer queries. + +$this->db->get_compiled_update() +================================ + +This works exactly the same way as ``$this->db->get_compiled_insert()`` except +that it produces an UPDATE SQL string instead of an INSERT SQL string. + +For more information view documentation for `$this->db->get_compiled_insert()`_. + + +************* +Deleting Data +************* + +$this->db->delete() +=================== + +Generates a delete SQL string and runs the query. + +:: + + $this->db->delete('mytable', array('id' => $id)); // Produces: // DELETE FROM mytable // WHERE id = $id + +The first parameter is the table name, the second is the where clause. +You can also use the where() or or_where() functions instead of passing +the data to the second parameter of the function:: + + $this->db->where('id', $id); + $this->db->delete('mytable'); + + // Produces: + // DELETE FROM mytable + // WHERE id = $id + + +An array of table names can be passed into delete() if you would like to +delete data from more than 1 table. + +:: + + $tables = array('table1', 'table2', 'table3'); + $this->db->where('id', '5'); + $this->db->delete($tables); + + +If you want to delete all data from a table, you can use the truncate() +function, or empty_table(). + +$this->db->empty_table() +======================== + +Generates a delete SQL string and runs the +query.:: + + $this->db->empty_table('mytable'); // Produces: DELETE FROM mytable + + +$this->db->truncate() +===================== + +Generates a truncate SQL string and runs the query. + +:: + + $this->db->from('mytable'); + $this->db->truncate(); + + // or + + $this->db->truncate('mytable'); + + // Produce: + // TRUNCATE mytable + +.. note:: If the TRUNCATE command isn't available, truncate() will + execute as "DELETE FROM table". + +$this->db->get_compiled_delete() +================================ +This works exactly the same way as ``$this->db->get_compiled_insert()`` except +that it produces a DELETE SQL string instead of an INSERT SQL string. + +For more information view documentation for `$this->db->get_compiled_insert()`_. + +*************** +Method Chaining +*************** + +Method chaining allows you to simplify your syntax by connecting +multiple functions. Consider this example:: + + $query = $this->db->select('title') + ->where('id', $id) + ->limit(10, 20) + ->get('mytable'); + +.. _ar-caching: + +********************* +Query Builder Caching +********************* + +While not "true" caching, Query Builder enables you to save (or "cache") +certain parts of your queries for reuse at a later point in your +script's execution. Normally, when an Query Builder call is completed, +all stored information is reset for the next call. With caching, you can +prevent this reset, and reuse information easily. + +Cached calls are cumulative. If you make 2 cached select() calls, and +then 2 uncached select() calls, this will result in 4 select() calls. +There are three Caching functions available: + +$this->db->start_cache() +======================== + +This function must be called to begin caching. All Query Builder queries +of the correct type (see below for supported queries) are stored for +later use. + +$this->db->stop_cache() +======================= + +This function can be called to stop caching. + +$this->db->flush_cache() +======================== + +This function deletes all items from the Query Builder cache. + +Here's a usage example:: + + $this->db->start_cache(); + $this->db->select('field1'); + $this->db->stop_cache(); + $this->db->get('tablename'); + //Generates: SELECT `field1` FROM (`tablename`) + + $this->db->select('field2'); + $this->db->get('tablename'); + //Generates: SELECT `field1`, `field2` FROM (`tablename`) + + $this->db->flush_cache(); + $this->db->select('field2'); + $this->db->get('tablename'); + //Generates: SELECT `field2` FROM (`tablename`) + + +.. note:: The following statements can be cached: select, from, join, + where, like, group_by, having, order_by, set + + +$this->db->reset_query() +======================== + +Resetting Query Builder allows you to start fresh with your query without +executing it first using a method like $this->db->get() or $this->db->insert(). +Just like the methods that execute a query, this will *not* reset items you've +cached using `Query Builder Caching`_. + +This is useful in situations where you are using Query Builder to generate SQL +(ex. ``$this->db->get_compiled_select()``) but then choose to, for instance, +run the query:: + + // Note that the second parameter of the get_compiled_select method is FALSE + $sql = $this->db->select(array('field1','field2')) + ->where('field3',5) + ->get_compiled_select('mytable', FALSE); + + // ... + // Do something crazy with the SQL code... like add it to a cron script for + // later execution or something... + // ... + + $data = $this->db->get()->result_array(); + + // Would execute and return an array of results of the following query: + // SELECT field1, field1 from mytable where field3 = 5; diff --git a/user_guide_src/source/general/models.rst b/user_guide_src/source/general/models.rst index b816f958a..0156b0460 100644 --- a/user_guide_src/source/general/models.rst +++ b/user_guide_src/source/general/models.rst @@ -55,7 +55,7 @@ model class might look like:: } .. note:: The functions in the above example use the :doc:`Active - Record <../database/active_record>` database functions. + Record <../database/query_builder>` database functions. .. note:: For the sake of simplicity in this example we're using $_POST directly. This is generally bad practice, and a more common approach diff --git a/user_guide_src/source/helpers/directory_helper.rst b/user_guide_src/source/helpers/directory_helper.rst index fd169886c..cf88732d3 100644 --- a/user_guide_src/source/helpers/directory_helper.rst +++ b/user_guide_src/source/helpers/directory_helper.rst @@ -59,7 +59,7 @@ be numerically indexed. Here is an example of a typical array:: [1] => config.html         [database] => Array (               - [0] => active_record.html               + [0] => query_builder.html               [1] => binds.html               [2] => configuration.html [3] => connecting.html               diff --git a/user_guide_src/source/overview/features.rst b/user_guide_src/source/overview/features.rst index 44db08a94..8c27b1436 100644 --- a/user_guide_src/source/overview/features.rst +++ b/user_guide_src/source/overview/features.rst @@ -15,7 +15,7 @@ CodeIgniter's main features. - Model-View-Controller Based System - Extremely Light Weight - Full Featured database classes with support for several platforms. -- Active Record Database Support +- Query Builder Database Support - Form and Data Validation - Security and XSS Filtering - Session Management diff --git a/user_guide_src/source/tutorial/create_news_items.rst b/user_guide_src/source/tutorial/create_news_items.rst index 794b67eed..bfaf13537 100644 --- a/user_guide_src/source/tutorial/create_news_items.rst +++ b/user_guide_src/source/tutorial/create_news_items.rst @@ -94,7 +94,7 @@ Model ----- The only thing that remains is writing a method that writes the data to -the database. You'll use the Active Record class to insert the +the database. You'll use the Query Builder class to insert the information and use the input library to get the posted data. Open up the model created earlier and add the following: diff --git a/user_guide_src/source/tutorial/index.rst b/user_guide_src/source/tutorial/index.rst index c959d04d2..b1ab331d1 100644 --- a/user_guide_src/source/tutorial/index.rst +++ b/user_guide_src/source/tutorial/index.rst @@ -16,7 +16,7 @@ This tutorial will primarily focus on: - Model-View-Controller basics - Routing basics - Form validation -- Performing basic database queries using "Active Record" +- Performing basic database queries using "Query Builder" The entire tutorial is split up over several pages, each explaining a small part of the functionality of the CodeIgniter framework. You'll go diff --git a/user_guide_src/source/tutorial/news_section.rst b/user_guide_src/source/tutorial/news_section.rst index 38e4214ca..82b3e3b38 100644 --- a/user_guide_src/source/tutorial/news_section.rst +++ b/user_guide_src/source/tutorial/news_section.rst @@ -54,7 +54,7 @@ seed records. Now that the database and a model have been set up, you'll need a method to get all of our posts from our database. To do this, the database abstraction layer that is included with CodeIgniter — `Active -Record <../database/active_record.html>`_ — is used. This makes it +Record <../database/query_builder.html>`_ — is used. This makes it possible to write your 'queries' once and make them work on `all supported database systems <../general/requirements.html>`_. Add the following code to your model. @@ -76,7 +76,7 @@ following code to your model. With this code you can perform two different queries. You can get all news records, or get a news item by its `slug <#>`_. You might have noticed that the $slug variable wasn't sanitized before running the -query; Active Record does this for you. +query; Query Builder does this for you. Display the news ---------------- -- cgit v1.2.3-24-g4f1b From d48f30d1f28fb70d6e06bc4c9607993a991e4c42 Mon Sep 17 00:00:00 2001 From: Jamie Rumbelow Date: Sun, 19 Feb 2012 12:39:03 +0000 Subject: Adding the change to the release notes --- user_guide_src/source/changelog.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index a4904fceb..5ad3c3cbc 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -42,7 +42,9 @@ Release Date: Not Released - Database - - Added new :doc:`Active Record ` methods that return + - Renamed the Active Record class to Query Builder to remove confusion with + the Active Record design pattern + - Added new :doc:`Query Builder ` methods that return the SQL string of queries without executing them: get_compiled_select(), get_compiled_insert(), get_compiled_update(), get_compiled_delete(). - Taking care of LIKE condition when used with MySQL UPDATE statement. -- cgit v1.2.3-24-g4f1b From ae123e0b3d749a13d8820d0a6b773ca25b0ed380 Mon Sep 17 00:00:00 2001 From: Jamie Rumbelow Date: Tue, 21 Feb 2012 16:39:56 +0000 Subject: Fixing an error in #1061 --- system/database/DB_query_builder.php | 50 ++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index b6ad0f3d8..fad879389 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -2103,8 +2103,8 @@ class CI_DB_query_builder extends CI_DB_driver { foreach ($this->qb_cache_exists as $val) { - $qb_variable = 'ar_'.$val; - $qb_cache_var = 'ar_cache_'.$val; + $qb_variable = 'qb_'.$val; + $qb_cache_var = 'qb_cache_'.$val; if (count($this->$qb_cache_var) === 0) { @@ -2168,21 +2168,21 @@ class CI_DB_query_builder extends CI_DB_driver { protected function _reset_select() { $this->_reset_run(array( - 'ar_select' => array(), - 'ar_from' => array(), - 'ar_join' => array(), - 'ar_where' => array(), - 'ar_like' => array(), - 'ar_groupby' => array(), - 'ar_having' => array(), - 'ar_orderby' => array(), - 'ar_wherein' => array(), - 'ar_aliased_tables' => array(), - 'ar_no_escape' => array(), - 'ar_distinct' => FALSE, - 'ar_limit' => FALSE, - 'ar_offset' => FALSE, - 'ar_order' => FALSE + 'qb_select' => array(), + 'qb_from' => array(), + 'qb_join' => array(), + 'qb_where' => array(), + 'qb_like' => array(), + 'qb_groupby' => array(), + 'qb_having' => array(), + 'qb_orderby' => array(), + 'qb_wherein' => array(), + 'qb_aliased_tables' => array(), + 'qb_no_escape' => array(), + 'qb_distinct' => FALSE, + 'qb_limit' => FALSE, + 'qb_offset' => FALSE, + 'qb_order' => FALSE ) ); } @@ -2199,14 +2199,14 @@ class CI_DB_query_builder extends CI_DB_driver { protected function _reset_write() { $this->_reset_run(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 + 'qb_set' => array(), + 'qb_from' => array(), + 'qb_where' => array(), + 'qb_like' => array(), + 'qb_orderby' => array(), + 'qb_keys' => array(), + 'qb_limit' => FALSE, + 'qb_order' => FALSE ) ); } -- cgit v1.2.3-24-g4f1b From a6a1496b27c87718106645b6f1438023a18ad40c Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sat, 3 Mar 2012 03:57:26 +0200 Subject: Remove pg_set_client_encoding() from db_connect(), db_pconnect() --- system/database/drivers/postgre/postgre_driver.php | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index ce5dc96c3..d8b43695c 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -126,13 +126,7 @@ class CI_DB_postgre_driver extends CI_DB { */ public function db_connect() { - $ret = @pg_connect($this->dsn); - if (is_resource($ret) && $this->char_set != '') - { - pg_set_client_encoding($ret, $this->char_set); - } - - return $ret; + return @pg_connect($this->dsn); } // -------------------------------------------------------------------- @@ -144,13 +138,7 @@ class CI_DB_postgre_driver extends CI_DB { */ public function db_pconnect() { - $ret = @pg_pconnect($this->dsn); - if (is_resource($ret) && $this->char_set != '') - { - pg_set_client_encoding($ret, $this->char_set); - } - - return $ret; + return @pg_pconnect($this->dsn); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 17c1bedbab426ce383138f2bc720a1134afbe475 Mon Sep 17 00:00:00 2001 From: Jamie Rumbelow Date: Tue, 6 Mar 2012 21:30:38 +0000 Subject: Updating a couple more references to the AR class in documentation [#1061] --- system/database/DB_driver.php | 2 +- system/database/DB_query_builder.php | 40 ++++++++++++++++++------------------ 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index e379755ed..6a20c001a 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1351,7 +1351,7 @@ class CI_DB_driver { // -------------------------------------------------------------------- /** - * Dummy method that allows Active Record class to be disabled + * Dummy method that allows Query Builder class to be disabled * * This function is used extensively by every db driver. * diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index ab59462b1..8cbe7d9f9 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -1058,7 +1058,7 @@ class CI_DB_query_builder extends CI_DB_driver { * * @access public * @param string the table name to select from (optional) - * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone + * @param boolean TRUE: resets QB values; FALSE: leave QB vaules alone * @return string */ public function get_compiled_select($table = '', $reset = TRUE) @@ -1286,7 +1286,7 @@ class CI_DB_query_builder extends CI_DB_driver { * * @access public * @param string the table to insert into - * @param boolean TRUE: reset AR values; FALSE: leave AR values alone + * @param boolean TRUE: reset QB values; FALSE: leave QB values alone * @return string */ public function get_compiled_insert($table = '', $reset = TRUE) @@ -1431,7 +1431,7 @@ class CI_DB_query_builder extends CI_DB_driver { * * @access public * @param string the table to update - * @param boolean TRUE: reset AR values; FALSE: leave AR values alone + * @param boolean TRUE: reset QB values; FALSE: leave QB values alone * @return string */ public function get_compiled_update($table = '', $reset = TRUE) @@ -1705,7 +1705,7 @@ class CI_DB_query_builder extends CI_DB_driver { * * @access public * @param string the table to delete from - * @param boolean TRUE: reset AR values; FALSE: leave AR values alone + * @param boolean TRUE: reset QB values; FALSE: leave QB values alone * @return string */ public function get_compiled_delete($table = '', $reset = TRUE) @@ -2039,7 +2039,7 @@ class CI_DB_query_builder extends CI_DB_driver { /** * Start Cache * - * Starts AR caching + * Starts QB caching * * @return void */ @@ -2053,7 +2053,7 @@ class CI_DB_query_builder extends CI_DB_driver { /** * Stop Cache * - * Stops AR caching + * Stops QB caching * * @return void */ @@ -2067,7 +2067,7 @@ class CI_DB_query_builder extends CI_DB_driver { /** * Flush Cache * - * Empties the AR cache + * Empties the QB cache * * @access public * @return void @@ -2075,17 +2075,17 @@ class CI_DB_query_builder extends CI_DB_driver { public function flush_cache() { $this->_reset_run(array( - 'ar_cache_select' => array(), - 'ar_cache_from' => array(), - 'ar_cache_join' => array(), - 'ar_cache_where' => array(), - 'ar_cache_like' => array(), - 'ar_cache_groupby' => array(), - 'ar_cache_having' => array(), - 'ar_cache_orderby' => array(), - 'ar_cache_set' => array(), - 'ar_cache_exists' => array(), - 'ar_cache_no_escape' => array() + 'qb_cache_select' => array(), + 'qb_cache_from' => array(), + 'qb_cache_join' => array(), + 'qb_cache_where' => array(), + 'qb_cache_like' => array(), + 'qb_cache_groupby' => array(), + 'qb_cache_having' => array(), + 'qb_cache_orderby' => array(), + 'qb_cache_set' => array(), + 'qb_cache_exists' => array(), + 'qb_cache_no_escape' => array() )); } @@ -2094,7 +2094,7 @@ class CI_DB_query_builder extends CI_DB_driver { /** * Merge Cache * - * When called, this function merges any cached AR arrays with + * When called, this function merges any cached QB arrays with * locally called ones. * * @return void @@ -2134,7 +2134,7 @@ class CI_DB_query_builder extends CI_DB_driver { /** * Reset Query Builder values. * - * Publicly-visible method to reset the AR values. + * Publicly-visible method to reset the QB values. * * @return void */ -- cgit v1.2.3-24-g4f1b From 0c09299363bb27d6a115ee1377167a6bc71e09a7 Mon Sep 17 00:00:00 2001 From: Jamie Rumbelow Date: Tue, 6 Mar 2012 22:05:16 +0000 Subject: accounting for the rename of protect_identifiers --- system/database/DB_driver.php | 4 ++-- system/database/DB_query_builder.php | 44 ++++++++++++++++++------------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 6a20c001a..986916964 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -74,7 +74,7 @@ class CI_DB_driver { public $cache_autodel = FALSE; public $CACHE; // The cache class object - protected $_protect_identifiers = TRUE; + protected $protect_identifiers = TRUE; protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped public function __construct($params) @@ -1208,7 +1208,7 @@ class CI_DB_driver { { if ( ! is_bool($protect_identifiers)) { - $protect_identifiers = $this->_protect_identifiers; + $protect_identifiers = $this->protect_identifiers; } if (is_array($item)) diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index 8cbe7d9f9..7691d5eba 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -212,7 +212,7 @@ class CI_DB_query_builder extends CI_DB_driver { $alias = $this->_create_alias_from_table(trim($select)); } - $sql = $this->_protect_identifiers($type.'('.trim($select).')').' AS '.$this->_protect_identifiers(trim($alias)); + $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->protect_identifiers(trim($alias)); $this->qb_select[] = $sql; if ($this->qb_caching === TRUE) @@ -280,7 +280,7 @@ class CI_DB_query_builder extends CI_DB_driver { $v = trim($v); $this->_track_aliases($v); - $v = $this->qb_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE); + $v = $this->qb_from[] = $this->protect_identifiers($v, TRUE, NULL, FALSE); if ($this->qb_caching === TRUE) { @@ -294,10 +294,10 @@ class CI_DB_query_builder extends CI_DB_driver { $val = trim($val); // Extract any aliases that might exist. We use this information - // in the _protect_identifiers to know whether to add a table prefix + // in the protect_identifiers to know whether to add a table prefix $this->_track_aliases($val); - $this->qb_from[] = $val = $this->_protect_identifiers($val, TRUE, NULL, FALSE); + $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE); if ($this->qb_caching === TRUE) { @@ -339,7 +339,7 @@ class CI_DB_query_builder extends CI_DB_driver { } // Extract any aliases that might exist. We use this information - // in the _protect_identifiers to know whether to add a table prefix + // in the protect_identifiers to know whether to add a table prefix $this->_track_aliases($table); // Strip apart the condition and protect the identifiers @@ -349,7 +349,7 @@ class CI_DB_query_builder extends CI_DB_driver { } // Assemble the JOIN statement - $this->qb_join[] = $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond; + $this->qb_join[] = $join = $type.'JOIN '.$this->protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond; if ($this->qb_caching === TRUE) { @@ -418,7 +418,7 @@ class CI_DB_query_builder extends CI_DB_driver { // If the escape value was not set will will base it on the global setting if ( ! is_bool($escape)) { - $escape = $this->_protect_identifiers; + $escape = $this->protect_identifiers; } foreach ($key as $k => $v) @@ -564,7 +564,7 @@ class CI_DB_query_builder extends CI_DB_driver { } $prefix = (count($this->qb_where) === 0) ? '' : $type; - $this->qb_where[] = $where_in = $prefix.$this->_protect_identifiers($key).$not.' IN ('.implode(', ', $this->qb_wherein).') '; + $this->qb_where[] = $where_in = $prefix.$this->protect_identifiers($key).$not.' IN ('.implode(', ', $this->qb_wherein).') '; if ($this->qb_caching === TRUE) { @@ -668,7 +668,7 @@ class CI_DB_query_builder extends CI_DB_driver { foreach ($field as $k => $v) { - $k = $this->_protect_identifiers($k); + $k = $this->protect_identifiers($k); $prefix = (count($this->qb_like) === 0) ? '' : $type; $v = $this->escape_like_str($v); @@ -829,7 +829,7 @@ class CI_DB_query_builder extends CI_DB_driver { if ($val != '') { - $this->qb_groupby[] = $val = $this->_protect_identifiers($val); + $this->qb_groupby[] = $val = $this->protect_identifiers($val); if ($this->qb_caching === TRUE) { @@ -1038,11 +1038,11 @@ class CI_DB_query_builder extends CI_DB_driver { { if ($escape === FALSE) { - $this->qb_set[$this->_protect_identifiers($k)] = $v; + $this->qb_set[$this->protect_identifiers($k)] = $v; } else { - $this->qb_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v); + $this->qb_set[$this->protect_identifiers($k, FALSE, TRUE)] = $this->escape($v); } } @@ -1213,7 +1213,7 @@ class CI_DB_query_builder extends CI_DB_driver { // Batch this baby for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100) { - $this->query($this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100))); + $this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100))); } $this->_reset_write(); @@ -1271,7 +1271,7 @@ class CI_DB_query_builder extends CI_DB_driver { foreach ($keys as $k) { - $this->qb_keys[] = $this->_protect_identifiers($k); + $this->qb_keys[] = $this->protect_identifiers($k); } return $this; @@ -1297,7 +1297,7 @@ class CI_DB_query_builder extends CI_DB_driver { } $sql = $this->_insert( - $this->_protect_identifiers( + $this->protect_identifiers( $this->qb_from[0], TRUE, NULL, FALSE ), array_keys($this->qb_set), @@ -1337,7 +1337,7 @@ class CI_DB_query_builder extends CI_DB_driver { } $sql = $this->_insert( - $this->_protect_identifiers( + $this->protect_identifiers( $this->qb_from[0], TRUE, NULL, FALSE ), array_keys($this->qb_set), @@ -1416,7 +1416,7 @@ class CI_DB_query_builder extends CI_DB_driver { $table = $this->qb_from[0]; } - $sql = $this->_replace($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->qb_set), array_values($this->qb_set)); + $sql = $this->_replace($this->protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->qb_set), array_values($this->qb_set)); $this->_reset_write(); return $this->query($sql); @@ -1444,7 +1444,7 @@ class CI_DB_query_builder extends CI_DB_driver { return FALSE; } - $sql = $this->_update($this->_protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set, $this->qb_where, $this->qb_orderby, $this->qb_limit); + $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set, $this->qb_where, $this->qb_orderby, $this->qb_limit); if ($reset === TRUE) { @@ -1491,7 +1491,7 @@ class CI_DB_query_builder extends CI_DB_driver { $this->limit($limit); } - $sql = $this->_update($this->_protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set, $this->qb_where, $this->qb_orderby, $this->qb_limit, $this->qb_like); + $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set, $this->qb_where, $this->qb_orderby, $this->qb_limit, $this->qb_like); $this->_reset_write(); return $this->query($sql); @@ -1577,7 +1577,7 @@ class CI_DB_query_builder extends CI_DB_driver { // Batch this baby for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100) { - $this->query($this->_update_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, 100), $this->_protect_identifiers($index), $this->qb_where)); + $this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, 100), $this->protect_identifiers($index), $this->qb_where)); } $this->_reset_write(); @@ -1898,7 +1898,7 @@ class CI_DB_query_builder extends CI_DB_driver { foreach ($this->qb_select as $key => $val) { $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL; - $this->qb_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape); + $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape); } $sql .= implode(', ', $this->qb_select); @@ -2121,7 +2121,7 @@ class CI_DB_query_builder extends CI_DB_driver { // If we are "protecting identifiers" we need to examine the "from" // portion of the query to determine if there are any aliases - if ($this->_protect_identifiers === TRUE AND count($this->qb_cache_from) > 0) + if ($this->protect_identifiers === TRUE AND count($this->qb_cache_from) > 0) { $this->_track_aliases($this->qb_from); } -- cgit v1.2.3-24-g4f1b From 0cd8c798de4b99b5ad41bacdeef77a4a7b815a03 Mon Sep 17 00:00:00 2001 From: Jamie Rumbelow Date: Thu, 8 Mar 2012 12:52:24 +0000 Subject: Just the method, not the property --- system/database/DB_driver.php | 2 +- system/database/DB_query_builder.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 986916964..9e1c605ec 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1208,7 +1208,7 @@ class CI_DB_driver { { if ( ! is_bool($protect_identifiers)) { - $protect_identifiers = $this->protect_identifiers; + $protect_identifiers = $this->_protect_identifiers; } if (is_array($item)) diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index 7691d5eba..8979dc15b 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -418,7 +418,7 @@ class CI_DB_query_builder extends CI_DB_driver { // If the escape value was not set will will base it on the global setting if ( ! is_bool($escape)) { - $escape = $this->protect_identifiers; + $escape = $this->_protect_identifiers; } foreach ($key as $k => $v) @@ -2121,7 +2121,7 @@ class CI_DB_query_builder extends CI_DB_driver { // If we are "protecting identifiers" we need to examine the "from" // portion of the query to determine if there are any aliases - if ($this->protect_identifiers === TRUE AND count($this->qb_cache_from) > 0) + if ($this->_protect_identifiers === TRUE AND count($this->qb_cache_from) > 0) { $this->_track_aliases($this->qb_from); } -- cgit v1.2.3-24-g4f1b From bcee50ff3247dee71d83bf273e52bc10096cd48c Mon Sep 17 00:00:00 2001 From: Jamie Rumbelow Date: Thu, 8 Mar 2012 13:00:15 +0000 Subject: missed one :bomb: --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 9e1c605ec..6a20c001a 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -74,7 +74,7 @@ class CI_DB_driver { public $cache_autodel = FALSE; public $CACHE; // The cache class object - protected $protect_identifiers = TRUE; + protected $_protect_identifiers = TRUE; protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped public function __construct($params) -- cgit v1.2.3-24-g4f1b From fead0550fcef5681a8a031433232c03b9cc1218d Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 20 Mar 2012 15:23:06 +0200 Subject: Remove EOF newlines --- system/database/drivers/cubrid/cubrid_forge.php | 2 +- system/database/drivers/cubrid/cubrid_result.php | 2 +- system/database/drivers/cubrid/cubrid_utility.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/system/database/drivers/cubrid/cubrid_forge.php b/system/database/drivers/cubrid/cubrid_forge.php index f0d9804cc..6b8097370 100644 --- a/system/database/drivers/cubrid/cubrid_forge.php +++ b/system/database/drivers/cubrid/cubrid_forge.php @@ -254,4 +254,4 @@ class CI_DB_cubrid_forge extends CI_DB_forge { } /* End of file cubrid_forge.php */ -/* Location: ./system/database/drivers/cubrid/cubrid_forge.php */ +/* Location: ./system/database/drivers/cubrid/cubrid_forge.php */ \ No newline at end of file diff --git a/system/database/drivers/cubrid/cubrid_result.php b/system/database/drivers/cubrid/cubrid_result.php index b954345c3..f891968d7 100644 --- a/system/database/drivers/cubrid/cubrid_result.php +++ b/system/database/drivers/cubrid/cubrid_result.php @@ -167,4 +167,4 @@ class CI_DB_cubrid_result extends CI_DB_result { } /* End of file cubrid_result.php */ -/* Location: ./system/database/drivers/cubrid/cubrid_result.php */ +/* Location: ./system/database/drivers/cubrid/cubrid_result.php */ \ No newline at end of file diff --git a/system/database/drivers/cubrid/cubrid_utility.php b/system/database/drivers/cubrid/cubrid_utility.php index c7c02a518..ead54f094 100644 --- a/system/database/drivers/cubrid/cubrid_utility.php +++ b/system/database/drivers/cubrid/cubrid_utility.php @@ -99,4 +99,4 @@ class CI_DB_cubrid_utility extends CI_DB_utility { } /* End of file cubrid_utility.php */ -/* Location: ./system/database/drivers/cubrid/cubrid_utility.php */ +/* Location: ./system/database/drivers/cubrid/cubrid_utility.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 135efb258559114bccd43a5f486efc3d14797fda Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 20 Mar 2012 15:30:56 +0200 Subject: Remove EOF newlines --- system/database/drivers/postgre/postgre_driver.php | 2 +- system/database/drivers/postgre/postgre_forge.php | 2 +- system/database/drivers/postgre/postgre_result.php | 2 +- system/database/drivers/postgre/postgre_utility.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index ab561add4..9680b3d96 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -686,4 +686,4 @@ class CI_DB_postgre_driver extends CI_DB { } /* End of file postgre_driver.php */ -/* Location: ./system/database/drivers/postgre/postgre_driver.php */ +/* Location: ./system/database/drivers/postgre/postgre_driver.php */ \ No newline at end of file diff --git a/system/database/drivers/postgre/postgre_forge.php b/system/database/drivers/postgre/postgre_forge.php index c98116ab6..ab9c95b89 100644 --- a/system/database/drivers/postgre/postgre_forge.php +++ b/system/database/drivers/postgre/postgre_forge.php @@ -273,4 +273,4 @@ class CI_DB_postgre_forge extends CI_DB_forge { } /* End of file postgre_forge.php */ -/* Location: ./system/database/drivers/postgre/postgre_forge.php */ +/* Location: ./system/database/drivers/postgre/postgre_forge.php */ \ No newline at end of file diff --git a/system/database/drivers/postgre/postgre_result.php b/system/database/drivers/postgre/postgre_result.php index 20c575fd8..394b8b6fd 100644 --- a/system/database/drivers/postgre/postgre_result.php +++ b/system/database/drivers/postgre/postgre_result.php @@ -166,4 +166,4 @@ class CI_DB_postgre_result extends CI_DB_result { } /* End of file postgre_result.php */ -/* Location: ./system/database/drivers/postgre/postgre_result.php */ +/* Location: ./system/database/drivers/postgre/postgre_result.php */ \ 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 d2c0079c6..cf29201ff 100644 --- a/system/database/drivers/postgre/postgre_utility.php +++ b/system/database/drivers/postgre/postgre_utility.php @@ -86,4 +86,4 @@ class CI_DB_postgre_utility extends CI_DB_utility { } /* End of file postgre_utility.php */ -/* Location: ./system/database/drivers/postgre/postgre_utility.php */ +/* Location: ./system/database/drivers/postgre/postgre_utility.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 6a96bf7ece4da23e2a802f48c408fa240da776f0 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Wed, 4 Apr 2012 19:13:03 +0700 Subject: Include PDO code coverage, remove unnecessary line on phpunit xml --- .travis.yml | 9 ++++++--- tests/travis/mysql.phpunit.xml | 5 ----- tests/travis/pdo/mysql.phpunit.xml | 32 ++++++++++++++++++++++++++++++++ tests/travis/pdo/pgsql.phpunit.xml | 32 ++++++++++++++++++++++++++++++++ tests/travis/pdo/sqlite.phpunit.xml | 32 ++++++++++++++++++++++++++++++++ tests/travis/pgsql.phpunit.xml | 5 ----- tests/travis/sqlite.phpunit.xml | 5 ----- 7 files changed, 102 insertions(+), 18 deletions(-) create mode 100644 tests/travis/pdo/mysql.phpunit.xml create mode 100644 tests/travis/pdo/pgsql.phpunit.xml create mode 100644 tests/travis/pdo/sqlite.phpunit.xml diff --git a/.travis.yml b/.travis.yml index 84029b964..033fd6ab0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,14 +8,17 @@ env: - DB=mysql - DB=pgsql - DB=sqlite + - DB=pdo/mysql + - DB=pdo/pgsql + - DB=pdo/sqlite before_script: - pyrus channel-discover pear.php-tools.net - pyrus install http://pear.php-tools.net/get/vfsStream-0.11.2.tgz - phpenv rehash - - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'DROP DATABASE IF EXISTS ci_test;' -U postgres; fi" - - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'create database ci_test;' -U postgres; fi" - - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'create database IF NOT EXISTS ci_test;'; fi" + - sh -c "if [ '$DB' = 'pgsql' | '$DB' = 'pdo/pgsql' ]; then psql -c 'DROP DATABASE IF EXISTS ci_test;' -U postgres; fi" + - sh -c "if [ '$DB' = 'pgsql' | '$DB' = 'pdo/pgsql' ]; then psql -c 'create database ci_test;' -U postgres; fi" + - sh -c "if [ '$DB' = 'mysql' | '$DB' = 'pdo/mysql' ]; then mysql -e 'create database IF NOT EXISTS ci_test;'; fi" script: phpunit --configuration tests/travis/$DB.phpunit.xml diff --git a/tests/travis/mysql.phpunit.xml b/tests/travis/mysql.phpunit.xml index 44d6d6ed9..e9556f758 100644 --- a/tests/travis/mysql.phpunit.xml +++ b/tests/travis/mysql.phpunit.xml @@ -28,10 +28,5 @@ PROJECT_BASE.'tests' '../../system/core/CodeIgniter.php' - - - \ No newline at end of file diff --git a/tests/travis/pdo/mysql.phpunit.xml b/tests/travis/pdo/mysql.phpunit.xml new file mode 100644 index 000000000..eb650fa1e --- /dev/null +++ b/tests/travis/pdo/mysql.phpunit.xml @@ -0,0 +1,32 @@ + + + + + + + + + ../../codeigniter/Setup_test.php + ../../codeigniter/core + ../../codeigniter/helpers + ../../codeigniter/libraries + ../../codeigniter/database + + + + + PEAR_INSTALL_DIR + PHP_LIBDIR + PROJECT_BASE.'tests' + '../../../system/core/CodeIgniter.php' + + + \ No newline at end of file diff --git a/tests/travis/pdo/pgsql.phpunit.xml b/tests/travis/pdo/pgsql.phpunit.xml new file mode 100644 index 000000000..351f690d5 --- /dev/null +++ b/tests/travis/pdo/pgsql.phpunit.xml @@ -0,0 +1,32 @@ + + + + + + + + + ../../codeigniter/Setup_test.php + ../../codeigniter/core + ../../codeigniter/helpers + ../../codeigniter/libraries + ../../codeigniter/database + + + + + PEAR_INSTALL_DIR + PHP_LIBDIR + PROJECT_BASE.'tests' + '../../../system/core/CodeIgniter.php' + + + \ No newline at end of file diff --git a/tests/travis/pdo/sqlite.phpunit.xml b/tests/travis/pdo/sqlite.phpunit.xml new file mode 100644 index 000000000..ae139b7a9 --- /dev/null +++ b/tests/travis/pdo/sqlite.phpunit.xml @@ -0,0 +1,32 @@ + + + + + + + + + ../../codeigniter/Setup_test.php + ../../codeigniter/core + ../../codeigniter/helpers + ../../codeigniter/libraries + ../../codeigniter/database + + + + + PEAR_INSTALL_DIR + PHP_LIBDIR + PROJECT_BASE.'tests' + '../../../system/core/CodeIgniter.php' + + + \ No newline at end of file diff --git a/tests/travis/pgsql.phpunit.xml b/tests/travis/pgsql.phpunit.xml index 9f52b40ae..ad8aeded2 100644 --- a/tests/travis/pgsql.phpunit.xml +++ b/tests/travis/pgsql.phpunit.xml @@ -28,10 +28,5 @@ PROJECT_BASE.'tests' '../../system/core/CodeIgniter.php' - - - \ No newline at end of file diff --git a/tests/travis/sqlite.phpunit.xml b/tests/travis/sqlite.phpunit.xml index 74ebb482b..628370e93 100644 --- a/tests/travis/sqlite.phpunit.xml +++ b/tests/travis/sqlite.phpunit.xml @@ -28,10 +28,5 @@ PROJECT_BASE.'tests' '../../system/core/CodeIgniter.php' - - - \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 44015c8d91ecea78ee4a10de579c36a859c2fcb6 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Wed, 4 Apr 2012 19:38:16 +0700 Subject: Set up PDO constant and configuration --- .travis.yml | 6 ++--- tests/mocks/database/config/pdo/mysql.php | 37 ++++++++++++++++++++++++++++++ tests/mocks/database/config/pdo/pgsql.php | 37 ++++++++++++++++++++++++++++++ tests/mocks/database/config/pdo/sqlite.php | 22 ++++++++++++++++++ tests/mocks/database/config/sqlite.php | 7 +++--- tests/travis/pdo/mysql.phpunit.xml | 2 +- tests/travis/pdo/pgsql.phpunit.xml | 2 +- tests/travis/pdo/sqlite.phpunit.xml | 2 +- 8 files changed, 105 insertions(+), 10 deletions(-) create mode 100644 tests/mocks/database/config/pdo/mysql.php create mode 100644 tests/mocks/database/config/pdo/pgsql.php create mode 100644 tests/mocks/database/config/pdo/sqlite.php diff --git a/.travis.yml b/.travis.yml index 033fd6ab0..97ea0422d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,9 +16,9 @@ before_script: - pyrus channel-discover pear.php-tools.net - pyrus install http://pear.php-tools.net/get/vfsStream-0.11.2.tgz - phpenv rehash - - sh -c "if [ '$DB' = 'pgsql' | '$DB' = 'pdo/pgsql' ]; then psql -c 'DROP DATABASE IF EXISTS ci_test;' -U postgres; fi" - - sh -c "if [ '$DB' = 'pgsql' | '$DB' = 'pdo/pgsql' ]; then psql -c 'create database ci_test;' -U postgres; fi" - - sh -c "if [ '$DB' = 'mysql' | '$DB' = 'pdo/mysql' ]; then mysql -e 'create database IF NOT EXISTS ci_test;'; fi" + - sh -c "if [ '$DB' = 'pgsql' ] || [ '$DB' = 'pdo/pgsql' ]; then psql -c 'DROP DATABASE IF EXISTS ci_test;' -U postgres; fi" + - sh -c "if [ '$DB' = 'pgsql' ] || [ '$DB' = 'pdo/pgsql' ]; then psql -c 'create database ci_test;' -U postgres; fi" + - sh -c "if [ '$DB' = 'mysql' ] || [ '$DB' = 'pdo/mysql' ]; then mysql -e 'create database IF NOT EXISTS ci_test;'; fi" script: phpunit --configuration tests/travis/$DB.phpunit.xml diff --git a/tests/mocks/database/config/pdo/mysql.php b/tests/mocks/database/config/pdo/mysql.php new file mode 100644 index 000000000..cefb6b008 --- /dev/null +++ b/tests/mocks/database/config/pdo/mysql.php @@ -0,0 +1,37 @@ + array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'travis', + 'password' => '', + 'database' => 'ci_test', + 'dbdriver' => 'pdo', + 'pdodriver' => 'mysql', + ), + + // Database configuration with failover + 'pdo/mysql_failover' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'not_travis', + 'password' => 'wrong password', + 'database' => 'not_ci_test', + 'dbdriver' => 'pdo', + 'pdodriver' => 'mysql', + 'failover' => array( + array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'travis', + 'password' => '', + 'database' => 'ci_test', + 'dbdriver' => 'pdo', + 'pdodriver' => 'mysql', + ), + ), + ), +); \ No newline at end of file diff --git a/tests/mocks/database/config/pdo/pgsql.php b/tests/mocks/database/config/pdo/pgsql.php new file mode 100644 index 000000000..b2f409d8d --- /dev/null +++ b/tests/mocks/database/config/pdo/pgsql.php @@ -0,0 +1,37 @@ + array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'postgres', + 'password' => '', + 'database' => 'ci_test', + 'dbdriver' => 'pdo', + 'pdodriver' => 'pgsql', + ), + + // Database configuration with failover + 'pdo/pgsql_failover' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'not_travis', + 'password' => 'wrong password', + 'database' => 'not_ci_test', + 'dbdriver' => 'pdo', + 'pdodriver' => 'pgsql', + 'failover' => array( + array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'postgres', + 'password' => '', + 'database' => 'ci_test', + 'dbdriver' => 'pdo', + 'pdodriver' => 'pgsql', + ), + ), + ), +); \ No newline at end of file diff --git a/tests/mocks/database/config/pdo/sqlite.php b/tests/mocks/database/config/pdo/sqlite.php new file mode 100644 index 000000000..c6827b41c --- /dev/null +++ b/tests/mocks/database/config/pdo/sqlite.php @@ -0,0 +1,22 @@ + array( + 'dsn' => 'sqlite:/'.realpath(__DIR__.'/..').'/ci_test.sqlite', + 'dbdriver' => 'pdo', + ), + + // Database configuration with failover + 'pdo/sqlite_failover' => array( + 'dsn' => 'sqlite:/'.realpath(__DIR__.'/..').'/not_exists.sqlite', + 'dbdriver' => 'pdo', + 'failover' => array( + array( + 'dsn' => 'sqlite:/'.realpath(__DIR__.'/..').'/ci_test.sqlite', + 'dbdriver' => 'pdo', + ), + ), + ), +); \ No newline at end of file diff --git a/tests/mocks/database/config/sqlite.php b/tests/mocks/database/config/sqlite.php index 8665e208d..c70986e7c 100644 --- a/tests/mocks/database/config/sqlite.php +++ b/tests/mocks/database/config/sqlite.php @@ -1,5 +1,4 @@ 'sqlite', 'password' => 'sqlite', 'database' => realpath(__DIR__.'/..').'/ci_test.sqlite', - 'dbdriver' => $dbdriver, + 'dbdriver' => is_php('5.4') ? 'sqlite3' : 'sqlite', ), // Database configuration with failover @@ -20,7 +19,7 @@ return array( 'username' => 'sqlite', 'password' => 'sqlite', 'database' => '../not_exists.sqlite', - 'dbdriver' => $dbdriver, + 'dbdriver' => is_php('5.4') ? 'sqlite3' : 'sqlite', 'failover' => array( array( 'dsn' => '', @@ -28,7 +27,7 @@ return array( 'username' => 'sqlite', 'password' => 'sqlite', 'database' => realpath(__DIR__.'/..').'/ci_testf.sqlite', - 'dbdriver' => $dbdriver, + 'dbdriver' => is_php('5.4') ? 'sqlite3' : 'sqlite', ), ), ), diff --git a/tests/travis/pdo/mysql.phpunit.xml b/tests/travis/pdo/mysql.phpunit.xml index eb650fa1e..69eece24f 100644 --- a/tests/travis/pdo/mysql.phpunit.xml +++ b/tests/travis/pdo/mysql.phpunit.xml @@ -10,7 +10,7 @@ stopOnIncomplete="false" stopOnSkipped="false"> - + diff --git a/tests/travis/pdo/pgsql.phpunit.xml b/tests/travis/pdo/pgsql.phpunit.xml index 351f690d5..e68c3e028 100644 --- a/tests/travis/pdo/pgsql.phpunit.xml +++ b/tests/travis/pdo/pgsql.phpunit.xml @@ -10,7 +10,7 @@ stopOnIncomplete="false" stopOnSkipped="false"> - + diff --git a/tests/travis/pdo/sqlite.phpunit.xml b/tests/travis/pdo/sqlite.phpunit.xml index ae139b7a9..1871f6221 100644 --- a/tests/travis/pdo/sqlite.phpunit.xml +++ b/tests/travis/pdo/sqlite.phpunit.xml @@ -10,7 +10,7 @@ stopOnIncomplete="false" stopOnSkipped="false"> - + -- cgit v1.2.3-24-g4f1b From f4c6c9b3061b464c959b6409f962228959f35287 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Wed, 4 Apr 2012 23:24:09 +0700 Subject: DB Drivers test --- tests/codeigniter/database/DB_driver_test.php | 36 +++++++++++++++++++++++++++ tests/mocks/autoloader.php | 15 +++++++++-- tests/mocks/database/config/ci_test.sqlite | 0 tests/mocks/database/config/pdo/sqlite.php | 3 +++ tests/mocks/database/db.php | 6 +++++ tests/mocks/database/db/driver.php | 36 +++++++++++++++++++++++++++ tests/mocks/database/drivers/mysql.php | 16 ++++++++++++ tests/mocks/database/drivers/pdo.php | 16 ++++++++++++ tests/mocks/database/drivers/postgre.php | 16 ++++++++++++ tests/mocks/database/drivers/sqlite.php | 16 ++++++++++++ 10 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 tests/codeigniter/database/DB_driver_test.php create mode 100644 tests/mocks/database/config/ci_test.sqlite create mode 100644 tests/mocks/database/db/driver.php create mode 100644 tests/mocks/database/drivers/mysql.php create mode 100644 tests/mocks/database/drivers/pdo.php create mode 100644 tests/mocks/database/drivers/postgre.php create mode 100644 tests/mocks/database/drivers/sqlite.php diff --git a/tests/codeigniter/database/DB_driver_test.php b/tests/codeigniter/database/DB_driver_test.php new file mode 100644 index 000000000..fb40f0608 --- /dev/null +++ b/tests/codeigniter/database/DB_driver_test.php @@ -0,0 +1,36 @@ +$driver_name($config[DB_DRIVER]); + + $this->assertTrue($driver->initialize()); + } + + protected function pdo($config) + { + return new Mock_Database_Drivers_PDO($config); + } + + protected function mysql($config) + { + return new Mock_Database_Drivers_Mysql($config); + } + + protected function sqlite($config) + { + return new Mock_Database_Drivers_Sqlite($config); + } + + protected function pgsql($config) + { + return new Mock_Database_Drivers_Postgre($config); + } + +} \ No newline at end of file diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php index dd5929206..22448139e 100644 --- a/tests/mocks/autoloader.php +++ b/tests/mocks/autoloader.php @@ -53,13 +53,24 @@ function autoload($class) $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR; $class = $subclass; } + elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) == 3) + { + $driver_path = BASEPATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR; + $dir = $driver_path.$m[1].DIRECTORY_SEPARATOR; + $file = $dir.$m[1].'_'.$m[2].'.php'; + } + elseif (strpos($class, 'CI_DB') === 0) + { + $dir = BASEPATH.'database'.DIRECTORY_SEPARATOR; + $file = $dir.str_replace('CI_DB', 'DB', $subclass).'.php'; + } else { $class = strtolower($class); } } - $file = $dir.$class.'.php'; + $file = (isset($file)) ? $file : $dir.$class.'.php'; if ( ! file_exists($file)) { @@ -71,7 +82,7 @@ function autoload($class) { return FALSE; } - + var_dump($file); throw new InvalidArgumentException("Unable to load $class."); } diff --git a/tests/mocks/database/config/ci_test.sqlite b/tests/mocks/database/config/ci_test.sqlite new file mode 100644 index 000000000..e69de29bb diff --git a/tests/mocks/database/config/pdo/sqlite.php b/tests/mocks/database/config/pdo/sqlite.php index c6827b41c..1e5043b00 100644 --- a/tests/mocks/database/config/pdo/sqlite.php +++ b/tests/mocks/database/config/pdo/sqlite.php @@ -6,16 +6,19 @@ return array( 'pdo/sqlite' => array( 'dsn' => 'sqlite:/'.realpath(__DIR__.'/..').'/ci_test.sqlite', 'dbdriver' => 'pdo', + 'pdodriver' => 'sqlite', ), // Database configuration with failover 'pdo/sqlite_failover' => array( 'dsn' => 'sqlite:/'.realpath(__DIR__.'/..').'/not_exists.sqlite', 'dbdriver' => 'pdo', + 'pdodriver' => 'sqlite', 'failover' => array( array( 'dsn' => 'sqlite:/'.realpath(__DIR__.'/..').'/ci_test.sqlite', 'dbdriver' => 'pdo', + 'pdodriver' => 'sqlite', ), ), ), diff --git a/tests/mocks/database/db.php b/tests/mocks/database/db.php index 43a0d391f..c30e6d2e6 100644 --- a/tests/mocks/database/db.php +++ b/tests/mocks/database/db.php @@ -45,6 +45,8 @@ class Mock_Database_DB { ); $config = array_merge($this->config[$group], $params); + $pdodriver = ( ! empty($config['pdodriver'])) ? $config['pdodriver'] : FALSE; + $failover = ( ! empty($config['failover'])) ? $config['failover'] : FALSE; if ( ! empty($config['dsn'])) { @@ -57,7 +59,11 @@ class Mock_Database_DB { } + // Build the parameter $other_params = array_slice($config, 6); + $other_params['dsn'] = $dsn; + if ($pdodriver) $other_params['pdodriver'] = $pdodriver; + if ($failover) $other_params['failover'] = $failover; return $dsn.'?'.http_build_query($other_params); } diff --git a/tests/mocks/database/db/driver.php b/tests/mocks/database/db/driver.php new file mode 100644 index 000000000..9bf5231e3 --- /dev/null +++ b/tests/mocks/database/db/driver.php @@ -0,0 +1,36 @@ +ci_db_driver = new $driver_class($config); + } + + /** + * Overloading method, emulate the actual driver method (multiple inheritance workaround) + */ + public function __call($method, $arguments) + { + if ( ! is_callable(array($this->ci_db_driver, $method))) + { + throw new BadMethodCallException($method. ' not exists or not implemented'); + } + + return call_user_func_array(array($this->ci_db_driver, $method), $arguments); + } +} + +class CI_DB extends CI_DB_Driver {} \ No newline at end of file diff --git a/tests/mocks/database/drivers/mysql.php b/tests/mocks/database/drivers/mysql.php new file mode 100644 index 000000000..34a74e2bf --- /dev/null +++ b/tests/mocks/database/drivers/mysql.php @@ -0,0 +1,16 @@ + Date: Wed, 4 Apr 2012 23:46:23 +0700 Subject: Fix sqlite issue on PHP 5.4 both for native and PDO driver --- not_exists.sqlite | 0 tests/mocks/database/config/ci_test.sqlite | 0 tests/mocks/database/config/pdo/sqlite.php | 20 ++++++++++++++++---- tests/mocks/database/db.php | 13 +++---------- 4 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 not_exists.sqlite delete mode 100644 tests/mocks/database/config/ci_test.sqlite diff --git a/not_exists.sqlite b/not_exists.sqlite new file mode 100644 index 000000000..e69de29bb diff --git a/tests/mocks/database/config/ci_test.sqlite b/tests/mocks/database/config/ci_test.sqlite deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/mocks/database/config/pdo/sqlite.php b/tests/mocks/database/config/pdo/sqlite.php index 1e5043b00..c68b4b213 100644 --- a/tests/mocks/database/config/pdo/sqlite.php +++ b/tests/mocks/database/config/pdo/sqlite.php @@ -4,20 +4,32 @@ return array( // Typical Database configuration 'pdo/sqlite' => array( - 'dsn' => 'sqlite:/'.realpath(__DIR__.'/..').'/ci_test.sqlite', + 'dsn' => 'sqlite:/'.realpath(__DIR__.'/../..').'/ci_test.sqlite', + 'hostname' => 'localhost', + 'username' => 'sqlite', + 'password' => 'sqlite', + 'database' => 'sqlite', 'dbdriver' => 'pdo', 'pdodriver' => 'sqlite', ), // Database configuration with failover 'pdo/sqlite_failover' => array( - 'dsn' => 'sqlite:/'.realpath(__DIR__.'/..').'/not_exists.sqlite', + 'dsn' => 'sqlite:not_exists.sqlite', + 'hostname' => 'localhost', + 'username' => 'sqlite', + 'password' => 'sqlite', + 'database' => 'sqlite', 'dbdriver' => 'pdo', 'pdodriver' => 'sqlite', 'failover' => array( array( - 'dsn' => 'sqlite:/'.realpath(__DIR__.'/..').'/ci_test.sqlite', - 'dbdriver' => 'pdo', + 'dsn' => 'sqlite:/'.realpath(__DIR__.'/../..').'/ci_test.sqlite', + 'hostname' => 'localhost', + 'username' => 'sqlite', + 'password' => 'sqlite', + 'database' => 'sqlite', + 'dbdriver' => 'pdo', 'pdodriver' => 'sqlite', ), ), diff --git a/tests/mocks/database/db.php b/tests/mocks/database/db.php index c30e6d2e6..59028ed9c 100644 --- a/tests/mocks/database/db.php +++ b/tests/mocks/database/db.php @@ -45,23 +45,16 @@ class Mock_Database_DB { ); $config = array_merge($this->config[$group], $params); + $dsnstring = ( ! empty($config['dsn'])) ? $config['dsn'] : FALSE; $pdodriver = ( ! empty($config['pdodriver'])) ? $config['pdodriver'] : FALSE; $failover = ( ! empty($config['failover'])) ? $config['failover'] : FALSE; - if ( ! empty($config['dsn'])) - { - $dsn = $config['dsn']; - } - else - { - $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password'] + $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password'] .'@'.$config['hostname'].'/'.$config['database']; - } - // Build the parameter $other_params = array_slice($config, 6); - $other_params['dsn'] = $dsn; + if ($dsnstring) $other_params['dsn'] = $dsnstring; if ($pdodriver) $other_params['pdodriver'] = $pdodriver; if ($failover) $other_params['failover'] = $failover; -- cgit v1.2.3-24-g4f1b From ffd7a0bcedbbac497c9987a3c9299b2edaa7c717 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Wed, 4 Apr 2012 23:47:40 +0700 Subject: Remove testing db --- not_exists.sqlite | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 not_exists.sqlite diff --git a/not_exists.sqlite b/not_exists.sqlite deleted file mode 100644 index e69de29bb..000000000 -- cgit v1.2.3-24-g4f1b From 26490e37f4bdb267b71431abd79782549cbbadda Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Wed, 4 Apr 2012 23:50:00 +0700 Subject: Add branch to travis list --- .travis.yml | 3 ++- tests/mocks/database/drivers/sqlite.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 97ea0422d..971f62f38 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,4 +25,5 @@ script: phpunit --configuration tests/travis/$DB.phpunit.xml branches: only: - develop - - master \ No newline at end of file + - master + - db-tests \ No newline at end of file diff --git a/tests/mocks/database/drivers/sqlite.php b/tests/mocks/database/drivers/sqlite.php index 49c68c50f..76a182cbf 100644 --- a/tests/mocks/database/drivers/sqlite.php +++ b/tests/mocks/database/drivers/sqlite.php @@ -11,6 +11,6 @@ class Mock_Database_Drivers_Sqlite extends Mock_Database_DB_Driver { */ public function __construct($config = array()) { - parent::__construct('CI_DB_sqlite_driver', $config); + parent::__construct('CI_DB_sqlite3_driver', $config); } } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 61ff054fb905f0514b942bf3e70a6c882bd64f7a Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Thu, 5 Apr 2012 01:23:56 +0700 Subject: Adding schema and initial Query Builder code coverage --- tests/codeigniter/database/query_builder/.gitkeep | 0 tests/mocks/autoloader.php | 4 ++-- tests/mocks/database/ci_test.sqlite | Bin 17408 -> 17408 bytes tests/mocks/database/config/sqlite.php | 6 +++--- tests/mocks/database/db/driver.php | 2 +- tests/mocks/database/schema/.gitkeep | 0 6 files changed, 6 insertions(+), 6 deletions(-) delete mode 100644 tests/codeigniter/database/query_builder/.gitkeep delete mode 100644 tests/mocks/database/schema/.gitkeep diff --git a/tests/codeigniter/database/query_builder/.gitkeep b/tests/codeigniter/database/query_builder/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php index 22448139e..f1bdb5d6f 100644 --- a/tests/mocks/autoloader.php +++ b/tests/mocks/autoloader.php @@ -62,7 +62,7 @@ function autoload($class) elseif (strpos($class, 'CI_DB') === 0) { $dir = BASEPATH.'database'.DIRECTORY_SEPARATOR; - $file = $dir.str_replace('CI_DB', 'DB', $subclass).'.php'; + $file = $dir.str_replace(array('CI_DB','active_record'), array('DB', 'active_rec'), $subclass).'.php'; } else { @@ -82,7 +82,7 @@ function autoload($class) { return FALSE; } - var_dump($file); + throw new InvalidArgumentException("Unable to load $class."); } diff --git a/tests/mocks/database/ci_test.sqlite b/tests/mocks/database/ci_test.sqlite index 37ce4f870..86d868af2 100755 Binary files a/tests/mocks/database/ci_test.sqlite and b/tests/mocks/database/ci_test.sqlite differ diff --git a/tests/mocks/database/config/sqlite.php b/tests/mocks/database/config/sqlite.php index c70986e7c..75fdacd96 100644 --- a/tests/mocks/database/config/sqlite.php +++ b/tests/mocks/database/config/sqlite.php @@ -9,7 +9,7 @@ return array( 'username' => 'sqlite', 'password' => 'sqlite', 'database' => realpath(__DIR__.'/..').'/ci_test.sqlite', - 'dbdriver' => is_php('5.4') ? 'sqlite3' : 'sqlite', + 'dbdriver' => 'sqlite3', ), // Database configuration with failover @@ -19,7 +19,7 @@ return array( 'username' => 'sqlite', 'password' => 'sqlite', 'database' => '../not_exists.sqlite', - 'dbdriver' => is_php('5.4') ? 'sqlite3' : 'sqlite', + 'dbdriver' => 'sqlite3', 'failover' => array( array( 'dsn' => '', @@ -27,7 +27,7 @@ return array( 'username' => 'sqlite', 'password' => 'sqlite', 'database' => realpath(__DIR__.'/..').'/ci_testf.sqlite', - 'dbdriver' => is_php('5.4') ? 'sqlite3' : 'sqlite', + 'dbdriver' => 'sqlite3', ), ), ), diff --git a/tests/mocks/database/db/driver.php b/tests/mocks/database/db/driver.php index 9bf5231e3..cb1820277 100644 --- a/tests/mocks/database/db/driver.php +++ b/tests/mocks/database/db/driver.php @@ -33,4 +33,4 @@ class Mock_Database_DB_Driver extends CI_DB_driver { } } -class CI_DB extends CI_DB_Driver {} \ No newline at end of file +class CI_DB extends Mock_Database_DB_QueryBuilder {} \ No newline at end of file diff --git a/tests/mocks/database/schema/.gitkeep b/tests/mocks/database/schema/.gitkeep deleted file mode 100644 index e69de29bb..000000000 -- cgit v1.2.3-24-g4f1b From 5b421665bac7609192917f7b0f34838534be099e Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Thu, 5 Apr 2012 01:24:50 +0700 Subject: Simple query builder tests --- .../database/query_builder/get_test.php | 48 ++++++++++++++++ tests/mocks/database/db/querybuilder.php | 10 ++++ tests/mocks/database/schema/skeleton.php | 65 ++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 tests/codeigniter/database/query_builder/get_test.php create mode 100644 tests/mocks/database/db/querybuilder.php create mode 100644 tests/mocks/database/schema/skeleton.php diff --git a/tests/codeigniter/database/query_builder/get_test.php b/tests/codeigniter/database/query_builder/get_test.php new file mode 100644 index 000000000..6f98ded4f --- /dev/null +++ b/tests/codeigniter/database/query_builder/get_test.php @@ -0,0 +1,48 @@ +set_dsn(DB_DRIVER), TRUE); + + $this->ci_instance_var('db', $db); + + $loader = new Mock_Core_Loader(); + $loader->dbforge(); + + $forge = $this->ci_instance->dbforge; + + Mock_Database_Schema_Skeleton::create_tables($forge); + Mock_Database_Schema_Skeleton::create_data($db); + + $this->query_builder = $db; + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_get_simple() + { + $jobs = $this->query_builder->get('job')->result_array(); + + // Dummy jobs contain 4 rows + $this->assertCount(4, $jobs); + + // Check rows item + $this->assertEquals('Developer', $jobs[0]['name']); + $this->assertEquals('Politician', $jobs[1]['name']); + $this->assertEquals('Accountant', $jobs[2]['name']); + $this->assertEquals('Musician', $jobs[3]['name']); + } + +} \ No newline at end of file diff --git a/tests/mocks/database/db/querybuilder.php b/tests/mocks/database/db/querybuilder.php new file mode 100644 index 000000000..1b95c92af --- /dev/null +++ b/tests/mocks/database/db/querybuilder.php @@ -0,0 +1,10 @@ +add_field(array( + 'id' => array( + 'type' => 'INT', + 'constraint' => 3, + ), + 'name' => array( + 'type' => 'VARCHAR', + 'constraint' => 40, + ), + 'description' => array( + 'type' => 'TEXT', + 'constraint' => 0, + ), + )); + $forge->add_key('id', TRUE); + $forge->create_table('job', TRUE); + } + + /** + * Create the dummy datas + * + * @return void + */ + public static function create_data($db) + { + // Job Data + $data = array( + 'job' => array( + array('id' => 1, 'name' => 'Developer', 'description' => 'Awesome job, but sometimes makes you bored'), + array('id' => 2, 'name' => 'Politician', 'description' => 'This is not really a job'), + array('id' => 3, 'name' => 'Accountant', 'description' => 'Boring job, but you will get free snack at lunch'), + array('id' => 4, 'name' => 'Musician', 'description' => 'Only Coldplay can actually called Musician'), + ), + ); + + foreach ($data as $table => $dummy_data) + { + $db->truncate($table); + + if (strpos(DB_DRIVER, 'sqlite') === FALSE) + { + $db->insert_batch($table, $dummy_data); + } + else + { + foreach ($dummy_data as $single_dummy_data) + { + $db->insert($table, $single_dummy_data); + } + } + } + } +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 55f804239cb0141fdac748607d8cdfa143efac92 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Thu, 5 Apr 2012 01:48:52 +0700 Subject: Add dsn to pgsql --- tests/mocks/database/config/pdo/pgsql.php | 4 ++-- tests/mocks/database/config/sqlite.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/mocks/database/config/pdo/pgsql.php b/tests/mocks/database/config/pdo/pgsql.php index b2f409d8d..5196e9ad9 100644 --- a/tests/mocks/database/config/pdo/pgsql.php +++ b/tests/mocks/database/config/pdo/pgsql.php @@ -4,7 +4,7 @@ return array( // Typical Database configuration 'pdo/pgsql' => array( - 'dsn' => '', + 'dsn' => 'pgsql:host=localhost;port=5432;dbname=ci_test;', 'hostname' => 'localhost', 'username' => 'postgres', 'password' => '', @@ -24,7 +24,7 @@ return array( 'pdodriver' => 'pgsql', 'failover' => array( array( - 'dsn' => '', + 'dsn' => 'pgsql:host=localhost;port=5432;dbname=ci_test;', 'hostname' => 'localhost', 'username' => 'postgres', 'password' => '', diff --git a/tests/mocks/database/config/sqlite.php b/tests/mocks/database/config/sqlite.php index 75fdacd96..755ce2a3a 100644 --- a/tests/mocks/database/config/sqlite.php +++ b/tests/mocks/database/config/sqlite.php @@ -26,7 +26,7 @@ return array( 'hostname' => 'localhost', 'username' => 'sqlite', 'password' => 'sqlite', - 'database' => realpath(__DIR__.'/..').'/ci_testf.sqlite', + 'database' => realpath(__DIR__.'/..').'/ci_test.sqlite', 'dbdriver' => 'sqlite3', ), ), -- cgit v1.2.3-24-g4f1b From 024098d05d00383bf82165e1f8bbadb53cbcaa9b Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Thu, 5 Apr 2012 01:51:57 +0700 Subject: Use more user-friendly method for dummy create data --- tests/mocks/database/schema/skeleton.php | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/tests/mocks/database/schema/skeleton.php b/tests/mocks/database/schema/skeleton.php index bf9e75b45..53df73bdd 100644 --- a/tests/mocks/database/schema/skeleton.php +++ b/tests/mocks/database/schema/skeleton.php @@ -48,17 +48,10 @@ class Mock_Database_Schema_Skeleton { foreach ($data as $table => $dummy_data) { $db->truncate($table); - - if (strpos(DB_DRIVER, 'sqlite') === FALSE) - { - $db->insert_batch($table, $dummy_data); - } - else + + foreach ($dummy_data as $single_dummy_data) { - foreach ($dummy_data as $single_dummy_data) - { - $db->insert($table, $single_dummy_data); - } + $db->insert($table, $single_dummy_data); } } } -- cgit v1.2.3-24-g4f1b From 1bfc0d9a2b47be1a2a0b8efda60ebdd9d7a303dd Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Thu, 5 Apr 2012 02:25:25 +0700 Subject: Fix postgre table datatype --- tests/mocks/database/schema/skeleton.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/mocks/database/schema/skeleton.php b/tests/mocks/database/schema/skeleton.php index 53df73bdd..47e8bfd35 100644 --- a/tests/mocks/database/schema/skeleton.php +++ b/tests/mocks/database/schema/skeleton.php @@ -12,7 +12,7 @@ class Mock_Database_Schema_Skeleton { // Job Table $forge->add_field(array( 'id' => array( - 'type' => 'INT', + 'type' => 'INTEGER', 'constraint' => 3, ), 'name' => array( @@ -21,11 +21,11 @@ class Mock_Database_Schema_Skeleton { ), 'description' => array( 'type' => 'TEXT', - 'constraint' => 0, ), )); $forge->add_key('id', TRUE); - $forge->create_table('job', TRUE); + $res = $forge->create_table('job'); + var_dump($res); } /** -- cgit v1.2.3-24-g4f1b From b0703b0bee89ab0eae83bae78daad5c5a3504043 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Thu, 5 Apr 2012 02:31:52 +0700 Subject: Resolve postgres issue on create table --- tests/mocks/database/schema/skeleton.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/mocks/database/schema/skeleton.php b/tests/mocks/database/schema/skeleton.php index 47e8bfd35..6610d6b59 100644 --- a/tests/mocks/database/schema/skeleton.php +++ b/tests/mocks/database/schema/skeleton.php @@ -7,7 +7,7 @@ class Mock_Database_Schema_Skeleton { * * @return void */ - public static function create_tables($forge) + public static function create_tables($forge, $driver) { // Job Table $forge->add_field(array( @@ -24,8 +24,7 @@ class Mock_Database_Schema_Skeleton { ), )); $forge->add_key('id', TRUE); - $res = $forge->create_table('job'); - var_dump($res); + $forge->create_table('job', (strpos($driver, 'pgsql') === FALSE)); } /** -- cgit v1.2.3-24-g4f1b From 333ce0db32865d1e6d6a96207e5c9306bd40c676 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Thu, 5 Apr 2012 02:34:06 +0700 Subject: Fixed schema --- tests/codeigniter/database/query_builder/get_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/codeigniter/database/query_builder/get_test.php b/tests/codeigniter/database/query_builder/get_test.php index 6f98ded4f..e6fe319c5 100644 --- a/tests/codeigniter/database/query_builder/get_test.php +++ b/tests/codeigniter/database/query_builder/get_test.php @@ -20,7 +20,7 @@ class Get_test extends CI_TestCase { $forge = $this->ci_instance->dbforge; - Mock_Database_Schema_Skeleton::create_tables($forge); + Mock_Database_Schema_Skeleton::create_tables($forge, DB_DRIVER); Mock_Database_Schema_Skeleton::create_data($db); $this->query_builder = $db; -- cgit v1.2.3-24-g4f1b From fd24fa614885d6c89e19aecb09ae97f95dd3db57 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Thu, 5 Apr 2012 02:42:46 +0700 Subject: Remove temporary line --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 971f62f38..97ea0422d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,5 +25,4 @@ script: phpunit --configuration tests/travis/$DB.phpunit.xml branches: only: - develop - - master - - db-tests \ No newline at end of file + - master \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 36897fba4c825e612e3e7d772174c95093aa7f96 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Thu, 5 Apr 2012 18:28:27 +0700 Subject: Test last upstream/develop --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 97ea0422d..a56f7b687 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,4 +25,5 @@ script: phpunit --configuration tests/travis/$DB.phpunit.xml branches: only: - develop - - master \ No newline at end of file + - master + - db-tests -- cgit v1.2.3-24-g4f1b From 55f622f76371949ae615ccdff76ed1e4bab55170 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Thu, 5 Apr 2012 19:01:34 +0700 Subject: Add select clause test --- .../database/query_builder/get_test.php | 31 +++++++----- .../database/query_builder/select_test.php | 37 +++++++++++++++ tests/mocks/database/schema/skeleton.php | 55 +++++++++++++++++++--- 3 files changed, 104 insertions(+), 19 deletions(-) create mode 100644 tests/codeigniter/database/query_builder/select_test.php diff --git a/tests/codeigniter/database/query_builder/get_test.php b/tests/codeigniter/database/query_builder/get_test.php index e6fe319c5..dd946a40c 100644 --- a/tests/codeigniter/database/query_builder/get_test.php +++ b/tests/codeigniter/database/query_builder/get_test.php @@ -9,19 +9,10 @@ class Get_test extends CI_TestCase { public function set_up() { - $config = Mock_Database_DB::config(DB_DRIVER); - $connection = new Mock_Database_DB($config); - $db = Mock_Database_DB::DB($connection->set_dsn(DB_DRIVER), TRUE); + $db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); - $this->ci_instance_var('db', $db); - - $loader = new Mock_Core_Loader(); - $loader->dbforge(); - - $forge = $this->ci_instance->dbforge; - - Mock_Database_Schema_Skeleton::create_tables($forge, DB_DRIVER); - Mock_Database_Schema_Skeleton::create_data($db); + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); $this->query_builder = $db; } @@ -44,5 +35,21 @@ class Get_test extends CI_TestCase { $this->assertEquals('Accountant', $jobs[2]['name']); $this->assertEquals('Musician', $jobs[3]['name']); } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_get_where() + { + $job1 = $this->query_builder->get('job', array('id' => 1))->result_array(); + + // Dummy jobs contain 1 rows + $this->assertCount(1, $job1); + + // Check rows item + $this->assertEquals('Developer', $job1[0]['name']); + } } \ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/select_test.php b/tests/codeigniter/database/query_builder/select_test.php new file mode 100644 index 000000000..ba2f21b64 --- /dev/null +++ b/tests/codeigniter/database/query_builder/select_test.php @@ -0,0 +1,37 @@ +query_builder = $db; + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_select_only_one_collumn() + { + $jobs_name = $this->query_builder->select('name') + ->get('job') + ->result_array(); + + // Check rows item + $this->assertArrayHasKey('name',$jobs_name[0]); + $this->assertFalse(array_key_exists('id', $jobs_name[0])); + $this->assertFalse(array_key_exists('description', $jobs_name[0])); + } + +} \ No newline at end of file diff --git a/tests/mocks/database/schema/skeleton.php b/tests/mocks/database/schema/skeleton.php index 6610d6b59..a3d5bac65 100644 --- a/tests/mocks/database/schema/skeleton.php +++ b/tests/mocks/database/schema/skeleton.php @@ -1,16 +1,57 @@ set_dsn($driver), TRUE); + + CI_TestCase::instance()->ci_instance_var('db', $db); + + $loader = new Mock_Core_Loader(); + $loader->dbforge(); + $forge = CI_TestCase::instance()->ci_instance_var('dbforge'); + + static::$db = $db; + static::$forge = $forge; + static::$driver = $driver; + } + + return static::$db; + } + /** * Create the dummy tables * * @return void */ - public static function create_tables($forge, $driver) + public static function create_tables() { // Job Table - $forge->add_field(array( + static::$forge->add_field(array( 'id' => array( 'type' => 'INTEGER', 'constraint' => 3, @@ -23,8 +64,8 @@ class Mock_Database_Schema_Skeleton { 'type' => 'TEXT', ), )); - $forge->add_key('id', TRUE); - $forge->create_table('job', (strpos($driver, 'pgsql') === FALSE)); + static::$forge->add_key('id', TRUE); + static::$forge->create_table('job', (strpos(static::$driver, 'pgsql') === FALSE)); } /** @@ -32,7 +73,7 @@ class Mock_Database_Schema_Skeleton { * * @return void */ - public static function create_data($db) + public static function create_data() { // Job Data $data = array( @@ -46,11 +87,11 @@ class Mock_Database_Schema_Skeleton { foreach ($data as $table => $dummy_data) { - $db->truncate($table); + static::$db->truncate($table); foreach ($dummy_data as $single_dummy_data) { - $db->insert($table, $single_dummy_data); + static::$db->insert($table, $single_dummy_data); } } } -- cgit v1.2.3-24-g4f1b From fdc0ea8802c48a5f74df97c013f683d4b264218a Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Thu, 5 Apr 2012 19:08:00 +0700 Subject: Min, Max, Avg and Sum --- .../database/query_builder/select_test.php | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/codeigniter/database/query_builder/select_test.php b/tests/codeigniter/database/query_builder/select_test.php index ba2f21b64..50a062c9f 100644 --- a/tests/codeigniter/database/query_builder/select_test.php +++ b/tests/codeigniter/database/query_builder/select_test.php @@ -33,5 +33,65 @@ class Select_test extends CI_TestCase { $this->assertFalse(array_key_exists('id', $jobs_name[0])); $this->assertFalse(array_key_exists('description', $jobs_name[0])); } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_select_min() + { + $job_min = $this->query_builder->select_min('id') + ->get('job') + ->result_array(); + + // Minimum id was 1 + $this->assertEquals('1', $job_min[0]['id']); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_select_max() + { + $job_max = $this->query_builder->select_max('id') + ->get('job') + ->result_array(); + + // Maximum id was 4 + $this->assertEquals('4', $job_max[0]['id']); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_select_avg() + { + $job_avg = $this->query_builder->select_avg('id') + ->get('job') + ->result_array(); + + // Average should be 2.5 + $this->assertEquals('2.5', $job_avg[0]['id']); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_select_sum() + { + $job_sum = $this->query_builder->select_sum('id') + ->get('job') + ->result_array(); + + // Sum of ids should be 10 + $this->assertEquals('10', $job_sum[0]['id']); + } } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From bf0ce689fae71a22abfaf1854c954f7a3b467d20 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Thu, 5 Apr 2012 19:09:29 +0700 Subject: Remove temporary branch from travis yml --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index a56f7b687..97ea0422d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,5 +25,4 @@ script: phpunit --configuration tests/travis/$DB.phpunit.xml branches: only: - develop - - master - - db-tests + - master \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 0793b1c918a0146920b1a03323f055521c4a9c66 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Thu, 5 Apr 2012 21:47:53 +0700 Subject: Rename to db, for more familiar naming convention --- .../database/query_builder/get_test.php | 10 +++--- .../database/query_builder/select_test.php | 36 ++++++++++------------ 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/tests/codeigniter/database/query_builder/get_test.php b/tests/codeigniter/database/query_builder/get_test.php index dd946a40c..0751c9332 100644 --- a/tests/codeigniter/database/query_builder/get_test.php +++ b/tests/codeigniter/database/query_builder/get_test.php @@ -5,16 +5,14 @@ class Get_test extends CI_TestCase { /** * @var object Database/Query Builder holder */ - protected $query_builder; + protected $db; public function set_up() { - $db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); Mock_Database_Schema_Skeleton::create_tables(); Mock_Database_Schema_Skeleton::create_data(); - - $this->query_builder = $db; } // ------------------------------------------------------------------------ @@ -24,7 +22,7 @@ class Get_test extends CI_TestCase { */ public function test_get_simple() { - $jobs = $this->query_builder->get('job')->result_array(); + $jobs = $this->db->get('job')->result_array(); // Dummy jobs contain 4 rows $this->assertCount(4, $jobs); @@ -43,7 +41,7 @@ class Get_test extends CI_TestCase { */ public function test_get_where() { - $job1 = $this->query_builder->get('job', array('id' => 1))->result_array(); + $job1 = $this->db->get('job', array('id' => 1))->result_array(); // Dummy jobs contain 1 rows $this->assertCount(1, $job1); diff --git a/tests/codeigniter/database/query_builder/select_test.php b/tests/codeigniter/database/query_builder/select_test.php index 50a062c9f..dbf432a7c 100644 --- a/tests/codeigniter/database/query_builder/select_test.php +++ b/tests/codeigniter/database/query_builder/select_test.php @@ -5,16 +5,14 @@ class Select_test extends CI_TestCase { /** * @var object Database/Query Builder holder */ - protected $query_builder; + protected $db; public function set_up() { - $db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); Mock_Database_Schema_Skeleton::create_tables(); Mock_Database_Schema_Skeleton::create_data(); - - $this->query_builder = $db; } // ------------------------------------------------------------------------ @@ -24,9 +22,9 @@ class Select_test extends CI_TestCase { */ public function test_select_only_one_collumn() { - $jobs_name = $this->query_builder->select('name') - ->get('job') - ->result_array(); + $jobs_name = $this->db->select('name') + ->get('job') + ->result_array(); // Check rows item $this->assertArrayHasKey('name',$jobs_name[0]); @@ -41,9 +39,9 @@ class Select_test extends CI_TestCase { */ public function test_select_min() { - $job_min = $this->query_builder->select_min('id') - ->get('job') - ->result_array(); + $job_min = $this->db->select_min('id') + ->get('job') + ->result_array(); // Minimum id was 1 $this->assertEquals('1', $job_min[0]['id']); @@ -56,9 +54,9 @@ class Select_test extends CI_TestCase { */ public function test_select_max() { - $job_max = $this->query_builder->select_max('id') - ->get('job') - ->result_array(); + $job_max = $this->db->select_max('id') + ->get('job') + ->result_array(); // Maximum id was 4 $this->assertEquals('4', $job_max[0]['id']); @@ -71,9 +69,9 @@ class Select_test extends CI_TestCase { */ public function test_select_avg() { - $job_avg = $this->query_builder->select_avg('id') - ->get('job') - ->result_array(); + $job_avg = $this->db->select_avg('id') + ->get('job') + ->result_array(); // Average should be 2.5 $this->assertEquals('2.5', $job_avg[0]['id']); @@ -86,9 +84,9 @@ class Select_test extends CI_TestCase { */ public function test_select_sum() { - $job_sum = $this->query_builder->select_sum('id') - ->get('job') - ->result_array(); + $job_sum = $this->db->select_sum('id') + ->get('job') + ->result_array(); // Sum of ids should be 10 $this->assertEquals('10', $job_sum[0]['id']); -- cgit v1.2.3-24-g4f1b From c0df5a231a28c6038f0a8ab702e090ce0e12b5a5 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Fri, 6 Apr 2012 17:35:12 +0700 Subject: Include insert test --- .travis.yml | 3 +- .../database/query_builder/insert_test.php | 47 ++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/codeigniter/database/query_builder/insert_test.php diff --git a/.travis.yml b/.travis.yml index 97ea0422d..971f62f38 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,4 +25,5 @@ script: phpunit --configuration tests/travis/$DB.phpunit.xml branches: only: - develop - - master \ No newline at end of file + - master + - db-tests \ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/insert_test.php b/tests/codeigniter/database/query_builder/insert_test.php new file mode 100644 index 000000000..5607e8ce1 --- /dev/null +++ b/tests/codeigniter/database/query_builder/insert_test.php @@ -0,0 +1,47 @@ +db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_insert() + { + $job_data = array('name' => 'Grocery Sales', 'description' => 'Discount!'); + + // Do normal insert + $this->assertTrue($this->db->insert('job', $job_data)); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_insert_batch() + { + $job_datas = array( + array('name' => 'Commedian', 'description' => 'Theres something in your teeth'), + array('name' => 'Cab Driver', 'description' => 'Iam yellow'), + ); + + // Do insert batch + $this->assertTrue($this->db->insert_batch('job', $job_datas)); + } + +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 4d759fbb102b0754d7ff915458e27600bdc99d1d Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Fri, 6 Apr 2012 17:44:40 +0700 Subject: Remove temporary branch from build list --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 971f62f38..97ea0422d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,5 +25,4 @@ script: phpunit --configuration tests/travis/$DB.phpunit.xml branches: only: - develop - - master - - db-tests \ No newline at end of file + - master \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 81b6b5d4ea2e63159c443bae19880a78f6ac2f9f Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Fri, 6 Apr 2012 17:58:22 +0700 Subject: Insert test --- .travis.yml | 3 ++- tests/codeigniter/database/query_builder/insert_test.php | 7 +++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 97ea0422d..971f62f38 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,4 +25,5 @@ script: phpunit --configuration tests/travis/$DB.phpunit.xml branches: only: - develop - - master \ No newline at end of file + - master + - db-tests \ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/insert_test.php b/tests/codeigniter/database/query_builder/insert_test.php index 5607e8ce1..80073b9ba 100644 --- a/tests/codeigniter/database/query_builder/insert_test.php +++ b/tests/codeigniter/database/query_builder/insert_test.php @@ -12,7 +12,6 @@ class Insert_test extends CI_TestCase { $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); Mock_Database_Schema_Skeleton::create_tables(); - Mock_Database_Schema_Skeleton::create_data(); } // ------------------------------------------------------------------------ @@ -22,7 +21,7 @@ class Insert_test extends CI_TestCase { */ public function test_insert() { - $job_data = array('name' => 'Grocery Sales', 'description' => 'Discount!'); + $job_data = array('id' => 1, 'name' => 'Grocery Sales', 'description' => 'Discount!'); // Do normal insert $this->assertTrue($this->db->insert('job', $job_data)); @@ -36,8 +35,8 @@ class Insert_test extends CI_TestCase { public function test_insert_batch() { $job_datas = array( - array('name' => 'Commedian', 'description' => 'Theres something in your teeth'), - array('name' => 'Cab Driver', 'description' => 'Iam yellow'), + array('id' => 2, 'name' => 'Commedian', 'description' => 'Theres something in your teeth'), + array('id' => 3, 'name' => 'Cab Driver', 'description' => 'Iam yellow'), ); // Do insert batch -- cgit v1.2.3-24-g4f1b From fc92fc2e4f262a3607898ca7cd8b361d95f09136 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Fri, 6 Apr 2012 18:00:55 +0700 Subject: Add truncate --- tests/codeigniter/database/query_builder/insert_test.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/codeigniter/database/query_builder/insert_test.php b/tests/codeigniter/database/query_builder/insert_test.php index 80073b9ba..78587d2e9 100644 --- a/tests/codeigniter/database/query_builder/insert_test.php +++ b/tests/codeigniter/database/query_builder/insert_test.php @@ -4,6 +4,7 @@ class Insert_test extends CI_TestCase { /** * @var object Database/Query Builder holder + * @see ./mocks/schema/skeleton.php */ protected $db; @@ -12,6 +13,9 @@ class Insert_test extends CI_TestCase { $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); Mock_Database_Schema_Skeleton::create_tables(); + + // Truncate the current datas + $this->db->truncate('job'); } // ------------------------------------------------------------------------ -- cgit v1.2.3-24-g4f1b From c0dd3986b5cf45dcf9de42d761b9641836bc6c93 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Fri, 6 Apr 2012 18:05:10 +0700 Subject: Remove temporary branch from build list --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 971f62f38..97ea0422d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,5 +25,4 @@ script: phpunit --configuration tests/travis/$DB.phpunit.xml branches: only: - develop - - master - - db-tests \ No newline at end of file + - master \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 80c0e3df5998a2a4b2376a6361f4690b783446df Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Sat, 7 Apr 2012 01:15:53 +0700 Subject: Check the insert result --- tests/codeigniter/database/query_builder/insert_test.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/codeigniter/database/query_builder/insert_test.php b/tests/codeigniter/database/query_builder/insert_test.php index 78587d2e9..5dfccce82 100644 --- a/tests/codeigniter/database/query_builder/insert_test.php +++ b/tests/codeigniter/database/query_builder/insert_test.php @@ -29,6 +29,12 @@ class Insert_test extends CI_TestCase { // Do normal insert $this->assertTrue($this->db->insert('job', $job_data)); + + $job_1 = $this->db->get('job')->row(); + + // Check the result + $this->assertEquals('Grocery Sales', $job_1->name); + } // ------------------------------------------------------------------------ @@ -45,6 +51,11 @@ class Insert_test extends CI_TestCase { // Do insert batch $this->assertTrue($this->db->insert_batch('job', $job_datas)); + + $job_2 = $this->db->get_where('job', array('id' => 2))->row(); + + // Check the result + $this->assertEquals('Commedian', $job_2->name); } } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 82e88b9b3031163d2f86f8e53450c1d779a08d8f Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 6 Apr 2012 21:18:59 +0300 Subject: Update _delete() and _update() with removal of unsupported LIMIT and ORDER BY --- system/database/drivers/postgre/postgre_driver.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index a5e296718..707aaaebe 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -493,11 +493,9 @@ 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 */ - protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) + protected function _update($table, $values, $where) { foreach ($values as $key => $val) { @@ -505,9 +503,7 @@ class CI_DB_postgre_driver extends CI_DB { } return 'UPDATE '.$table.' SET '.implode(', ', $valstr) - .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '') - .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '') - .( ! $limit ? '' : ' LIMIT '.$limit); + .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : ''); } // -------------------------------------------------------------------- @@ -519,10 +515,9 @@ class CI_DB_postgre_driver extends CI_DB { * * @param string the table name * @param array the where clause - * @param string the limit clause * @return string */ - protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table, $where = array(), $like = array()) { if (count($where) > 0 OR count($like) > 0) { @@ -539,6 +534,7 @@ class CI_DB_postgre_driver extends CI_DB { } // -------------------------------------------------------------------- + /** * Limit string * -- cgit v1.2.3-24-g4f1b From 8e160e471259ad01c0e994e7ce5797c2a51afd7a Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 6 Apr 2012 21:27:38 +0300 Subject: Fix changelog --- user_guide_src/source/changelog.rst | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 665eba2a8..db3a57a4b 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -80,9 +80,6 @@ Release Date: Not Released - db_set_charset() now only requires one parameter (collation was only needed due to legacy support for MySQL versions prior to 5.1). - Added DSN string support for CUBRID. - Added persistent connections support for CUBRID. -<<<<<<< HEAD - - CUBRID driver's utility class now overrides list_databases() in order to use cubrid_list_dbs(). -======= - Added random ordering support for MSSQL. - Added random ordering support for SQLSRV. - Added support for SQLite3 database driver. @@ -95,7 +92,7 @@ Release Date: Not Released - num_rows() is now only called explicitly by the developer and no longer re-executes statements. - Added replace() support for SQLite. - Renamed internal method _escape_identifiers() to escape_identifiers(). ->>>>>>> ea09a8a5552f2aacdeab0c88a605fe44047ebd0a + - CUBRID driver's utility class now overrides list_databases() in order to use cubrid_list_dbs(). - Libraries -- cgit v1.2.3-24-g4f1b From 6a91a0482620212f091e14301a1a8889571d969f Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Sat, 7 Apr 2012 02:09:11 +0700 Subject: Update reference and do the tests --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 97ea0422d..971f62f38 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,4 +25,5 @@ script: phpunit --configuration tests/travis/$DB.phpunit.xml branches: only: - develop - - master \ No newline at end of file + - master + - db-tests \ No newline at end of file -- cgit v1.2.3-24-g4f1b From d489747059db6a91d5532f35437a812a81f2b98b Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Sat, 7 Apr 2012 03:16:25 +0700 Subject: Remove batch test from sqlite --- .../database/query_builder/insert_test.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/tests/codeigniter/database/query_builder/insert_test.php b/tests/codeigniter/database/query_builder/insert_test.php index 5dfccce82..b86feeb74 100644 --- a/tests/codeigniter/database/query_builder/insert_test.php +++ b/tests/codeigniter/database/query_builder/insert_test.php @@ -30,10 +30,11 @@ class Insert_test extends CI_TestCase { // Do normal insert $this->assertTrue($this->db->insert('job', $job_data)); - $job_1 = $this->db->get('job')->row(); + $jobs = $this->db->get('job')->result_array(); + $job1 = $jobs[0]; // Check the result - $this->assertEquals('Grocery Sales', $job_1->name); + $this->assertEquals('Grocery Sales', $job1['name']); } @@ -49,13 +50,18 @@ class Insert_test extends CI_TestCase { array('id' => 3, 'name' => 'Cab Driver', 'description' => 'Iam yellow'), ); - // Do insert batch - $this->assertTrue($this->db->insert_batch('job', $job_datas)); + // Do insert batch except for sqlite driver + if (strpos(DB_DRIVER, 'sqlite') === FALSE) + { + $this->assertTrue($this->db->insert('job', $job_datas[0])); - $job_2 = $this->db->get_where('job', array('id' => 2))->row(); + $job_2 = $this->db->where('id', 2)->get('job')->row(); + $job_3 = $this->db->where('id', 3)->get('job')->row(); - // Check the result - $this->assertEquals('Commedian', $job_2->name); + // Check the result + $this->assertEquals('Commedian', $job_2->name); + $this->assertEquals('Cab Driver', $job_3->name); + } } } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 5ce26c912f11fb859e661a43a6667fba5a5a3ca2 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Sat, 7 Apr 2012 03:19:35 +0700 Subject: Typo --- tests/codeigniter/database/query_builder/insert_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/codeigniter/database/query_builder/insert_test.php b/tests/codeigniter/database/query_builder/insert_test.php index b86feeb74..53ce23c19 100644 --- a/tests/codeigniter/database/query_builder/insert_test.php +++ b/tests/codeigniter/database/query_builder/insert_test.php @@ -53,7 +53,7 @@ class Insert_test extends CI_TestCase { // Do insert batch except for sqlite driver if (strpos(DB_DRIVER, 'sqlite') === FALSE) { - $this->assertTrue($this->db->insert('job', $job_datas[0])); + $this->assertTrue($this->db->insert_batch('job', $job_datas)); $job_2 = $this->db->where('id', 2)->get('job')->row(); $job_3 = $this->db->where('id', 3)->get('job')->row(); -- cgit v1.2.3-24-g4f1b From d115f8327a8f16d957cc4e0876225037bb2f5868 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Sat, 7 Apr 2012 03:22:39 +0700 Subject: Remove temporary branch from build list --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 971f62f38..97ea0422d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,5 +25,4 @@ script: phpunit --configuration tests/travis/$DB.phpunit.xml branches: only: - develop - - master - - db-tests \ No newline at end of file + - master \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 918be7963f6fa3461d612ea7ca4c9774c12f9da5 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 9 Apr 2012 16:29:01 +0300 Subject: Add a parameter to SQLite3 and PDO _close() due to CI_DB_driver being abstract --- system/database/drivers/pdo/pdo_driver.php | 3 ++- system/database/drivers/sqlite3/sqlite3_driver.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/system/database/drivers/pdo/pdo_driver.php b/system/database/drivers/pdo/pdo_driver.php index 45f8cc1fb..60151b900 100644 --- a/system/database/drivers/pdo/pdo_driver.php +++ b/system/database/drivers/pdo/pdo_driver.php @@ -672,9 +672,10 @@ class CI_DB_pdo_driver extends CI_DB { /** * Close DB Connection * + * @param object * @return void */ - protected function _close() + protected function _close($conn_id) { $this->conn_id = NULL; } diff --git a/system/database/drivers/sqlite3/sqlite3_driver.php b/system/database/drivers/sqlite3/sqlite3_driver.php index 49cfbe7ec..fb45bee9c 100644 --- a/system/database/drivers/sqlite3/sqlite3_driver.php +++ b/system/database/drivers/sqlite3/sqlite3_driver.php @@ -417,9 +417,10 @@ class CI_DB_sqlite3_driver extends CI_DB { /** * Close DB Connection * + * @param object (ignored) * @return void */ - protected function _close() + protected function _close($conn_id) { $this->conn_id->close(); } -- cgit v1.2.3-24-g4f1b From 416f360a19118c4692e430f0a1f5d12f81876ee7 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 9 Apr 2012 16:32:25 +0300 Subject: Fix changelog merge conflict --- user_guide_src/source/changelog.rst | 4 ---- 1 file changed, 4 deletions(-) diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 6b47437d6..8146a5139 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -90,15 +90,11 @@ Release Date: Not Released - num_rows() is now only called explicitly by the developer and no longer re-executes statements. - Added replace() support for SQLite. - Renamed internal method _escape_identifiers() to escape_identifiers(). -<<<<<<< HEAD - - CUBRID driver's utility class now overrides list_databases() in order to use cubrid_list_dbs(). -======= - Added SQLite support for drop_table() in :doc:`Database Forge `. - Added ODBC support for create_database(), drop_database() and drop_table() in :doc:`Database Forge `. - Added PDO support for create_database(), drop_database and drop_table() in :doc:`Database Forge `. - Added MSSQL, SQLSRV support for optimize_table() in :doc:`Database Utility `. - Improved CUBRID support for list_databases() in :doc:`Database Utility ` (until now only the currently used database was returned). ->>>>>>> 918be7963f6fa3461d612ea7ca4c9774c12f9da5 - Libraries -- cgit v1.2.3-24-g4f1b From fb61edcf8fb31f91cf53527604446be7dd5bd4be Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 10 Apr 2012 14:04:12 +0300 Subject: Add back an unused argument to _delete() due to CI_DB_driver being abstract --- system/database/drivers/postgre/postgre_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index 2a842848c..93d4c6bf5 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -528,7 +528,7 @@ class CI_DB_postgre_driver extends CI_DB { * @param string the limit clause (ignored) * @return string */ - protected function _delete($table, $where = array(), $like = array()) + protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) { $conditions = array(); -- cgit v1.2.3-24-g4f1b From 8f3566fffcb9015ba53dbf52922fc5724ffa6045 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 12 Apr 2012 14:30:05 +0300 Subject: Changed default CI_DB_result::_data_seek() return value to FALSE and removed the method from drivers that don't support it --- system/database/DB_result.php | 2 +- system/database/drivers/cubrid/cubrid_result.php | 2 +- system/database/drivers/interbase/interbase_result.php | 17 ----------------- system/database/drivers/mssql/mssql_result.php | 4 ++-- system/database/drivers/mysql/mysql_result.php | 2 +- system/database/drivers/mysqli/mysqli_result.php | 4 ++-- system/database/drivers/oci8/oci8_result.php | 4 ---- system/database/drivers/odbc/odbc_result.php | 16 ---------------- system/database/drivers/pdo/pdo_result.php | 17 ----------------- system/database/drivers/postgre/postgre_result.php | 2 +- system/database/drivers/sqlite/sqlite_result.php | 2 +- system/database/drivers/sqlite3/sqlite3_result.php | 4 ---- system/database/drivers/sqlsrv/sqlsrv_result.php | 16 ---------------- 13 files changed, 9 insertions(+), 83 deletions(-) diff --git a/system/database/DB_result.php b/system/database/DB_result.php index bb09c014c..196febe2c 100644 --- a/system/database/DB_result.php +++ b/system/database/DB_result.php @@ -384,7 +384,7 @@ class CI_DB_result { public function list_fields() { return array(); } public function field_data() { return array(); } public function free_result() { $this->result_id = FALSE; } - protected function _data_seek() { return TRUE; } + protected function _data_seek() { return FALSE; } protected function _fetch_assoc() { return array(); } protected function _fetch_object() { return array(); } diff --git a/system/database/drivers/cubrid/cubrid_result.php b/system/database/drivers/cubrid/cubrid_result.php index 6a61a213d..58dcff29a 100644 --- a/system/database/drivers/cubrid/cubrid_result.php +++ b/system/database/drivers/cubrid/cubrid_result.php @@ -163,7 +163,7 @@ class CI_DB_cubrid_result extends CI_DB_result { * this internally before fetching results to make sure the * result set starts at zero * - * @return array + * @return bool */ protected function _data_seek($n = 0) { diff --git a/system/database/drivers/interbase/interbase_result.php b/system/database/drivers/interbase/interbase_result.php index fd4178dec..5ddb6fa47 100644 --- a/system/database/drivers/interbase/interbase_result.php +++ b/system/database/drivers/interbase/interbase_result.php @@ -130,23 +130,6 @@ class CI_DB_interbase_result extends CI_DB_result { // -------------------------------------------------------------------- - /** - * 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 - * - * @return array - */ - protected function _data_seek($n = 0) - { - // Interbase driver doesn't implement a suitable function - return FALSE; - } - - // -------------------------------------------------------------------- - /** * Result - associative array * diff --git a/system/database/drivers/mssql/mssql_result.php b/system/database/drivers/mssql/mssql_result.php index 2723f4614..4cc87f4cf 100644 --- a/system/database/drivers/mssql/mssql_result.php +++ b/system/database/drivers/mssql/mssql_result.php @@ -126,11 +126,11 @@ class CI_DB_mssql_result extends CI_DB_result { /** * Data Seek * - * Moves the internal pointer to the desired offset. We call + * Moves the internal pointer to the desired offset. We call * this internally before fetching results to make sure the * result set starts at zero * - * @return array + * @return bool */ protected function _data_seek($n = 0) { diff --git a/system/database/drivers/mysql/mysql_result.php b/system/database/drivers/mysql/mysql_result.php index f76076f4c..14d6d072a 100644 --- a/system/database/drivers/mysql/mysql_result.php +++ b/system/database/drivers/mysql/mysql_result.php @@ -128,7 +128,7 @@ class CI_DB_mysql_result extends CI_DB_result { * this internally before fetching results to make sure the * result set starts at zero * - * @return array + * @return bool */ protected function _data_seek($n = 0) { diff --git a/system/database/drivers/mysqli/mysqli_result.php b/system/database/drivers/mysqli/mysqli_result.php index cf0362217..9b4d494d4 100644 --- a/system/database/drivers/mysqli/mysqli_result.php +++ b/system/database/drivers/mysqli/mysqli_result.php @@ -125,11 +125,11 @@ class CI_DB_mysqli_result extends CI_DB_result { /** * Data Seek * - * Moves the internal pointer to the desired offset. We call + * Moves the internal pointer to the desired offset. We call * this internally before fetching results to make sure the * result set starts at zero * - * @return array + * @return bool */ protected function _data_seek($n = 0) { diff --git a/system/database/drivers/oci8/oci8_result.php b/system/database/drivers/oci8/oci8_result.php index aad24cfd9..7b05e0a43 100644 --- a/system/database/drivers/oci8/oci8_result.php +++ b/system/database/drivers/oci8/oci8_result.php @@ -244,8 +244,6 @@ class CI_DB_oci8_result extends CI_DB_result { $this->row_data[$row_index++] = $row; } - // Un-comment the following line, in case it becomes needed - // $this->_data_seek(); return $this->result_array = $this->row_data; } @@ -299,8 +297,6 @@ class CI_DB_oci8_result extends CI_DB_result { $this->result_object[$row_index++] = $row; } - // Un-comment the following line, in case it becomes needed - // $this->_data_seek(); return $this->result_object; } diff --git a/system/database/drivers/odbc/odbc_result.php b/system/database/drivers/odbc/odbc_result.php index 30cc979ce..ecba5977e 100644 --- a/system/database/drivers/odbc/odbc_result.php +++ b/system/database/drivers/odbc/odbc_result.php @@ -139,22 +139,6 @@ class CI_DB_odbc_result extends CI_DB_result { // -------------------------------------------------------------------- - /** - * 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 - * - * @return bool - */ - protected function _data_seek($n = 0) - { - return FALSE; - } - - // -------------------------------------------------------------------- - /** * Result - associative array * diff --git a/system/database/drivers/pdo/pdo_result.php b/system/database/drivers/pdo/pdo_result.php index 5bbd85d75..19aee1dfc 100644 --- a/system/database/drivers/pdo/pdo_result.php +++ b/system/database/drivers/pdo/pdo_result.php @@ -92,7 +92,6 @@ class CI_DB_pdo_result extends CI_DB_result { $res_handler = 'result_'.$type; $this->$res_handler = array(); - $this->_data_seek(0); while ($row = $this->$res_method()) { @@ -227,22 +226,6 @@ class CI_DB_pdo_result extends CI_DB_result { // -------------------------------------------------------------------- - /** - * 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 - * - * @return bool - */ - protected function _data_seek($n = 0) - { - return FALSE; - } - - // -------------------------------------------------------------------- - /** * Result - associative array * diff --git a/system/database/drivers/postgre/postgre_result.php b/system/database/drivers/postgre/postgre_result.php index 394b8b6fd..f913bc9eb 100644 --- a/system/database/drivers/postgre/postgre_result.php +++ b/system/database/drivers/postgre/postgre_result.php @@ -128,7 +128,7 @@ class CI_DB_postgre_result extends CI_DB_result { * this internally before fetching results to make sure the * result set starts at zero * - * @return array + * @return bool */ protected function _data_seek($n = 0) { diff --git a/system/database/drivers/sqlite/sqlite_result.php b/system/database/drivers/sqlite/sqlite_result.php index 4af80abf7..741dc9d8d 100644 --- a/system/database/drivers/sqlite/sqlite_result.php +++ b/system/database/drivers/sqlite/sqlite_result.php @@ -112,7 +112,7 @@ class CI_DB_sqlite_result extends CI_DB_result { * this internally before fetching results to make sure the * result set starts at zero * - * @return array + * @return bool */ protected function _data_seek($n = 0) { diff --git a/system/database/drivers/sqlite3/sqlite3_result.php b/system/database/drivers/sqlite3/sqlite3_result.php index 64482e379..d83d6b2cd 100644 --- a/system/database/drivers/sqlite3/sqlite3_result.php +++ b/system/database/drivers/sqlite3/sqlite3_result.php @@ -198,8 +198,6 @@ class CI_DB_sqlite3_result extends CI_DB_result { $this->row_data[$row_index++] = $row; } - // Un-comment the following line, in case it becomes needed - // $this->_data_seek(); return $this->result_array = $this->row_data; } @@ -266,8 +264,6 @@ class CI_DB_sqlite3_result extends CI_DB_result { $this->num_rows = count($this->result_object); } - // Un-comment the following line, in case it becomes needed - // $this->_data_seek(); return $this->result_object; } diff --git a/system/database/drivers/sqlsrv/sqlsrv_result.php b/system/database/drivers/sqlsrv/sqlsrv_result.php index 10d790e4b..0802677fc 100644 --- a/system/database/drivers/sqlsrv/sqlsrv_result.php +++ b/system/database/drivers/sqlsrv/sqlsrv_result.php @@ -123,22 +123,6 @@ class CI_DB_sqlsrv_result extends CI_DB_result { // -------------------------------------------------------------------- - /** - * 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 - * - * @return void - */ - protected function _data_seek($n = 0) - { - // Not implemented - } - - // -------------------------------------------------------------------- - /** * Result - associative array * -- cgit v1.2.3-24-g4f1b From d3c9447c83c59d674132d801f4fc5278a9875162 Mon Sep 17 00:00:00 2001 From: Kevin King Date: Thu, 12 Apr 2012 13:46:20 -0300 Subject: Adding in .ics MIME type for calendar invites. --- system/libraries/Email.php | 1 + 1 file changed, 1 insertion(+) diff --git a/system/libraries/Email.php b/system/libraries/Email.php index 48c3bf3ab..103c3cb25 100644 --- a/system/libraries/Email.php +++ b/system/libraries/Email.php @@ -1891,6 +1891,7 @@ class CI_Email { 'tiff' => 'image/tiff', 'tif' => 'image/tiff', 'css' => 'text/css', + 'ics' => 'text/calendar', 'html' => 'text/html', 'htm' => 'text/html', 'shtml' => 'text/html', -- cgit v1.2.3-24-g4f1b From 8b52222a684892e3306a093821d4507792acd3ea Mon Sep 17 00:00:00 2001 From: Rogerio Prado de Jesus Date: Fri, 13 Apr 2012 01:18:03 -0300 Subject: Update Release Date for version 2.1.1 at user_guide_src/source/changelog.rst --- user_guide_src/source/changelog.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 8146a5139..5f6d5912c 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -210,7 +210,7 @@ Bug fixes for 3.0 Version 2.1.1 ============= -Release Date: November 14, 2011 +Release Date: Not Released - General Changes - Fixed support for docx, xlsx files in mimes.php. -- cgit v1.2.3-24-g4f1b From 0f2211711deceb74157d6811116acc0376d3157d Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 13 Apr 2012 14:52:16 +0300 Subject: Switch erroneously declared protected properties in Output class to public --- system/core/Output.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/system/core/Output.php b/system/core/Output.php index 01fd1d867..3cb40626a 100755 --- a/system/core/Output.php +++ b/system/core/Output.php @@ -43,37 +43,37 @@ class CI_Output { * * @var string */ - protected $final_output; + public $final_output; /** * Cache expiration time * * @var int */ - protected $cache_expiration = 0; + public $cache_expiration = 0; /** * List of server headers * * @var array */ - protected $headers = array(); + public $headers = array(); /** * List of mime types * * @var array */ - protected $mime_types = array(); + public $mime_types = array(); /** * Determines wether profiler is enabled * * @var book */ - protected $enable_profiler = FALSE; + public $enable_profiler = FALSE; /** * Determines if output compression is enabled * * @var bool */ - protected $_zlib_oc = FALSE; + protected $_zlib_oc = FALSE; /** * List of profiler sections * @@ -85,7 +85,7 @@ class CI_Output { * * @var bool */ - protected $parse_exec_vars = TRUE; + public $parse_exec_vars = TRUE; public function __construct() { -- cgit v1.2.3-24-g4f1b From 3c0a4529b05ed8b3d9fadf97238e3d462d0ce733 Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Sun, 15 Apr 2012 13:30:44 +0200 Subject: Added the ability to insert objects with insert_batch() in Active Reccord. --- system/database/DB_active_rec.php | 3 ++- user_guide_src/source/changelog.rst | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index a5df7f31a..e78b2a893 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -1252,11 +1252,12 @@ abstract class CI_DB_active_record extends CI_DB_driver { $key = array($key => $value); } - $keys = array_keys(current($key)); + $keys = array_keys($this->_object_to_array(current($key))); sort($keys); foreach ($key as $row) { + $row = $this->_object_to_array($row); if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0) { // batch function above returns an error on an empty array diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 5f6d5912c..0cac8aea0 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -54,6 +54,7 @@ Release Date: Not Released - Database + - Added the ability to insert objects with insert_batch() in :doc:`Active Record `. - Added new :doc:`Active Record ` methods that return the SQL string of queries without executing them: get_compiled_select(), get_compiled_insert(), get_compiled_update(), get_compiled_delete(). -- cgit v1.2.3-24-g4f1b From 3684d349d1f5239ad9703c07f14ae77389818daf Mon Sep 17 00:00:00 2001 From: Korri Date: Tue, 17 Apr 2012 00:35:08 -0400 Subject: Adding a package adds configuration folder to the end of the list. --- system/core/Loader.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/core/Loader.php b/system/core/Loader.php index 027ed20e5..a4039c6f0 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -673,7 +673,7 @@ class CI_Loader { // Add config file path $config =& $this->_ci_get_component('config'); - array_unshift($config->_config_paths, $path); + array_push($config->_config_paths, $path); } // -------------------------------------------------------------------- @@ -713,7 +713,7 @@ class CI_Loader { array_shift($this->_ci_model_paths); array_shift($this->_ci_helper_paths); array_shift($this->_ci_view_paths); - array_shift($config->_config_paths); + array_pop($config->_config_paths); } else { -- cgit v1.2.3-24-g4f1b From eafad45b9fbd37f42058366ca7bd17d6bb848ed8 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 17 Apr 2012 09:15:05 -0400 Subject: Updated supported database driver list --- application/config/database.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/application/config/database.php b/application/config/database.php index 8c06dd2e6..59b223896 100644 --- a/application/config/database.php +++ b/application/config/database.php @@ -43,7 +43,8 @@ | ['password'] The password used to connect to the database | ['database'] The name of the database you want to connect to | ['dbdriver'] The database type. e.g.: mysql. Currently supported: - mysql, mysqli, pdo, postgre, odbc, mssql, sqlite, oci8 + cubrid, interbase, mssql, mysql, mysqli, oci8, odbc, + pdo, postgre, sqlite, sqlite3, sqlsrv | ['dbprefix'] You can add an optional prefix, which will be added | to the table name when using the Active Record class | ['pconnect'] TRUE/FALSE - Whether to use a persistent connection -- cgit v1.2.3-24-g4f1b From 43481d0be0f508feadd6ef51403236606fb23cc6 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 17 Apr 2012 09:17:40 -0400 Subject: minor style fix --- application/config/database.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/config/database.php b/application/config/database.php index 59b223896..f08d88761 100644 --- a/application/config/database.php +++ b/application/config/database.php @@ -43,8 +43,8 @@ | ['password'] The password used to connect to the database | ['database'] The name of the database you want to connect to | ['dbdriver'] The database type. e.g.: mysql. Currently supported: - cubrid, interbase, mssql, mysql, mysqli, oci8, odbc, - pdo, postgre, sqlite, sqlite3, sqlsrv +| cubrid, interbase, mssql, mysql, mysqli, oci8, +| odbc, pdo, postgre, sqlite, sqlite3, sqlsrv | ['dbprefix'] You can add an optional prefix, which will be added | to the table name when using the Active Record class | ['pconnect'] TRUE/FALSE - Whether to use a persistent connection -- cgit v1.2.3-24-g4f1b From ad4750509885ad5bb368fc308f86d8c06d45b15c Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 19 Apr 2012 13:21:06 -0400 Subject: Normalize comments in core files --- system/core/CodeIgniter.php | 7 + system/core/Common.php | 341 ++++++++++++++++++++++---------------------- system/core/Config.php | 1 - system/core/Controller.php | 13 ++ system/core/Exceptions.php | 9 +- system/core/Lang.php | 3 + system/core/Loader.php | 2 - system/core/Model.php | 3 + system/core/Output.php | 10 ++ system/core/Router.php | 1 - system/core/Security.php | 11 +- 11 files changed, 217 insertions(+), 184 deletions(-) diff --git a/system/core/CodeIgniter.php b/system/core/CodeIgniter.php index 4885f310c..9714d095c 100755 --- a/system/core/CodeIgniter.php +++ b/system/core/CodeIgniter.php @@ -230,6 +230,13 @@ // Load the base controller class require BASEPATH.'core/Controller.php'; + /** + * Reference to the CI_Controller method. + * + * Returns current CI instance object + * + * @return object + */ function &get_instance() { return CI_Controller::get_instance(); diff --git a/system/core/Common.php b/system/core/Common.php index aeb784bbe..075b221bd 100644 --- a/system/core/Common.php +++ b/system/core/Common.php @@ -39,17 +39,18 @@ // ------------------------------------------------------------------------ -/** - * Determines if the current version of PHP is greater then the supplied value - * - * Since there are a few places where we conditionally test for PHP > 5 - * we'll set a static variable. - * - * @param string - * @return bool TRUE if the current version is $version or higher - */ + if ( ! function_exists('is_php')) { + /** + * Determines if the current version of PHP is greater then the supplied value + * + * Since there are a few places where we conditionally test for PHP > 5 + * we'll set a static variable. + * + * @param string + * @return bool TRUE if the current version is $version or higher + */ function is_php($version = '5.0.0') { static $_is_php; @@ -66,18 +67,18 @@ if ( ! function_exists('is_php')) // ------------------------------------------------------------------------ -/** - * Tests for file writability - * - * is_writable() returns TRUE on Windows servers when you really can't write to - * the file, based on the read-only attribute. is_writable() is also unreliable - * on Unix servers if safe_mode is on. - * - * @param string - * @return void - */ if ( ! function_exists('is_really_writable')) { + /** + * Tests for file writability + * + * is_writable() returns TRUE on Windows servers when you really can't write to + * the file, based on the read-only attribute. is_writable() is also unreliable + * on Unix servers if safe_mode is on. + * + * @param string + * @return void + */ function is_really_writable($file) { // If we're on a Unix server with safe_mode off we call is_writable @@ -114,20 +115,20 @@ if ( ! function_exists('is_really_writable')) // ------------------------------------------------------------------------ -/** - * Class registry - * - * This function acts as a singleton. If the requested class does not - * exist it is instantiated and set to a static variable. If it has - * previously been instantiated the variable is returned. - * - * @param string the class name being requested - * @param string the directory where the class should be found - * @param string the class name prefix - * @return object - */ if ( ! function_exists('load_class')) { + /** + * Class registry + * + * This function acts as a singleton. If the requested class does not + * exist it is instantiated and set to a static variable. If it has + * previously been instantiated the variable is returned. + * + * @param string the class name being requested + * @param string the directory where the class should be found + * @param string the class name prefix + * @return object + */ function &load_class($class, $directory = 'libraries', $prefix = 'CI_') { static $_classes = array(); @@ -187,15 +188,15 @@ if ( ! function_exists('load_class')) // -------------------------------------------------------------------- -/** - * Keeps track of which libraries have been loaded. This function is - * called by the load_class() function above - * - * @param string - * @return array - */ if ( ! function_exists('is_loaded')) { + /** + * Keeps track of which libraries have been loaded. This function is + * called by the load_class() function above + * + * @param string + * @return array + */ function &is_loaded($class = '') { static $_is_loaded = array(); @@ -211,17 +212,17 @@ if ( ! function_exists('is_loaded')) // ------------------------------------------------------------------------ -/** - * Loads the main config.php file - * - * This function lets us grab the config file even if the Config class - * hasn't been instantiated yet - * - * @param array - * @return array - */ if ( ! function_exists('get_config')) { + /** + * Loads the main config.php file + * + * This function lets us grab the config file even if the Config class + * hasn't been instantiated yet + * + * @param array + * @return array + */ function &get_config($replace = array()) { static $_config; @@ -271,14 +272,14 @@ if ( ! function_exists('get_config')) // ------------------------------------------------------------------------ -/** - * Returns the specified config item - * - * @param string - * @return mixed - */ if ( ! function_exists('config_item')) { + /** + * Returns the specified config item + * + * @param string + * @return mixed + */ function config_item($item) { static $_config_item = array(); @@ -300,22 +301,22 @@ if ( ! function_exists('config_item')) // ------------------------------------------------------------------------ -/** - * Error Handler - * - * This function lets us invoke the exception class and - * display errors using the standard error template located - * in application/errors/errors.php - * This function will send the error page directly to the - * browser and exit. - * - * @param string - * @param int - * @param string - * @return void - */ if ( ! function_exists('show_error')) { + /** + * Error Handler + * + * This function lets us invoke the exception class and + * display errors using the standard error template located + * in application/errors/errors.php + * This function will send the error page directly to the + * browser and exit. + * + * @param string + * @param int + * @param string + * @return void + */ function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered') { $_error =& load_class('Exceptions', 'core'); @@ -326,19 +327,19 @@ if ( ! function_exists('show_error')) // ------------------------------------------------------------------------ -/** - * 404 Page Handler - * - * This function is similar to the show_error() function above - * However, instead of the standard error template it displays - * 404 errors. - * - * @param string - * @param bool - * @return void - */ if ( ! function_exists('show_404')) { + /** + * 404 Page Handler + * + * This function is similar to the show_error() function above + * However, instead of the standard error template it displays + * 404 errors. + * + * @param string + * @param bool + * @return void + */ function show_404($page = '', $log_error = TRUE) { $_error =& load_class('Exceptions', 'core'); @@ -349,19 +350,19 @@ if ( ! function_exists('show_404')) // ------------------------------------------------------------------------ -/** - * Error Logging Interface - * - * We use this as a simple mechanism to access the logging - * class and send messages to be logged. - * - * @param string - * @param string - * @param bool - * @return void - */ if ( ! function_exists('log_message')) { + /** + * Error Logging Interface + * + * We use this as a simple mechanism to access the logging + * class and send messages to be logged. + * + * @param string + * @param string + * @param bool + * @return void + */ function log_message($level = 'error', $message, $php_error = FALSE) { static $_log; @@ -378,59 +379,59 @@ if ( ! function_exists('log_message')) // ------------------------------------------------------------------------ -/** - * Set HTTP Status Header - * - * @param int the status code - * @param string - * @return void - */ if ( ! function_exists('set_status_header')) { + /** + * Set HTTP Status Header + * + * @param int the status code + * @param string + * @return void + */ function set_status_header($code = 200, $text = '') { $stati = array( - 200 => 'OK', - 201 => 'Created', - 202 => 'Accepted', - 203 => 'Non-Authoritative Information', - 204 => 'No Content', - 205 => 'Reset Content', - 206 => 'Partial Content', - - 300 => 'Multiple Choices', - 301 => 'Moved Permanently', - 302 => 'Found', - 304 => 'Not Modified', - 305 => 'Use Proxy', - 307 => 'Temporary Redirect', - - 400 => 'Bad Request', - 401 => 'Unauthorized', - 403 => 'Forbidden', - 404 => 'Not Found', - 405 => 'Method Not Allowed', - 406 => 'Not Acceptable', - 407 => 'Proxy Authentication Required', - 408 => 'Request Timeout', - 409 => 'Conflict', - 410 => 'Gone', - 411 => 'Length Required', - 412 => 'Precondition Failed', - 413 => 'Request Entity Too Large', - 414 => 'Request-URI Too Long', - 415 => 'Unsupported Media Type', - 416 => 'Requested Range Not Satisfiable', - 417 => 'Expectation Failed', - 422 => 'Unprocessable Entity', - - 500 => 'Internal Server Error', - 501 => 'Not Implemented', - 502 => 'Bad Gateway', - 503 => 'Service Unavailable', - 504 => 'Gateway Timeout', - 505 => 'HTTP Version Not Supported' - ); + 200 => 'OK', + 201 => 'Created', + 202 => 'Accepted', + 203 => 'Non-Authoritative Information', + 204 => 'No Content', + 205 => 'Reset Content', + 206 => 'Partial Content', + + 300 => 'Multiple Choices', + 301 => 'Moved Permanently', + 302 => 'Found', + 304 => 'Not Modified', + 305 => 'Use Proxy', + 307 => 'Temporary Redirect', + + 400 => 'Bad Request', + 401 => 'Unauthorized', + 403 => 'Forbidden', + 404 => 'Not Found', + 405 => 'Method Not Allowed', + 406 => 'Not Acceptable', + 407 => 'Proxy Authentication Required', + 408 => 'Request Timeout', + 409 => 'Conflict', + 410 => 'Gone', + 411 => 'Length Required', + 412 => 'Precondition Failed', + 413 => 'Request Entity Too Large', + 414 => 'Request-URI Too Long', + 415 => 'Unsupported Media Type', + 416 => 'Requested Range Not Satisfiable', + 417 => 'Expectation Failed', + 422 => 'Unprocessable Entity', + + 500 => 'Internal Server Error', + 501 => 'Not Implemented', + 502 => 'Bad Gateway', + 503 => 'Service Unavailable', + 504 => 'Gateway Timeout', + 505 => 'HTTP Version Not Supported' + ); if ($code == '' OR ! is_numeric($code)) { @@ -466,25 +467,25 @@ if ( ! function_exists('set_status_header')) // -------------------------------------------------------------------- -/** - * Exception Handler - * - * This is the custom exception handler that is declaired at the top - * of Codeigniter.php. The main reason we use this is to permit - * PHP errors to be logged in our own log files since the user may - * not have access to server logs. Since this function - * effectively intercepts PHP errors, however, we also need - * to display errors based on the current error_reporting level. - * We do that with the use of a PHP error template. - * - * @param int - * @param string - * @param string - * @param int - * @return void - */ if ( ! function_exists('_exception_handler')) { + /** + * Exception Handler + * + * This is the custom exception handler that is declaired at the top + * of Codeigniter.php. The main reason we use this is to permit + * PHP errors to be logged in our own log files since the user may + * not have access to server logs. Since this function + * effectively intercepts PHP errors, however, we also need + * to display errors based on the current error_reporting level. + * We do that with the use of a PHP error template. + * + * @param int + * @param string + * @param string + * @param int + * @return void + */ function _exception_handler($severity, $message, $filepath, $line) { // We don't bother with "strict" notices since they tend to fill up @@ -518,18 +519,18 @@ if ( ! function_exists('_exception_handler')) // -------------------------------------------------------------------- -/** - * Remove Invisible Characters - * - * This prevents sandwiching null characters - * between ascii characters, like Java\0script. - * - * @param string - * @param bool - * @return string - */ if ( ! function_exists('remove_invisible_characters')) { + /** + * Remove Invisible Characters + * + * This prevents sandwiching null characters + * between ascii characters, like Java\0script. + * + * @param string + * @param bool + * @return string + */ function remove_invisible_characters($str, $url_encoded = TRUE) { $non_displayables = array(); @@ -556,14 +557,14 @@ if ( ! function_exists('remove_invisible_characters')) // ------------------------------------------------------------------------ -/** - * Returns HTML escaped variable - * - * @param mixed - * @return mixed - */ if ( ! function_exists('html_escape')) { + /** + * Returns HTML escaped variable + * + * @param mixed + * @return mixed + */ function html_escape($var) { return is_array($var) diff --git a/system/core/Config.php b/system/core/Config.php index 91826bd41..e94362912 100755 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -188,7 +188,6 @@ class CI_Config { /** * Fetch a config file item * - * * @param string the config item name * @param string the index name * @param bool diff --git a/system/core/Controller.php b/system/core/Controller.php index 05e1bf5bf..e73e887a0 100644 --- a/system/core/Controller.php +++ b/system/core/Controller.php @@ -41,8 +41,16 @@ */ class CI_Controller { + /** + * Reference to the global CI instance + * + * @var object + */ private static $instance; + /** + * Set up controller properties and methods + */ public function __construct() { self::$instance =& $this; @@ -60,6 +68,11 @@ class CI_Controller { log_message('debug', 'Controller Class Initialized'); } + /** + * Return the CI object + * + * @return object + */ public static function &get_instance() { return self::$instance; diff --git a/system/core/Exceptions.php b/system/core/Exceptions.php index f36b31598..dd5841333 100755 --- a/system/core/Exceptions.php +++ b/system/core/Exceptions.php @@ -36,12 +36,6 @@ */ class CI_Exceptions { - public $action; - public $severity; - public $message; - public $filename; - public $line; - /** * Nesting level of the output buffering mechanism * @@ -69,6 +63,9 @@ class CI_Exceptions { E_STRICT => 'Runtime Notice' ); + /** + * Initialize execption class + */ public function __construct() { $this->ob_level = ob_get_level(); diff --git a/system/core/Lang.php b/system/core/Lang.php index 9ef76f4d6..82013844c 100755 --- a/system/core/Lang.php +++ b/system/core/Lang.php @@ -51,6 +51,9 @@ class CI_Lang { */ public $is_loaded = array(); + /** + * Initialize language class + */ public function __construct() { log_message('debug', 'Language Class Initialized'); diff --git a/system/core/Loader.php b/system/core/Loader.php index 027ed20e5..e1531843a 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -138,7 +138,6 @@ class CI_Loader { * * This method is called once in CI_Controller. * - * @param array * @return object */ public function initialize() @@ -1123,7 +1122,6 @@ class CI_Loader { * The config/autoload.php file contains an array that permits sub-systems, * libraries, and helpers to be loaded automatically. * - * @param array * @return void */ protected function _ci_autoloader() diff --git a/system/core/Model.php b/system/core/Model.php index 49b8d34e4..3855d49e7 100755 --- a/system/core/Model.php +++ b/system/core/Model.php @@ -38,6 +38,9 @@ */ class CI_Model { + /** + * Initialize CI_Model Class + */ public function __construct() { log_message('debug', 'Model Class Initialized'); diff --git a/system/core/Output.php b/system/core/Output.php index 3cb40626a..671303094 100755 --- a/system/core/Output.php +++ b/system/core/Output.php @@ -44,42 +44,49 @@ class CI_Output { * @var string */ public $final_output; + /** * Cache expiration time * * @var int */ public $cache_expiration = 0; + /** * List of server headers * * @var array */ public $headers = array(); + /** * List of mime types * * @var array */ public $mime_types = array(); + /** * Determines wether profiler is enabled * * @var book */ public $enable_profiler = FALSE; + /** * Determines if output compression is enabled * * @var bool */ protected $_zlib_oc = FALSE; + /** * List of profiler sections * * @var array */ protected $_profiler_sections = array(); + /** * Whether or not to parse variables like {elapsed_time} and {memory_usage} * @@ -87,6 +94,9 @@ class CI_Output { */ public $parse_exec_vars = TRUE; + /** + * Set up Output class + */ public function __construct() { $this->_zlib_oc = @ini_get('zlib.output_compression'); diff --git a/system/core/Router.php b/system/core/Router.php index 5477fed5d..b34911859 100755 --- a/system/core/Router.php +++ b/system/core/Router.php @@ -211,7 +211,6 @@ class CI_Router { * input, and sets the current class/method * * @param array - * @param bool * @return void */ protected function _set_request($segments = array()) diff --git a/system/core/Security.php b/system/core/Security.php index ac39ce97b..8d81babf2 100755 --- a/system/core/Security.php +++ b/system/core/Security.php @@ -102,6 +102,9 @@ class CI_Security { 'Redirect\s+302' ); + /** + * Initialize security class + */ public function __construct() { // Is CSRF protection enabled? @@ -558,8 +561,8 @@ class CI_Security { * Callback function for xss_clean() to remove whitespace from * things like j a v a s c r i p t * - * @param type - * @return type + * @param array + * @return string */ protected function _compact_exploded_words($matches) { @@ -568,8 +571,8 @@ class CI_Security { // -------------------------------------------------------------------- - /* - * Remove Evil HTML Attributes (like evenhandlers and style) + /** + * Remove Evil HTML Attributes (like event handlers and style) * * It removes the evil attribute and either: * - Everything up until a space -- cgit v1.2.3-24-g4f1b From 40403d21274d5e0792c7ab816ad984d6387d5c20 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 19 Apr 2012 16:38:50 -0400 Subject: Additional formatting fixes --- system/core/Config.php | 4 +- system/core/Controller.php | 4 +- system/core/Exceptions.php | 26 +++--- system/core/Hooks.php | 6 +- system/core/Input.php | 221 +++++++++++++++++++++++++-------------------- system/core/Lang.php | 9 +- system/core/Loader.php | 39 +++++--- system/core/Model.php | 4 +- system/core/Output.php | 12 +-- system/core/Router.php | 16 +++- system/core/Security.php | 84 ++++++++--------- system/core/URI.php | 18 ++-- system/core/Utf8.php | 4 +- 13 files changed, 243 insertions(+), 204 deletions(-) diff --git a/system/core/Config.php b/system/core/Config.php index e94362912..fc6183444 100755 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -46,12 +46,14 @@ class CI_Config { * @var array */ public $config = array(); + /** * List of all loaded config files * * @var array */ public $is_loaded = array(); + /** * List of paths to search when trying to load a config file. * This must be public as it's used by the Loader class. @@ -353,4 +355,4 @@ class CI_Config { } /* End of file Config.php */ -/* Location: ./system/core/Config.php */ +/* Location: ./system/core/Config.php */ \ No newline at end of file diff --git a/system/core/Controller.php b/system/core/Controller.php index e73e887a0..0c6c47183 100644 --- a/system/core/Controller.php +++ b/system/core/Controller.php @@ -25,8 +25,6 @@ * @filesource */ -// ------------------------------------------------------------------------ - /** * CodeIgniter Application Controller Class * @@ -80,4 +78,4 @@ class CI_Controller { } /* End of file Controller.php */ -/* Location: ./system/core/Controller.php */ +/* Location: ./system/core/Controller.php */ \ No newline at end of file diff --git a/system/core/Exceptions.php b/system/core/Exceptions.php index dd5841333..f55f9fcba 100755 --- a/system/core/Exceptions.php +++ b/system/core/Exceptions.php @@ -49,19 +49,19 @@ class CI_Exceptions { * @var array */ public $levels = array( - E_ERROR => 'Error', - E_WARNING => 'Warning', - E_PARSE => 'Parsing Error', - E_NOTICE => 'Notice', - E_CORE_ERROR => 'Core Error', - E_CORE_WARNING => 'Core Warning', - E_COMPILE_ERROR => 'Compile Error', - E_COMPILE_WARNING => 'Compile Warning', - E_USER_ERROR => 'User Error', - E_USER_WARNING => 'User Warning', - E_USER_NOTICE => 'User Notice', - E_STRICT => 'Runtime Notice' - ); + E_ERROR => 'Error', + E_WARNING => 'Warning', + E_PARSE => 'Parsing Error', + E_NOTICE => 'Notice', + E_CORE_ERROR => 'Core Error', + E_CORE_WARNING => 'Core Warning', + E_COMPILE_ERROR => 'Compile Error', + E_COMPILE_WARNING => 'Compile Warning', + E_USER_ERROR => 'User Error', + E_USER_WARNING => 'User Warning', + E_USER_NOTICE => 'User Notice', + E_STRICT => 'Runtime Notice' + ); /** * Initialize execption class diff --git a/system/core/Hooks.php b/system/core/Hooks.php index 68e30ef0f..672fe29dd 100755 --- a/system/core/Hooks.php +++ b/system/core/Hooks.php @@ -43,13 +43,15 @@ class CI_Hooks { * * @var bool */ - public $enabled = FALSE; + public $enabled = FALSE; + /** * List of all hooks set in config/hooks.php * * @var array */ - public $hooks = array(); + public $hooks = array(); + /** * Determines wether hook is in progress, used to prevent infinte loops * diff --git a/system/core/Input.php b/system/core/Input.php index 6e6885992..049935632 100755 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -43,45 +43,51 @@ class CI_Input { * * @var string */ - public $ip_address = FALSE; + public $ip_address = FALSE; + /** * user agent (web browser) being used by the current user * * @var string */ - public $user_agent = FALSE; + public $user_agent = FALSE; + /** * If FALSE, then $_GET will be set to an empty array * * @var bool */ - protected $_allow_get_array = TRUE; + protected $_allow_get_array = TRUE; + /** * If TRUE, then newlines are standardized * * @var bool */ - protected $_standardize_newlines = TRUE; + protected $_standardize_newlines = TRUE; + /** * Determines whether the XSS filter is always active when GET, POST or COOKIE data is encountered * Set automatically based on config setting * * @var bool */ - protected $_enable_xss = FALSE; + protected $_enable_xss = FALSE; + /** * Enables a CSRF cookie token to be set. * Set automatically based on config setting * * @var bool */ - protected $_enable_csrf = FALSE; + protected $_enable_csrf = FALSE; + /** * List of all HTTP request headers * * @var array */ - protected $headers = array(); + protected $headers = array(); /** * Constructor @@ -141,12 +147,12 @@ class CI_Input { // -------------------------------------------------------------------- /** - * Fetch an item from the GET array - * - * @param string - * @param bool - * @return string - */ + * Fetch an item from the GET array + * + * @param string + * @param bool + * @return string + */ public function get($index = NULL, $xss_clean = FALSE) { // Check if a field has been provided @@ -168,12 +174,12 @@ class CI_Input { // -------------------------------------------------------------------- /** - * Fetch an item from the POST array - * - * @param string - * @param bool - * @return string - */ + * Fetch an item from the POST array + * + * @param string + * @param bool + * @return string + */ public function post($index = NULL, $xss_clean = FALSE) { // Check if a field has been provided @@ -196,12 +202,12 @@ class CI_Input { // -------------------------------------------------------------------- /** - * Fetch an item from either the GET array or the POST - * - * @param string The index key - * @param bool XSS cleaning - * @return string - */ + * Fetch an item from either the GET array or the POST + * + * @param string The index key + * @param bool XSS cleaning + * @return string + */ public function get_post($index = '', $xss_clean = FALSE) { return isset($_POST[$index]) @@ -212,12 +218,12 @@ class CI_Input { // -------------------------------------------------------------------- /** - * Fetch an item from the COOKIE array - * - * @param string - * @param bool - * @return string - */ + * Fetch an item from the COOKIE array + * + * @param string + * @param bool + * @return string + */ public function cookie($index = '', $xss_clean = FALSE) { return $this->_fetch_from_array($_COOKIE, $index, $xss_clean); @@ -226,21 +232,21 @@ class CI_Input { // ------------------------------------------------------------------------ /** - * Set cookie - * - * Accepts seven parameters, or you can submit an associative - * array in the first parameter containing all the values. - * - * @param mixed - * @param string the value of the cookie - * @param string the number of seconds until expiration - * @param string the cookie domain. Usually: .yourdomain.com - * @param string the cookie path - * @param string the cookie prefix - * @param bool true makes the cookie secure - * @param bool true makes the cookie accessible via http(s) only (no javascript) - * @return void - */ + * Set cookie + * + * Accepts seven parameters, or you can submit an associative + * array in the first parameter containing all the values. + * + * @param mixed + * @param string the value of the cookie + * @param string the number of seconds until expiration + * @param string the cookie domain. Usually: .yourdomain.com + * @param string the cookie path + * @param string the cookie prefix + * @param bool true makes the cookie secure + * @param bool true makes the cookie accessible via http(s) only (no javascript) + * @return void + */ public function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE) { if (is_array($name)) @@ -291,12 +297,12 @@ class CI_Input { // -------------------------------------------------------------------- /** - * Fetch an item from the SERVER array - * - * @param string - * @param bool - * @return string - */ + * Fetch an item from the SERVER array + * + * @param string + * @param bool + * @return string + */ public function server($index = '', $xss_clean = FALSE) { return $this->_fetch_from_array($_SERVER, $index, $xss_clean); @@ -305,10 +311,10 @@ class CI_Input { // -------------------------------------------------------------------- /** - * Fetch the IP Address - * - * @return string - */ + * Fetch the IP Address + * + * @return string + */ public function ip_address() { if ($this->ip_address !== FALSE) @@ -362,13 +368,13 @@ class CI_Input { // -------------------------------------------------------------------- /** - * Validate IP Address - * - * Updated version suggested by Geert De Deckere - * - * @param string - * @return bool - */ + * Validate IP Address + * + * Updated version suggested by Geert De Deckere + * + * @param string + * @return bool + */ public function valid_ip($ip) { return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); @@ -377,10 +383,10 @@ class CI_Input { // -------------------------------------------------------------------- /** - * User Agent - * - * @return string - */ + * User Agent + * + * @return string + */ public function user_agent() { if ($this->user_agent !== FALSE) @@ -394,24 +400,39 @@ class CI_Input { // -------------------------------------------------------------------- /** - * Sanitize Globals - * - * This function does the following: - * - * - Unsets $_GET data (if query strings are not enabled) - * - Unsets all globals if register_globals is enabled - * - Standardizes newline characters to \n - * - * @return void - */ + * Sanitize Globals + * + * This function does the following: + * + * - Unsets $_GET data (if query strings are not enabled) + * - Unsets all globals if register_globals is enabled + * - Standardizes newline characters to \n + * + * @return void + */ protected function _sanitize_globals() { // It would be "wrong" to unset any of these GLOBALS. - $protected = array('_SERVER', '_GET', '_POST', '_FILES', '_REQUEST', - '_SESSION', '_ENV', 'GLOBALS', 'HTTP_RAW_POST_DATA', - 'system_folder', 'application_folder', 'BM', 'EXT', - 'CFG', 'URI', 'RTR', 'OUT', 'IN' - ); + $protected = array( + '_SERVER', + '_GET', + '_POST', + '_FILES', + '_REQUEST', + '_SESSION', + '_ENV', + 'GLOBALS', + 'HTTP_RAW_POST_DATA', + 'system_folder', + 'application_folder', + 'BM', + 'EXT', + 'CFG', + 'URI', + 'RTR', + 'OUT' + 'IN' + ); // Unset globals for securiy. // This is effectively the same as register_globals = off @@ -493,14 +514,14 @@ class CI_Input { // -------------------------------------------------------------------- /** - * Clean Input Data - * - * This is a helper function. It escapes data and - * standardizes newline characters to \n - * - * @param string - * @return string - */ + * Clean Input Data + * + * This is a helper function. It escapes data and + * standardizes newline characters to \n + * + * @param string + * @return string + */ protected function _clean_input_data($str) { if (is_array($str)) @@ -550,15 +571,15 @@ class CI_Input { // -------------------------------------------------------------------- /** - * Clean Keys - * - * This is a helper function. To prevent malicious users - * from trying to exploit keys we make sure that keys are - * only named with alpha-numeric text and a few other items. - * - * @param string - * @return string - */ + * Clean Keys + * + * This is a helper function. To prevent malicious users + * from trying to exploit keys we make sure that keys are + * only named with alpha-numeric text and a few other items. + * + * @param string + * @return string + */ protected function _clean_input_keys($str) { if ( ! preg_match('/^[a-z0-9:_\/-]+$/i', $str)) @@ -695,4 +716,4 @@ class CI_Input { } /* End of file Input.php */ -/* Location: ./system/core/Input.php */ +/* Location: ./system/core/Input.php */ \ No newline at end of file diff --git a/system/core/Lang.php b/system/core/Lang.php index 82013844c..7abdc6102 100755 --- a/system/core/Lang.php +++ b/system/core/Lang.php @@ -25,8 +25,6 @@ * @filesource */ -// ------------------------------------------------------------------------ - /** * Language Class * @@ -43,13 +41,14 @@ class CI_Lang { * * @var array */ - public $language = array(); + public $language = array(); + /** * List of loaded language files * * @var array */ - public $is_loaded = array(); + public $is_loaded = array(); /** * Initialize language class @@ -164,4 +163,4 @@ class CI_Lang { } /* End of file Lang.php */ -/* Location: ./system/core/Lang.php */ +/* Location: ./system/core/Lang.php */ \ No newline at end of file diff --git a/system/core/Loader.php b/system/core/Loader.php index e1531843a..b99ce6964 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -45,75 +45,86 @@ class CI_Loader { * @var int */ protected $_ci_ob_level; + /** * List of paths to load views from * * @var array */ - protected $_ci_view_paths = array(); + protected $_ci_view_paths = array(); + /** * List of paths to load libraries from * * @var array */ - protected $_ci_library_paths = array(); + protected $_ci_library_paths = array(); + /** * List of paths to load models from * * @var array */ - protected $_ci_model_paths = array(); + protected $_ci_model_paths = array(); + /** * List of paths to load helpers from * * @var array */ - protected $_ci_helper_paths = array(); + protected $_ci_helper_paths = array(); + /** * List of loaded base classes * * @var array */ - protected $_base_classes = array(); // Set by the controller class + protected $_base_classes = array(); // Set by the controller class + /** * List of cached variables * * @var array */ - protected $_ci_cached_vars = array(); + protected $_ci_cached_vars = array(); + /** * List of loaded classes * * @var array */ - protected $_ci_classes = array(); + protected $_ci_classes = array(); + /** * List of loaded files * * @var array */ - protected $_ci_loaded_files = array(); + protected $_ci_loaded_files = array(); + /** * List of loaded models * * @var array */ - protected $_ci_models = array(); + protected $_ci_models = array(); + /** * List of loaded helpers * * @var array */ - protected $_ci_helpers = array(); + protected $_ci_helpers = array(); + /** * List of class name mappings * * @var array */ - protected $_ci_varmap = array( - 'unit_test' => 'unit', - 'user_agent' => 'agent' - ); + protected $_ci_varmap = array( + 'unit_test' => 'unit', + 'user_agent' => 'agent' + ); /** * Constructor diff --git a/system/core/Model.php b/system/core/Model.php index 3855d49e7..7c9971970 100755 --- a/system/core/Model.php +++ b/system/core/Model.php @@ -25,8 +25,6 @@ * @filesource */ -// ------------------------------------------------------------------------ - /** * CodeIgniter Model Class * @@ -62,4 +60,4 @@ class CI_Model { } /* End of file Model.php */ -/* Location: ./system/core/Model.php */ +/* Location: ./system/core/Model.php */ \ No newline at end of file diff --git a/system/core/Output.php b/system/core/Output.php index 671303094..0683539c9 100755 --- a/system/core/Output.php +++ b/system/core/Output.php @@ -50,35 +50,35 @@ class CI_Output { * * @var int */ - public $cache_expiration = 0; + public $cache_expiration = 0; /** * List of server headers * * @var array */ - public $headers = array(); + public $headers = array(); /** * List of mime types * * @var array */ - public $mime_types = array(); + public $mime_types = array(); /** * Determines wether profiler is enabled * * @var book */ - public $enable_profiler = FALSE; + public $enable_profiler = FALSE; /** * Determines if output compression is enabled * * @var bool */ - protected $_zlib_oc = FALSE; + protected $_zlib_oc = FALSE; /** * List of profiler sections @@ -92,7 +92,7 @@ class CI_Output { * * @var bool */ - public $parse_exec_vars = TRUE; + public $parse_exec_vars = TRUE; /** * Set up Output class diff --git a/system/core/Router.php b/system/core/Router.php index b34911859..b5c200214 100755 --- a/system/core/Router.php +++ b/system/core/Router.php @@ -44,36 +44,42 @@ class CI_Router { * @var object */ public $config; + /** * List of routes * * @var array */ - public $routes = array(); + public $routes = array(); + /** * List of error routes * * @var array */ - public $error_routes = array(); + public $error_routes = array(); + /** * Current class name * * @var string */ - public $class = ''; + public $class = ''; + /** * Current method name * * @var string */ - public $method = 'index'; + public $method = 'index'; + /** * Sub-directory that contains the requested controller class * * @var string */ - public $directory = ''; + public $directory = ''; + /** * Default controller (and method if specific) * diff --git a/system/core/Security.php b/system/core/Security.php index 8d81babf2..139511661 100755 --- a/system/core/Security.php +++ b/system/core/Security.php @@ -41,14 +41,14 @@ class CI_Security { * * @var string */ - protected $_xss_hash = ''; + protected $_xss_hash = ''; /** * Random Hash for Cross Site Request Forgery Protection Cookie * * @var string */ - protected $_csrf_hash = ''; + protected $_csrf_hash = ''; /** * Expiration time for Cross Site Request Forgery Protection Cookie @@ -56,21 +56,21 @@ class CI_Security { * * @var int */ - protected $_csrf_expire = 7200; + protected $_csrf_expire = 7200; /** * Token name for Cross Site Request Forgery Protection Cookie * * @var string */ - protected $_csrf_token_name = 'ci_csrf_token'; + protected $_csrf_token_name = 'ci_csrf_token'; /** * Cookie name for Cross Site Request Forgery Protection Cookie * * @var string */ - protected $_csrf_cookie_name = 'ci_csrf_token'; + protected $_csrf_cookie_name = 'ci_csrf_token'; /** * List of never allowed strings @@ -78,17 +78,17 @@ class CI_Security { * @var array */ protected $_never_allowed_str = array( - 'document.cookie' => '[removed]', - 'document.write' => '[removed]', - '.parentNode' => '[removed]', - '.innerHTML' => '[removed]', - 'window.location' => '[removed]', - '-moz-binding' => '[removed]', - '' => '-->', - ' '<![CDATA[', - '' => '<comment>' - ); + 'document.cookie' => '[removed]', + 'document.write' => '[removed]', + '.parentNode' => '[removed]', + '.innerHTML' => '[removed]', + 'window.location' => '[removed]', + '-moz-binding' => '[removed]', + '' => '-->', + ' '<![CDATA[', + '' => '<comment>' + ); /** * List of never allowed regex replacement @@ -96,11 +96,11 @@ class CI_Security { * @var array */ protected $_never_allowed_regex = array( - 'javascript\s*:', - 'expression\s*(\(|&\#40;)', // CSS and IE - 'vbscript\s*:', // IE, surprise! - 'Redirect\s+302' - ); + 'javascript\s*:', + 'expression\s*(\(|&\#40;)', // CSS and IE + 'vbscript\s*:', // IE, surprise! + 'Redirect\s+302' + ); /** * Initialize security class @@ -365,9 +365,9 @@ class CI_Security { * These words are compacted back to their correct state. */ $words = array( - 'javascript', 'expression', 'vbscript', 'script', - 'applet', 'alert', 'document', 'write', 'cookie', 'window' - ); + 'javascript', 'expression', 'vbscript', 'script', + 'applet', 'alert', 'document', 'write', 'cookie', 'window' + ); foreach ($words as $word) { @@ -525,23 +525,23 @@ class CI_Security { public function sanitize_filename($str, $relative_path = FALSE) { $bad = array( - '../', '', '<', '>', - "'", '"', '&', '$', '#', - '{', '}', '[', ']', '=', - ';', '?', '%20', '%22', - '%3c', // < - '%253c', // < - '%3e', // > - '%0e', // > - '%28', // ( - '%29', // ) - '%2528', // ( - '%26', // & - '%24', // $ - '%3f', // ? - '%3b', // ; - '%3d' // = - ); + '../', '', '<', '>', + "'", '"', '&', '$', '#', + '{', '}', '[', ']', '=', + ';', '?', '%20', '%22', + '%3c', // < + '%253c', // < + '%3e', // > + '%0e', // > + '%28', // ( + '%29', // ) + '%2528', // ( + '%26', // & + '%24', // $ + '%3f', // ? + '%3b', // ; + '%3d' // = + ); if ( ! $relative_path) { @@ -841,4 +841,4 @@ class CI_Security { } /* End of file Security.php */ -/* Location: ./system/core/Security.php */ +/* Location: ./system/core/Security.php */ \ No newline at end of file diff --git a/system/core/URI.php b/system/core/URI.php index 48bb7ae3c..140295338 100755 --- a/system/core/URI.php +++ b/system/core/URI.php @@ -24,8 +24,6 @@ * @since Version 1.0 */ -// ------------------------------------------------------------------------ - /** * URI Class * @@ -44,26 +42,29 @@ class CI_URI { * * @var array */ - public $keyval = array(); + public $keyval = array(); + /** * Current uri string * * @var string */ public $uri_string; + /** * List of uri segments * * @var array */ - public $segments = array(); + public $segments = array(); + /** * Re-indexed list of uri segments * Starts at 1 instead of 0 * * @var array */ - public $rsegments = array(); + public $rsegments = array(); /** * Constructor @@ -326,6 +327,7 @@ class CI_URI { } // -------------------------------------------------------------------- + /** * Re-index Segments * @@ -406,6 +408,9 @@ class CI_URI { { return $this->_uri_to_assoc($n, $default, 'segment'); } + + // -------------------------------------------------------------------- + /** * Identical to above only it uses the re-routed segment array * @@ -501,7 +506,6 @@ class CI_URI { /** * Generate a URI string from an associative array * - * * @param array an associative array of key/values * @return array */ @@ -647,4 +651,4 @@ class CI_URI { } /* End of file URI.php */ -/* Location: ./system/core/URI.php */ +/* Location: ./system/core/URI.php */ \ No newline at end of file diff --git a/system/core/Utf8.php b/system/core/Utf8.php index ba3567453..122020aea 100644 --- a/system/core/Utf8.php +++ b/system/core/Utf8.php @@ -25,8 +25,6 @@ * @filesource */ -// ------------------------------------------------------------------------ - /** * Utf8 Class * @@ -161,4 +159,4 @@ class CI_Utf8 { } /* End of file Utf8.php */ -/* Location: ./system/core/Utf8.php */ +/* Location: ./system/core/Utf8.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 67cb3eef37fa826f56f61cfd2ac597052d9051f9 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 19 Apr 2012 16:41:52 -0400 Subject: Fix syntax error --- system/core/Input.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/core/Input.php b/system/core/Input.php index 049935632..7ee8f3685 100755 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -430,7 +430,7 @@ class CI_Input { 'CFG', 'URI', 'RTR', - 'OUT' + 'OUT', 'IN' ); -- cgit v1.2.3-24-g4f1b From bb8ae01bff0fa7cb3b14fb5f1310d944c414cf81 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 20 Apr 2012 10:31:51 -0400 Subject: added suggested styleguide addition for future consistency --- user_guide_src/source/general/styleguide.rst | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/user_guide_src/source/general/styleguide.rst b/user_guide_src/source/general/styleguide.rst index 2b91d1cc0..925954c03 100644 --- a/user_guide_src/source/general/styleguide.rst +++ b/user_guide_src/source/general/styleguide.rst @@ -149,7 +149,7 @@ months down the line. There is not a required format for comments, but the following are recommended. `DocBlock `_ -style comments preceding class and method declarations so they can be +style comments preceding class, method, and property declarations so they can be picked up by IDEs:: /** @@ -172,6 +172,17 @@ picked up by IDEs:: * @return string */ function xml_encode($str) + +:: + + /** + * Data for class manipulation + * + * @var array + */ + public $data + + Use single line comments within code, leaving a blank line between large comment blocks and code. -- cgit v1.2.3-24-g4f1b From ab3cab5a8f2afdd45ec8c6c365e43722781e54e6 Mon Sep 17 00:00:00 2001 From: dixy Date: Sat, 21 Apr 2012 00:58:00 +0200 Subject: Use parameter 'get_as_float' of microtime() --- system/core/Benchmark.php | 11 ++++------- system/database/DB_driver.php | 8 ++++---- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/system/core/Benchmark.php b/system/core/Benchmark.php index f6b634deb..39027e809 100755 --- a/system/core/Benchmark.php +++ b/system/core/Benchmark.php @@ -61,7 +61,7 @@ class CI_Benchmark { */ public function mark($name) { - $this->marker[$name] = microtime(); + $this->marker[$name] = microtime(TRUE); } // -------------------------------------------------------------------- @@ -93,13 +93,10 @@ class CI_Benchmark { if ( ! isset($this->marker[$point2])) { - $this->marker[$point2] = microtime(); + $this->marker[$point2] = microtime(TRUE); } - list($sm, $ss) = explode(' ', $this->marker[$point1]); - list($em, $es) = explode(' ', $this->marker[$point2]); - - return number_format(($em + $es) - ($sm + $ss), $decimals); + return number_format($this->marker[$point2] - $this->marker[$point1], $decimals); } // -------------------------------------------------------------------- @@ -122,4 +119,4 @@ class CI_Benchmark { } /* End of file Benchmark.php */ -/* Location: ./system/core/Benchmark.php */ +/* Location: ./system/core/Benchmark.php */ \ No newline at end of file diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 61b05d52b..cb04c7103 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -335,7 +335,7 @@ abstract class CI_DB_driver { } // Start the Query Timer - $time_start = list($sm, $ss) = explode(' ', microtime()); + $time_start = microtime(TRUE); // Run the Query if (FALSE === ($this->result_id = $this->simple_query($sql))) @@ -370,12 +370,12 @@ abstract class CI_DB_driver { } // Stop and aggregate the query time results - $time_end = list($em, $es) = explode(' ', microtime()); - $this->benchmark += ($em + $es) - ($sm + $ss); + $time_end = microtime(TRUE); + $this->benchmark += $time_end - $time_start; if ($this->save_queries == TRUE) { - $this->query_times[] = ($em + $es) - ($sm + $ss); + $this->query_times[] = $time_end - $time_start; } // Increment the query counter -- cgit v1.2.3-24-g4f1b From b3f774bbbb81293326136e3e13297c3b8d49dfa4 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 23 Apr 2012 12:57:57 +0300 Subject: Fix issue #1265 --- system/core/CodeIgniter.php | 16 ++++++++-------- user_guide_src/source/changelog.rst | 1 + 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/system/core/CodeIgniter.php b/system/core/CodeIgniter.php index 4885f310c..92187fa16 100755 --- a/system/core/CodeIgniter.php +++ b/system/core/CodeIgniter.php @@ -275,12 +275,12 @@ { $x = explode('/', $RTR->routes['404_override'], 2); $class = $x[0]; - $method = (isset($x[1]) ? $x[1] : 'index'); + $method = isset($x[1]) ? $x[1] : 'index'; if ( ! class_exists($class)) { if ( ! file_exists(APPPATH.'controllers/'.$class.'.php')) { - show_404("{$class}/{$method}"); + show_404($class.'/'.$method); } include_once(APPPATH.'controllers/'.$class.'.php'); @@ -288,7 +288,7 @@ } else { - show_404("{$class}/{$method}"); + show_404($class.'/'.$method); } } @@ -337,12 +337,12 @@ { $x = explode('/', $RTR->routes['404_override'], 2); $class = $x[0]; - $method = (isset($x[1]) ? $x[1] : 'index'); + $method = isset($x[1]) ? $x[1] : 'index'; if ( ! class_exists($class)) { if ( ! file_exists(APPPATH.'controllers/'.$class.'.php')) { - show_404("{$class}/{$method}"); + show_404($class.'/'.$method); } include_once(APPPATH.'controllers/'.$class.'.php'); @@ -352,7 +352,7 @@ } else { - show_404("{$class}/{$method}"); + show_404($class.'/'.$method); } } @@ -393,10 +393,10 @@ * Close the DB connection if one exists * ------------------------------------------------------ */ - if (class_exists('CI_DB') && isset($CI->db)) + if (class_exists('CI_DB') && isset($CI->db) && ! $CI->db->pconnect) { $CI->db->close(); } /* End of file CodeIgniter.php */ -/* Location: ./system/core/CodeIgniter.php */ +/* Location: ./system/core/CodeIgniter.php */ \ No newline at end of file diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 0cac8aea0..f9b087e83 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -207,6 +207,7 @@ Bug fixes for 3.0 - Fixed a bug (#798) - update() used to ignore LIKE conditions that were set with like(). - Fixed a bug in Oracle's and MSSQL's delete() methods where an erroneous SQL statement was generated when used with limit(). - Fixed a bug in SQLSRV's delete() method where like() and limit() conditions were ignored. +- Fixed a bug (#1265) - Database connections were always closed, regardless of the 'pconnect' option value. Version 2.1.1 ============= -- cgit v1.2.3-24-g4f1b From 31d30bcf206ed2e9168b93f74511708d6bc4b505 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 23 Apr 2012 08:58:42 -0400 Subject: Merge upstream --- system/core/CodeIgniter.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/system/core/CodeIgniter.php b/system/core/CodeIgniter.php index 9714d095c..7deccd048 100755 --- a/system/core/CodeIgniter.php +++ b/system/core/CodeIgniter.php @@ -25,8 +25,6 @@ * @filesource */ -// ------------------------------------------------------------------------ - /** * System Initialization File * @@ -406,4 +404,4 @@ } /* End of file CodeIgniter.php */ -/* Location: ./system/core/CodeIgniter.php */ +/* Location: ./system/core/CodeIgniter.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From d52e3b19eb03bcd38f89ccbf5aae68364f715343 Mon Sep 17 00:00:00 2001 From: Sam Li Date: Mon, 23 Apr 2012 23:04:35 +0800 Subject: Added fix for issue: https://github.com/EllisLab/CodeIgniter/issues/1277 --- system/helpers/download_helper.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/system/helpers/download_helper.php b/system/helpers/download_helper.php index 19192a147..18fe96223 100644 --- a/system/helpers/download_helper.php +++ b/system/helpers/download_helper.php @@ -100,6 +100,9 @@ if ( ! function_exists('force_download')) $x[count($x) - 1] = strtoupper($extension); $filename = implode('.', $x); } + + //Clean output buffer + ob_clean(); // Generate the server headers header('Content-Type: '.$mime); -- cgit v1.2.3-24-g4f1b From bbd9dd3df9d373fbac8560db926a9f70a5bde3b7 Mon Sep 17 00:00:00 2001 From: Sam Li Date: Mon, 23 Apr 2012 23:13:46 +0800 Subject: Tweak to comments to conform to style guide. --- system/helpers/download_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/helpers/download_helper.php b/system/helpers/download_helper.php index 18fe96223..8b87f8179 100644 --- a/system/helpers/download_helper.php +++ b/system/helpers/download_helper.php @@ -101,7 +101,7 @@ if ( ! function_exists('force_download')) $filename = implode('.', $x); } - //Clean output buffer + // Clean output buffer ob_clean(); // Generate the server headers -- cgit v1.2.3-24-g4f1b From 48a7fbbeb53e82e9298036d40c42ec2564699ed0 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 23 Apr 2012 11:58:16 -0400 Subject: Use tabs to separate class properties --- system/core/Benchmark.php | 4 ++-- system/core/CodeIgniter.php | 2 +- system/core/Common.php | 1 - system/core/Config.php | 6 +++--- system/core/Controller.php | 2 +- system/core/Exceptions.php | 4 ++-- system/core/Hooks.php | 6 +++--- system/core/Input.php | 14 +++++++------- system/core/Lang.php | 4 ++-- system/core/Loader.php | 22 +++++++++++----------- system/core/Output.php | 14 +++++++------- system/core/Router.php | 10 +++++----- system/core/Security.php | 14 +++++++------- system/core/URI.php | 6 +++--- 14 files changed, 54 insertions(+), 55 deletions(-) diff --git a/system/core/Benchmark.php b/system/core/Benchmark.php index 39027e809..c17e95a19 100755 --- a/system/core/Benchmark.php +++ b/system/core/Benchmark.php @@ -46,7 +46,7 @@ class CI_Benchmark { * * @var array */ - public $marker = array(); + public $marker = array(); // -------------------------------------------------------------------- @@ -119,4 +119,4 @@ class CI_Benchmark { } /* End of file Benchmark.php */ -/* Location: ./system/core/Benchmark.php */ \ No newline at end of file +/* Location: ./system/core/Benchmark.php */ diff --git a/system/core/CodeIgniter.php b/system/core/CodeIgniter.php index 293582243..793c4687e 100755 --- a/system/core/CodeIgniter.php +++ b/system/core/CodeIgniter.php @@ -40,7 +40,7 @@ /** * CodeIgniter Version * - * @var string + * @var string * */ define('CI_VERSION', '3.0-dev'); diff --git a/system/core/Common.php b/system/core/Common.php index 075b221bd..78aa6e874 100644 --- a/system/core/Common.php +++ b/system/core/Common.php @@ -39,7 +39,6 @@ // ------------------------------------------------------------------------ - if ( ! function_exists('is_php')) { /** diff --git a/system/core/Config.php b/system/core/Config.php index fc6183444..1eab08b82 100755 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -45,14 +45,14 @@ class CI_Config { * * @var array */ - public $config = array(); + public $config = array(); /** * List of all loaded config files * * @var array */ - public $is_loaded = array(); + public $is_loaded = array(); /** * List of paths to search when trying to load a config file. @@ -60,7 +60,7 @@ class CI_Config { * * @var array */ - public $_config_paths = array(APPPATH); + public $_config_paths = array(APPPATH); /** * Constructor diff --git a/system/core/Controller.php b/system/core/Controller.php index 0c6c47183..1f69146d0 100644 --- a/system/core/Controller.php +++ b/system/core/Controller.php @@ -42,7 +42,7 @@ class CI_Controller { /** * Reference to the global CI instance * - * @var object + * @var object */ private static $instance; diff --git a/system/core/Exceptions.php b/system/core/Exceptions.php index f55f9fcba..2e9f0c766 100755 --- a/system/core/Exceptions.php +++ b/system/core/Exceptions.php @@ -39,14 +39,14 @@ class CI_Exceptions { /** * Nesting level of the output buffering mechanism * - * @var int + * @var int */ public $ob_level; /** * List if available error levels * - * @var array + * @var array */ public $levels = array( E_ERROR => 'Error', diff --git a/system/core/Hooks.php b/system/core/Hooks.php index 672fe29dd..b42ecbe20 100755 --- a/system/core/Hooks.php +++ b/system/core/Hooks.php @@ -43,21 +43,21 @@ class CI_Hooks { * * @var bool */ - public $enabled = FALSE; + public $enabled = FALSE; /** * List of all hooks set in config/hooks.php * * @var array */ - public $hooks = array(); + public $hooks = array(); /** * Determines wether hook is in progress, used to prevent infinte loops * * @var bool */ - public $in_progress = FALSE; + public $in_progress = FALSE; /** * Initialize the Hooks Preferences diff --git a/system/core/Input.php b/system/core/Input.php index 7ee8f3685..fc2a550bc 100755 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -43,28 +43,28 @@ class CI_Input { * * @var string */ - public $ip_address = FALSE; + public $ip_address = FALSE; /** * user agent (web browser) being used by the current user * * @var string */ - public $user_agent = FALSE; + public $user_agent = FALSE; /** * If FALSE, then $_GET will be set to an empty array * * @var bool */ - protected $_allow_get_array = TRUE; + protected $_allow_get_array = TRUE; /** * If TRUE, then newlines are standardized * * @var bool */ - protected $_standardize_newlines = TRUE; + protected $_standardize_newlines = TRUE; /** * Determines whether the XSS filter is always active when GET, POST or COOKIE data is encountered @@ -72,7 +72,7 @@ class CI_Input { * * @var bool */ - protected $_enable_xss = FALSE; + protected $_enable_xss = FALSE; /** * Enables a CSRF cookie token to be set. @@ -80,14 +80,14 @@ class CI_Input { * * @var bool */ - protected $_enable_csrf = FALSE; + protected $_enable_csrf = FALSE; /** * List of all HTTP request headers * * @var array */ - protected $headers = array(); + protected $headers = array(); /** * Constructor diff --git a/system/core/Lang.php b/system/core/Lang.php index 7abdc6102..2963012fc 100755 --- a/system/core/Lang.php +++ b/system/core/Lang.php @@ -41,14 +41,14 @@ class CI_Lang { * * @var array */ - public $language = array(); + public $language = array(); /** * List of loaded language files * * @var array */ - public $is_loaded = array(); + public $is_loaded = array(); /** * Initialize language class diff --git a/system/core/Loader.php b/system/core/Loader.php index b99ce6964..7fb3eab41 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -51,77 +51,77 @@ class CI_Loader { * * @var array */ - protected $_ci_view_paths = array(); + protected $_ci_view_paths = array(); /** * List of paths to load libraries from * * @var array */ - protected $_ci_library_paths = array(); + protected $_ci_library_paths = array(); /** * List of paths to load models from * * @var array */ - protected $_ci_model_paths = array(); + protected $_ci_model_paths = array(); /** * List of paths to load helpers from * * @var array */ - protected $_ci_helper_paths = array(); + protected $_ci_helper_paths = array(); /** * List of loaded base classes * * @var array */ - protected $_base_classes = array(); // Set by the controller class + protected $_base_classes = array(); // Set by the controller class /** * List of cached variables * * @var array */ - protected $_ci_cached_vars = array(); + protected $_ci_cached_vars = array(); /** * List of loaded classes * * @var array */ - protected $_ci_classes = array(); + protected $_ci_classes = array(); /** * List of loaded files * * @var array */ - protected $_ci_loaded_files = array(); + protected $_ci_loaded_files = array(); /** * List of loaded models * * @var array */ - protected $_ci_models = array(); + protected $_ci_models = array(); /** * List of loaded helpers * * @var array */ - protected $_ci_helpers = array(); + protected $_ci_helpers = array(); /** * List of class name mappings * * @var array */ - protected $_ci_varmap = array( + protected $_ci_varmap = array( 'unit_test' => 'unit', 'user_agent' => 'agent' ); diff --git a/system/core/Output.php b/system/core/Output.php index 0683539c9..513c657a6 100755 --- a/system/core/Output.php +++ b/system/core/Output.php @@ -50,49 +50,49 @@ class CI_Output { * * @var int */ - public $cache_expiration = 0; + public $cache_expiration = 0; /** * List of server headers * * @var array */ - public $headers = array(); + public $headers = array(); /** * List of mime types * * @var array */ - public $mime_types = array(); + public $mime_types = array(); /** * Determines wether profiler is enabled * * @var book */ - public $enable_profiler = FALSE; + public $enable_profiler = FALSE; /** * Determines if output compression is enabled * * @var bool */ - protected $_zlib_oc = FALSE; + protected $_zlib_oc = FALSE; /** * List of profiler sections * * @var array */ - protected $_profiler_sections = array(); + protected $_profiler_sections = array(); /** * Whether or not to parse variables like {elapsed_time} and {memory_usage} * * @var bool */ - public $parse_exec_vars = TRUE; + public $parse_exec_vars = TRUE; /** * Set up Output class diff --git a/system/core/Router.php b/system/core/Router.php index b5c200214..9314052fe 100755 --- a/system/core/Router.php +++ b/system/core/Router.php @@ -50,35 +50,35 @@ class CI_Router { * * @var array */ - public $routes = array(); + public $routes = array(); /** * List of error routes * * @var array */ - public $error_routes = array(); + public $error_routes = array(); /** * Current class name * * @var string */ - public $class = ''; + public $class = ''; /** * Current method name * * @var string */ - public $method = 'index'; + public $method = 'index'; /** * Sub-directory that contains the requested controller class * * @var string */ - public $directory = ''; + public $directory = ''; /** * Default controller (and method if specific) diff --git a/system/core/Security.php b/system/core/Security.php index 139511661..974e2e428 100755 --- a/system/core/Security.php +++ b/system/core/Security.php @@ -41,14 +41,14 @@ class CI_Security { * * @var string */ - protected $_xss_hash = ''; + protected $_xss_hash = ''; /** * Random Hash for Cross Site Request Forgery Protection Cookie * * @var string */ - protected $_csrf_hash = ''; + protected $_csrf_hash = ''; /** * Expiration time for Cross Site Request Forgery Protection Cookie @@ -56,28 +56,28 @@ class CI_Security { * * @var int */ - protected $_csrf_expire = 7200; + protected $_csrf_expire = 7200; /** * Token name for Cross Site Request Forgery Protection Cookie * * @var string */ - protected $_csrf_token_name = 'ci_csrf_token'; + protected $_csrf_token_name = 'ci_csrf_token'; /** * Cookie name for Cross Site Request Forgery Protection Cookie * * @var string */ - protected $_csrf_cookie_name = 'ci_csrf_token'; + protected $_csrf_cookie_name = 'ci_csrf_token'; /** * List of never allowed strings * * @var array */ - protected $_never_allowed_str = array( + protected $_never_allowed_str = array( 'document.cookie' => '[removed]', 'document.write' => '[removed]', '.parentNode' => '[removed]', @@ -95,7 +95,7 @@ class CI_Security { * * @var array */ - protected $_never_allowed_regex = array( + protected $_never_allowed_regex = array( 'javascript\s*:', 'expression\s*(\(|&\#40;)', // CSS and IE 'vbscript\s*:', // IE, surprise! diff --git a/system/core/URI.php b/system/core/URI.php index 140295338..ff2c458b8 100755 --- a/system/core/URI.php +++ b/system/core/URI.php @@ -42,7 +42,7 @@ class CI_URI { * * @var array */ - public $keyval = array(); + public $keyval = array(); /** * Current uri string @@ -56,7 +56,7 @@ class CI_URI { * * @var array */ - public $segments = array(); + public $segments = array(); /** * Re-indexed list of uri segments @@ -64,7 +64,7 @@ class CI_URI { * * @var array */ - public $rsegments = array(); + public $rsegments = array(); /** * Constructor -- cgit v1.2.3-24-g4f1b From 4b47e980696fdcd9e3cf36e05e075c401d20ffbd Mon Sep 17 00:00:00 2001 From: st2 Date: Tue, 24 Apr 2012 17:45:44 +0800 Subject: Update system/database/drivers/sqlsrv/sqlsrv_driver.php --- system/database/drivers/sqlsrv/sqlsrv_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/database/drivers/sqlsrv/sqlsrv_driver.php b/system/database/drivers/sqlsrv/sqlsrv_driver.php index 951567033..c318f3060 100644 --- a/system/database/drivers/sqlsrv/sqlsrv_driver.php +++ b/system/database/drivers/sqlsrv/sqlsrv_driver.php @@ -440,7 +440,7 @@ class CI_DB_sqlsrv_driver extends CI_DB { $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); } - return 'UPDATE '.$table.' SET '.implode(', ', $valstr).' WHERE '.$where; + return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 25ead353e8e7cf17d627eda1fe10ef218418c1ff Mon Sep 17 00:00:00 2001 From: George Petsagourakis Date: Tue, 24 Apr 2012 20:07:58 +0300 Subject: Added Greek characters to the foreign_chars.php --- application/config/foreign_chars.php | 61 +++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/application/config/foreign_chars.php b/application/config/foreign_chars.php index e2d3f236e..be97b6310 100644 --- a/application/config/foreign_chars.php +++ b/application/config/foreign_chars.php @@ -40,50 +40,55 @@ $foreign_characters = array( '/Ä/' => 'Ae', '/Ü/' => 'Ue', '/Ö/' => 'Oe', - '/À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => 'A', - '/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/' => 'a', + '/À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ|Α|Ά/' => 'A', + '/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª|α|ά/' => 'a', '/Ç|Ć|Ĉ|Ċ|Č/' => 'C', '/ç|ć|ĉ|ċ|č/' => 'c', - '/Ð|Ď|Đ/' => 'Dj', - '/ð|ď|đ/' => 'dj', - '/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě/' => 'E', - '/è|é|ê|ë|ē|ĕ|ė|ę|ě/' => 'e', - '/Ĝ|Ğ|Ġ|Ģ/' => 'G', - '/ĝ|ğ|ġ|ģ/' => 'g', + '/Ð|Ď|Đ|Δ/' => 'Dj', + '/ð|ď|đ|δ/' => 'dj', + '/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě|Ε|Έ/' => 'E', + '/è|é|ê|ë|ē|ĕ|ė|ę|ě|έ|ε/' => 'e', + '/Ĝ|Ğ|Ġ|Ģ|Γ/' => 'G', + '/ĝ|ğ|ġ|ģ|γ/' => 'g', '/Ĥ|Ħ/' => 'H', '/ĥ|ħ/' => 'h', - '/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ/' => 'I', - '/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı/' => 'i', + '/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ|Η|Ή|Ί|Ι|Ϊ/' => 'I', + '/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı|η|ή|ί|ι|ϊ/' => 'i', '/Ĵ/' => 'J', '/ĵ/' => 'j', - '/Ķ/' => 'K', - '/ķ/' => 'k', - '/Ĺ|Ļ|Ľ|Ŀ|Ł/' => 'L', - '/ĺ|ļ|ľ|ŀ|ł/' => 'l', - '/Ñ|Ń|Ņ|Ň/' => 'N', - '/ñ|ń|ņ|ň|ʼn/' => 'n', - '/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ/' => 'O', - '/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º/' => 'o', - '/Ŕ|Ŗ|Ř/' => 'R', - '/ŕ|ŗ|ř/' => 'r', - '/Ś|Ŝ|Ş|Š/' => 'S', - '/ś|ŝ|ş|š|ſ/' => 's', - '/Ţ|Ť|Ŧ/' => 'T', + '/Ķ|Κ/' => 'K', + '/ķ|κ/' => 'k', + '/Ĺ|Ļ|Ľ|Ŀ|Ł|Λ/' => 'L', + '/ĺ|ļ|ľ|ŀ|ł|λ/' => 'l', + '/Ñ|Ń|Ņ|Ň|Ν/' => 'N', + '/ñ|ń|ņ|ň|ʼn|ν/' => 'n', + '/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ|Ο|Ό|Ω|Ώ/' => 'O', + '/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º|ο|ό|ω|ώ/' => 'o', + '/Ŕ|Ŗ|Ř|Ρ/' => 'R', + '/ŕ|ŗ|ř|ρ/' => 'r', + '/Ś|Ŝ|Ş|Š|Σ/' => 'S', + '/ś|ŝ|ş|š|ſ|σ|ς/' => 's', + '/Ţ|Ť|Ŧ|τ/' => 'T', '/ţ|ť|ŧ/' => 't', '/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ/' => 'U', - '/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'u', - '/Ý|Ÿ|Ŷ/' => 'Y', + '/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ|υ|ύ|ϋ/' => 'u', + '/Ý|Ÿ|Ŷ|Υ|Ύ|Ϋ/' => 'Y', '/ý|ÿ|ŷ/' => 'y', '/Ŵ/' => 'W', '/ŵ/' => 'w', - '/Ź|Ż|Ž/' => 'Z', - '/ź|ż|ž/' => 'z', + '/Ź|Ż|Ž|Ζ/' => 'Z', + '/ź|ż|ž|ζ/' => 'z', '/Æ|Ǽ/' => 'AE', '/ß/'=> 'ss', '/IJ/' => 'IJ', '/ij/' => 'ij', '/Œ/' => 'OE', - '/ƒ/' => 'f' + '/ƒ/' => 'f', + '/ξ/' => 'ks', + '/π/' => 'p', + '/β/' => 'v', + '/μ/' => 'm', + '/ψ/' => 'ps', ); /* End of file foreign_chars.php */ -- cgit v1.2.3-24-g4f1b From 07f09ac02a0e23a863b34aa1bdc03f501303f659 Mon Sep 17 00:00:00 2001 From: George Petsagourakis Date: Tue, 24 Apr 2012 20:09:29 +0300 Subject: Added Romanian characters to the foreign_chars.php fixes #131 --- application/config/foreign_chars.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/application/config/foreign_chars.php b/application/config/foreign_chars.php index be97b6310..41de123da 100644 --- a/application/config/foreign_chars.php +++ b/application/config/foreign_chars.php @@ -66,10 +66,10 @@ $foreign_characters = array( '/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º|ο|ό|ω|ώ/' => 'o', '/Ŕ|Ŗ|Ř|Ρ/' => 'R', '/ŕ|ŗ|ř|ρ/' => 'r', - '/Ś|Ŝ|Ş|Š|Σ/' => 'S', - '/ś|ŝ|ş|š|ſ|σ|ς/' => 's', - '/Ţ|Ť|Ŧ|τ/' => 'T', - '/ţ|ť|ŧ/' => 't', + '/Ś|Ŝ|Ş|Ș|Š|Σ/' => 'S', + '/ś|ŝ|ş|ș|š|ſ|σ|ς/' => 's', + '/Ț|Ţ|Ť|Ŧ|τ/' => 'T', + '/ț|ţ|ť|ŧ/' => 't', '/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ/' => 'U', '/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ|υ|ύ|ϋ/' => 'u', '/Ý|Ÿ|Ŷ|Υ|Ύ|Ϋ/' => 'Y', -- cgit v1.2.3-24-g4f1b From e7bbb1dc97c6f2027db5f24383f8d93c4ba41d82 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 24 Apr 2012 21:11:21 +0300 Subject: Add changelog entry for already fixed issue #23 --- user_guide_src/source/changelog.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index f9b087e83..3bd8f989e 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -197,7 +197,7 @@ Bug fixes for 3.0 - Fixed a bug in the library loader where some PHP versions wouldn't execute the class constructor. - Fixed a bug (#88) - An unexisting property was used for configuration of the Memcache cache driver. - Fixed a bug (#14) - create_database() method in the :doc:`Database Forge Library ` didn't utilize the configured database character set. -- Fixed a bug (#1238) - delete_all() in the `Database Caching Library ` used to delete .htaccess and index.html files, which is a potential security risk. +- Fixed a bug (#23, #1238) - delete_all() in the `Database Caching Library ` used to delete .htaccess and index.html files, which is a potential security risk. - Fixed a bug in :doc:`Trackback Library ` method validate_url() where it didn't actually do anything, due to input not being passed by reference. - Fixed a bug (#11, #183, #863) - CI_Form_validation::_execute() silently continued to the next rule, if a rule method/function is not found. - Fixed a bug (#1242) - read_dir() in the :doc:`Zip Library ` wasn't compatible with Windows. -- cgit v1.2.3-24-g4f1b From 14c350c77c232ab7a5cf68c30fdbab5cdac2d403 Mon Sep 17 00:00:00 2001 From: st2 Date: Wed, 25 Apr 2012 02:48:49 +0800 Subject: Update system/helpers/file_helper.php --- system/helpers/file_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/helpers/file_helper.php b/system/helpers/file_helper.php index 3a373efd3..a5aabecd6 100644 --- a/system/helpers/file_helper.php +++ b/system/helpers/file_helper.php @@ -145,7 +145,7 @@ if ( ! function_exists('delete_files')) { delete_files($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $level + 1, $htdocs); } - elseif ($htdocs === TRUE && ! preg_match('/^(\.htaccess|index\.(html|htm|php))$/', $filename)) + elseif ($htdocs === TRUE && ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename)) { @unlink($path.DIRECTORY_SEPARATOR.$filename); } -- cgit v1.2.3-24-g4f1b From 72b1076858662f535d42811c005e718f3114a1a8 Mon Sep 17 00:00:00 2001 From: George Petsagourakis Date: Wed, 25 Apr 2012 11:26:46 +0300 Subject: Added foreign_characters.php changes in the changelog. --- user_guide_src/source/changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index f9b087e83..65790cab1 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -29,6 +29,7 @@ Release Date: Not Released - Added support for 3gp, 3g2, mp4, wmv, f4v, vlc Video files to mimes.php. - Added support for m4a, aac, m4u, xspf, au, ac3, flac, ogg Audio files to mimes.php. - Added support for kmz and kml (Google Earth) files to mimes.php. + - Added Romanian and Greek characters in foreign_characters.php - Updated support for doc files in mimes.php. - Added application/xml for xml and application/xml, text/xsl for xsl in mimes.php. - Changed logger to only chmod when file is first created. -- cgit v1.2.3-24-g4f1b From a28812aed2aa56d4e8e69ac3560ad33aa4b82f38 Mon Sep 17 00:00:00 2001 From: Chad Hedgcock Date: Wed, 25 Apr 2012 23:29:52 -0300 Subject: Corrected path to jquery --- user_guide_src/source/libraries/javascript.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/user_guide_src/source/libraries/javascript.rst b/user_guide_src/source/libraries/javascript.rst index 5e80fb998..d5e09c314 100644 --- a/user_guide_src/source/libraries/javascript.rst +++ b/user_guide_src/source/libraries/javascript.rst @@ -86,14 +86,14 @@ The jQuery Class To initialize the jQuery class manually in your controller constructor, use the $this->load->library function:: - $this->load->library('jquery'); + $this->load->library('javascript/jquery'); You may send an optional parameter to determine whether or not a script tag for the main jQuery file will be automatically included when loading the library. It will be created by default. To prevent this, load the library as follows:: - $this->load->library('jquery', FALSE); + $this->load->library('javascript/jquery', FALSE); Once loaded, the jQuery library object will be available using: $this->jquery -- cgit v1.2.3-24-g4f1b From 606d246d7583a42582c9c283bc2163fec68d30eb Mon Sep 17 00:00:00 2001 From: Jamie Rumbelow Date: Thu, 26 Apr 2012 13:38:44 +0100 Subject: Adding some update notes for 3.0.0 (renaming AR to QB) --- user_guide_src/source/installation/upgrade_300.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/user_guide_src/source/installation/upgrade_300.rst b/user_guide_src/source/installation/upgrade_300.rst index 4c594ab17..e434e8d45 100644 --- a/user_guide_src/source/installation/upgrade_300.rst +++ b/user_guide_src/source/installation/upgrade_300.rst @@ -31,3 +31,13 @@ Step 3: Remove $autoload['core'] from your config/autoload.php Use of the `$autoload['core']` config array has been deprecated as of CodeIgniter 1.4.1 and is now removed. Move any entries that you might have listed there to `$autoload['libraries']` instead. + +Step 4: Update your config/database.php +======================================= + +Due to 3.0.0's renaming of Active Record to Query Builder, inside your _config/database.php_, you will +need to rename the `$active_record` variable to `$query_builder`. + + $active_group = 'default'; + // $active_record = TRUE; + $query_builder = TRUE; \ No newline at end of file -- cgit v1.2.3-24-g4f1b From ffe7a0a32c414bfda8a115860a55bf7ceedf6f79 Mon Sep 17 00:00:00 2001 From: Jamie Rumbelow Date: Thu, 26 Apr 2012 13:48:18 +0100 Subject: A couple more documentation updates --- system/database/drivers/interbase/interbase_driver.php | 2 +- system/database/drivers/sqlite3/sqlite3_driver.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/system/database/drivers/interbase/interbase_driver.php b/system/database/drivers/interbase/interbase_driver.php index 6587d72ff..1b18de803 100644 --- a/system/database/drivers/interbase/interbase_driver.php +++ b/system/database/drivers/interbase/interbase_driver.php @@ -29,7 +29,7 @@ * Firebird/Interbase Database Adapter Class * * Note: _DB is an extender class that the app controller - * creates dynamically based on whether the active record + * creates dynamically based on whether the query builder * class is being used or not. * * @package CodeIgniter diff --git a/system/database/drivers/sqlite3/sqlite3_driver.php b/system/database/drivers/sqlite3/sqlite3_driver.php index fb45bee9c..d22f6a442 100644 --- a/system/database/drivers/sqlite3/sqlite3_driver.php +++ b/system/database/drivers/sqlite3/sqlite3_driver.php @@ -29,7 +29,7 @@ * SQLite3 Database Adapter Class * * Note: _DB is an extender class that the app controller - * creates dynamically based on whether the active record + * creates dynamically based on whether the query builder * class is being used or not. * * @package CodeIgniter -- cgit v1.2.3-24-g4f1b From e16192cacc55a6fae356ec27320a5d4e77bd2949 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Fri, 27 Apr 2012 02:13:21 +0700 Subject: PDO patch and run the test --- .travis.yml | 3 ++- system/database/drivers/pdo/pdo_driver.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 97ea0422d..38fa7163a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,4 +25,5 @@ script: phpunit --configuration tests/travis/$DB.phpunit.xml branches: only: - develop - - master \ No newline at end of file + - master + - pdo-patch \ No newline at end of file diff --git a/system/database/drivers/pdo/pdo_driver.php b/system/database/drivers/pdo/pdo_driver.php index 60151b900..d7d4db358 100644 --- a/system/database/drivers/pdo/pdo_driver.php +++ b/system/database/drivers/pdo/pdo_driver.php @@ -225,7 +225,7 @@ class CI_DB_pdo_driver extends CI_DB { * * Reference: http://www.php.net/manual/en/ref.pdo-mysql.connection.php */ - if ($this->subdriver === 'mysql' && ! is_php('5.3.6') && ! empty($this->char_set)) + if ($this->pdodriver === 'mysql' && ! is_php('5.3.6') && ! empty($this->char_set)) { $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES '.$this->char_set .( ! empty($this->db_collat) ? " COLLATE '".$this->dbcollat."'" : ''); -- cgit v1.2.3-24-g4f1b From 5dba032b6d53cf91ab733c61bd5795c81f021af6 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Fri, 27 Apr 2012 02:18:34 +0700 Subject: Remove temporary branch from travis whitelist --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 38fa7163a..97ea0422d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,5 +25,4 @@ script: phpunit --configuration tests/travis/$DB.phpunit.xml branches: only: - develop - - master - - pdo-patch \ No newline at end of file + - master \ No newline at end of file -- cgit v1.2.3-24-g4f1b From cdfbd5281e4dec7278a07943832c2590c1141eb3 Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Thu, 26 Apr 2012 22:13:31 +0100 Subject: Fixed issue #122 - ruri_string() in subdirs. --- system/core/Router.php | 5 ++++- system/core/URI.php | 4 ++-- user_guide_src/source/changelog.rst | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/system/core/Router.php b/system/core/Router.php index 5477fed5d..fa88cb3dc 100755 --- a/system/core/Router.php +++ b/system/core/Router.php @@ -237,9 +237,12 @@ class CI_Router { $segments[1] = 'index'; } + // This is being routed to a file in a sub directory + $this->directory and array_unshift($segments, trim($this->directory, '/')); + // Update our "routed" segment array to contain the segments. // Note: If there is no custom routing, this array will be - // identical to $this->uri->segments + // identical to $this->uri->segments $this->uri->rsegments = $segments; } diff --git a/system/core/URI.php b/system/core/URI.php index 48bb7ae3c..17300df07 100755 --- a/system/core/URI.php +++ b/system/core/URI.php @@ -641,10 +641,10 @@ class CI_URI { */ public function ruri_string() { - return '/'.implode('/', $this->rsegment_array()); + return implode('/', $this->rsegment_array()); } } /* End of file URI.php */ -/* Location: ./system/core/URI.php */ +/* Location: ./system/core/URI.php */ \ No newline at end of file diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 1c8647bb7..615e0f734 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -195,6 +195,7 @@ Bug fixes for 3.0 - Fixed a bug in :doc:`Trackback Library ` method validate_url() where it didn't actually do anything, due to input not being passed by reference. - Fixed a bug (#11, #183, #863) - CI_Form_validation::_execute() silently continued to the next rule, if a rule method/function is not found. - Fixed a bug (#1242) Added Windows path compatibility to function read_dir of ZIP library +- Fixed a bug (#122) Where routed uri string was being reported incorrectly in sub-directories Version 2.1.1 ============= -- cgit v1.2.3-24-g4f1b