public
Last active

Idea to help solve CodeIgniter Query Builder issue with SELECT strings being exploded at commas.

  • Download Gist
DB_query_builder.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/**
* Select
*
* Generates the SELECT portion of the query
*
* @param string
* @param mixed
* @return CI_DB_query_builder
*/
public function select($select = '*', $escape = NULL)
{
if (is_string($select))
{
// Find all parentheses sets.
preg_match_all('/\((.*)\)/', $select, $func_groups);
 
// Inside each parentheses set, replace all commas with
// a temporary value, which will be reverted back later.
// We do this to avoid issues with using explode() later.
foreach ($func_groups as $func_group)
{
$func_replace = str_replace(',', '{comma}', $func_group);
$select = str_replace($func_group, $func_replace, $select);
}
 
$select = explode(',', $select);
}
 
// If the escape value was not set will will base it on the global setting
is_bool($escape) OR $escape = $this->_protect_identifiers;
 
foreach ($select as $val)
{
// Put commas back in where they were temporarily replaced.
$val = str_replace('{comma}', ',', trim($val));
 
if ($val !== '')
{
$this->qb_select[] = $val;
$this->qb_no_escape[] = $escape;
 
if ($this->qb_caching === TRUE)
{
$this->qb_cache_select[] = $val;
$this->qb_cache_exists[] = 'select';
$this->qb_cache_no_escape[] = $escape;
}
}
}
 
return $this;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.