Fixed issue with multiple fields in order_by, removed extranious spaces in SQL generation

This commit is contained in:
Timothy Warren 2012-08-23 01:17:27 +00:00
parent 90c6760196
commit a4f730b081
5 changed files with 124 additions and 92 deletions

View File

@ -42,6 +42,20 @@ if ( ! function_exists('do_include'))
} }
} }
if ( ! function_exists('mb_trim'))
{
/**
* Multibyte-safe trim function
*
* @param string
* @return string
*/
function mb_trim($string)
{
return preg_replace("/(^\s+)|(\s+$)/us", "", $string);
}
}
/** /**
* Load a Query class * Load a Query class
* *

View File

@ -211,14 +211,19 @@ abstract class DB_PDO extends PDO {
return array_map(array($this, 'quote_ident'), $ident); return array_map(array($this, 'quote_ident'), $ident);
} }
// If the string is already quoted, return the string // Handle comma-separated identifiers
if (($pos = strpos($ident, $this->escape_char)) !== FALSE && $pos === 0) if (strpos($ident, ',') !== FALSE)
{ {
return $ident; $parts = explode(',', $ident);
$parts = array_map('mb_trim', $parts);
$parts = array_map(array($this, 'quote_ident'), $parts);
$ident = implode(',', $parts);
} }
// Split each identifier by the period // Split each identifier by the period
$hiers = explode('.', $ident); $hiers = explode('.', $ident);
$hiers = array_map('mb_trim', $hiers);
// Return the re-compiled string // Return the re-compiled string
return implode('.', array_map(array($this, '_quote'), $hiers)); return implode('.', array_map(array($this, '_quote'), $hiers));
@ -234,11 +239,18 @@ abstract class DB_PDO extends PDO {
*/ */
protected function _quote($str) protected function _quote($str)
{ {
// Don't quote numbers
if ( ! is_string($str) && is_numeric($str)) if ( ! is_string($str) && is_numeric($str))
{ {
return $str; return $str;
} }
// Don't add additional quotes
if (strpos($str, $this->escape_char) === 0 || strrpos($str, $this->escape_char) === 0)
{
return $str;
}
return "{$this->escape_char}{$str}{$this->escape_char}"; return "{$this->escape_char}{$str}{$this->escape_char}";
} }

View File

@ -161,13 +161,6 @@ class Query_Builder {
*/ */
private $having_map; private $having_map;
/**
* Query parser to safely escape conditions
*
* @var object
*/
private $parser;
/** /**
* Convenience property for connection management * Convenience property for connection management
* *
@ -317,7 +310,7 @@ class Query_Builder {
} }
} }
$this->select_string .= implode(', ', $safe_array); $this->select_string .= implode(',', $safe_array);
unset($safe_array); unset($safe_array);
@ -676,7 +669,7 @@ class Query_Builder {
$item = $this->quote_ident($f_array[0]); $item = $this->quote_ident($f_array[0]);
// Simple key value, or an operator // Simple key value, or an operator
$item .= (count($f_array) === 1) ? '= ?' : " {$f_array[1]} ?"; $item .= (count($f_array) === 1) ? '=?' : " {$f_array[1]} ?";
// Put in the query map for select statements // Put in the query map for select statements
$this->query_map[] = array( $this->query_map[] = array(
@ -841,7 +834,7 @@ class Query_Builder {
$this->set_array_keys = array_map(array($this->db, 'quote_ident'), $this->set_array_keys); $this->set_array_keys = array_map(array($this->db, 'quote_ident'), $this->set_array_keys);
// Generate the "set" string // Generate the "set" string
$this->set_string = implode('=?, ', $this->set_array_keys); $this->set_string = implode('=?,', $this->set_array_keys);
$this->set_string .= '=?'; $this->set_string .= '=?';
return $this; return $this;
@ -859,7 +852,7 @@ class Query_Builder {
*/ */
public function join($table, $condition, $type='') public function join($table, $condition, $type='')
{ {
$table = implode(" ", array_map(array($this->db, 'quote_ident'), explode(' ', trim($table)))); $table = implode(' ', array_map(array($this->db, 'quote_ident'), explode(' ', trim($table))));
$parser = new query_parser(); $parser = new query_parser();
@ -876,7 +869,7 @@ class Query_Builder {
} }
} }
$parsed_condition = implode(' ', $parts['combined']); $parsed_condition = implode('', $parts['combined']);
$condition = $table . ' ON ' . $parsed_condition; $condition = $table . ' ON ' . $parsed_condition;
@ -908,7 +901,7 @@ class Query_Builder {
$this->group_array[] = $this->quote_ident($field); $this->group_array[] = $this->quote_ident($field);
} }
$this->group_string = ' GROUP BY ' . implode(', ', $this->group_array); $this->group_string = ' GROUP BY ' . implode(',', $this->group_array);
return $this; return $this;
} }
@ -1371,8 +1364,8 @@ class Query_Builder {
$param_count = count($this->set_array_keys); $param_count = count($this->set_array_keys);
$params = array_fill(0, $param_count, '?'); $params = array_fill(0, $param_count, '?');
$sql = "INSERT INTO {$table} (" $sql = "INSERT INTO {$table} ("
. implode(', ', $this->set_array_keys) . . implode(',', $this->set_array_keys) .
') VALUES ('.implode(', ', $params).')'; ') VALUES ('.implode(',', $params).')';
break; break;
case "update": case "update":

View File

@ -160,6 +160,19 @@ abstract class QBTest extends UnitTestCase {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestMultiOrderBy()
{
if (empty($this->db)) return;
$query = $this->db->from('create_test')
->order_by('id, key')
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
public function TestSelectAvg() public function TestSelectAvg()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;

Binary file not shown.