Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX - Ignores SQL functions on the SELECT statement. #1612

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion system/database/DB_query_builder.php
Expand Up @@ -75,7 +75,9 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
protected $qb_cache_set = array();

protected $qb_no_escape = array();
protected $qb_cache_no_escape = array();
protected $qb_cache_no_escape = array();

protected $qb_function = array();

/**
* Select
Expand All @@ -88,6 +90,22 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
*/
public function select($select = '*', $escape = NULL)
{
// is there a function? For ex. date_format('<date>', '<format>')
$pattern = '/[\w_]+\s*(\(.*?\))[\s\w]*/';

do
{
preg_match($pattern, $select, $matches);

if (count($matches) > 0)
{
$this->qb_function[] = $matches[0];

$select = str_replace($matches[0], '[function]', $select);
}

} while(count($matches) > 0);

if (is_string($select))
{
$select = explode(',', $select);
Expand Down Expand Up @@ -2048,6 +2066,13 @@ protected function _compile_select($select_override = FALSE)
foreach ($this->qb_select as $key => $val)
{
$no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;

if ($this->qb_select[$key] == '[function]')
{
$this->qb_select[$key] = array_shift($this->qb_function);
continue;
}

$this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
}

Expand Down