Fix some issues with multiple array items in where statements

This commit is contained in:
Timothy Warren 2014-11-07 12:14:46 -05:00
parent e0bc5ce903
commit 275cb30ee1
6 changed files with 154 additions and 77 deletions

2
.gitignore vendored
View File

@ -8,3 +8,5 @@ coverage/*
vendor/*
composer.lock
docs/phpdoc*
.project
all_tests

View File

@ -361,7 +361,7 @@ abstract class Abstract_Query_Builder {
* @param string $conj
* @return Query_Builder
*/
protected function _where_string($key, $val=array(), $conj='AND')
protected function _where_string($key, $val=array(), $defaultConj='AND')
{
// Create key/value placeholders
foreach($this->_where($key, $val) as $f => $val)
@ -374,13 +374,11 @@ abstract class Abstract_Query_Builder {
// Simple key value, or an operator
$item .= (count($f_array) === 1) ? '=?' : " {$f_array[1]} ?";
// Get the type of the first item in the query map
$first_item = current($this->query_map);
$last_item = end($this->query_map);
// Determine the correct conjunction
if (empty($this->query_map) || stripos($first_item['conjunction'], 'JOIN') !== FALSE)
$conjunctionList = array_pluck($this->query_map, 'conjunction');
if (empty($this->query_map) || ( ! regex_in_array($conjunctionList, "/^ ?\n?WHERE/i")))
{
$conj = "\nWHERE ";
}
@ -390,7 +388,7 @@ abstract class Abstract_Query_Builder {
}
else
{
$conj = " {$conj} ";
$conj = " {$defaultConj} ";
}
$this->_append_map($conj, $item, 'where');

View File

@ -164,13 +164,13 @@ class Query_Builder extends Abstract_Query_Builder implements Query_Builder_Inte
/**
* Alias to driver util class
* @var \Query\Driver\Abstract_Util
* @var \Query\Driver\Util\Abstract_Util
*/
public $util;
/**
* Alias to driver sql class
* @var \Query\Driver\SQL_Interface
* @var \Query\Driver\SQL\SQL_Interface
*/
public $sql;

View File

@ -123,6 +123,59 @@ function array_zipper(Array $zipper_input)
// --------------------------------------------------------------------------
/**
* Get an array out of an multi-dimensional array based on a common
* key
*
* @param array $array
* @param string $key
* @return array
*/
function array_pluck(Array $array, $key)
{
$output = array();
// No point iterating over an empty array
if (empty($array)) return $array;
foreach($array as $inner_array)
{
if (array_key_exists($key, $inner_array))
{
$output[] = $inner_array[$key];
}
}
return $output;
}
// --------------------------------------------------------------------------
/**
* Determine whether a value in the passed array matches the pattern
* passed
*
* @param array $array
* @param string $pattern
* @return bool
*/
function regex_in_array(Array $array, $pattern)
{
if (empty($array)) return FALSE;
foreach($array as $item)
{
if (is_scalar($item))
{
if (preg_match($pattern, $item)) return TRUE;
}
}
return FALSE;
}
// --------------------------------------------------------------------------
/**
* Connection function
*

View File

@ -11,8 +11,6 @@ test:
php_unit:
config: 'tests/phpunit.xml'
run_from: 'tests'
#args: ' --debug '
coverage: '../coverage/'
php_docblock_checker:
allowed_warnings: 0
skip_classes: true

View File

@ -167,6 +167,71 @@ abstract class QBTest extends Query_TestCase {
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
public function testSelectAvg()
{
$query = $this->db->select_avg('id', 'di')
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
public function testSelectSum()
{
$query = $this->db->select_sum('id', 'di')
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
public function testSelectDistinct()
{
$query = $this->db->select_sum('id', 'di')
->distinct()
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
public function testSelectGet()
{
$query = $this->db->select('id, key as k, val')
->get('test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
public function testSelectFromGet()
{
$query = $this->db->select('id, key as k, val')
->from('test ct')
->where('id >', 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
public function testSelectFromLimitGet()
{
$query = $this->db->select('id, key as k, val')
->from('test ct')
->where('id >', 1)
->limit(3)
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
@ -335,72 +400,6 @@ abstract class QBTest extends Query_TestCase {
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
public function testSelectAvg()
{
$query = $this->db->select_avg('id', 'di')
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
public function testSelectSum()
{
$query = $this->db->select_sum('id', 'di')
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
public function testSelectDistinct()
{
$query = $this->db->select_sum('id', 'di')
->distinct()
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
public function testSelectGet()
{
$query = $this->db->select('id, key as k, val')
->get('test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
public function testSelectFromGet()
{
$query = $this->db->select('id, key as k, val')
->from('test ct')
->where('id >', 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
public function testSelectFromLimitGet()
{
$query = $this->db->select('id, key as k, val')
->from('test ct')
->where('id >', 1)
->limit(3)
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
// ! Query modifier tests
// --------------------------------------------------------------------------
@ -572,6 +571,21 @@ abstract class QBTest extends Query_TestCase {
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
public function testJoinWithMultipleWhereValues()
{
$query = $this->db->from('test ct')
->join('join cj', 'cj.id=ct.id', 'inner')
->where(array(
'ct.id < ' => 3,
'ct.key' => 'foo'
))
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
// ! DB update tests
// --------------------------------------------------------------------------
@ -680,6 +694,18 @@ abstract class QBTest extends Query_TestCase {
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
public function testDeleteWithMultipleWhereValues()
{
$query = $this->db->delete('test', array(
'id' => 5,
'key' => 'gogle'
));
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
// ! Non-data read queries
// --------------------------------------------------------------------------