diff options
Diffstat (limited to 'system/database')
-rw-r--r-- | system/database/DB.php | 18 | ||||
-rw-r--r-- | system/database/DB_active_rec.php | 10 | ||||
-rw-r--r-- | system/database/DB_driver.php | 12 | ||||
-rw-r--r-- | system/database/DB_forge.php | 8 | ||||
-rw-r--r-- | system/database/DB_result.php | 85 | ||||
-rw-r--r-- | system/database/drivers/mssql/mssql_driver.php | 4 | ||||
-rw-r--r-- | system/database/drivers/mysql/mysql_driver.php | 20 | ||||
-rw-r--r-- | system/database/drivers/mysql/mysql_forge.php | 23 | ||||
-rw-r--r-- | system/database/drivers/mysqli/mysqli_driver.php | 10 | ||||
-rw-r--r-- | system/database/drivers/oci8/oci8_driver.php | 6 | ||||
-rw-r--r-- | system/database/drivers/odbc/odbc_driver.php | 4 | ||||
-rw-r--r-- | system/database/drivers/odbc/odbc_result.php | 4 | ||||
-rw-r--r-- | system/database/drivers/postgre/postgre_driver.php | 14 | ||||
-rw-r--r-- | system/database/drivers/sqlite/sqlite_driver.php | 4 |
14 files changed, 160 insertions, 62 deletions
diff --git a/system/database/DB.php b/system/database/DB.php index fb0516ba4..93ee3922a 100644 --- a/system/database/DB.php +++ b/system/database/DB.php @@ -27,7 +27,21 @@ function &DB($params = '', $active_record_override = NULL) // Load the DB config file if a DSN string wasn't passed if (is_string($params) AND strpos($params, '://') === FALSE) { - include(APPPATH.'config/database'.EXT); + + $file_path = APPPATH.'config/'.ENVIRONMENT.'/database'.EXT; + + if ( ! file_exists($file_path)) + { + log_message('debug', 'Database config for '.ENVIRONMENT.' environment is not found. Trying global config.'); + $file_path = APPPATH.'config/database'.EXT; + + if ( ! file_exists($file_path)) + { + continue; + } + } + + include($file_path); if ( ! isset($db) OR count($db) == 0) { @@ -74,7 +88,7 @@ function &DB($params = '', $active_record_override = NULL) { parse_str($dns['query'], $extra); - foreach($extra as $key => $val) + foreach ($extra as $key => $val) { // booleans please if (strtoupper($val) == "TRUE") diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index ce9d1c1af..ee72dbbf4 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -954,7 +954,7 @@ class CI_DB_active_record extends CI_DB_driver { } else { - $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v); + $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v); } } @@ -1156,7 +1156,7 @@ class CI_DB_active_record extends CI_DB_driver { $this->ar_set[] = array(); return; } - + ksort($row); // puts $row in the same order as our keys if ($escape === FALSE) @@ -1167,7 +1167,7 @@ class CI_DB_active_record extends CI_DB_driver { { $clean = array(); - foreach($row as $value) + foreach ($row as $value) { $clean[] = $this->escape($value); } @@ -1425,7 +1425,7 @@ class CI_DB_active_record extends CI_DB_driver { $index_set = FALSE; $clean = array(); - foreach($v as $k2 => $v2) + foreach ($v as $k2 => $v2) { if ($k2 == $index) { @@ -1569,7 +1569,7 @@ class CI_DB_active_record extends CI_DB_driver { } elseif (is_array($table)) { - foreach($table as $single_table) + foreach ($table as $single_table) { $this->delete($single_table, $where, $limit, FALSE); } diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 2d8f592e3..e7a9de475 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -767,7 +767,7 @@ class CI_DB_driver { if ($query->num_rows() > 0) { - foreach($query->result_array() as $row) + foreach ($query->result_array() as $row) { if (isset($row['TABLE_NAME'])) { @@ -834,7 +834,7 @@ class CI_DB_driver { $query = $this->query($sql); $retval = array(); - foreach($query->result_array() as $row) + foreach ($query->result_array() as $row) { if (isset($row['COLUMN_NAME'])) { @@ -904,7 +904,7 @@ class CI_DB_driver { $fields = array(); $values = array(); - foreach($data as $key => $val) + foreach ($data as $key => $val) { $fields[] = $this->_escape_identifiers($key); $values[] = $this->escape($val); @@ -932,7 +932,7 @@ class CI_DB_driver { } $fields = array(); - foreach($data as $key => $val) + foreach ($data as $key => $val) { $fields[$this->_protect_identifiers($key)] = $this->escape($val); } @@ -1175,7 +1175,7 @@ class CI_DB_driver { $trace = debug_backtrace(); - foreach($trace as $call) + foreach ($trace as $call) { if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE) { @@ -1248,7 +1248,7 @@ class CI_DB_driver { { $escaped_array = array(); - foreach($item as $k => $v) + foreach ($item as $k => $v) { $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v); } diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index e6a64f3b8..a71fca78f 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -99,7 +99,7 @@ class CI_DB_forge { { if (is_array($key)) { - foreach($key as $one) + foreach ($key as $one) { $this->add_key($one, $primary); } @@ -333,6 +333,12 @@ class CI_DB_forge { foreach ($field as $k => $v) { + // If no name provided, use the current name + if ( ! isset($field[$k]['name'])) + { + $field[$k]['name'] = $k; + } + $this->add_field(array($k => $field[$k])); if (count($this->fields) == 0) diff --git a/system/database/DB_result.php b/system/database/DB_result.php index fb791cf30..76e1d6abb 100644 --- a/system/database/DB_result.php +++ b/system/database/DB_result.php @@ -28,13 +28,14 @@ */ class CI_DB_result { - var $conn_id = NULL; - var $result_id = NULL; - var $result_array = array(); - var $result_object = array(); - var $current_row = 0; - var $num_rows = 0; - var $row_data = NULL; + var $conn_id = NULL; + var $result_id = NULL; + var $result_array = array(); + var $result_object = array(); + var $custom_result_object = array(); + var $current_row = 0; + var $num_rows = 0; + var $row_data = NULL; /** @@ -46,11 +47,48 @@ class CI_DB_result { */ function result($type = 'object') { - return ($type == 'object') ? $this->result_object() : $this->result_array(); + if ($type == 'array') return $this->result_array(); + else if ($type == 'object') return $this->result_object(); + else return $this->custom_result_object($type); } // -------------------------------------------------------------------- + /** + * Custom query result. + * + * @param class_name A string that represents the type of object you want back + * @return array of objects + */ + function custom_result_object($class_name) + { + if (array_key_exists($class_name, $this->custom_result_object)) + { + return $this->custom_result_object[$class_name]; + } + + if ($this->result_id === FALSE OR $this->num_rows() == 0) + { + return array(); + } + + // add the data to the object + $this->_data_seek(0); + $result_object = array(); + while ($row = $this->_fetch_object()) + { + $object = new $class_name(); + foreach ($row as $key => $value) + { + $object->$key = $value; + } + $result_object[] = $object; + } + + // return the array + return $this->custom_result_object[$class_name] = $result_object; + } + /** * Query result. "object" version. * @@ -142,7 +180,9 @@ class CI_DB_result { $n = 0; } - return ($type == 'object') ? $this->row_object($n) : $this->row_array($n); + if ($type == 'object') return $this->row_object($n); + else if ($type == 'array') return $this->row_array($n); + else return $this->custom_row_object($n, $type); } // -------------------------------------------------------------------- @@ -179,7 +219,30 @@ class CI_DB_result { // -------------------------------------------------------------------- - /** + /** + * Returns a single result row - custom object version + * + * @access public + * @return object + */ + function custom_row_object($n, $type) + { + $result = $this->custom_result_object($type); + + if (count($result) == 0) + { + return $result; + } + + if ($n != $this->current_row AND isset($result[$n])) + { + $this->current_row = $n; + } + + return $result[$this->current_row]; + } + + /** * Returns a single result row - object version * * @access public @@ -339,4 +402,4 @@ class CI_DB_result { // END DB_result class /* End of file DB_result.php */ -/* Location: ./system/database/DB_result.php */
\ No newline at end of file +/* Location: ./system/database/DB_result.php */ diff --git a/system/database/drivers/mssql/mssql_driver.php b/system/database/drivers/mssql/mssql_driver.php index 5a69132cd..5048c0b4a 100644 --- a/system/database/drivers/mssql/mssql_driver.php +++ b/system/database/drivers/mssql/mssql_driver.php @@ -253,7 +253,7 @@ class CI_DB_mssql_driver extends CI_DB { { if (is_array($str)) { - foreach($str as $key => $val) + foreach ($str as $key => $val) { $str[$key] = $this->escape_str($val, $like); } @@ -551,7 +551,7 @@ class CI_DB_mssql_driver extends CI_DB { */ function _update($table, $values, $where, $orderby = array(), $limit = FALSE) { - foreach($values as $key => $val) + foreach ($values as $key => $val) { $valstr[] = $key." = ".$val; } diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php index df18c912e..4ff9b0a11 100644 --- a/system/database/drivers/mysql/mysql_driver.php +++ b/system/database/drivers/mysql/mysql_driver.php @@ -286,13 +286,13 @@ class CI_DB_mysql_driver extends CI_DB { { if (is_array($str)) { - foreach($str as $key => $val) - { + foreach ($str as $key => $val) + { $str[$key] = $this->escape_str($val, $like); - } + } - return $str; - } + return $str; + } if (function_exists('mysql_real_escape_string') AND is_resource($this->conn_id)) { @@ -590,9 +590,9 @@ class CI_DB_mysql_driver extends CI_DB { */ function _update($table, $values, $where, $orderby = array(), $limit = FALSE) { - foreach($values as $key => $val) + foreach ($values as $key => $val) { - $valstr[] = $key." = ".$val; + $valstr[] = $key . ' = ' . $val; } $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; @@ -627,11 +627,11 @@ class CI_DB_mysql_driver extends CI_DB { $ids = array(); $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : ''; - foreach($values as $key => $val) + foreach ($values as $key => $val) { $ids[] = $val[$index]; - foreach(array_keys($val) as $field) + foreach (array_keys($val) as $field) { if ($field != $index) { @@ -643,7 +643,7 @@ class CI_DB_mysql_driver extends CI_DB { $sql = "UPDATE ".$table." SET "; $cases = ''; - foreach($final as $k => $v) + foreach ($final as $k => $v) { $cases .= $k.' = CASE '."\n"; foreach ($v as $row) diff --git a/system/database/drivers/mysql/mysql_forge.php b/system/database/drivers/mysql/mysql_forge.php index c02b8cb3a..529ec980d 100644 --- a/system/database/drivers/mysql/mysql_forge.php +++ b/system/database/drivers/mysql/mysql_forge.php @@ -87,11 +87,26 @@ class CI_DB_mysql_forge extends CI_DB_forge { if (array_key_exists('TYPE', $attributes)) { $sql .= ' '.$attributes['TYPE']; - } - if (array_key_exists('CONSTRAINT', $attributes)) - { - $sql .= '('.$attributes['CONSTRAINT'].')'; + if (array_key_exists('CONSTRAINT', $attributes)) + { + switch ($attributes['TYPE']) + { + case 'decimal': + case 'float': + case 'numeric': + $sql .= '('.implode(',', $attributes['CONSTRAINT']).')'; + break; + + case 'enum': + case 'set': + $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")'; + break; + + default: + $sql .= '('.$attributes['CONSTRAINT'].')'; + } + } } if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE) diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php index 8942100d4..ccdabce1a 100644 --- a/system/database/drivers/mysqli/mysqli_driver.php +++ b/system/database/drivers/mysqli/mysqli_driver.php @@ -287,7 +287,7 @@ class CI_DB_mysqli_driver extends CI_DB { { if (is_array($str)) { - foreach($str as $key => $val) + foreach ($str as $key => $val) { $str[$key] = $this->escape_str($val, $like); } @@ -571,7 +571,7 @@ class CI_DB_mysqli_driver extends CI_DB { */ function _update($table, $values, $where, $orderby = array(), $limit = FALSE) { - foreach($values as $key => $val) + foreach ($values as $key => $val) { $valstr[] = $key." = ".$val; } @@ -607,11 +607,11 @@ class CI_DB_mysqli_driver extends CI_DB { $ids = array(); $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : ''; - foreach($values as $key => $val) + foreach ($values as $key => $val) { $ids[] = $val[$index]; - foreach(array_keys($val) as $field) + foreach (array_keys($val) as $field) { if ($field != $index) { @@ -623,7 +623,7 @@ class CI_DB_mysqli_driver extends CI_DB { $sql = "UPDATE ".$table." SET "; $cases = ''; - foreach($final as $k => $v) + foreach ($final as $k => $v) { $cases .= $k.' = CASE '."\n"; foreach ($v as $row) diff --git a/system/database/drivers/oci8/oci8_driver.php b/system/database/drivers/oci8/oci8_driver.php index 64f53cc3f..14df104ff 100644 --- a/system/database/drivers/oci8/oci8_driver.php +++ b/system/database/drivers/oci8/oci8_driver.php @@ -253,7 +253,7 @@ class CI_DB_oci8_driver extends CI_DB { $sql = "begin $package.$procedure("; $have_cursor = FALSE; - foreach($params as $param) + foreach ($params as $param) { $sql .= $param['name'] . ","; @@ -395,7 +395,7 @@ class CI_DB_oci8_driver extends CI_DB { { if (is_array($str)) { - foreach($str as $key => $val) + foreach ($str as $key => $val) { $str[$key] = $this->escape_str($val, $like); } @@ -655,7 +655,7 @@ class CI_DB_oci8_driver extends CI_DB { */ function _update($table, $values, $where, $orderby = array(), $limit = FALSE) { - foreach($values as $key => $val) + foreach ($values as $key => $val) { $valstr[] = $key." = ".$val; } diff --git a/system/database/drivers/odbc/odbc_driver.php b/system/database/drivers/odbc/odbc_driver.php index c8e03c356..81e0d7cf2 100644 --- a/system/database/drivers/odbc/odbc_driver.php +++ b/system/database/drivers/odbc/odbc_driver.php @@ -264,7 +264,7 @@ class CI_DB_odbc_driver extends CI_DB { { if (is_array($str)) { - foreach($str as $key => $val) + foreach ($str as $key => $val) { $str[$key] = $this->escape_str($val, $like); } @@ -523,7 +523,7 @@ class CI_DB_odbc_driver extends CI_DB { */ function _update($table, $values, $where, $orderby = array(), $limit = FALSE) { - foreach($values as $key => $val) + foreach ($values as $key => $val) { $valstr[] = $key." = ".$val; } diff --git a/system/database/drivers/odbc/odbc_result.php b/system/database/drivers/odbc/odbc_result.php index a81a2b8b7..5d64a464f 100644 --- a/system/database/drivers/odbc/odbc_result.php +++ b/system/database/drivers/odbc/odbc_result.php @@ -188,7 +188,7 @@ class CI_DB_odbc_result extends CI_DB_result { */ function _odbc_fetch_object(& $odbc_result) { $rs = array(); - $rs_obj = false; + $rs_obj = FALSE; if (odbc_fetch_into($odbc_result, $rs)) { foreach ($rs as $k=>$v) { $field_name= odbc_field_name($odbc_result, $k+1); @@ -210,7 +210,7 @@ class CI_DB_odbc_result extends CI_DB_result { */ function _odbc_fetch_array(& $odbc_result) { $rs = array(); - $rs_assoc = false; + $rs_assoc = FALSE; if (odbc_fetch_into($odbc_result, $rs)) { $rs_assoc=array(); foreach ($rs as $k=>$v) { diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index 0bb7974d8..47ff36246 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -283,7 +283,7 @@ class CI_DB_postgre_driver extends CI_DB { { if (is_array($str)) { - foreach($str as $key => $val) + foreach ($str as $key => $val) { $str[$key] = $this->escape_str($val, $like); } @@ -330,21 +330,21 @@ class CI_DB_postgre_driver extends CI_DB { $v = $this->_version(); $v = $v['server']; - $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') + if ($table == NULL && $v >= '8.1') { $sql='SELECT LASTVAL() as ins_id'; } - elseif ($table != null && $column != null && $v >= '8.0') + 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); } - elseif ($table != null) + elseif ($table != NULL) { // seq_name passed in table parameter $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table); @@ -568,7 +568,7 @@ class CI_DB_postgre_driver extends CI_DB { */ function _update($table, $values, $where, $orderby = array(), $limit = FALSE) { - foreach($values as $key => $val) + foreach ($values as $key => $val) { $valstr[] = $key." = ".$val; } diff --git a/system/database/drivers/sqlite/sqlite_driver.php b/system/database/drivers/sqlite/sqlite_driver.php index 5bfc1f558..eb4e585b3 100644 --- a/system/database/drivers/sqlite/sqlite_driver.php +++ b/system/database/drivers/sqlite/sqlite_driver.php @@ -280,7 +280,7 @@ class CI_DB_sqlite_driver extends CI_DB { { if (is_array($str)) { - foreach($str as $key => $val) + foreach ($str as $key => $val) { $str[$key] = $this->escape_str($val, $like); } @@ -537,7 +537,7 @@ class CI_DB_sqlite_driver extends CI_DB { */ function _update($table, $values, $where, $orderby = array(), $limit = FALSE) { - foreach($values as $key => $val) + foreach ($values as $key => $val) { $valstr[] = $key." = ".$val; } |