summaryrefslogtreecommitdiffstats
path: root/system/database/DB_query_builder.php
diff options
context:
space:
mode:
Diffstat (limited to 'system/database/DB_query_builder.php')
-rw-r--r--system/database/DB_query_builder.php49
1 files changed, 36 insertions, 13 deletions
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;
@@ -2104,6 +2102,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
*
* Takes an object as input and converts the class variables to array key/vals