From 6e7047576338e896a43a35eb2fa79136adc01d8d Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 18 Jul 2012 00:46:33 +0300 Subject: Fix WHERE escaping/prefixing --- system/database/DB_driver.php | 17 ++++- system/database/DB_query_builder.php | 121 ++++++++++++++++++++++++----------- 2 files changed, 100 insertions(+), 38 deletions(-) diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index d63a1d955..b7c6b4e8e 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1168,8 +1168,21 @@ abstract class CI_DB_driver { */ protected function _get_operator($str) { - return preg_match('/(=|!|<|>| IS NULL| IS NOT NULL| BETWEEN)/i', $str, $match) - ? $match[1] : FALSE; + static $_operators = array( + '\s*(?:<|>|!)?=\s*', // =, <=, >=, != + '\s*<>?\s*', // <, <> + '\s*>\s*', // > + '\s+IS NULL', // IS NULL + '\s+IS NOT NULL', // IS NOT NULL + '\s+LIKE\s+', // LIKE + '\s+NOT LIKE\s+', // NOT LIKE + '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value + '\s+IN\s*\([^\)]+\)', // IN(list) + '\s+NOT IN\s*\([^\)]+\)' // NOT IN (list) + ); + + return preg_match('/'.implode('|', $_operators).'/i', $str, $match) + ? $match[0] : FALSE; } // -------------------------------------------------------------------- diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index 479b7f24a..92cb8c1d5 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -467,13 +467,6 @@ abstract class CI_DB_query_builder extends CI_DB_driver { ? $this->_group_get_type('') : $this->_group_get_type($type); - if ($escape === TRUE) - { - $k = (($op = $this->_get_operator($k)) !== FALSE) - ? $this->escape_identifiers(trim(substr($k, 0, strpos($k, $op)))).' '.strstr($k, $op) - : $this->escape_identifiers(trim($k)); - } - if (is_null($v) && ! $this->_has_operator($k)) { // value appears not to have been set, assign the test to IS NULL @@ -493,10 +486,11 @@ abstract class CI_DB_query_builder extends CI_DB_driver { } } - $this->qb_where[] = $prefix.$k.$v; + $this->qb_where[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); if ($this->qb_caching === TRUE) { - $this->qb_cache_where[] = $prefix.$k.$v; + // check this shit + $this->qb_cache_where[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); $this->qb_cache_exists[] = 'where'; } @@ -607,14 +601,13 @@ abstract class CI_DB_query_builder extends CI_DB_driver { $this->qb_wherein[] = $this->escape($value); } - if ($escape === TRUE) - { - $key = $this->escape_identifiers(trim($key)); - } - $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type); - $this->qb_where[] = $where_in = $prefix.$key.$not.' IN ('.implode(', ', $this->qb_wherein).') '; + $where_in = array( + 'condition' => $prefix.$key.$not.' IN('.implode(', ', $this->qb_wherein).')', + 'escape' => $escape + ); + $this->qb_where[] = $where_in; if ($this->qb_caching === TRUE) { $this->qb_cache_where[] = $where_in; @@ -769,11 +762,15 @@ abstract class CI_DB_query_builder extends CI_DB_driver { $this->qb_where_group_started = TRUE; $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type; - $this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' ('; + $where = array( + 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (', + 'escape' => FALSE + ); + $this->qb_where[] = $where; if ($this->qb_caching) { - $this->qb_cache_where[] = $value; + $this->qb_cache_where[] = $where; } return $this; @@ -825,11 +822,15 @@ abstract class CI_DB_query_builder extends CI_DB_driver { public function group_end() { $this->qb_where_group_started = FALSE; - $this->qb_where[] = $value = str_repeat(' ', $this->qb_where_group_count--) . ')'; + $where = array( + 'condition' => str_repeat(' ', $this->qb_where_group_count--).')', + 'escape' => FALSE + ); + $this->qb_where[] = $where; if ($this->qb_caching) { - $this->qb_cache_where[] = $value; + $this->qb_cache_where[] = $where; } return $this; @@ -2067,49 +2068,97 @@ abstract class CI_DB_query_builder extends CI_DB_driver { $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 .= $this->_compile_conditions(); + + // Write the "LIMIT" portion of the query + if (is_numeric($this->qb_limit)) { - $sql .= "\nWHERE "; + return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset); } - $sql .= implode("\n", $this->qb_where); + return $sql; + } - // Write the "LIKE" portion of the query - if (count($this->qb_like) > 0) + // -------------------------------------------------------------------- + + /** + * Compile WHERE statement + * + * Escapes identifiers in WHERE, LIKE, HAVING, GROUP BY, ORDER BY + * statements at execution time. Required so that aliases are tracked + * properly, regardless of wether e.g. where() is called prior to + * join() and dbprefix is added only if needed. + * + * @return string + */ + protected function _compile_conditions() + { + // WHERE + if (count($this->qb_where) > 0) { - if (count($this->qb_where) > 0) + $sql = "\nWHERE "; + + for ($i = 0, $c = count($this->qb_where); $i < $c; $i++) { - $sql .= "\nAND "; + if ($this->qb_where[$i]['escape'] === FALSE) + { + $this->qb_where[$i] = $this->qb_where[$i]['condition']; + continue; + } + + $op = preg_quote($this->_get_operator($this->qb_where[$i]['condition'])); + if ( ! preg_match('/^(\s*(?:AND|OR)\s+)?(\(?)(.*)('.$op.')(.*(?qb_where[$i]['condition'], $matches)) + { + $this->qb_where[$i] = $this->qb_where[$i]['condition']; + continue; + } + + // $matches = array( + // 0 => 'OR (test <= foo)', /* the whole thing */ + // 1 => 'OR ', /* optional */ + // 2 => '(', /* optional */ + // 3 => 'test', /* the field name */ + // 4 => ' <= ', /* $op */ + // 5 => 'foo', /* optional, if $op is e.g. 'IS NULL' */ + // 6 => ')' /* optional */ + // ); + empty($matches[5]) OR $matches[5] = ' '.$this->protect_identifiers(trim($matches[5])); + $this->qb_where[$i] = $matches[1].$matches[2].$this->protect_identifiers(trim($matches[3])) + .' '.trim($matches[4]).$matches[5].$matches[6]; } + $sql .= implode("\n", $this->qb_where); + } + else + { + $sql = ''; + } + + // LIKE + if (count($this->qb_like) > 0) + { + $sql .= ($sql === '') ? "\nWHERE " : "\nAND "; $sql .= implode("\n", $this->qb_like); } - // Write the "GROUP BY" portion of the query + // GROUP BY if (count($this->qb_groupby) > 0) { $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby); } - // Write the "HAVING" portion of the query + // HAVING if (count($this->qb_having) > 0) { $sql .= "\nHAVING ".implode("\n", $this->qb_having); } - // Write the "ORDER BY" portion of the query + // ORDER BY if (count($this->qb_orderby) > 0) { $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby); } - // 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; } -- cgit v1.2.3-24-g4f1b From ededc4a32a96315f18b7234153aa9cf7c87ca3ce Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 18 Jul 2012 01:16:15 +0300 Subject: Change _like() to append to the qb_where array --- system/database/DB_query_builder.php | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index 92cb8c1d5..75da1c792 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -692,7 +692,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { /** * Like * - * Called by like() or orlike() + * Called by like() or or_like() * * @param mixed * @param mixed @@ -708,8 +708,8 @@ abstract class CI_DB_query_builder extends CI_DB_driver { foreach ($field as $k => $v) { - $k = $this->protect_identifiers($k); - $prefix = (count($this->qb_like) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type); + $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) + ? $this->_group_get_type('') : $this->_group_get_type($type); $v = $this->escape_like_str($v); if ($side === 'none') @@ -735,13 +735,12 @@ abstract class CI_DB_query_builder extends CI_DB_driver { $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr); } - $this->qb_like[] = $like_statement; + $this->qb_where[] = array('condition' => $like_statement, 'escape' => $this->_protect_identifiers); if ($this->qb_caching === TRUE) { - $this->qb_cache_like[] = $like_statement; - $this->qb_cache_exists[] = 'like'; + $this->qb_cache_where[] = $like_statement; + $this->qb_cache_exists[] = 'where'; } - } return $this; @@ -2134,13 +2133,6 @@ abstract class CI_DB_query_builder extends CI_DB_driver { $sql = ''; } - // LIKE - if (count($this->qb_like) > 0) - { - $sql .= ($sql === '') ? "\nWHERE " : "\nAND "; - $sql .= implode("\n", $this->qb_like); - } - // GROUP BY if (count($this->qb_groupby) > 0) { -- cgit v1.2.3-24-g4f1b From b04786599e1b032078f1d3bdd8941405d47447a0 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 18 Jul 2012 15:34:46 +0300 Subject: Remove dependancies on qb_like and remove unneeded parameters from _delete(), _like(), _update(), _update_batch() --- system/database/DB_driver.php | 53 ++++---- system/database/DB_query_builder.php | 147 ++++++++++----------- system/database/drivers/cubrid/cubrid_driver.php | 10 +- system/database/drivers/ibase/ibase_driver.php | 36 +---- system/database/drivers/mssql/mssql_driver.php | 41 ++---- system/database/drivers/mysql/mysql_driver.php | 10 +- system/database/drivers/mysqli/mysqli_driver.php | 10 +- system/database/drivers/oci8/oci8_driver.php | 17 +-- system/database/drivers/odbc/odbc_driver.php | 34 +++++ system/database/drivers/pdo/pdo_driver.php | 13 +- .../drivers/pdo/subdrivers/pdo_4d_driver.php | 37 +----- .../drivers/pdo/subdrivers/pdo_cubrid_driver.php | 10 +- .../drivers/pdo/subdrivers/pdo_dblib_driver.php | 41 ++---- .../drivers/pdo/subdrivers/pdo_firebird_driver.php | 36 +---- .../drivers/pdo/subdrivers/pdo_ibm_driver.php | 37 +----- .../drivers/pdo/subdrivers/pdo_informix_driver.php | 37 +----- .../drivers/pdo/subdrivers/pdo_mysql_driver.php | 10 +- .../drivers/pdo/subdrivers/pdo_oci_driver.php | 17 +-- .../drivers/pdo/subdrivers/pdo_odbc_driver.php | 37 +----- .../drivers/pdo/subdrivers/pdo_pgsql_driver.php | 67 +++------- .../drivers/pdo/subdrivers/pdo_sqlsrv_driver.php | 41 ++---- system/database/drivers/postgre/postgre_driver.php | 69 +++------- system/database/drivers/sqlsrv/sqlsrv_driver.php | 41 ++---- 23 files changed, 297 insertions(+), 554 deletions(-) diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index b7c6b4e8e..10306d721 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1118,31 +1118,19 @@ abstract class CI_DB_driver { * Generates a platform-specific update string from the supplied data * * @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 - * @param array the like clause * @return string */ - protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) + protected function _update($table, $values) { foreach ($values as $key => $val) { $valstr[] = $key.' = '.$val; } - $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); - - if ( ! empty($like)) - { - $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); - } - return 'UPDATE '.$table.' SET '.implode(', ', $valstr) - .$where - .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '') - .($limit ? ' LIMIT '.$limit : ''); + .$this->_compile_where() + .(empty($this->qb_orderby) ? '' : ' ORDER BY '.implode(', ', $this->qb_orderby)) + .($this->qb_limit ? ' LIMIT '.(int) $this->qb_limit : ''); } // -------------------------------------------------------------------- @@ -1155,7 +1143,7 @@ abstract class CI_DB_driver { */ protected function _has_operator($str) { - return (bool) preg_match('/(\s|<|>|!|=|IS NULL|IS NOT NULL|BETWEEN)/i', trim($str)); + return (bool) preg_match('/(<|>|!|=|\sIS NULL|\sIS NOT NULL|\sBETWEEN|\sLIKE|\sIN\s*\(|\s)/i', trim($str)); } // -------------------------------------------------------------------- @@ -1169,18 +1157,29 @@ abstract class CI_DB_driver { protected function _get_operator($str) { static $_operators = array( - '\s*(?:<|>|!)?=\s*', // =, <=, >=, != - '\s*<>?\s*', // <, <> - '\s*>\s*', // > - '\s+IS NULL', // IS NULL - '\s+IS NOT NULL', // IS NOT NULL - '\s+LIKE\s+', // LIKE - '\s+NOT LIKE\s+', // NOT LIKE - '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value - '\s+IN\s*\([^\)]+\)', // IN(list) - '\s+NOT IN\s*\([^\)]+\)' // NOT IN (list) + '\s*(?:<|>|!)?=\s*', // =, <=, >=, != + '\s*<>?\s*', // <, <> + '\s*>\s*', // > + '\s+IS NULL', // IS NULL + '\s+IS NOT NULL', // IS NOT NULL + '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value + '\s+IN\s*\([^\)]+\)', // IN(list) + '\s+NOT IN\s*\([^\)]+\)' // NOT IN (list) + ); + + static $_like = array( + '\s+LIKE\s+\S+', // LIKE 'expr' + '\s+NOT LIKE\s+\S+', // NOT LIKE 'expr' ); + if ($this->_like_escape_str !== '') + { + $_like[0] .= preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr))); + $_like[1] .= preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr))); + } + + $_operators = array_merge($_operators, $_like); + return preg_match('/'.implode('|', $_operators).'/i', $str, $match) ? $match[0] : FALSE; } diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index 75da1c792..29b75cd1d 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -47,7 +47,6 @@ abstract class CI_DB_query_builder extends CI_DB_driver { protected $qb_from = array(); protected $qb_join = array(); protected $qb_where = array(); - protected $qb_like = array(); protected $qb_groupby = array(); protected $qb_having = array(); protected $qb_keys = array(); @@ -443,12 +442,12 @@ abstract class CI_DB_query_builder extends CI_DB_driver { /** * Where * - * Called by where() or or_where() + * Called by where(), or_where() * * @param mixed * @param mixed * @param string - * @param mixed + * @param bool * @return object */ protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL) @@ -477,7 +476,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { { if ($escape === TRUE) { - $v = ' '.$this->escape($v); + $v = ' '.(is_int($v) ? $v : $this->escape($v)); } if ( ! $this->_has_operator($k)) @@ -628,12 +627,14 @@ abstract class CI_DB_query_builder extends CI_DB_driver { * multiple calls with AND * * @param mixed - * @param mixed + * @param string + * @param string + * @param bool * @return object */ - public function like($field, $match = '', $side = 'both') + public function like($field, $match = '', $side = 'both', $escape = NULL) { - return $this->_like($field, $match, 'AND ', $side); + return $this->_like($field, $match, 'AND ', $side, '', $escape); } // -------------------------------------------------------------------- @@ -645,12 +646,14 @@ abstract class CI_DB_query_builder extends CI_DB_driver { * multiple calls with AND * * @param mixed - * @param mixed + * @param string + * @param string + * @param bool * @return object */ - public function not_like($field, $match = '', $side = 'both') + public function not_like($field, $match = '', $side = 'both', $escape = NULL) { - return $this->_like($field, $match, 'AND ', $side, 'NOT'); + return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape); } // -------------------------------------------------------------------- @@ -662,12 +665,14 @@ abstract class CI_DB_query_builder extends CI_DB_driver { * multiple calls with OR * * @param mixed - * @param mixed + * @param string + * @param string + * @param bool * @return object */ - public function or_like($field, $match = '', $side = 'both') + public function or_like($field, $match = '', $side = 'both', $escape = NULL) { - return $this->_like($field, $match, 'OR ', $side); + return $this->_like($field, $match, 'OR ', $side, '', $escape); } // -------------------------------------------------------------------- @@ -679,12 +684,14 @@ abstract class CI_DB_query_builder extends CI_DB_driver { * multiple calls with OR * * @param mixed - * @param mixed + * @param string + * @param string + * @param bool * @return object */ - public function or_not_like($field, $match = '', $side = 'both') + public function or_not_like($field, $match = '', $side = 'both', $escape = NULL) { - return $this->_like($field, $match, 'OR ', $side, 'NOT'); + return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape); } // -------------------------------------------------------------------- @@ -692,50 +699,55 @@ abstract class CI_DB_query_builder extends CI_DB_driver { /** * Like * - * Called by like() or or_like() + * Called by like(), or_like(), not_like, or_not_like() * * @param mixed - * @param mixed * @param string + * @param string + * @param string + * @param string + * @param bool * @return object */ - protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '') + protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL) { if ( ! is_array($field)) { $field = array($field => $match); } + is_bool($escape) OR $escape = $this->_protect_identifiers; + $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) + ? $this->_group_get_type('') : $this->_group_get_type($type); + foreach ($field as $k => $v) { - $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) - ? $this->_group_get_type('') : $this->_group_get_type($type); $v = $this->escape_like_str($v); if ($side === 'none') { - $like_statement = "{$prefix} $k $not LIKE '{$v}'"; + $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}'"; } elseif ($side === 'before') { - $like_statement = "{$prefix} $k $not LIKE '%{$v}'"; + $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}'"; } elseif ($side === 'after') { - $like_statement = "{$prefix} $k $not LIKE '{$v}%'"; + $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}%'"; } else { - $like_statement = "{$prefix} $k $not LIKE '%{$v}%'"; + $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); + $like_statement .= sprintf($this->_like_escape_str, $this->_like_escape_chr); } - $this->qb_where[] = array('condition' => $like_statement, 'escape' => $this->_protect_identifiers); + $this->qb_where[] = array('condition' => $like_statement, 'escape' => $escape); if ($this->qb_caching === TRUE) { $this->qb_cache_where[] = $like_statement; @@ -1558,7 +1570,7 @@ abstract 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); if ($reset === TRUE) { @@ -1605,7 +1617,7 @@ abstract 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->_reset_write(); return $this->query($sql); @@ -1687,7 +1699,7 @@ abstract 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->_reset_write(); @@ -1893,12 +1905,12 @@ abstract class CI_DB_query_builder extends CI_DB_driver { $this->limit($limit); } - if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0) + if (count($this->qb_where) === 0 && count($this->qb_wherein) === 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); + $sql = $this->_delete($table); if ($reset_data) { $this->_reset_write(); @@ -1915,21 +1927,12 @@ abstract class CI_DB_query_builder extends CI_DB_driver { * Generates a platform-specific delete string from the supplied data * * @param string the table name - * @param array the where clause - * @param array the like clause - * @param string the limit clause * @return string */ - protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table) { - $conditions = array(); - - empty($where) OR $conditions[] = implode(' ', $where); - empty($like) OR $conditions[] = implode(' ', $like); - - return 'DELETE FROM '.$table - .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '') - .($limit ? ' LIMIT '.(int) $limit : ''); + return 'DELETE FROM '.$table.$this->_compile_where() + .($this->qb_limit ? ' LIMIT '.(int) $this->qb_limit : ''); } // -------------------------------------------------------------------- @@ -2069,6 +2072,24 @@ abstract class CI_DB_query_builder extends CI_DB_driver { $sql .= $this->_compile_conditions(); + // GROUP BY + if (count($this->qb_groupby) > 0) + { + $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby); + } + + // HAVING + if (count($this->qb_having) > 0) + { + $sql .= "\nHAVING ".implode("\n", $this->qb_having); + } + + // ORDER BY + if (count($this->qb_orderby) > 0) + { + $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby); + } + // Write the "LIMIT" portion of the query if (is_numeric($this->qb_limit)) { @@ -2083,14 +2104,14 @@ abstract class CI_DB_query_builder extends CI_DB_driver { /** * Compile WHERE statement * - * Escapes identifiers in WHERE, LIKE, HAVING, GROUP BY, ORDER BY - * statements at execution time. Required so that aliases are tracked - * properly, regardless of wether e.g. where() is called prior to - * join() and dbprefix is added only if needed. + * Escapes identifiers in WHERE statements at execution time. + * Required so that aliases are tracked properly, regardless of wether + * e.g. where() is called prior to join() and dbprefix is added only + * if needed. * * @return string */ - protected function _compile_conditions() + protected function _compile_where() { // WHERE if (count($this->qb_where) > 0) @@ -2126,32 +2147,10 @@ abstract class CI_DB_query_builder extends CI_DB_driver { .' '.trim($matches[4]).$matches[5].$matches[6]; } - $sql .= implode("\n", $this->qb_where); - } - else - { - $sql = ''; + return implode("\n", $this->qb_where); } - // GROUP BY - if (count($this->qb_groupby) > 0) - { - $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby); - } - - // HAVING - if (count($this->qb_having) > 0) - { - $sql .= "\nHAVING ".implode("\n", $this->qb_having); - } - - // ORDER BY - if (count($this->qb_orderby) > 0) - { - $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby); - } - - return $sql; + return ''; } // -------------------------------------------------------------------- @@ -2363,7 +2362,6 @@ abstract class CI_DB_query_builder extends CI_DB_driver { 'qb_from' => array(), 'qb_join' => array(), 'qb_where' => array(), - 'qb_like' => array(), 'qb_groupby' => array(), 'qb_having' => array(), 'qb_orderby' => array(), @@ -2392,7 +2390,6 @@ abstract class CI_DB_query_builder extends CI_DB_driver { 'qb_set' => array(), 'qb_from' => array(), 'qb_where' => array(), - 'qb_like' => array(), 'qb_orderby' => array(), 'qb_keys' => array(), 'qb_limit' => FALSE diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index a3d0287f5..e2ace3320 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -396,10 +396,10 @@ class CI_DB_cubrid_driver extends CI_DB { * * @param string the table name * @param array the update data - * @param array the where clause + * @param string the where key * @return string */ - protected function _update_batch($table, $values, $index, $where = NULL) + protected function _update_batch($table, $values, $index) { $ids = array(); foreach ($values as $key => $val) @@ -423,9 +423,9 @@ class CI_DB_cubrid_driver extends CI_DB { .'ELSE '.$k.' END, '; } - return 'UPDATE '.$table.' SET '.substr($cases, 0, -2) - .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '') - .$index.' IN ('.implode(',', $ids).')'; + $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); + + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where(); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/ibase/ibase_driver.php b/system/database/drivers/ibase/ibase_driver.php index c9027670d..86c1fee6d 100644 --- a/system/database/drivers/ibase/ibase_driver.php +++ b/system/database/drivers/ibase/ibase_driver.php @@ -328,29 +328,12 @@ class CI_DB_ibase_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 (ignored) - * @param array the like clause * @return string */ - protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) + protected function _update($table, $values) { - foreach ($values as $key => $val) - { - $valstr[] = $key.' = '.$val; - } - - $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); - - if ( ! empty($like)) - { - $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); - } - - return 'UPDATE '.$table.' SET '.implode(', ', $valstr) - .$where - .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : ''); + $this->qb_limit = FALSE; + return parent::_update($table, $values); } // -------------------------------------------------------------------- @@ -379,19 +362,12 @@ class CI_DB_ibase_driver extends CI_DB { * Generates a platform-specific delete string from the supplied data * * @param string the table name - * @param array the where clause - * @param array the like clause - * @param string the limit clause (ignored) * @return string */ - protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table) { - $conditions = array(); - - empty($where) OR $conditions[] = implode(' ', $where); - empty($like) OR $conditions[] = implode(' ', $like); - - return 'DELETE FROM '.$table.(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : ''); + $this->qb_limit = FALSE; + return parent::_delete($table); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/mssql/mssql_driver.php b/system/database/drivers/mssql/mssql_driver.php index 1714704a8..672c3161c 100644 --- a/system/database/drivers/mssql/mssql_driver.php +++ b/system/database/drivers/mssql/mssql_driver.php @@ -388,27 +388,13 @@ class CI_DB_mssql_driver extends CI_DB { * * @param string the table name * @param array the update data - * @param array the where clause - * @param array the orderby clause (ignored) - * @param array the limit clause (ignored) - * @param array the like clause * @return string */ - protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) + protected function _update($table, $values) { - foreach($values as $key => $val) - { - $valstr[] = $key.' = '.$val; - } - - $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); - - if ( ! empty($like)) - { - $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); - } - - return 'UPDATE '.$table.' SET '.implode(', ', $valstr).' WHERE '.$where; + $this->qb_limit = FALSE; + $this->qb_orderby = array(); + return parent::_update($table, $values); } // -------------------------------------------------------------------- @@ -437,23 +423,16 @@ class CI_DB_mssql_driver extends CI_DB { * Generates a platform-specific delete string from the supplied data * * @param string the table name - * @param array the where clause - * @param array the like clause - * @param string the limit clause * @return string */ - protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table) { - $conditions = array(); - - empty($where) OR $conditions[] = implode(' ', $where); - empty($like) OR $conditions[] = implode(' ', $like); - - $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : ''; + if ($this->qb_limit) + { + return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_where().') DELETE FROM ci_delete'; + } - return ($limit) - ? 'WITH ci_delete AS (SELECT TOP '.$limit.' * FROM '.$table.$conditions.') DELETE FROM ci_delete' - : 'DELETE FROM '.$table.$conditions; + return parent::_delete($table); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php index 29db90408..634430665 100644 --- a/system/database/drivers/mysql/mysql_driver.php +++ b/system/database/drivers/mysql/mysql_driver.php @@ -420,10 +420,10 @@ class CI_DB_mysql_driver extends CI_DB { * * @param string the table name * @param array the update data - * @param array the where clause + * @param string the where key * @return string */ - protected function _update_batch($table, $values, $index, $where = NULL) + protected function _update_batch($table, $values, $index) { $ids = array(); foreach ($values as $key => $val) @@ -447,9 +447,9 @@ class CI_DB_mysql_driver extends CI_DB { .'ELSE '.$k.' END, '; } - return 'UPDATE '.$table.' SET '.substr($cases, 0, -2) - .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '') - .$index.' IN('.implode(',', $ids).')'; + $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); + + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where(); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php index be61aab20..5498aa244 100644 --- a/system/database/drivers/mysqli/mysqli_driver.php +++ b/system/database/drivers/mysqli/mysqli_driver.php @@ -412,10 +412,10 @@ class CI_DB_mysqli_driver extends CI_DB { * * @param string the table name * @param array the update data - * @param array the where clause + * @param string the where key * @return string */ - protected function _update_batch($table, $values, $index, $where = NULL) + protected function _update_batch($table, $values, $index) { $ids = array(); foreach ($values as $key => $val) @@ -439,11 +439,9 @@ class CI_DB_mysqli_driver extends CI_DB { .'ELSE '.$k.' END, '; } - $where = ($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : ''; + $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); - return 'UPDATE '.$table.' SET '.substr($cases, 0, -2) - .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '') - .$index.' IN('.implode(',', $ids).')'; + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where(); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/oci8/oci8_driver.php b/system/database/drivers/oci8/oci8_driver.php index 691247fee..a0f26c257 100644 --- a/system/database/drivers/oci8/oci8_driver.php +++ b/system/database/drivers/oci8/oci8_driver.php @@ -611,20 +611,17 @@ class CI_DB_oci8_driver extends CI_DB { * Generates a platform-specific delete string from the supplied data * * @param string the table name - * @param array the where clause - * @param array the like clause - * @param string the limit clause * @return string */ - protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table) { - $conditions = array(); - - empty($where) OR $conditions[] = implode(' ', $where); - empty($like) OR $conditions[] = implode(' ', $like); - empty($limit) OR $conditions[] = 'rownum <= '.$limit; + if ($this->qb_limit) + { + $this->where('rownum <= ', (int) $this->qb_limit, FALSE); + $this->qb_limit = FALSE; + } - return 'DELETE FROM '.$table.(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : ''); + return parent::_delete($table); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/odbc/odbc_driver.php b/system/database/drivers/odbc/odbc_driver.php index 8f0a474b0..f62400245 100644 --- a/system/database/drivers/odbc/odbc_driver.php +++ b/system/database/drivers/odbc/odbc_driver.php @@ -306,6 +306,24 @@ class CI_DB_odbc_driver extends CI_DB { // -------------------------------------------------------------------- + /** + * Update statement + * + * Generates a platform-specific update string from the supplied data + * + * @param string the table name + * @param array the update data + * @return string + */ + protected function _update($table, $values) + { + $this->qb_limit = FALSE; + $this->qb_orderby = array(); + return parent::_update($table, $values); + } + + // -------------------------------------------------------------------- + /** * Truncate statement * @@ -324,6 +342,22 @@ class CI_DB_odbc_driver extends CI_DB { // -------------------------------------------------------------------- + /** + * Delete statement + * + * Generates a platform-specific delete string from the supplied data + * + * @param string the table name + * @return string + */ + protected function _delete($table) + { + $this->qb_limit = FALSE; + return parent::_delete($table); + } + + // -------------------------------------------------------------------- + /** * Close DB Connection * diff --git a/system/database/drivers/pdo/pdo_driver.php b/system/database/drivers/pdo/pdo_driver.php index b36a3d927..a6e2a6264 100644 --- a/system/database/drivers/pdo/pdo_driver.php +++ b/system/database/drivers/pdo/pdo_driver.php @@ -360,14 +360,12 @@ class CI_DB_pdo_driver extends CI_DB { * * @param string the table name * @param array the update data - * @param array the where clause + * @param string the where key * @return string */ - protected function _update_batch($table, $values, $index, $where = NULL) + protected function _update_batch($table, $values, $index) { $ids = array(); - $where = ($where !== '' && count($where) >=1) ? implode(" ", $where).' AND ' : ''; - foreach ($values as $key => $val) { $ids[] = $val[$index]; @@ -381,9 +379,7 @@ class CI_DB_pdo_driver extends CI_DB { } } - $sql = 'UPDATE '.$table.' SET '; $cases = ''; - foreach ($final as $k => $v) { $cases .= $k.' = CASE '."\n"; @@ -396,10 +392,9 @@ class CI_DB_pdo_driver extends CI_DB { $cases .= 'ELSE '.$k.' END, '; } - $sql .= substr($cases, 0, -2); - $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')'; + $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); - return $sql; + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where(); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_4d_driver.php b/system/database/drivers/pdo/subdrivers/pdo_4d_driver.php index e287f5c63..014112401 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_4d_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_4d_driver.php @@ -152,27 +152,13 @@ class CI_DB_pdo_4d_driver extends CI_DB_pdo_driver { * * @param string the table name * @param array the update data - * @param array the where clause - * @param array the orderby clause (ignored) - * @param array the limit clause (ignored) - * @param array the like clause * @return string */ - protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) + protected function _update($table, $values) { - foreach ($values as $key => $val) - { - $valstr[] = $key.' = '.$val; - } - - $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); - - if ( ! empty($like)) - { - $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); - } - - return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where; + $this->qb_limit = FALSE; + $this->qb_orderby = array(); + return parent::_update($table, $values); } // -------------------------------------------------------------------- @@ -183,21 +169,12 @@ class CI_DB_pdo_4d_driver extends CI_DB_pdo_driver { * Generates a platform-specific delete string from the supplied data * * @param string the table name - * @param array the where clause - * @param array the like clause - * @param string the limit clause (ignored) * @return string */ - protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table) { - $conditions = array(); - - empty($where) OR $conditions[] = implode(' ', $where); - empty($like) OR $conditions[] = implode(' ', $like); - - $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : ''; - - return 'DELETE FROM '.$table.$conditions; + $this->qb_limit = FALSE; + return parent::_delete($table); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_cubrid_driver.php b/system/database/drivers/pdo/subdrivers/pdo_cubrid_driver.php index 05eeacfe6..be85c8644 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_cubrid_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_cubrid_driver.php @@ -133,10 +133,10 @@ class CI_DB_pdo_cubrid_driver extends CI_DB_pdo_driver { * * @param string the table name * @param array the update data - * @param array the where clause + * @param string the where key * @return string */ - protected function _update_batch($table, $values, $index, $where = NULL) + protected function _update_batch($table, $values, $index) { $ids = array(); foreach ($values as $key => $val) @@ -160,9 +160,9 @@ class CI_DB_pdo_cubrid_driver extends CI_DB_pdo_driver { .'ELSE '.$k.' END), '; } - return 'UPDATE '.$table.' SET '.substr($cases, 0, -2) - .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '') - .$index.' IN('.implode(',', $ids).')'; + $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); + + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where(); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php b/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php index 7060c9eb9..6df9cc638 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php @@ -175,27 +175,13 @@ class CI_DB_pdo_dblib_driver extends CI_DB_pdo_driver { * * @param string the table name * @param array the update data - * @param array the where clause - * @param array the orderby clause (ignored) - * @param array the limit clause (ignored) - * @param array the like clause * @return string */ - protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) + protected function _update($table, $values) { - foreach ($values as $key => $val) - { - $valstr[] = $key.' = '.$val; - } - - $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); - - if ( ! empty($like)) - { - $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); - } - - return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where; + $this->qb_limit = FALSE; + $this->qb_orderby = array(); + return parent::_update($table, $values); } // -------------------------------------------------------------------- @@ -206,23 +192,16 @@ class CI_DB_pdo_dblib_driver extends CI_DB_pdo_driver { * Generates a platform-specific delete string from the supplied data * * @param string the table name - * @param array the where clause - * @param array the like clause - * @param string the limit clause * @return string */ - protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table) { - $conditions = array(); - - empty($where) OR $conditions[] = implode(' ', $where); - empty($like) OR $conditions[] = implode(' ', $like); - - $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : ''; + if ($this->qb_limit) + { + return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_where().') DELETE FROM ci_delete'; + } - return ($limit) - ? 'WITH ci_delete AS (SELECT TOP '.$limit.' * FROM '.$table.$conditions.') DELETE FROM ci_delete' - : 'DELETE FROM '.$table.$conditions; + return parent::_delete($table); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_firebird_driver.php b/system/database/drivers/pdo/subdrivers/pdo_firebird_driver.php index c074a9a78..ee21ed22f 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_firebird_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_firebird_driver.php @@ -161,29 +161,12 @@ class CI_DB_pdo_firebird_driver extends CI_DB_pdo_driver { * * @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 (ignored) - * @param array the like clause * @return string */ - protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) + protected function _update($table, $values) { - foreach ($values as $key => $val) - { - $valstr[] = $key.' = '.$val; - } - - $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); - - if ( ! empty($like)) - { - $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); - } - - return 'UPDATE '.$table.' SET '.implode(', ', $valstr) - .$where - .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : ''); + $this->qb_limit = FALSE; + return parent::_update($table, $values); } // -------------------------------------------------------------------- @@ -212,19 +195,12 @@ class CI_DB_pdo_firebird_driver extends CI_DB_pdo_driver { * Generates a platform-specific delete string from the supplied data * * @param string the table name - * @param array the where clause - * @param array the like clause - * @param string the limit clause (ignored) * @return string */ - protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table) { - $conditions = array(); - - empty($where) OR $conditions[] = implode(' ', $where); - empty($like) OR $conditions[] = implode(' ', $like); - - return 'DELETE FROM '.$table.(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : ''); + $this->qb_limit = FALSE; + return parent::_delete($table); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_ibm_driver.php b/system/database/drivers/pdo/subdrivers/pdo_ibm_driver.php index 832c03c96..7563a42d6 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_ibm_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_ibm_driver.php @@ -187,27 +187,13 @@ class CI_DB_pdo_ibm_driver extends CI_DB_pdo_driver { * * @param string the table name * @param array the update data - * @param array the where clause - * @param array the orderby clause (ignored) - * @param array the limit clause (ignored) - * @param array the like clause * @return string */ - protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) + protected function _update($table, $values) { - foreach ($values as $key => $val) - { - $valstr[] = $key.' = '.$val; - } - - $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); - - if ( ! empty($like)) - { - $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); - } - - return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where; + $this->qb_limit = FALSE; + $this->qb_orderby = array(); + return parent::_update($table, $values); } // -------------------------------------------------------------------- @@ -218,21 +204,12 @@ class CI_DB_pdo_ibm_driver extends CI_DB_pdo_driver { * Generates a platform-specific delete string from the supplied data * * @param string the table name - * @param array the where clause - * @param array the like clause - * @param string the limit clause (ignored) * @return string */ - protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table) { - $conditions = array(); - - empty($where) OR $conditions[] = implode(' ', $where); - empty($like) OR $conditions[] = implode(' ', $like); - - $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : ''; - - return 'DELETE FROM '.$table.$conditions; + $this->qb_limit = FALSE; + return parent::_delete($table); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_informix_driver.php b/system/database/drivers/pdo/subdrivers/pdo_informix_driver.php index a3efc63dc..a6869a7d2 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_informix_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_informix_driver.php @@ -181,27 +181,13 @@ class CI_DB_pdo_informix_driver extends CI_DB_pdo_driver { * * @param string the table name * @param array the update data - * @param array the where clause - * @param array the orderby clause (ignored) - * @param array the limit clause (ignored) - * @param array the like clause * @return string */ - protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) + protected function _update($table, $values) { - foreach ($values as $key => $val) - { - $valstr[] = $key.' = '.$val; - } - - $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); - - if ( ! empty($like)) - { - $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); - } - - return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where; + $this->qb_limit = FALSE; + $this->qb_orderby = array(); + return parent::_update($table, $values); } // -------------------------------------------------------------------- @@ -230,21 +216,12 @@ class CI_DB_pdo_informix_driver extends CI_DB_pdo_driver { * Generates a platform-specific delete string from the supplied data * * @param string the table name - * @param array the where clause - * @param array the like clause - * @param string the limit clause (ignored) * @return string */ - protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table) { - $conditions = array(); - - empty($where) OR $conditions[] = implode(' ', $where); - empty($like) OR $conditions[] = implode(' ', $like); - - $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : ''; - - return 'DELETE FROM '.$table.$conditions; + $this->qb_limit = FALSE; + return parent::_delete($table); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_mysql_driver.php b/system/database/drivers/pdo/subdrivers/pdo_mysql_driver.php index 78afe246c..e10a84545 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_mysql_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_mysql_driver.php @@ -161,10 +161,10 @@ class CI_DB_pdo_mysql_driver extends CI_DB_pdo_driver { * * @param string the table name * @param array the update data - * @param array the where clause + * @param string the where key * @return string */ - protected function _update_batch($table, $values, $index, $where = NULL) + protected function _update_batch($table, $values, $index) { $ids = array(); foreach ($values as $key => $val) @@ -188,9 +188,9 @@ class CI_DB_pdo_mysql_driver extends CI_DB_pdo_driver { .'ELSE '.$k.' END), '; } - return 'UPDATE '.$table.' SET '.substr($cases, 0, -2) - .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '') - .$index.' IN('.implode(',', $ids).')'; + $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); + + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where(); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_oci_driver.php b/system/database/drivers/pdo/subdrivers/pdo_oci_driver.php index 56ec1bce1..494d82c3f 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_oci_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_oci_driver.php @@ -190,20 +190,17 @@ class CI_DB_pdo_oci_driver extends CI_DB_pdo_driver { * Generates a platform-specific delete string from the supplied data * * @param string the table name - * @param array the where clause - * @param array the like clause - * @param string the limit clause * @return string */ - protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table) { - $conditions = array(); - - empty($where) OR $conditions[] = implode(' ', $where); - empty($like) OR $conditions[] = implode(' ', $like); - empty($limit) OR $conditions[] = 'rownum <= '.$limit; + if ($this->qb_limit) + { + $this->where('rownum <= ', (int) $this->qb_limit, FALSE); + $this->qb_limit = FALSE; + } - return 'DELETE FROM '.$table.(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : ''); + return parent::_delete($table); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_odbc_driver.php b/system/database/drivers/pdo/subdrivers/pdo_odbc_driver.php index 392754ff7..722acad89 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_odbc_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_odbc_driver.php @@ -179,27 +179,13 @@ class CI_DB_pdo_odbc_driver extends CI_DB_pdo_driver { * * @param string the table name * @param array the update data - * @param array the where clause - * @param array the orderby clause (ignored) - * @param array the limit clause (ignored) - * @param array the like clause * @return string */ - protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) + protected function _update($table, $values) { - foreach ($values as $key => $val) - { - $valstr[] = $key.' = '.$val; - } - - $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); - - if ( ! empty($like)) - { - $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); - } - - return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where; + $this->qb_limit = FALSE; + $this->qb_orderby = array(); + return parent::_update($table, $values); } // -------------------------------------------------------------------- @@ -228,21 +214,12 @@ class CI_DB_pdo_odbc_driver extends CI_DB_pdo_driver { * Generates a platform-specific delete string from the supplied data * * @param string the table name - * @param array the where clause - * @param array the like clause - * @param string the limit clause * @return string */ - protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table) { - $conditions = array(); - - empty($where) OR $conditions[] = implode(' ', $where); - empty($like) OR $conditions[] = implode(' ', $like); - - $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : ''; - - return 'DELETE FROM '.$table.$conditions; + $this->qb_limit = FALSE; + return parent::_delete($table); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_pgsql_driver.php b/system/database/drivers/pdo/subdrivers/pdo_pgsql_driver.php index 9a476f143..d2afd1d71 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_pgsql_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_pgsql_driver.php @@ -164,27 +164,13 @@ class CI_DB_pdo_pgsql_driver extends CI_DB_pdo_driver { * * @param string the table name * @param array the update data - * @param array the where clause - * @param array the orderby clause (ignored) - * @param array the limit clause (ignored) - * @param array the like clause * @return string */ - protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) + protected function _update($table, $values) { - foreach ($values as $key => $val) - { - $valstr[] = $key.' = '.$val; - } - - $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); - - if ( ! empty($like)) - { - $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); - } - - return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where; + $this->qb_limit = FALSE; + $this->qb_orderby = array(); + return parent::_update($table, $values); } // -------------------------------------------------------------------- @@ -196,10 +182,10 @@ class CI_DB_pdo_pgsql_driver extends CI_DB_pdo_driver { * * @param string the table name * @param array the update data - * @param array the where clause + * @param string the where key * @return string */ - protected function _update_batch($table, $values, $index, $where = NULL) + protected function _update_batch($table, $values, $index) { $ids = array(); foreach ($values as $key => $val) @@ -218,14 +204,14 @@ class CI_DB_pdo_pgsql_driver extends CI_DB_pdo_driver { $cases = ''; foreach ($final as $k => $v) { - $cases .= $k.' = (CASE '.$k."\n" + $cases .= $k.' = (CASE '.$index."\n" .implode("\n", $v)."\n" .'ELSE '.$k.' END), '; } - return 'UPDATE '.$table.' SET '.substr($cases, 0, -2) - .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '') - .$index.' IN('.implode(',', $ids).')'; + $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); + + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where(); } // -------------------------------------------------------------------- @@ -236,19 +222,12 @@ class CI_DB_pdo_pgsql_driver extends CI_DB_pdo_driver { * Generates a platform-specific delete string from the supplied data * * @param string the table name - * @param array the where clause - * @param array the like clause - * @param string the limit clause (ignored) * @return string */ - protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table) { - $conditions = array(); - - empty($where) OR $conditions[] = implode(' ', $where); - empty($like) OR $conditions[] = implode(' ', $like); - - return 'DELETE FROM '.$table.(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : ''); + $this->qb_limit = FALSE; + return parent::_delete($table); } // -------------------------------------------------------------------- @@ -273,11 +252,12 @@ class CI_DB_pdo_pgsql_driver extends CI_DB_pdo_driver { /** * Where * - * Called by where() or or_where() + * Called by where(), or_where() * * @param mixed * @param mixed * @param string + * @param bool * @return object */ protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL) @@ -296,10 +276,6 @@ class CI_DB_pdo_pgsql_driver extends CI_DB_pdo_driver { ? $this->_group_get_type('') : $this->_group_get_type($type); - $k = (($op = $this->_get_operator($k)) !== FALSE) - ? $this->protect_identifiers(substr($k, 0, strpos($k, $op)), FALSE, $escape).strstr($k, $op) - : $this->protect_identifiers($k, FALSE, $escape); - if (is_null($v) && ! $this->_has_operator($k)) { // value appears not to have been set, assign the test to IS NULL @@ -308,13 +284,13 @@ class CI_DB_pdo_pgsql_driver extends CI_DB_pdo_driver { if ( ! is_null($v)) { - if ($escape === TRUE) + if (is_bool($v)) { - $v = ' '.$this->escape($v); + $v = ' '.($v ? 'TRUE' : 'FALSE'); } - elseif (is_bool($v)) + elseif ($escape === TRUE) { - $v = ($v ? ' TRUE' : ' FALSE'); + $v = ' '.(is_int($v) ? $v : $this->escape($v)); } if ( ! $this->_has_operator($k)) @@ -323,10 +299,11 @@ class CI_DB_pdo_pgsql_driver extends CI_DB_pdo_driver { } } - $this->qb_where[] = $prefix.$k.$v; + $this->qb_where[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); if ($this->qb_caching === TRUE) { - $this->qb_cache_where[] = $prefix.$k.$v; + // check this shit + $this->qb_cache_where[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); $this->qb_cache_exists[] = 'where'; } diff --git a/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php b/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php index f125b8f50..1896225f0 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php @@ -204,27 +204,13 @@ class CI_DB_pdo_sqlsrv_driver extends CI_DB_pdo_driver { * * @param string the table name * @param array the update data - * @param array the where clause - * @param array the orderby clause (ignored) - * @param array the limit clause (ignored) - * @param array the like clause * @return string */ - protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) + protected function _update($table, $values) { - foreach ($values as $key => $val) - { - $valstr[] = $key.' = '.$val; - } - - $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); - - if ( ! empty($like)) - { - $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); - } - - return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where; + $this->qb_limit = FALSE; + $this->qb_orderby = array(); + return parent::_update($table, $values); } // -------------------------------------------------------------------- @@ -235,23 +221,16 @@ class CI_DB_pdo_sqlsrv_driver extends CI_DB_pdo_driver { * Generates a platform-specific delete string from the supplied data * * @param string the table name - * @param array the where clause - * @param array the like clause - * @param string the limit clause * @return string */ - protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table) { - $conditions = array(); - - empty($where) OR $conditions[] = implode(' ', $where); - empty($like) OR $conditions[] = implode(' ', $like); - - $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : ''; + if ($this->qb_limit) + { + return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_where().') DELETE FROM ci_delete'; + } - return ($limit) - ? 'WITH ci_delete AS (SELECT TOP '.$limit.' * FROM '.$table.$conditions.') DELETE FROM ci_delete' - : 'DELETE FROM '.$table.$conditions; + return parent::_delete($table); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index 031740851..15059f3d4 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -475,27 +475,13 @@ 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 (ignored) - * @param array the limit clause (ignored) - * @param array the like clause * @return string */ - protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) + protected function _update($table, $values) { - foreach ($values as $key => $val) - { - $valstr[] = $key.' = '.$val; - } - - $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); - - if ( ! empty($like)) - { - $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); - } - - return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where; + $this->qb_limit = FALSE; + $this->qb_orderby = array(); + return parent::_update($table, $values); } // -------------------------------------------------------------------- @@ -507,10 +493,10 @@ class CI_DB_postgre_driver extends CI_DB { * * @param string the table name * @param array the update data - * @param array the where clause + * @param string the where key * @return string */ - protected function _update_batch($table, $values, $index, $where = NULL) + protected function _update_batch($table, $values, $index) { $ids = array(); foreach ($values as $key => $val) @@ -534,9 +520,9 @@ class CI_DB_postgre_driver extends CI_DB { .'ELSE '.$k.' END), '; } - return 'UPDATE '.$table.' SET '.substr($cases, 0, -2) - .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '') - .$index.' IN('.implode(',', $ids).')'; + $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); + + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where(); } // -------------------------------------------------------------------- @@ -547,19 +533,12 @@ class CI_DB_postgre_driver extends CI_DB { * Generates a platform-specific delete string from the supplied data * * @param string the table name - * @param array the where clause - * @param array the like clause - * @param string the limit clause (ignored) * @return string */ - protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table) { - $conditions = array(); - - empty($where) OR $conditions[] = implode(' ', $where); - empty($like) OR $conditions[] = implode(' ', $like); - - return 'DELETE FROM '.$table.(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : ''); + $this->qb_limit = FALSE; + return parent::_delete($table); } // -------------------------------------------------------------------- @@ -584,12 +563,12 @@ class CI_DB_postgre_driver extends CI_DB { /** * Where * - * Called by where() or or_where() + * Called by where(), or_where() * * @param mixed * @param mixed * @param string - * @param mixed + * @param bool * @return object */ protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL) @@ -608,13 +587,6 @@ class CI_DB_postgre_driver extends CI_DB { ? $this->_group_get_type('') : $this->_group_get_type($type); - if ($escape === TRUE) - { - $k = (($op = $this->_get_operator($k)) !== FALSE) - ? $this->escape_identifiers(trim(substr($k, 0, strpos($k, $op)))).' '.strstr($k, $op) - : $this->escape_identifiers(trim($k)); - } - if (is_null($v) && ! $this->_has_operator($k)) { // value appears not to have been set, assign the test to IS NULL @@ -623,13 +595,13 @@ class CI_DB_postgre_driver extends CI_DB { if ( ! is_null($v)) { - if ($escape === TRUE) + if (is_bool($v)) { - $v = ' '.$this->escape($v); + $v = ' '.($v ? 'TRUE' : 'FALSE'); } - elseif (is_bool($v)) + elseif ($escape === TRUE) { - $v = ($v ? ' TRUE' : ' FALSE'); + $v = ' '.(is_int($v) ? $v : $this->escape($v)); } if ( ! $this->_has_operator($k)) @@ -638,10 +610,11 @@ class CI_DB_postgre_driver extends CI_DB { } } - $this->qb_where[] = $prefix.$k.$v; + $this->qb_where[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); if ($this->qb_caching === TRUE) { - $this->qb_cache_where[] = $prefix.$k.$v; + // check this shit + $this->qb_cache_where[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); $this->qb_cache_exists[] = 'where'; } diff --git a/system/database/drivers/sqlsrv/sqlsrv_driver.php b/system/database/drivers/sqlsrv/sqlsrv_driver.php index 8bd18bd76..6baa152e8 100644 --- a/system/database/drivers/sqlsrv/sqlsrv_driver.php +++ b/system/database/drivers/sqlsrv/sqlsrv_driver.php @@ -384,27 +384,13 @@ class CI_DB_sqlsrv_driver extends CI_DB { * * @param string the table name * @param array the update data - * @param array the where clause - * @param array the orderby clause (ignored) - * @param array the limit clause (ignored) - * @param array the like clause * @return string */ - protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) + protected function _update($table, $values) { - foreach ($values as $key => $val) - { - $valstr[] = $key.' = '.$val; - } - - $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); - - if ( ! empty($like)) - { - $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); - } - - return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where; + $this->qb_limit = FALSE; + $this->qb_orderby = array(); + return parent::_update($table, $values); } // -------------------------------------------------------------------- @@ -433,23 +419,16 @@ class CI_DB_sqlsrv_driver extends CI_DB { * Generates a platform-specific delete string from the supplied data * * @param string the table name - * @param array the where clause - * @param array the like clause - * @param string the limit clause * @return string */ - protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table) { - $conditions = array(); - - empty($where) OR $conditions[] = implode(' ', $where); - empty($like) OR $conditions[] = implode(' ', $like); - - $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : ''; + if ($this->qb_limit) + { + return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_where().') DELETE FROM ci_delete'; + } - return ($limit) - ? 'WITH ci_delete AS (SELECT TOP '.$limit.' * FROM '.$table.$conditions.') DELETE FROM ci_delete' - : 'DELETE FROM '.$table.$conditions; + return parent::_delete($table); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From d40459d94f91219f080caabebd627fdc319b0f42 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 18 Jul 2012 16:46:39 +0300 Subject: Merge where() and having() logic - it's structurally identical and only the keyword differs --- system/database/DB_query_builder.php | 124 ++++++--------------- system/database/drivers/cubrid/cubrid_driver.php | 2 +- system/database/drivers/mssql/mssql_driver.php | 2 +- system/database/drivers/mysql/mysql_driver.php | 2 +- system/database/drivers/mysqli/mysqli_driver.php | 2 +- system/database/drivers/pdo/pdo_driver.php | 2 +- .../drivers/pdo/subdrivers/pdo_cubrid_driver.php | 2 +- .../drivers/pdo/subdrivers/pdo_dblib_driver.php | 2 +- .../drivers/pdo/subdrivers/pdo_mysql_driver.php | 2 +- .../drivers/pdo/subdrivers/pdo_pgsql_driver.php | 20 ++-- .../drivers/pdo/subdrivers/pdo_sqlsrv_driver.php | 2 +- system/database/drivers/postgre/postgre_driver.php | 20 ++-- system/database/drivers/sqlsrv/sqlsrv_driver.php | 2 +- 13 files changed, 69 insertions(+), 115 deletions(-) diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index 29b75cd1d..34a77c551 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -416,7 +416,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { */ public function where($key, $value = NULL, $escape = NULL) { - return $this->_where($key, $value, 'AND ', $escape); + return $this->_wh('qb_where', $key, $value, 'AND ', $escape); } // -------------------------------------------------------------------- @@ -434,24 +434,27 @@ abstract class CI_DB_query_builder extends CI_DB_driver { */ public function or_where($key, $value = NULL, $escape = NULL) { - return $this->_where($key, $value, 'OR ', $escape); + return $this->_wh('qb_where', $key, $value, 'OR ', $escape); } // -------------------------------------------------------------------- /** - * Where + * WHERE, HAVING * - * Called by where(), or_where() + * Called by where(), or_where(), having(), or_having() * + * @param string 'qb_where' or 'qb_having' * @param mixed * @param mixed * @param string * @param bool * @return object */ - protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL) + protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL) { + $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where'; + if ( ! is_array($key)) { $key = array($key => $value); @@ -462,7 +465,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { foreach ($key as $k => $v) { - $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) + $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type); @@ -485,12 +488,11 @@ abstract class CI_DB_query_builder extends CI_DB_driver { } } - $this->qb_where[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); + $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); if ($this->qb_caching === TRUE) { - // check this shit - $this->qb_cache_where[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); - $this->qb_cache_exists[] = 'where'; + $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); + $this->qb_cache_exists[] = substr($qb_key, 3); } } @@ -916,7 +918,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { */ public function having($key, $value = '', $escape = NULL) { - return $this->_having($key, $value, 'AND ', $escape); + return $this->_wh('qb_having', $key, $value, 'AND ', $escape); } // -------------------------------------------------------------------- @@ -933,58 +935,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { */ public function or_having($key, $value = '', $escape = NULL) { - return $this->_having($key, $value, 'OR ', $escape); - } - - // -------------------------------------------------------------------- - - /** - * Sets the HAVING values - * - * Called by having() or or_having() - * - * @param string - * @param string - * @param string - * @param bool - * @return object - */ - protected function _having($key, $value = '', $type = 'AND ', $escape = NULL) - { - if ( ! is_array($key)) - { - $key = array($key => $value); - } - - is_bool($escape) OR $escape = $this->_protect_identifiers; - - foreach ($key as $k => $v) - { - $prefix = (count($this->qb_having) === 0) ? '' : $type; - - $k = $this->_has_operator($k) - ? $this->protect_identifiers(substr($k, 0, strpos(rtrim($k), ' ')), FALSE, $escape).strchr(rtrim($k), ' ') - : $this->protect_identifiers($k, FALSE, $escape); - - 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; + return $this->_wh('qb_having', $key, $value, 'OR ', $escape); } // -------------------------------------------------------------------- @@ -1931,7 +1882,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { */ protected function _delete($table) { - return 'DELETE FROM '.$table.$this->_compile_where() + return 'DELETE FROM '.$table.$this->_compile_wh('qb_where') .($this->qb_limit ? ' LIMIT '.(int) $this->qb_limit : ''); } @@ -2070,7 +2021,8 @@ abstract class CI_DB_query_builder extends CI_DB_driver { $sql .= "\n".implode("\n", $this->qb_join); } - $sql .= $this->_compile_conditions(); + // WHERE + $sql .= $this->_compile_wh('qb_where'); // GROUP BY if (count($this->qb_groupby) > 0) @@ -2079,10 +2031,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { } // HAVING - if (count($this->qb_having) > 0) - { - $sql .= "\nHAVING ".implode("\n", $this->qb_having); - } + $sql .= $this->_compile_wh('qb_having'); // ORDER BY if (count($this->qb_orderby) > 0) @@ -2090,7 +2039,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby); } - // Write the "LIMIT" portion of the query + // LIMIT if (is_numeric($this->qb_limit)) { return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset); @@ -2102,34 +2051,35 @@ abstract class CI_DB_query_builder extends CI_DB_driver { // -------------------------------------------------------------------- /** - * Compile WHERE statement + * Compile WHERE, HAVING statements + * + * Escapes identifiers in WHERE and HAVING statements at execution time. * - * Escapes identifiers in WHERE statements at execution time. * Required so that aliases are tracked properly, regardless of wether - * e.g. where() is called prior to join() and dbprefix is added only - * if needed. + * where(), or_where(), having(), or_having are called prior to from(), + * join() and dbprefix is added only if needed. * - * @return string + * @param string 'qb_where' or 'qb_having' + * @return string SQL statement */ - protected function _compile_where() + protected function _compile_wh($qb_key) { - // WHERE - if (count($this->qb_where) > 0) + if (count($this->$qb_key) > 0) { - $sql = "\nWHERE "; + $sql = ($qb_key === 'qb_having') ? "\nHAVING " : "\nWHERE "; - for ($i = 0, $c = count($this->qb_where); $i < $c; $i++) + for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++) { - if ($this->qb_where[$i]['escape'] === FALSE) + if ($this->{$qb_key}[$i]['escape'] === FALSE) { - $this->qb_where[$i] = $this->qb_where[$i]['condition']; + $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition']; continue; } - $op = preg_quote($this->_get_operator($this->qb_where[$i]['condition'])); - if ( ! preg_match('/^(\s*(?:AND|OR)\s+)?(\(?)(.*)('.$op.')(.*(?qb_where[$i]['condition'], $matches)) + $op = preg_quote($this->_get_operator($this->{$qb_key}[$i]['condition'])); + if ( ! preg_match('/^(\s*(?:AND|OR)\s+)?(\(?)(.*)('.$op.')(.*(?{$qb_key}[$i]['condition'], $matches)) { - $this->qb_where[$i] = $this->qb_where[$i]['condition']; + $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition']; continue; } @@ -2143,11 +2093,11 @@ abstract class CI_DB_query_builder extends CI_DB_driver { // 6 => ')' /* optional */ // ); empty($matches[5]) OR $matches[5] = ' '.$this->protect_identifiers(trim($matches[5])); - $this->qb_where[$i] = $matches[1].$matches[2].$this->protect_identifiers(trim($matches[3])) + $this->{$qb_key}[$i] = $matches[1].$matches[2].$this->protect_identifiers(trim($matches[3])) .' '.trim($matches[4]).$matches[5].$matches[6]; } - return implode("\n", $this->qb_where); + return implode("\n", $this->$qb_key); } return ''; diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index e2ace3320..e243aae9f 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -425,7 +425,7 @@ class CI_DB_cubrid_driver extends CI_DB { $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); - return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where(); + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where'); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/mssql/mssql_driver.php b/system/database/drivers/mssql/mssql_driver.php index 672c3161c..35cd85702 100644 --- a/system/database/drivers/mssql/mssql_driver.php +++ b/system/database/drivers/mssql/mssql_driver.php @@ -429,7 +429,7 @@ class CI_DB_mssql_driver extends CI_DB { { if ($this->qb_limit) { - return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_where().') DELETE FROM ci_delete'; + return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete'; } return parent::_delete($table); diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php index 634430665..0a15fe447 100644 --- a/system/database/drivers/mysql/mysql_driver.php +++ b/system/database/drivers/mysql/mysql_driver.php @@ -449,7 +449,7 @@ class CI_DB_mysql_driver extends CI_DB { $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); - return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where(); + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where'); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php index 5498aa244..5f5a31d34 100644 --- a/system/database/drivers/mysqli/mysqli_driver.php +++ b/system/database/drivers/mysqli/mysqli_driver.php @@ -441,7 +441,7 @@ class CI_DB_mysqli_driver extends CI_DB { $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); - return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where(); + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where'); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/pdo_driver.php b/system/database/drivers/pdo/pdo_driver.php index a6e2a6264..ee5af783e 100644 --- a/system/database/drivers/pdo/pdo_driver.php +++ b/system/database/drivers/pdo/pdo_driver.php @@ -394,7 +394,7 @@ class CI_DB_pdo_driver extends CI_DB { $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); - return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where(); + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where'); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_cubrid_driver.php b/system/database/drivers/pdo/subdrivers/pdo_cubrid_driver.php index be85c8644..741126310 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_cubrid_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_cubrid_driver.php @@ -162,7 +162,7 @@ class CI_DB_pdo_cubrid_driver extends CI_DB_pdo_driver { $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); - return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where(); + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where'); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php b/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php index 6df9cc638..20d510ff6 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php @@ -198,7 +198,7 @@ class CI_DB_pdo_dblib_driver extends CI_DB_pdo_driver { { if ($this->qb_limit) { - return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_where().') DELETE FROM ci_delete'; + return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete'; } return parent::_delete($table); diff --git a/system/database/drivers/pdo/subdrivers/pdo_mysql_driver.php b/system/database/drivers/pdo/subdrivers/pdo_mysql_driver.php index e10a84545..67da156bf 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_mysql_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_mysql_driver.php @@ -190,7 +190,7 @@ class CI_DB_pdo_mysql_driver extends CI_DB_pdo_driver { $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); - return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where(); + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where'); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_pgsql_driver.php b/system/database/drivers/pdo/subdrivers/pdo_pgsql_driver.php index d2afd1d71..510a2a38f 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_pgsql_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_pgsql_driver.php @@ -211,7 +211,7 @@ class CI_DB_pdo_pgsql_driver extends CI_DB_pdo_driver { $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); - return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where(); + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where'); } // -------------------------------------------------------------------- @@ -250,18 +250,21 @@ class CI_DB_pdo_pgsql_driver extends CI_DB_pdo_driver { // -------------------------------------------------------------------- /** - * Where + * WHERE, HAVING * - * Called by where(), or_where() + * Called by where(), or_where(), having(), or_having() * + * @param string 'qb_where' or 'qb_having' * @param mixed * @param mixed * @param string * @param bool * @return object */ - protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL) + protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL) { + $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where'; + if ( ! is_array($key)) { $key = array($key => $value); @@ -272,7 +275,7 @@ class CI_DB_pdo_pgsql_driver extends CI_DB_pdo_driver { foreach ($key as $k => $v) { - $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) + $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type); @@ -299,12 +302,11 @@ class CI_DB_pdo_pgsql_driver extends CI_DB_pdo_driver { } } - $this->qb_where[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); + $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); if ($this->qb_caching === TRUE) { - // check this shit - $this->qb_cache_where[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); - $this->qb_cache_exists[] = 'where'; + $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); + $this->qb_cache_exists[] = substr($qb_key, 3); } } diff --git a/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php b/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php index 1896225f0..39cb5f9ef 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php @@ -227,7 +227,7 @@ class CI_DB_pdo_sqlsrv_driver extends CI_DB_pdo_driver { { if ($this->qb_limit) { - return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_where().') DELETE FROM ci_delete'; + return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete'; } return parent::_delete($table); diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index 15059f3d4..1c03f6831 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -522,7 +522,7 @@ class CI_DB_postgre_driver extends CI_DB { $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); - return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where(); + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where'); } // -------------------------------------------------------------------- @@ -561,18 +561,21 @@ class CI_DB_postgre_driver extends CI_DB { // -------------------------------------------------------------------- /** - * Where + * WHERE, HAVING * - * Called by where(), or_where() + * Called by where(), or_where(), having(), or_having() * + * @param string 'qb_where' or 'qb_having' * @param mixed * @param mixed * @param string * @param bool * @return object */ - protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL) + protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL) { + $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where'; + if ( ! is_array($key)) { $key = array($key => $value); @@ -583,7 +586,7 @@ class CI_DB_postgre_driver extends CI_DB { foreach ($key as $k => $v) { - $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) + $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type); @@ -610,12 +613,11 @@ class CI_DB_postgre_driver extends CI_DB { } } - $this->qb_where[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); + $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); if ($this->qb_caching === TRUE) { - // check this shit - $this->qb_cache_where[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); - $this->qb_cache_exists[] = 'where'; + $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); + $this->qb_cache_exists[] = substr($qb_key, 3); } } diff --git a/system/database/drivers/sqlsrv/sqlsrv_driver.php b/system/database/drivers/sqlsrv/sqlsrv_driver.php index 6baa152e8..9ea6facb3 100644 --- a/system/database/drivers/sqlsrv/sqlsrv_driver.php +++ b/system/database/drivers/sqlsrv/sqlsrv_driver.php @@ -425,7 +425,7 @@ class CI_DB_sqlsrv_driver extends CI_DB { { if ($this->qb_limit) { - return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_where().') DELETE FROM ci_delete'; + return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete'; } return parent::_delete($table); -- cgit v1.2.3-24-g4f1b From 94611df88cf99ae530258a25e2051e901b9ffcc7 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 19 Jul 2012 12:29:54 +0300 Subject: Remove qb_wherein property --- system/database/DB_query_builder.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index 34a77c551..7b0565df9 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -54,7 +54,6 @@ abstract class CI_DB_query_builder extends CI_DB_driver { protected $qb_offset = FALSE; protected $qb_orderby = array(); protected $qb_set = array(); - protected $qb_wherein = array(); protected $qb_aliased_tables = array(); protected $qb_store_array = array(); protected $qb_where_group_started = FALSE; @@ -597,14 +596,15 @@ abstract class CI_DB_query_builder extends CI_DB_driver { $not = ($not) ? ' NOT' : ''; + $where_in = array(); foreach ($values as $value) { - $this->qb_wherein[] = $this->escape($value); + $wherein[] = $this->escape($value); } $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type); $where_in = array( - 'condition' => $prefix.$key.$not.' IN('.implode(', ', $this->qb_wherein).')', + 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')', 'escape' => $escape ); @@ -615,8 +615,6 @@ abstract class CI_DB_query_builder extends CI_DB_driver { $this->qb_cache_exists[] = 'where'; } - // reset the array for multiple calls - $this->qb_wherein = array(); return $this; } @@ -1856,7 +1854,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { $this->limit($limit); } - if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0) + if (count($this->qb_where) === 0) { return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE; } @@ -2315,7 +2313,6 @@ abstract class CI_DB_query_builder extends CI_DB_driver { 'qb_groupby' => array(), 'qb_having' => array(), 'qb_orderby' => array(), - 'qb_wherein' => array(), 'qb_aliased_tables' => array(), 'qb_no_escape' => array(), 'qb_distinct' => FALSE, -- cgit v1.2.3-24-g4f1b From c9b924c1498847d8f324d81c8994fff0b95f26dc Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 19 Jul 2012 13:06:02 +0300 Subject: Remove _limit()'s extra parameters and qb_limit, qb_offset unneeded typecasts + add _compile_group_by() method --- system/database/DB_driver.php | 2 +- system/database/DB_query_builder.php | 49 ++++++++++++++++------ system/database/drivers/ibase/ibase_driver.php | 15 +++---- system/database/drivers/mssql/mssql_driver.php | 20 +++------ system/database/drivers/oci8/oci8_driver.php | 10 ++--- .../drivers/pdo/subdrivers/pdo_4d_driver.php | 6 +-- .../drivers/pdo/subdrivers/pdo_dblib_driver.php | 16 ++++--- .../drivers/pdo/subdrivers/pdo_firebird_driver.php | 10 ++--- .../drivers/pdo/subdrivers/pdo_informix_driver.php | 6 +-- .../drivers/pdo/subdrivers/pdo_oci_driver.php | 10 ++--- .../drivers/pdo/subdrivers/pdo_odbc_driver.php | 6 +-- .../drivers/pdo/subdrivers/pdo_pgsql_driver.php | 6 +-- .../drivers/pdo/subdrivers/pdo_sqlsrv_driver.php | 18 ++++---- system/database/drivers/postgre/postgre_driver.php | 6 +-- system/database/drivers/sqlsrv/sqlsrv_driver.php | 16 ++++--- 15 files changed, 95 insertions(+), 101 deletions(-) diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 10306d721..43ea10023 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1130,7 +1130,7 @@ abstract class CI_DB_driver { return 'UPDATE '.$table.' SET '.implode(', ', $valstr) .$this->_compile_where() .(empty($this->qb_orderby) ? '' : ' ORDER BY '.implode(', ', $this->qb_orderby)) - .($this->qb_limit ? ' LIMIT '.(int) $this->qb_limit : ''); + .($this->qb_limit ? ' LIMIT '.$this->qb_limit : ''); } // -------------------------------------------------------------------- diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index 7b0565df9..55b97bb3f 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -874,15 +874,18 @@ abstract class CI_DB_query_builder extends CI_DB_driver { * GROUP BY * * @param string + * @param bool * @return object */ - public function group_by($by) + public function group_by($by, $escape = NULL) { if (is_string($by)) { $by = explode(',', $by); } + is_bool($escape) OR $escape = $this->_protect_identifiers; + foreach ($by as $val) { $val = trim($val); @@ -1005,7 +1008,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { * @param int the offset value * @return object */ - public function limit($value, $offset = NULL) + public function limit($value, $offset = FALSE) { is_null($value) OR $this->qb_limit = (int) $value; empty($offset) OR $this->qb_offset = (int) $offset; @@ -1035,13 +1038,11 @@ abstract class CI_DB_query_builder extends CI_DB_driver { * Generates a platform-specific LIMIT clause * * @param string the sql query string - * @param int the number of rows to limit the query to - * @param int the offset value * @return string */ - protected function _limit($sql, $limit, $offset) + protected function _limit($sql) { - return $sql.' LIMIT '.($offset ? $offset.', ' : '').$limit; + return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').$this->qb_limit; } // -------------------------------------------------------------------- @@ -1881,7 +1882,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { protected function _delete($table) { return 'DELETE FROM '.$table.$this->_compile_wh('qb_where') - .($this->qb_limit ? ' LIMIT '.(int) $this->qb_limit : ''); + .($this->qb_limit ? ' LIMIT '.$this->qb_limit : ''); } // -------------------------------------------------------------------- @@ -2023,10 +2024,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { $sql .= $this->_compile_wh('qb_where'); // GROUP BY - if (count($this->qb_groupby) > 0) - { - $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby); - } + $sql .= $this->_compile_group_by(); // HAVING $sql .= $this->_compile_wh('qb_having'); @@ -2038,9 +2036,9 @@ abstract class CI_DB_query_builder extends CI_DB_driver { } // LIMIT - if (is_numeric($this->qb_limit)) + if ($this->qb_limit) { - return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset); + return $this->_limit($sql."\n"); } return $sql; @@ -2103,6 +2101,31 @@ abstract class CI_DB_query_builder extends CI_DB_driver { // -------------------------------------------------------------------- + /** + * Compile GROUP BY + * + * Escapes identifiers in GROUP BY statements at execution time. + * + * Required so that aliases are tracked properly, regardless of wether + * group_by() is called prior to from(), join() and dbprefix is added + * only if needed. + * + * @return string SQL statement + */ + protected function _compile_group_by() + { + if (count($this->qb_groupby) > 0) + { + $sql = "\nGROUP BY "; + + $sql .= implode(', ', $this->qb_groupby); + } + + return ''; + } + + // -------------------------------------------------------------------- + /** * Object to Array * diff --git a/system/database/drivers/ibase/ibase_driver.php b/system/database/drivers/ibase/ibase_driver.php index 86c1fee6d..7b37b9999 100644 --- a/system/database/drivers/ibase/ibase_driver.php +++ b/system/database/drivers/ibase/ibase_driver.php @@ -285,7 +285,10 @@ class CI_DB_ibase_driver extends CI_DB { */ protected function _field_data($table) { - return $this->_limit('SELECT * FROM '.$this->protect_identifiers($table), 1, NULL); + $this->qb_limit = 1; + $sql = $this->_limit('SELECT * FROM '.$this->protect_identifiers($table)); + $this->qb_limit = 0; + return $sql; } // -------------------------------------------------------------------- @@ -378,22 +381,20 @@ class CI_DB_ibase_driver extends CI_DB { * Generates a platform-specific LIMIT clause * * @param string the sql query string - * @param int the number of rows to limit the query to - * @param int the offset value * @return string */ - protected function _limit($sql, $limit, $offset) + protected function _limit($sql) { // Limit clause depends on if Interbase or Firebird if (stripos($this->version(), 'firebird') !== FALSE) { - $select = 'FIRST '. (int) $limit - .($offset ? ' SKIP '. (int) $offset : ''); + $select = 'FIRST '.$this->qb_limit + .($this->qb_offset ? ' SKIP '.$this->qb_offset : ''); } else { $select = 'ROWS ' - .($offset ? (int) $offset.' TO '.($limit + $offset) : (int) $limit); + .($this->qb_offset ? $this->qb_offset.' TO '.($this->qb_limit + $this->qb_offset) : $this->qb_limit); } return preg_replace('`SELECT`i', 'SELECT '.$select, $sql); diff --git a/system/database/drivers/mssql/mssql_driver.php b/system/database/drivers/mssql/mssql_driver.php index 35cd85702..bfc82a6c2 100644 --- a/system/database/drivers/mssql/mssql_driver.php +++ b/system/database/drivers/mssql/mssql_driver.php @@ -429,7 +429,7 @@ class CI_DB_mssql_driver extends CI_DB { { if ($this->qb_limit) { - return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete'; + return 'WITH ci_delete AS (SELECT TOP '.$this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete'; } return parent::_delete($table); @@ -443,25 +443,17 @@ class CI_DB_mssql_driver extends CI_DB { * Generates a platform-specific LIMIT clause * * @param string the sql query string - * @param int the number of rows to limit the query to - * @param int the offset value * @return string */ - protected function _limit($sql, $limit, $offset) + protected function _limit($sql) { - // As of SQL Server 2012 (11.0.*) OFFSET is supported - if (version_compare($this->version(), '11', '>=')) - { - return $sql.' OFFSET '.(int) $offset.' ROWS FETCH NEXT '.(int) $limit.' ROWS ONLY'; - } - - $limit = $offset + $limit; + $limit = $this->qb_offset + $this->qb_limit; // As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported, // however an ORDER BY clause is required for it to work - if (version_compare($this->version(), '9', '>=') && $offset && ! empty($this->qb_orderby)) + if (version_compare($this->version(), '9', '>=') && $this->qb_offset && ! empty($this->qb_orderby)) { - $orderby = 'ORDER BY '.implode(', ', $this->qb_orderby); + $orderby = trim($this->_compile_order_by()); // We have to strip the ORDER BY clause $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby))); @@ -469,7 +461,7 @@ class CI_DB_mssql_driver extends CI_DB { return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n" .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql) ."\n) ".$this->escape_identifiers('CI_subquery') - ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.((int) $offset + 1).' AND '.$limit; + ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit; } return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql); diff --git a/system/database/drivers/oci8/oci8_driver.php b/system/database/drivers/oci8/oci8_driver.php index a0f26c257..dcc46527c 100644 --- a/system/database/drivers/oci8/oci8_driver.php +++ b/system/database/drivers/oci8/oci8_driver.php @@ -617,7 +617,7 @@ class CI_DB_oci8_driver extends CI_DB { { if ($this->qb_limit) { - $this->where('rownum <= ', (int) $this->qb_limit, FALSE); + $this->where('rownum <= ',$this->qb_limit, FALSE); $this->qb_limit = FALSE; } @@ -632,15 +632,13 @@ class CI_DB_oci8_driver extends CI_DB { * Generates a platform-specific LIMIT clause * * @param string the sql query string - * @param int the number of rows to limit the query to - * @param int the offset value * @return string */ - protected function _limit($sql, $limit, $offset) + protected function _limit($sql) { $this->limit_used = TRUE; - return 'SELECT * FROM (SELECT inner_query.*, rownum rnum FROM ('.$sql.') inner_query WHERE rownum < '.($offset + $limit + 1).')' - .($offset ? ' WHERE rnum >= '.($offset + 1): ''); + return 'SELECT * FROM (SELECT inner_query.*, rownum rnum FROM ('.$sql.') inner_query WHERE rownum < '.($this->qb_offset + $this->qb_limit + 1).')' + .($this->qb_offset ? ' WHERE rnum >= '.($this->qb_offset + 1): ''); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_4d_driver.php b/system/database/drivers/pdo/subdrivers/pdo_4d_driver.php index 014112401..0e6877c28 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_4d_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_4d_driver.php @@ -185,13 +185,11 @@ class CI_DB_pdo_4d_driver extends CI_DB_pdo_driver { * Generates a platform-specific LIMIT clause * * @param string the sql query string - * @param int the number of rows to limit the query to - * @param int the offset value * @return string */ - protected function _limit($sql, $limit, $offset) + protected function _limit($sql) { - return $sql.' LIMIT '.$limit.($offset ? ' OFFSET '.$offset : ''); + return $sql.' LIMIT '.$this->qb_limit.($this->qb_offset ? ' OFFSET '.$this->qb_offset : ''); } } diff --git a/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php b/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php index 20d510ff6..ad699ce23 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php @@ -198,7 +198,7 @@ class CI_DB_pdo_dblib_driver extends CI_DB_pdo_driver { { if ($this->qb_limit) { - return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete'; + return 'WITH ci_delete AS (SELECT TOP '.$this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete'; } return parent::_delete($table); @@ -212,19 +212,17 @@ class CI_DB_pdo_dblib_driver extends CI_DB_pdo_driver { * Generates a platform-specific LIMIT clause * * @param string the sql query string - * @param int the number of rows to limit the query to - * @param int the offset value * @return string */ - protected function _limit($sql, $limit, $offset) + protected function _limit($sql) { - $limit = $offset + $limit; + $limit = $this->qb_offset + $this->qb_limit; // As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported, // however an ORDER BY clause is required for it to work - if (version_compare($this->version(), '9', '>=') && $offset && ! empty($this->qb_orderby)) + if (version_compare($this->version(), '9', '>=') && $this->qb_offset && ! empty($this->qb_orderby)) { - $orderby = 'ORDER BY '.implode(', ', $this->qb_orderby); + $orderby = trim($this->_compile_order_by()); // We have to strip the ORDER BY clause $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby))); @@ -232,7 +230,7 @@ class CI_DB_pdo_dblib_driver extends CI_DB_pdo_driver { return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n" .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql) ."\n) ".$this->escape_identifiers('CI_subquery') - ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.((int) $offset + 1).' AND '.$limit; + ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit; } return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql); @@ -241,4 +239,4 @@ class CI_DB_pdo_dblib_driver extends CI_DB_pdo_driver { } /* End of file pdo_dblib_driver.php */ -/* Location: ./system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php */ +/* Location: ./system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php */ \ No newline at end of file diff --git a/system/database/drivers/pdo/subdrivers/pdo_firebird_driver.php b/system/database/drivers/pdo/subdrivers/pdo_firebird_driver.php index ee21ed22f..5b36342d2 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_firebird_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_firebird_driver.php @@ -211,22 +211,20 @@ class CI_DB_pdo_firebird_driver extends CI_DB_pdo_driver { * Generates a platform-specific LIMIT clause * * @param string the sql query string - * @param int the number of rows to limit the query to - * @param int the offset value * @return string */ - protected function _limit($sql, $limit, $offset) + protected function _limit($sql) { // Limit clause depends on if Interbase or Firebird if (stripos($this->version(), 'firebird') !== FALSE) { - $select = 'FIRST '. (int) $limit - .($offset > 0 ? ' SKIP '. (int) $offset : ''); + $select = 'FIRST '.$this->qb_limit + .($this->qb_offset > 0 ? ' SKIP '.$this->qb_offset : ''); } else { $select = 'ROWS ' - .($offset > 0 ? (int) $offset.' TO '.($limit + $offset) : (int) $limit); + .($this->qb_offset > 0 ? $this->qb_offset.' TO '.($this->qb_limit + $this->qb_offset) : $this->qb_limit); } return preg_replace('`SELECT`i', 'SELECT '.$select, $sql); diff --git a/system/database/drivers/pdo/subdrivers/pdo_informix_driver.php b/system/database/drivers/pdo/subdrivers/pdo_informix_driver.php index a6869a7d2..82480498a 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_informix_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_informix_driver.php @@ -232,13 +232,11 @@ class CI_DB_pdo_informix_driver extends CI_DB_pdo_driver { * Generates a platform-specific LIMIT clause * * @param string the sql query string - * @param int the number of rows to limit the query to - * @param int the offset value * @return string */ - protected function _limit($sql, $limit, $offset) + protected function _limit($sql) { - $select = 'SELECT '.($offset ? 'SKIP '.$offset : '').'FIRST '.$limit.' '; + $select = 'SELECT '.($this->qb_offset ? 'SKIP '.$this->qb_offset : '').'FIRST '.$this->qb_limit.' '; return preg_replace('/^(SELECT\s)/i', $select, $sql, 1); } diff --git a/system/database/drivers/pdo/subdrivers/pdo_oci_driver.php b/system/database/drivers/pdo/subdrivers/pdo_oci_driver.php index 494d82c3f..cfbb639a8 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_oci_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_oci_driver.php @@ -196,7 +196,7 @@ class CI_DB_pdo_oci_driver extends CI_DB_pdo_driver { { if ($this->qb_limit) { - $this->where('rownum <= ', (int) $this->qb_limit, FALSE); + $this->where('rownum <= ',$this->qb_limit, FALSE); $this->qb_limit = FALSE; } @@ -211,14 +211,12 @@ class CI_DB_pdo_oci_driver extends CI_DB_pdo_driver { * Generates a platform-specific LIMIT clause * * @param string the sql query string - * @param int the number of rows to limit the query to - * @param int the offset value * @return string */ - protected function _limit($sql, $limit, $offset) + protected function _limit($sql) { - return 'SELECT * FROM (SELECT inner_query.*, rownum rnum FROM ('.$sql.') inner_query WHERE rownum < '.($offset + $limit + 1).')' - .($offset ? ' WHERE rnum >= '.($offset + 1): ''); + return 'SELECT * FROM (SELECT inner_query.*, rownum rnum FROM ('.$sql.') inner_query WHERE rownum < '.($this->qb_offset + $this->qb_limit + 1).')' + .($this->qb_offset ? ' WHERE rnum >= '.($this->qb_offset + 1): ''); } } diff --git a/system/database/drivers/pdo/subdrivers/pdo_odbc_driver.php b/system/database/drivers/pdo/subdrivers/pdo_odbc_driver.php index 722acad89..0c3467484 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_odbc_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_odbc_driver.php @@ -230,13 +230,11 @@ class CI_DB_pdo_odbc_driver extends CI_DB_pdo_driver { * Generates a platform-specific LIMIT clause * * @param string the sql query string - * @param int the number of rows to limit the query to - * @param int the offset value * @return string */ - protected function _limit($sql, $limit, $offset) + protected function _limit($sql) { - return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql); + return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$this->qb_limit.' ', $sql); } } diff --git a/system/database/drivers/pdo/subdrivers/pdo_pgsql_driver.php b/system/database/drivers/pdo/subdrivers/pdo_pgsql_driver.php index 510a2a38f..07cf8f56b 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_pgsql_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_pgsql_driver.php @@ -238,13 +238,11 @@ class CI_DB_pdo_pgsql_driver extends CI_DB_pdo_driver { * Generates a platform-specific LIMIT clause * * @param string the sql query string - * @param int the number of rows to limit the query to - * @param int the offset value * @return string */ - protected function _limit($sql, $limit, $offset) + protected function _limit($sql) { - return $sql.' LIMIT '.$limit.($offset ? ' OFFSET '.$offset : ''); + return $sql.' LIMIT '.$this->qb_limit.($this->qb_offset ? ' OFFSET '.$this->qb_offset : ''); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php b/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php index 39cb5f9ef..399d134af 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php @@ -227,7 +227,7 @@ class CI_DB_pdo_sqlsrv_driver extends CI_DB_pdo_driver { { if ($this->qb_limit) { - return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete'; + return 'WITH ci_delete AS (SELECT TOP '.$this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete'; } return parent::_delete($table); @@ -241,24 +241,22 @@ class CI_DB_pdo_sqlsrv_driver extends CI_DB_pdo_driver { * Generates a platform-specific LIMIT clause * * @param string the sql query string - * @param int the number of rows to limit the query to - * @param int the offset value * @return string */ - protected function _limit($sql, $limit, $offset) + protected function _limit($sql) { // As of SQL Server 2012 (11.0.*) OFFSET is supported if (version_compare($this->version(), '11', '>=')) { - return $sql.' OFFSET '.(int) $offset.' ROWS FETCH NEXT '.(int) $limit.' ROWS ONLY'; + return $sql.' OFFSET '.(int) $this->qb_offset.' ROWS FETCH NEXT '.$this->qb_limit.' ROWS ONLY'; } - $limit = $offset + $limit; + $limit = $this->qb_offset + $this->qb_limit; // An ORDER BY clause is required for ROW_NUMBER() to work - if ($offset && ! empty($this->qb_orderby)) + if ($this->qb_offset && ! empty($this->qb_orderby)) { - $orderby = 'ORDER BY '.implode(', ', $this->qb_orderby); + $orderby = trim($this->_compile_order_by()); // We have to strip the ORDER BY clause $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby))); @@ -266,7 +264,7 @@ class CI_DB_pdo_sqlsrv_driver extends CI_DB_pdo_driver { return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n" .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql) ."\n) ".$this->escape_identifiers('CI_subquery') - ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.((int) $offset + 1).' AND '.$limit; + ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit; } return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql); @@ -275,4 +273,4 @@ class CI_DB_pdo_sqlsrv_driver extends CI_DB_pdo_driver { } /* End of file pdo_sqlsrv_driver.php */ -/* Location: ./system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php */ +/* Location: ./system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php */ \ No newline at end of file diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index 1c03f6831..ddcf3f7c3 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -549,13 +549,11 @@ class CI_DB_postgre_driver extends CI_DB { * Generates a platform-specific LIMIT clause * * @param string the sql query string - * @param int the number of rows to limit the query to - * @param int the offset value * @return string */ - protected function _limit($sql, $limit, $offset) + protected function _limit($sql) { - return $sql.' LIMIT '.$limit.($offset ? ' OFFSET '.$offset : ''); + return $sql.' LIMIT '.$this->qb_limit.($this->qb_offset ? ' OFFSET '.$this->qb_offset : ''); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/sqlsrv/sqlsrv_driver.php b/system/database/drivers/sqlsrv/sqlsrv_driver.php index 9ea6facb3..4eb12f910 100644 --- a/system/database/drivers/sqlsrv/sqlsrv_driver.php +++ b/system/database/drivers/sqlsrv/sqlsrv_driver.php @@ -425,7 +425,7 @@ class CI_DB_sqlsrv_driver extends CI_DB { { if ($this->qb_limit) { - return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete'; + return 'WITH ci_delete AS (SELECT TOP '.$this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete'; } return parent::_delete($table); @@ -439,24 +439,22 @@ class CI_DB_sqlsrv_driver extends CI_DB { * Generates a platform-specific LIMIT clause * * @param string the sql query string - * @param int the number of rows to limit the query to - * @param int the offset value * @return string */ - protected function _limit($sql, $limit, $offset) + protected function _limit($sql) { // As of SQL Server 2012 (11.0.*) OFFSET is supported if (version_compare($this->version(), '11', '>=')) { - return $sql.' OFFSET '.(int) $offset.' ROWS FETCH NEXT '.(int) $limit.' ROWS ONLY'; + return $sql.' OFFSET '.(int) $this->qb_offset.' ROWS FETCH NEXT '.$this->qb_limit.' ROWS ONLY'; } - $limit = $offset + $limit; + $limit = $this->qb_offset + $this->qb_limit; // An ORDER BY clause is required for ROW_NUMBER() to work - if ($offset && ! empty($this->qb_orderby)) + if ($this->qb_offset && ! empty($this->qb_orderby)) { - $orderby = 'ORDER BY '.implode(', ', $this->qb_orderby); + $orderby = trim($this->_compile_order_by()); // We have to strip the ORDER BY clause $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby))); @@ -464,7 +462,7 @@ class CI_DB_sqlsrv_driver extends CI_DB { return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n" .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql) ."\n) ".$this->escape_identifiers('CI_subquery') - ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.((int) $offset + 1).' AND '.$limit; + ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit; } return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql); -- cgit v1.2.3-24-g4f1b From 96feb586c7fc2c232675590fe4e1032198a39535 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 19 Jul 2012 13:12:34 +0300 Subject: Implement group_by() compiler and no_escape feature --- system/database/DB_query_builder.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index 55b97bb3f..6c247f957 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -879,21 +879,24 @@ abstract class CI_DB_query_builder extends CI_DB_driver { */ public function group_by($by, $escape = NULL) { + is_bool($escape) OR $escape = $this->_protect_identifiers; + if (is_string($by)) { - $by = explode(',', $by); + $by = ($escape === TRUE) + ? explode(',', $by) + : array($by); } - is_bool($escape) OR $escape = $this->_protect_identifiers; - foreach ($by as $val) { $val = trim($val); if ($val !== '') { - $this->qb_groupby[] = $val = $this->protect_identifiers($val); + $val = array('field' => $val, 'escape' => $escape); + $this->qb_groupby[] = $val; if ($this->qb_caching === TRUE) { $this->qb_cache_groupby[] = $val; @@ -2118,6 +2121,13 @@ abstract class CI_DB_query_builder extends CI_DB_driver { { $sql = "\nGROUP BY "; + for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++) + { + $this->qb_groupby[$i] = ($this->qb_groupby[$i]['escape'] === FALSE) + ? $this->qb_groupby[$i]['field'] + : $this->protect_identifiers($qb_groupby[$i]['field']); + } + $sql .= implode(', ', $this->qb_groupby); } -- cgit v1.2.3-24-g4f1b From 2d486231c0fbc9a5c9ad5bf6897e7bb1aff275ba Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 19 Jul 2012 14:46:51 +0300 Subject: Implement _compile_order_by() --- system/database/DB_driver.php | 4 +- system/database/DB_query_builder.php | 107 ++++++++++++--------- system/database/drivers/mssql/mssql_driver.php | 6 +- .../drivers/pdo/subdrivers/pdo_dblib_driver.php | 6 +- .../drivers/pdo/subdrivers/pdo_sqlsrv_driver.php | 6 +- system/database/drivers/sqlsrv/sqlsrv_driver.php | 6 +- 6 files changed, 78 insertions(+), 57 deletions(-) diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 43ea10023..a327f4ad9 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1128,8 +1128,8 @@ abstract class CI_DB_driver { } return 'UPDATE '.$table.' SET '.implode(', ', $valstr) - .$this->_compile_where() - .(empty($this->qb_orderby) ? '' : ' ORDER BY '.implode(', ', $this->qb_orderby)) + .$this->_compile_wh('qb_where') + .$this->_compile_order_by() .($this->qb_limit ? ' LIMIT '.$this->qb_limit : ''); } diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index 6c247f957..416132e16 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -948,54 +948,50 @@ abstract class CI_DB_query_builder extends CI_DB_driver { * Sets the ORDER BY value * * @param string - * @param string direction: asc or desc + * @param string direction: ASC or DESC * @param bool enable field name escaping * @return object */ public function order_by($orderby, $direction = '', $escape = NULL) { - if (strtolower($direction) === 'random') + $direction = trim($direction); + + if (strtolower($direction) === 'random' OR $orderby === $this->_random_keyword) + { + // Random ordered results don't need a field name + $orderby = $this->_random_keyword; + $direction = ''; + } + elseif (empty($orderby)) { - $orderby = ''; // Random results want or don't need a field name - $direction = $this->_random_keyword; + return $this; } - elseif (trim($direction) !== '') + elseif ($direction !== '') { - $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : ' ASC'; + $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : ''; } is_bool($escape) OR $escape = $this->_protect_identifiers; - if ($escape === TRUE && strpos($orderby, ',') !== FALSE) + if ($escape === FALSE) { - $temp = array(); - foreach (explode(',', $orderby) as $part) - { - $part = trim($part); - if ( ! in_array($part, $this->qb_aliased_tables)) - { - $part = preg_match('/^(.+)\s+(ASC|DESC)$/i', $part, $matches) - ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2] - : $this->protect_identifiers($part); - } - - $temp[] = $part; - } - - $orderby = implode(', ', $temp); + $qb_orderby[] = array(array('field' => $orderby, 'direction' => $direction, $escape => FALSE)); } - elseif ($direction !== $this->_random_keyword && $escape === TRUE) + else { - $orderby = preg_match('/^(.+)\s+(ASC|DESC)$/i', $orderby, $matches) - ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2] - : $this->protect_identifiers($orderby); + $qb_orderby = array(); + foreach (explode(',', $orderby) as $field) + { + $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE)) + ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE) + : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE); + } } - $this->qb_orderby[] = $orderby_statement = $orderby.$direction; - + $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby); if ($this->qb_caching === TRUE) { - $this->qb_cache_orderby[] = $orderby_statement; + $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby); $this->qb_cache_exists[] = 'orderby'; } @@ -2023,20 +2019,10 @@ abstract class CI_DB_query_builder extends CI_DB_driver { $sql .= "\n".implode("\n", $this->qb_join); } - // WHERE - $sql .= $this->_compile_wh('qb_where'); - - // GROUP BY - $sql .= $this->_compile_group_by(); - - // HAVING - $sql .= $this->_compile_wh('qb_having'); - - // ORDER BY - if (count($this->qb_orderby) > 0) - { - $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby); - } + $sql .= $this->_compile_wh('qb_where') + .$this->_compile_group_by() + .$this->_compile_wh('qb_having') + .$this->_compile_order_by(); // ORDER BY // LIMIT if ($this->qb_limit) @@ -2136,6 +2122,41 @@ abstract class CI_DB_query_builder extends CI_DB_driver { // -------------------------------------------------------------------- + /** + * Compile ORDER BY + * + * Escapes identifiers in ORDER BY statements at execution time. + * + * Required so that aliases are tracked properly, regardless of wether + * order_by() is called prior to from(), join() and dbprefix is added + * only if needed. + * + * @return string SQL statement + */ + protected function _compile_order_by() + { + if (count($this->qb_orderby) > 0) + { + $sql = "\nORDER BY "; + + for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++) + { + if ($this->qb_orderby[$i]['escape'] !== FALSE) + { + $this->qb_orderby[$i]['field'] = $this->protect_identifiers($field); + } + + $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction']; + } + + $sql .= implode(', ', $this->qb_orderby); + } + + return ''; + } + + // -------------------------------------------------------------------- + /** * Object to Array * diff --git a/system/database/drivers/mssql/mssql_driver.php b/system/database/drivers/mssql/mssql_driver.php index bfc82a6c2..edc6a8480 100644 --- a/system/database/drivers/mssql/mssql_driver.php +++ b/system/database/drivers/mssql/mssql_driver.php @@ -453,13 +453,13 @@ class CI_DB_mssql_driver extends CI_DB { // however an ORDER BY clause is required for it to work if (version_compare($this->version(), '9', '>=') && $this->qb_offset && ! empty($this->qb_orderby)) { - $orderby = trim($this->_compile_order_by()); + $orderby = $this->_compile_order_by(); // We have to strip the ORDER BY clause - $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby))); + $sql = trim(substr($sql, 0, strrpos($sql, $orderby))); return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n" - .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql) + .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.trim($orderby).') AS '.$this->escape_identifiers('CI_rownum').', ', $sql) ."\n) ".$this->escape_identifiers('CI_subquery') ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit; } diff --git a/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php b/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php index ad699ce23..d6465cda2 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php @@ -222,13 +222,13 @@ class CI_DB_pdo_dblib_driver extends CI_DB_pdo_driver { // however an ORDER BY clause is required for it to work if (version_compare($this->version(), '9', '>=') && $this->qb_offset && ! empty($this->qb_orderby)) { - $orderby = trim($this->_compile_order_by()); + $orderby = $this->_compile_order_by(); // We have to strip the ORDER BY clause - $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby))); + $sql = trim(substr($sql, 0, strrpos($sql, $orderby))); return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n" - .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql) + .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.trim($orderby).') AS '.$this->escape_identifiers('CI_rownum').', ', $sql) ."\n) ".$this->escape_identifiers('CI_subquery') ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit; } diff --git a/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php b/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php index 399d134af..4b5747d90 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php @@ -256,13 +256,13 @@ class CI_DB_pdo_sqlsrv_driver extends CI_DB_pdo_driver { // An ORDER BY clause is required for ROW_NUMBER() to work if ($this->qb_offset && ! empty($this->qb_orderby)) { - $orderby = trim($this->_compile_order_by()); + $orderby = $this->_compile_order_by(); // We have to strip the ORDER BY clause - $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby))); + $sql = trim(substr($sql, 0, strrpos($sql, $orderby))); return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n" - .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql) + .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.trim($orderby).') AS '.$this->escape_identifiers('CI_rownum').', ', $sql) ."\n) ".$this->escape_identifiers('CI_subquery') ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit; } diff --git a/system/database/drivers/sqlsrv/sqlsrv_driver.php b/system/database/drivers/sqlsrv/sqlsrv_driver.php index 4eb12f910..badbb8e90 100644 --- a/system/database/drivers/sqlsrv/sqlsrv_driver.php +++ b/system/database/drivers/sqlsrv/sqlsrv_driver.php @@ -454,13 +454,13 @@ class CI_DB_sqlsrv_driver extends CI_DB { // An ORDER BY clause is required for ROW_NUMBER() to work if ($this->qb_offset && ! empty($this->qb_orderby)) { - $orderby = trim($this->_compile_order_by()); + $orderby = $this->_compile_order_by(); // We have to strip the ORDER BY clause - $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby))); + $sql = trim(substr($sql, 0, strrpos($sql, $orderby))); return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n" - .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql) + .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.trim($orderby).') AS '.$this->escape_identifiers('CI_rownum').', ', $sql) ."\n) ".$this->escape_identifiers('CI_subquery') ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit; } -- cgit v1.2.3-24-g4f1b From 822317b2a8a9872819cd22de6782b44f5c267d2f Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 19 Jul 2012 16:00:32 +0300 Subject: Add back a removed paramter description --- system/database/DB_driver.php | 1 + 1 file changed, 1 insertion(+) diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index a327f4ad9..371b6db96 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1118,6 +1118,7 @@ abstract class CI_DB_driver { * Generates a platform-specific update string from the supplied data * * @param string the table name + * @param array the update data * @return string */ protected function _update($table, $values) -- cgit v1.2.3-24-g4f1b From e8be24b1c4bc3dd6fb78133d15857e2b23972c5b Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 19 Jul 2012 16:11:17 +0300 Subject: Fix CI_DB_driver::_get_operator() --- system/database/DB_driver.php | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 371b6db96..f848cfe4e 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1157,29 +1157,28 @@ abstract class CI_DB_driver { */ protected function _get_operator($str) { - static $_operators = array( - '\s*(?:<|>|!)?=\s*', // =, <=, >=, != - '\s*<>?\s*', // <, <> - '\s*>\s*', // > - '\s+IS NULL', // IS NULL - '\s+IS NOT NULL', // IS NOT NULL - '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value - '\s+IN\s*\([^\)]+\)', // IN(list) - '\s+NOT IN\s*\([^\)]+\)' // NOT IN (list) - ); + static $_operators; - static $_like = array( - '\s+LIKE\s+\S+', // LIKE 'expr' - '\s+NOT LIKE\s+\S+', // NOT LIKE 'expr' - ); - - if ($this->_like_escape_str !== '') + if (empty($_operators)) { - $_like[0] .= preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr))); - $_like[1] .= preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr))); - } + $_les = ($this->_like_escape_str !== '') + ? preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr))) + : ''; - $_operators = array_merge($_operators, $_like); + $_operators = array( + '\s*(?:<|>|!)?=\s*', // =, <=, >=, != + '\s*<>?\s*', // <, <> + '\s*>\s*', // > + '\s+IS NULL', // IS NULL + '\s+IS NOT NULL', // IS NOT NULL + '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value + '\s+IN\s*\([^\)]+\)', // IN(list) + '\s+NOT IN\s*\([^\)]+\)', // NOT IN (list) + '\s+LIKE\s+\S+'.$_les, // LIKE 'expr'[ ESCAPE '%s'] + '\s+NOT LIKE\s+\S+'.$_les // NOT LIKE 'expr'[ ESCAPE '%s'] + ); + + } return preg_match('/'.implode('|', $_operators).'/i', $str, $match) ? $match[0] : FALSE; -- cgit v1.2.3-24-g4f1b From 55b82c95be57a6727d45d6ae9652ff612f96bc37 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 19 Jul 2012 16:20:11 +0300 Subject: Hope this regexp works for travis ... --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2496def0b..0a76229cd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,4 +24,4 @@ script: phpunit --coverage-text --configuration tests/travis/$DB.phpunit.xml branches: only: - develop - - /^feature\/.+$/ \ No newline at end of file + - /^feature.+/ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 9d3aa1bc9f09c226ce0a55c285cb7fe808db5fa7 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 12 Oct 2012 12:14:09 +0300 Subject: Fix _get_operator() for 'LIKE expr ESCAPE' --- system/database/DB_driver.php | 3 +-- system/database/DB_query_builder.php | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index ea56d3819..02c64f9f1 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1167,9 +1167,8 @@ abstract class CI_DB_driver { if (empty($_operators)) { $_les = ($this->_like_escape_str !== '') - ? preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr))) + ? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr))) : ''; - $_operators = array( '\s*(?:<|>|!)?=\s*', // =, <=, >=, != '\s*<>?\s*', // <, <> diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index 139f467e6..9c6cb7e45 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -2050,8 +2050,6 @@ abstract class CI_DB_query_builder extends CI_DB_driver { { if (count($this->$qb_key) > 0) { - $sql = ($qb_key === 'qb_having') ? "\nHAVING " : "\nWHERE "; - for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++) { if ($this->{$qb_key}[$i]['escape'] === FALSE) @@ -2081,7 +2079,8 @@ abstract class CI_DB_query_builder extends CI_DB_driver { .' '.trim($matches[4]).$matches[5].$matches[6]; } - return implode("\n", $this->$qb_key); + return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ") + .implode("\n", $this->$qb_key); } return ''; -- cgit v1.2.3-24-g4f1b From 13f5054a478ee52a9ef262216248337ef40d6677 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 12 Oct 2012 12:31:02 +0300 Subject: Fix delete() with multiple tables and an erroneous variable --- system/database/DB_query_builder.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index 9c6cb7e45..ab04e4db2 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -1832,10 +1832,8 @@ abstract class CI_DB_query_builder extends CI_DB_driver { { foreach ($table as $single_table) { - $this->delete($single_table, $where, $limit, FALSE); + $this->delete($single_table, $where, $limit, $reset_data); } - - $this->_reset_write(); return; } else @@ -2109,7 +2107,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { { $this->qb_groupby[$i] = ($this->qb_groupby[$i]['escape'] === FALSE) ? $this->qb_groupby[$i]['field'] - : $this->protect_identifiers($qb_groupby[$i]['field']); + : $this->protect_identifiers($this->qb_groupby[$i]['field']); } $sql .= implode(', ', $this->qb_groupby); -- cgit v1.2.3-24-g4f1b From 0bcf590db467e4aeb755e79daaccd38c83fe2439 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 12 Oct 2012 13:03:29 +0300 Subject: Fix having(), group_by() --- system/database/DB_query_builder.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index ab04e4db2..ac8ff48a3 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -920,7 +920,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { * @param bool * @return object */ - public function having($key, $value = '', $escape = NULL) + public function having($key, $value = NULL, $escape = NULL) { return $this->_wh('qb_having', $key, $value, 'AND ', $escape); } @@ -937,7 +937,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { * @param bool * @return object */ - public function or_having($key, $value = '', $escape = NULL) + public function or_having($key, $value = NULL, $escape = NULL) { return $this->_wh('qb_having', $key, $value, 'OR ', $escape); } @@ -1812,7 +1812,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { * @param mixed the where clause * @param mixed the limit clause * @param bool - * @return object + * @return mixed */ public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE) { @@ -2101,8 +2101,6 @@ abstract class CI_DB_query_builder extends CI_DB_driver { { if (count($this->qb_groupby) > 0) { - $sql = "\nGROUP BY "; - for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++) { $this->qb_groupby[$i] = ($this->qb_groupby[$i]['escape'] === FALSE) @@ -2110,7 +2108,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { : $this->protect_identifiers($this->qb_groupby[$i]['field']); } - $sql .= implode(', ', $this->qb_groupby); + return "\nGROUP BY ".implode(', ', $this->qb_groupby); } return ''; -- cgit v1.2.3-24-g4f1b From f2ec8b870e29e0bf346e7adf1968b0f7660669b6 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 12 Oct 2012 14:01:13 +0300 Subject: Fix where() with literal multiple conditions --- system/database/DB_query_builder.php | 43 ++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index ac8ff48a3..49592840b 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -2056,25 +2056,36 @@ abstract class CI_DB_query_builder extends CI_DB_driver { continue; } - $op = preg_quote($this->_get_operator($this->{$qb_key}[$i]['condition'])); - if ( ! preg_match('/^(\s*(?:AND|OR)\s+)?(\(?)(.*)('.$op.')(.*(?{$qb_key}[$i]['condition'], $matches)) + // Split multiple conditions + $conditions = preg_split( + '/(\s*AND\s+|\s*OR\s+)/i', + $this->{$qb_key}[$i]['condition'], + -1, + PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY + ); + + for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++) { - $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition']; - continue; + if (($op = $this->_get_operator($conditions[$ci])) === FALSE + OR ! preg_match('/^(\(?)(.*)('.preg_quote($op).')(.*(? '(test <= foo)', /* the whole thing */ + // 1 => '(', /* optional */ + // 2 => 'test', /* the field name */ + // 3 => ' <= ', /* $op */ + // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */ + // 5 => ')' /* optional */ + // ); + empty($matches[4]) OR $matches[4] = ' '.$this->protect_identifiers(trim($matches[4])); + $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2])) + .' '.trim($matches[3]).$matches[4].$matches[5]; } - // $matches = array( - // 0 => 'OR (test <= foo)', /* the whole thing */ - // 1 => 'OR ', /* optional */ - // 2 => '(', /* optional */ - // 3 => 'test', /* the field name */ - // 4 => ' <= ', /* $op */ - // 5 => 'foo', /* optional, if $op is e.g. 'IS NULL' */ - // 6 => ')' /* optional */ - // ); - empty($matches[5]) OR $matches[5] = ' '.$this->protect_identifiers(trim($matches[5])); - $this->{$qb_key}[$i] = $matches[1].$matches[2].$this->protect_identifiers(trim($matches[3])) - .' '.trim($matches[4]).$matches[5].$matches[6]; + $this->{$qb_key}[$i] = implode('', $conditions); } return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ") -- cgit v1.2.3-24-g4f1b From cc02db959db576f256eb62887d326493e44d45af Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 12 Oct 2012 14:30:10 +0300 Subject: Fix where_in() --- system/database/DB_query_builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index 49592840b..54fb50f6a 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -599,7 +599,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { $where_in = array(); foreach ($values as $value) { - $wherein[] = $this->escape($value); + $where_in[] = $this->escape($value); } $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type); -- cgit v1.2.3-24-g4f1b From fc043b3d00a94c473a03cd6927e83e3518e391c0 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 12 Oct 2012 14:46:14 +0300 Subject: Fix order_by() --- system/database/DB_query_builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index 54fb50f6a..936d114bd 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -2148,7 +2148,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { { if ($this->qb_orderby[$i]['escape'] !== FALSE) { - $this->qb_orderby[$i]['field'] = $this->protect_identifiers($field); + $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']); } $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction']; -- cgit v1.2.3-24-g4f1b From a23e10fd2369cc85c4b942c5de6a8cf05a5b2b67 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 12 Oct 2012 14:54:25 +0300 Subject: Really fix order_by() --- system/database/DB_query_builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index 936d114bd..4f89d78d0 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -2154,7 +2154,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction']; } - $sql .= implode(', ', $this->qb_orderby); + return "\nORDER BY ".implode(', ', $this->qb_orderby); } return ''; -- cgit v1.2.3-24-g4f1b From e66d6243aaf13053631641973a0beff656a94510 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 22 Oct 2012 16:39:12 +0300 Subject: Fix issues #1476, #1909 --- system/libraries/Pagination.php | 4 ++-- user_guide_src/source/changelog.rst | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/system/libraries/Pagination.php b/system/libraries/Pagination.php index e1e729bb0..36b57b332 100644 --- a/system/libraries/Pagination.php +++ b/system/libraries/Pagination.php @@ -157,7 +157,7 @@ class CI_Pagination { // See if we are using a prefix or suffix on links if ($this->prefix !== '' OR $this->suffix !== '') { - $this->cur_page = (int) str_replace(array($this->prefix, $this->suffix), '', $CI->uri->segment($this->uri_segment)); + $this->cur_page = (int) str_replace(array($this->prefix, $this->suffix), '', $CI->uri->rsegment($this->uri_segment)); } if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE) @@ -169,7 +169,7 @@ class CI_Pagination { } elseif ( ! $this->cur_page && $CI->uri->segment($this->uri_segment) !== $base_page) { - $this->cur_page = (int) $CI->uri->segment($this->uri_segment); + $this->cur_page = (int) $CI->uri->rsegment($this->uri_segment); } // Set current page to 1 if it's not valid or if using page numbers instead of offset diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 4ee382fce..6d2b0d161 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -369,6 +369,7 @@ Bug fixes for 3.0 - Fixed a bug (#1897) - :doc:`Email Library ` triggered PHP E_WARNING errors when *mail* protocol used and ``to()`` is never called. - Fixed a bug (#1409) - :doc:`Email Library ` didn't properly handle multibyte characters when applying Q-encoding to headers. - Fixed a bug where :doc:`Email Library ` didn't honor it's *wordwrap* setting while handling alternative messages. +- Fixed a bug (#1476, #1909) - :doc:`Pagination Library ` didn't take into account actual routing when determining the current page. Version 2.1.3 ============= -- cgit v1.2.3-24-g4f1b From 3fb026713013b60845c4cfe633a8a59a30b9c7dd Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 22 Oct 2012 16:48:01 +0300 Subject: Add is_https() as a common function --- system/core/Common.php | 18 +++++++++++++++ system/core/Config.php | 2 +- system/core/Security.php | 2 +- user_guide_src/source/changelog.rst | 3 ++- user_guide_src/source/general/common_functions.rst | 26 +++++++++++++--------- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/system/core/Common.php b/system/core/Common.php index 341402c6b..2dd31d3e9 100644 --- a/system/core/Common.php +++ b/system/core/Common.php @@ -330,6 +330,24 @@ if ( ! function_exists('get_mimes')) // ------------------------------------------------------------------------ +if ( ! function_exists('is_https')) +{ + /** + * Is HTTPS? + * + * Determines if the application is accessed via an encrypted + * (HTTPS) connection. + * + * @return bool + */ + function is_https() + { + return ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off'); + } +} + +// ------------------------------------------------------------------------ + if ( ! function_exists('show_error')) { /** diff --git a/system/core/Config.php b/system/core/Config.php index 8e4f998ef..e78128c76 100644 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -75,7 +75,7 @@ class CI_Config { { if (isset($_SERVER['HTTP_HOST'])) { - $base_url = ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') ? 'https' : 'http'; + $base_url = is_https() ? 'https' : 'http'; $base_url .= '://'.$_SERVER['HTTP_HOST'] .str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); } diff --git a/system/core/Security.php b/system/core/Security.php index b22d2cf19..2fbc5b34c 100644 --- a/system/core/Security.php +++ b/system/core/Security.php @@ -198,7 +198,7 @@ class CI_Security { $expire = time() + $this->_csrf_expire; $secure_cookie = (bool) config_item('cookie_secure'); - if ($secure_cookie && (empty($_SERVER['HTTPS']) OR strtolower($_SERVER['HTTPS']) === 'off')) + if ($secure_cookie && ! is_https()) { return FALSE; } diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 6d2b0d161..d3f91de01 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -233,10 +233,11 @@ Release Date: Not Released - Modified ``valid_ip()`` to use PHP's ``filter_var()``. - Added support for arrays and network addresses (e.g. 192.168.1.1/24) for use with the *proxy_ips* setting. - :doc:`Common functions ` changes include: - - Added ``get_mimes()`` function to return the *config/mimes.php* array. + - Added function ``get_mimes()`` to return the *config/mimes.php* array. - Added support for HTTP code 303 ("See Other") in ``set_status_header()``. - Removed redundant conditional to determine HTTP server protocol in ``set_status_header()``. - Changed ``_exception_handler()`` to respect php.ini *display_errors* setting. + - Added function ``is_https()`` to check if a secure connection is used. - Added support for HTTP-Only cookies with new config option *cookie_httponly* (default FALSE). - Renamed method ``_call_hook()`` to ``call_hook()`` in the :doc:`Hooks Library `. - :doc:`Output Library ` changes include: diff --git a/user_guide_src/source/general/common_functions.rst b/user_guide_src/source/general/common_functions.rst index 99126f900..f3d48ac91 100644 --- a/user_guide_src/source/general/common_functions.rst +++ b/user_guide_src/source/general/common_functions.rst @@ -7,7 +7,7 @@ defined, and are available to you at any point. These do not require loading any libraries or helpers. is_php('version_number') -========================== +======================== is_php() determines of the PHP version being used is greater than the supplied version_number. @@ -24,7 +24,7 @@ greater than the supplied version number. Returns FALSE if the installed version of PHP is lower than the supplied version number. is_really_writable('path/to/file') -==================================== +================================== is_writable() returns TRUE on Windows servers when you really can't write to the file as the OS reports to PHP as FALSE only if the @@ -44,7 +44,7 @@ recommended on platforms where this information may be unreliable. } config_item('item_key') -========================= +======================= The :doc:`Config library <../libraries/config>` is the preferred way of accessing configuration information, however config_item() can be used @@ -56,8 +56,8 @@ show_error('message'), show_404('page'), log_message('level', 'message') These are each outlined on the :doc:`Error Handling ` page. -set_status_header(code, 'text'); -================================ +set_status_header(code, 'text') +=============================== Permits you to manually set a server status header. Example:: @@ -68,19 +68,25 @@ Permits you to manually set a server status header. Example:: a full list of headers. remove_invisible_characters($str) -=================================== +================================= This function prevents inserting null characters between ascii characters, like Java\\0script. html_escape($mixed) -==================== +=================== -This function provides short cut for htmlspecialchars() function. It +This function provides short cut for ``htmlspecialchars()`` function. It accepts string and array. To prevent Cross Site Scripting (XSS), it is very useful. get_mimes() -============= +=========== -This function returns the MIMEs array from config/mimes.php. \ No newline at end of file +This function returns the MIMEs array *from config/mimes.php*. + +is_https() +========== + +Returns TRUE if a secure (HTTPS) connection is used and FALSE +in any other case (including non-HTTP requests). \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 811b45a69e873e9bf6b9c6adf210ab9b6ccfb056 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 22 Oct 2012 16:58:33 +0300 Subject: Sync .travis.yml with develop --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1c7e87667..62acf05e5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,4 +29,4 @@ script: phpunit --coverage-text --configuration tests/travis/$DB.phpunit.xml branches: only: - develop - - /^feature.+/ + - /^feature\/.+$/ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From e0874d247de8106c1a4e05ac3fd1645ae6be2045 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 22 Oct 2012 17:03:42 +0300 Subject: Add a changelog entry --- 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 d3f91de01..cd503785c 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -371,6 +371,7 @@ Bug fixes for 3.0 - Fixed a bug (#1409) - :doc:`Email Library ` didn't properly handle multibyte characters when applying Q-encoding to headers. - Fixed a bug where :doc:`Email Library ` didn't honor it's *wordwrap* setting while handling alternative messages. - Fixed a bug (#1476, #1909) - :doc:`Pagination Library ` didn't take into account actual routing when determining the current page. +- Fixed a bug (#1766) - :doc:`Query Builder ` didn't always take into account the *dbprefix* setting. Version 2.1.3 ============= -- cgit v1.2.3-24-g4f1b From 082aa4025ff5764cf10d429903bf48f66a65ce9e Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 22 Oct 2012 19:41:55 +0300 Subject: Fix where() & having() escaping/prefixing literal values containing a period --- system/database/DB_query_builder.php | 44 ++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php index 4f89d78d0..1ab165835 100644 --- a/system/database/DB_query_builder.php +++ b/system/database/DB_query_builder.php @@ -2067,7 +2067,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++) { if (($op = $this->_get_operator($conditions[$ci])) === FALSE - OR ! preg_match('/^(\(?)(.*)('.preg_quote($op).')(.*(? 'foo', /* optional, if $op is e.g. 'IS NULL' */ // 5 => ')' /* optional */ // ); - empty($matches[4]) OR $matches[4] = ' '.$this->protect_identifiers(trim($matches[4])); + + if ( ! empty($matches[4])) + { + $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4])); + $matches[4] = ' '.$matches[4]; + } + $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2])) .' '.trim($matches[3]).$matches[4].$matches[5]; } @@ -2114,7 +2120,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { { for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++) { - $this->qb_groupby[$i] = ($this->qb_groupby[$i]['escape'] === FALSE) + $this->qb_groupby[$i] = ($this->qb_groupby[$i]['escape'] === FALSE OR $this->_is_literal($this->qb_groupby[$i]['field'])) ? $this->qb_groupby[$i]['field'] : $this->protect_identifiers($this->qb_groupby[$i]['field']); } @@ -2146,7 +2152,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver { for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++) { - if ($this->qb_orderby[$i]['escape'] !== FALSE) + if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field'])) { $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']); } @@ -2323,6 +2329,36 @@ abstract class CI_DB_query_builder extends CI_DB_driver { // -------------------------------------------------------------------- + /** + * Is literal + * + * Determines if a string represents a literal value or a field name + * + * @param string + * @return bool + */ + protected function _is_literal($str) + { + $str = trim($str); + + if (empty($str)) + { + return TRUE; + } + + static $_str; + + if (empty($_str)) + { + $_str = ($this->_escape_char !== '"') + ? array('"', "'") : array("'"); + } + + return (ctype_digit($str) OR in_array($str[0], $_str, TRUE)); + } + + // -------------------------------------------------------------------- + /** * Reset Query Builder values. * -- cgit v1.2.3-24-g4f1b From 9f6bdc0b1b9f56997527652a0e1d09a9b233d32e Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 22 Oct 2012 23:31:10 +0300 Subject: Fix #1913 --- system/database/DB_result.php | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/system/database/DB_result.php b/system/database/DB_result.php index d44df6c02..e747044d8 100644 --- a/system/database/DB_result.php +++ b/system/database/DB_result.php @@ -251,27 +251,24 @@ class CI_DB_result { /** * Query result. Acts as a wrapper function for the following functions. * - * @param string + * @param mixed * @param string can be "object" or "array" - * @return mixed either a result object or array + * @return mixed */ public function row($n = 0, $type = 'object') { if ( ! is_numeric($n)) { // We cache the row data for subsequent uses - if ( ! is_array($this->row_data)) - { - $this->row_data = $this->row_array(0); - } + is_array($this->row_data) OR $this->row_data = $this->row_array(0); - // array_key_exists() instead of isset() to allow for MySQL NULL values - if (array_key_exists($n, $this->row_data)) + // array_key_exists() instead of isset() to allow for NULL values + if (empty($this->row_data) OR ! array_key_exists($n, $this->row_data)) { - return $this->row_data[$n]; + return NULL; } - // reset the $n variable if the result was not achieved - $n = 0; + + return $this->row_data[$n]; } if ($type === 'object') return $this->row_object($n); -- cgit v1.2.3-24-g4f1b From 96a4ca6605d6a8a94eea96ed00ab1cf31a8cdd35 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 23 Oct 2012 01:11:48 +0300 Subject: Add a test for issue #273 --- tests/codeigniter/database/query_builder/like_test.php | 16 ++++++++++++++++ tests/mocks/database/schema/skeleton.php | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/codeigniter/database/query_builder/like_test.php b/tests/codeigniter/database/query_builder/like_test.php index 5f3e52228..20ce5d99c 100644 --- a/tests/codeigniter/database/query_builder/like_test.php +++ b/tests/codeigniter/database/query_builder/like_test.php @@ -87,4 +87,20 @@ class Like_test extends CI_TestCase { $this->assertEquals('Musician', $jobs[2]['name']); } + // ------------------------------------------------------------------------ + + /** + * GitHub issue #273 + * + * @see ./mocks/schema/skeleton.php + */ + public function test_like_spaces_and_tabs() + { + $spaces = $this->db->like('key', ' ')->get('misc')->result_array(); + $tabs = $this->db->like('key', "\t")->get('misc')->result_array(); + + $this->assertEquals(1, count($spaces)); + $this->assertEquals(1, count($tabs)); + } + } \ No newline at end of file diff --git a/tests/mocks/database/schema/skeleton.php b/tests/mocks/database/schema/skeleton.php index 18e1ddd4d..fb9aaefee 100644 --- a/tests/mocks/database/schema/skeleton.php +++ b/tests/mocks/database/schema/skeleton.php @@ -129,7 +129,8 @@ class Mock_Database_Schema_Skeleton { ), 'misc' => array( array('id' => 1, 'key' => '\\xxxfoo456', 'value' => 'Entry with \\xxx'), - array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%') + array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%'), + array('id' => 3, 'key' => ' One two three tab') ) ); -- cgit v1.2.3-24-g4f1b From 348bd1e2623ae13ef0dc576464d98eb3673ef15a Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 23 Oct 2012 01:24:11 +0300 Subject: Fix a mock db entry --- tests/codeigniter/database/query_builder/like_test.php | 4 ++-- tests/mocks/database/schema/skeleton.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/codeigniter/database/query_builder/like_test.php b/tests/codeigniter/database/query_builder/like_test.php index 20ce5d99c..2736fbe0b 100644 --- a/tests/codeigniter/database/query_builder/like_test.php +++ b/tests/codeigniter/database/query_builder/like_test.php @@ -96,8 +96,8 @@ class Like_test extends CI_TestCase { */ public function test_like_spaces_and_tabs() { - $spaces = $this->db->like('key', ' ')->get('misc')->result_array(); - $tabs = $this->db->like('key', "\t")->get('misc')->result_array(); + $spaces = $this->db->like('value', ' ')->get('misc')->result_array(); + $tabs = $this->db->like('value', "\t")->get('misc')->result_array(); $this->assertEquals(1, count($spaces)); $this->assertEquals(1, count($tabs)); diff --git a/tests/mocks/database/schema/skeleton.php b/tests/mocks/database/schema/skeleton.php index fb9aaefee..2226835c4 100644 --- a/tests/mocks/database/schema/skeleton.php +++ b/tests/mocks/database/schema/skeleton.php @@ -130,7 +130,7 @@ class Mock_Database_Schema_Skeleton { 'misc' => array( array('id' => 1, 'key' => '\\xxxfoo456', 'value' => 'Entry with \\xxx'), array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%'), - array('id' => 3, 'key' => ' One two three tab') + array('id' => 3, 'key' => 'spaces and tabs', 'value' => ' One two three tab') ) ); -- cgit v1.2.3-24-g4f1b From f5f898f8f30968fb36413a14de2dc6a4599b79a6 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 23 Oct 2012 02:13:29 +0300 Subject: Fix issue #779 --- system/core/URI.php | 7 ++----- tests/codeigniter/core/URI_test.php | 12 ++++++------ user_guide_src/source/changelog.rst | 1 + 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/system/core/URI.php b/system/core/URI.php index 6a8b1a5ac..15e6a5599 100644 --- a/system/core/URI.php +++ b/system/core/URI.php @@ -165,11 +165,8 @@ class CI_URI { */ protected function _set_uri_string($str) { - // Filter out control characters - $str = remove_invisible_characters($str, FALSE); - - // If the URI contains only a slash we'll kill it - $this->uri_string = ($str === '/') ? '' : $str; + // Filter out control characters and trim slashes + $this->uri_string = trim(remove_invisible_characters($str, FALSE), '/'); } // -------------------------------------------------------------------- diff --git a/tests/codeigniter/core/URI_test.php b/tests/codeigniter/core/URI_test.php index 60ed1a4e9..e2deabe51 100644 --- a/tests/codeigniter/core/URI_test.php +++ b/tests/codeigniter/core/URI_test.php @@ -40,13 +40,13 @@ class URI_test extends CI_TestCase { '/index.php?/controller/method/?var=foo' => 'controller/method' ); - foreach($requests as $request => $expected) + foreach ($requests as $request => $expected) { $_SERVER['SCRIPT_NAME'] = '/index.php'; $_SERVER['REQUEST_URI'] = $request; $this->uri->_fetch_uri_string(); - $this->assertEquals($expected, $this->uri->uri_string ); + $this->assertEquals($expected, $this->uri->uri_string); } // Test a subfolder @@ -60,10 +60,10 @@ class URI_test extends CI_TestCase { unset($_SERVER['REQUEST_URI']); // life to path info - $_SERVER['PATH_INFO'] = $a = '/controller/method/'; + $_SERVER['PATH_INFO'] = '/controller/method/'; $this->uri->_fetch_uri_string(); - $this->assertEquals($a, $this->uri->uri_string); + $this->assertEquals('controller/method', $this->uri->uri_string); // death to path info // At this point your server must be seriously drunk @@ -72,7 +72,7 @@ class URI_test extends CI_TestCase { $_SERVER['QUERY_STRING'] = '/controller/method/'; $this->uri->_fetch_uri_string(); - $this->assertEquals($a, $this->uri->uri_string); + $this->assertEquals('controller/method', $this->uri->uri_string); // At this point your server is a labotomy victim unset($_SERVER['QUERY_STRING']); @@ -80,7 +80,7 @@ class URI_test extends CI_TestCase { $_GET['/controller/method/'] = ''; $this->uri->_fetch_uri_string(); - $this->assertEquals($a, $this->uri->uri_string); + $this->assertEquals('controller/method', $this->uri->uri_string); // Test coverage implies that these will work // uri_protocol: REQUEST_URI diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index cd503785c..f0a61d45d 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -372,6 +372,7 @@ Bug fixes for 3.0 - Fixed a bug where :doc:`Email Library ` didn't honor it's *wordwrap* setting while handling alternative messages. - Fixed a bug (#1476, #1909) - :doc:`Pagination Library ` didn't take into account actual routing when determining the current page. - Fixed a bug (#1766) - :doc:`Query Builder ` didn't always take into account the *dbprefix* setting. +- Fixed a bug (#779) - :doc:`URI Class ` didn't always trim slashes from the *uri_string* as shown in the documentation. Version 2.1.3 ============= -- cgit v1.2.3-24-g4f1b