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

@ -21,7 +21,7 @@ abstract class QBTest extends UnitTestCase {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Get Tests // ! Get Tests
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestGet() public function TestGet()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;
@ -30,7 +30,7 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestGetLimit() public function TestGetLimit()
@ -41,7 +41,7 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestGetLimitSkip() public function TestGetLimitSkip()
@ -52,59 +52,59 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestGetWhere() public function TestGetWhere()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;
$query = $this->db->get_where('create_test', array('id !=' => 1), 2, 1); $query = $this->db->get_where('create_test', array('id !=' => 1), 2, 1);
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestHaving() public function TestHaving()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;
$query = $this->db->select('id') $query = $this->db->select('id')
->from('create_test') ->from('create_test')
->group_by('id') ->group_by('id')
->having(array('id >' => 1)) ->having(array('id >' => 1))
->having('id !=', 3) ->having('id !=', 3)
->get(); ->get();
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestOrHaving() public function TestOrHaving()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;
$query = $this->db->select('id') $query = $this->db->select('id')
->from('create_test') ->from('create_test')
->group_by('id') ->group_by('id')
->having(array('id >' => 1)) ->having(array('id >' => 1))
->or_having('id !=', 3) ->or_having('id !=', 3)
->get(); ->get();
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestGetViews() public function TestGetViews()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;
$this->assertTrue(is_array($this->db->get_views())); $this->assertTrue(is_array($this->db->get_views()));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Select Tests // ! Select Tests
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -120,7 +120,7 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestSelectWhereGet2() public function TestSelectWhereGet2()
@ -133,68 +133,81 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestSelectMax() public function TestSelectMax()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;
$query = $this->db->select_max('id', 'di') $query = $this->db->select_max('id', 'di')
->get('create_test'); ->get('create_test');
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestSelectMin() public function TestSelectMin()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;
$query = $this->db->select_min('id', 'di') $query = $this->db->select_min('id', 'di')
->get('create_test'); ->get('create_test');
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
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;
$query = $this->db->select_avg('id', 'di') $query = $this->db->select_avg('id', 'di')
->get('create_test'); ->get('create_test');
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestSelectSum() public function TestSelectSum()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;
$query = $this->db->select_sum('id', 'di') $query = $this->db->select_sum('id', 'di')
->get('create_test'); ->get('create_test');
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestSelectDistinct() public function TestSelectDistinct()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;
$query = $this->db->select_sum('id', 'di') $query = $this->db->select_sum('id', 'di')
->distinct() ->distinct()
->get('create_test'); ->get('create_test');
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestSelectGet() public function TestSelectGet()
@ -206,7 +219,7 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestSelectFromGet() public function TestSelectFromGet()
@ -220,7 +233,7 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestSelectFromLimitGet() public function TestSelectFromLimitGet()
@ -235,7 +248,7 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Query modifier tests // ! Query modifier tests
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -255,7 +268,7 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestOrderByRandom() public function TestOrderByRandom()
@ -272,7 +285,7 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestGroupBy() public function TestGroupBy()
@ -293,7 +306,7 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestOrWhere() public function TestOrWhere()
@ -309,7 +322,7 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestLike() public function TestLike()
@ -322,7 +335,7 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestJoin() public function TestJoin()
@ -335,7 +348,7 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! DB update tests // ! DB update tests
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -351,7 +364,7 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestUpdate() public function TestUpdate()
@ -366,7 +379,7 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestDelete() public function TestDelete()
@ -377,58 +390,58 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Non-data read queries // ! Non-data read queries
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestCountAll() public function TestCountAll()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;
$query = $this->db->count_all('create_test'); $query = $this->db->count_all('create_test');
$this->assertTrue(is_numeric($query)); $this->assertTrue(is_numeric($query));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestCountAllResults() public function TestCountAllResults()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;
$query = $this->db->count_all_results('create_test'); $query = $this->db->count_all_results('create_test');
$this->assertTrue(is_numeric($query)); $this->assertTrue(is_numeric($query));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestCountAllResults2() public function TestCountAllResults2()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;
$query = $this->db->select('id, key as k, val') $query = $this->db->select('id, key as k, val')
->from('create_test') ->from('create_test')
->where(' id ', 1) ->where(' id ', 1)
->or_where('key >', 0) ->or_where('key >', 0)
->limit(2, 1) ->limit(2, 1)
->count_all_results(); ->count_all_results();
$this->assertTrue(is_numeric($query)); $this->assertTrue(is_numeric($query));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestNumRows() public function TestNumRows()
{ {
$query = $this->db->get('create_test'); $query = $this->db->get('create_test');
$this->assertTrue(is_numeric($this->db->num_rows())); $this->assertTrue(is_numeric($this->db->num_rows()));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Error Tests // ! Error Tests
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Handles invalid drivers * Handles invalid drivers
*/ */
@ -442,14 +455,14 @@ abstract class QBTest extends UnitTestCase {
'pass' => NULL, 'pass' => NULL,
'type' => 'QGYFHGEG' 'type' => 'QGYFHGEG'
); );
$this->expectException('BadDBDriverException'); $this->expectException('BadDBDriverException');
$this->db = new Query_Builder($params); $this->db = new Query_Builder($params);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestBadConnection() public function TestBadConnection()
{ {
$params = array( $params = array(
@ -460,11 +473,11 @@ abstract class QBTest extends UnitTestCase {
'pass' => NULL, 'pass' => NULL,
'type' => 'pgsql' 'type' => 'pgsql'
); );
$this->expectException('BadConnectionException'); $this->expectException('BadConnectionException');
$this->db = new Query_Builder($params); $this->db = new Query_Builder($params);
} }
} }

Binary file not shown.