Implemented where_in method of query builder
This commit is contained in:
parent
b550cacdd6
commit
01020853e2
@ -174,8 +174,6 @@ class Query_Builder {
|
|||||||
*/
|
*/
|
||||||
public function like($field, $val, $pos='both')
|
public function like($field, $val, $pos='both')
|
||||||
{
|
{
|
||||||
// @todo Fix like syntax
|
|
||||||
|
|
||||||
$field = $this->db->quote_ident($field);
|
$field = $this->db->quote_ident($field);
|
||||||
|
|
||||||
// Add the like string into the order map
|
// Add the like string into the order map
|
||||||
@ -209,15 +207,13 @@ class Query_Builder {
|
|||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specify condition(s) in the where clause of a query
|
* Do all the repeditive stuff for where type methods
|
||||||
* Note: this function works with key / value, or a
|
|
||||||
* passed array with key / value pairs
|
|
||||||
*
|
*
|
||||||
* @param mixed $key
|
* @param mixed $key
|
||||||
* @param mixed $val
|
* @param mixed $val
|
||||||
* @return $this
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function where($key, $val=array())
|
private function _where($key, $val=array())
|
||||||
{
|
{
|
||||||
$where = array();
|
$where = array();
|
||||||
|
|
||||||
@ -237,22 +233,35 @@ class Query_Builder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $where;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specify condition(s) in the where clause of a query
|
||||||
|
* Note: this function works with key / value, or a
|
||||||
|
* passed array with key / value pairs
|
||||||
|
*
|
||||||
|
* @param mixed $key
|
||||||
|
* @param mixed $val
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function where($key, $val=array())
|
||||||
|
{
|
||||||
|
$where = $this->_where($key, $val);
|
||||||
|
|
||||||
// Create key/value placeholders
|
// Create key/value placeholders
|
||||||
foreach($where as $f => $val)
|
foreach($where as $f => $val)
|
||||||
{
|
{
|
||||||
// Split each key by spaces, incase there
|
// Split each key by spaces, in case there
|
||||||
// is an operator such as >, <, !=, etc.
|
// is an operator such as >, <, !=, etc.
|
||||||
$f_array = explode(' ', trim($f));
|
$f_array = explode(' ', trim($f));
|
||||||
|
|
||||||
// Simple key = val
|
$item = $this->db->quote_ident($f_array[0]);
|
||||||
if (count($f_array) === 1)
|
|
||||||
{
|
// Simple key value, or an operator
|
||||||
$item = $this->db->quote_ident($f_array[0]) . '= ?';
|
$item .= (count($f_array === 1)) ? '= ?' : " {$f_array[1]} ?";
|
||||||
}
|
|
||||||
else // Other operators
|
|
||||||
{
|
|
||||||
$item = $this->db->quote_ident($f_array[0]) . " {$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(
|
||||||
@ -276,23 +285,7 @@ class Query_Builder {
|
|||||||
*/
|
*/
|
||||||
public function or_where($field, $val=array())
|
public function or_where($field, $val=array())
|
||||||
{
|
{
|
||||||
$where = array();
|
$where = $this->_where($field, $val);
|
||||||
|
|
||||||
// Key and value passed? Add them to the where array
|
|
||||||
if (is_scalar($field) && is_scalar($val))
|
|
||||||
{
|
|
||||||
$where[$field] = $val;
|
|
||||||
$this->values[] = $val;
|
|
||||||
}
|
|
||||||
// Array or object, loop through and add to the where array
|
|
||||||
elseif ( ! is_scalar($field))
|
|
||||||
{
|
|
||||||
foreach($key as $k => $v)
|
|
||||||
{
|
|
||||||
$where[$k] = $v;
|
|
||||||
$this->values[] = $v;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create key/value placeholders
|
// Create key/value placeholders
|
||||||
foreach($where as $f => $val)
|
foreach($where as $f => $val)
|
||||||
@ -333,7 +326,22 @@ class Query_Builder {
|
|||||||
*/
|
*/
|
||||||
public function where_in($field, $val=array())
|
public function where_in($field, $val=array())
|
||||||
{
|
{
|
||||||
// @todo Implement Where_in method
|
$field = $this->db->quote_ident($field);
|
||||||
|
$params = array_fill(0, count($val), '?');
|
||||||
|
|
||||||
|
foreach($val as $v)
|
||||||
|
{
|
||||||
|
$this->values[] = $v;
|
||||||
|
}
|
||||||
|
|
||||||
|
$string = $field.' IN ('.implode(',', $params).') ';
|
||||||
|
|
||||||
|
$this->query_map[] = array(
|
||||||
|
'type' => 'where_in',
|
||||||
|
'conjunction' => ( ! empty($this->query_map)) ? ' AND ' : ' WHERE ',
|
||||||
|
'string' => $string
|
||||||
|
);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,35 +30,35 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
$params->host = 'localhost';
|
$params->host = 'localhost';
|
||||||
$params->user = 'sysdba';
|
$params->user = 'sysdba';
|
||||||
$params->pass = 'masterkey';
|
$params->pass = 'masterkey';
|
||||||
$this->qb = new Query_Builder($params);
|
$this->db = new Query_Builder($params);
|
||||||
|
|
||||||
//echo '<hr /> Firebird Queries <hr />';
|
echo '<hr /> Firebird Queries <hr />';
|
||||||
}
|
}
|
||||||
|
|
||||||
function TestGet()
|
function TestGet()
|
||||||
{
|
{
|
||||||
$query = $this->qb->get('create_test ct');
|
$query = $this->db->get('create_test ct');
|
||||||
|
|
||||||
$this->assertTrue(is_resource($query));
|
$this->assertTrue(is_resource($query));
|
||||||
}
|
}
|
||||||
|
|
||||||
function TestGetLimit()
|
function TestGetLimit()
|
||||||
{
|
{
|
||||||
$query = $this->qb->get('create_test', 2);
|
$query = $this->db->get('create_test', 2);
|
||||||
|
|
||||||
$this->assertTrue(is_resource($query));
|
$this->assertTrue(is_resource($query));
|
||||||
}
|
}
|
||||||
|
|
||||||
function TestGetLimitSkip()
|
function TestGetLimitSkip()
|
||||||
{
|
{
|
||||||
$query = $this->qb->get('create_test', 2, 1);
|
$query = $this->db->get('create_test', 2, 1);
|
||||||
|
|
||||||
$this->assertTrue(is_resource($query));
|
$this->assertTrue(is_resource($query));
|
||||||
}
|
}
|
||||||
|
|
||||||
function TestSelectWhereGet()
|
function TestSelectWhereGet()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
->where('id >', 1)
|
->where('id >', 1)
|
||||||
->where('id <', 800)
|
->where('id <', 800)
|
||||||
->get('create_test', 2, 1);
|
->get('create_test', 2, 1);
|
||||||
@ -68,7 +68,7 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
|
|
||||||
function TestSelectWhereGet2()
|
function TestSelectWhereGet2()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
->where(' id ', 1)
|
->where(' id ', 1)
|
||||||
|
|
||||||
->get('create_test', 2, 1);
|
->get('create_test', 2, 1);
|
||||||
@ -78,7 +78,7 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
|
|
||||||
function TestSelectGet()
|
function TestSelectGet()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
->get('create_test', 2, 1);
|
->get('create_test', 2, 1);
|
||||||
|
|
||||||
$this->assertTrue(is_resource($query));
|
$this->assertTrue(is_resource($query));
|
||||||
@ -86,7 +86,7 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
|
|
||||||
function TestSelectFromGet()
|
function TestSelectFromGet()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
->from('create_test ct')
|
->from('create_test ct')
|
||||||
->where('id >', 1)
|
->where('id >', 1)
|
||||||
->get();
|
->get();
|
||||||
@ -96,7 +96,7 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
|
|
||||||
function TestSelectFromLimitGet()
|
function TestSelectFromLimitGet()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
->from('create_test ct')
|
->from('create_test ct')
|
||||||
->where('id >', 1)
|
->where('id >', 1)
|
||||||
->limit(3)
|
->limit(3)
|
||||||
@ -107,7 +107,7 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
|
|
||||||
function TestOrderBy()
|
function TestOrderBy()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
->from('create_test')
|
->from('create_test')
|
||||||
->where('id >', 0)
|
->where('id >', 0)
|
||||||
->where('id <', 9000)
|
->where('id <', 9000)
|
||||||
@ -121,7 +121,7 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
|
|
||||||
function TestOrderByRand()
|
function TestOrderByRand()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
->from('create_test')
|
->from('create_test')
|
||||||
->where('id >', 0)
|
->where('id >', 0)
|
||||||
->where('id <', 9000)
|
->where('id <', 9000)
|
||||||
@ -134,7 +134,7 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
|
|
||||||
function TestOrWhere()
|
function TestOrWhere()
|
||||||
{
|
{
|
||||||
$query = $this->qb->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)
|
||||||
@ -146,7 +146,7 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
|
|
||||||
/*function TestGroupBy()
|
/*function TestGroupBy()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
->from('create_test')
|
->from('create_test')
|
||||||
->where('id >', 0)
|
->where('id >', 0)
|
||||||
->where('id <', 9000)
|
->where('id <', 9000)
|
||||||
@ -162,16 +162,25 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
|
|
||||||
function TestLike()
|
function TestLike()
|
||||||
{
|
{
|
||||||
$query = $this->qb->from('create_test')
|
$query = $this->db->from('create_test')
|
||||||
->like('key', 'og')
|
->like('key', 'og')
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
$this->assertTrue(is_resource($query));
|
$this->assertTrue(is_resource($query));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function TestWhereIn()
|
||||||
|
{
|
||||||
|
$query = $this->db->from('create_test')
|
||||||
|
->where_in('key', array(12, 96, "works"))
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$this->assertTrue(is_resource($query));
|
||||||
|
}
|
||||||
|
|
||||||
function TestInsert()
|
function TestInsert()
|
||||||
{
|
{
|
||||||
$query = $this->qb->set('id', 4)
|
$query = $this->db->set('id', 4)
|
||||||
->set('key', 4)
|
->set('key', 4)
|
||||||
->set('val', 5)
|
->set('val', 5)
|
||||||
->insert('create_test');
|
->insert('create_test');
|
||||||
@ -181,7 +190,7 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
|
|
||||||
function TestUpdate()
|
function TestUpdate()
|
||||||
{
|
{
|
||||||
$query = $this->qb->set('id', 4)
|
$query = $this->db->set('id', 4)
|
||||||
->set('key', 'gogle')
|
->set('key', 'gogle')
|
||||||
->set('val', 'non-word')
|
->set('val', 'non-word')
|
||||||
->where('id', 4)
|
->where('id', 4)
|
||||||
@ -192,7 +201,7 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
|
|
||||||
function TestDelete()
|
function TestDelete()
|
||||||
{
|
{
|
||||||
$query = $this->qb->where('id', 4)->delete('create_test');
|
$query = $this->db->where('id', 4)->delete('create_test');
|
||||||
|
|
||||||
$this->assertTrue($query);
|
$this->assertTrue($query);
|
||||||
}
|
}
|
||||||
|
@ -26,35 +26,35 @@
|
|||||||
$params->type = 'sqlite';
|
$params->type = 'sqlite';
|
||||||
$params->file = $path;
|
$params->file = $path;
|
||||||
$params->host = 'localhost';
|
$params->host = 'localhost';
|
||||||
$this->qb = new Query_Builder($params);
|
$this->db = new Query_Builder($params);
|
||||||
|
|
||||||
//echo '<hr /> SQLite Queries <hr />';
|
echo '<hr /> SQLite Queries <hr />';
|
||||||
}
|
}
|
||||||
|
|
||||||
function TestGet()
|
function TestGet()
|
||||||
{
|
{
|
||||||
$query = $this->qb->get('create_test ct');
|
$query = $this->db->get('create_test ct');
|
||||||
|
|
||||||
$this->assertIsA($query, 'PDOStatement');
|
$this->assertIsA($query, 'PDOStatement');
|
||||||
}
|
}
|
||||||
|
|
||||||
function TestGetLimit()
|
function TestGetLimit()
|
||||||
{
|
{
|
||||||
$query = $this->qb->get('create_test', 2);
|
$query = $this->db->get('create_test', 2);
|
||||||
|
|
||||||
$this->assertIsA($query, 'PDOStatement');
|
$this->assertIsA($query, 'PDOStatement');
|
||||||
}
|
}
|
||||||
|
|
||||||
function TestGetLimitSkip()
|
function TestGetLimitSkip()
|
||||||
{
|
{
|
||||||
$query = $this->qb->get('create_test', 2, 1);
|
$query = $this->db->get('create_test', 2, 1);
|
||||||
|
|
||||||
$this->assertIsA($query, 'PDOStatement');
|
$this->assertIsA($query, 'PDOStatement');
|
||||||
}
|
}
|
||||||
|
|
||||||
function TestSelectWhereGet()
|
function TestSelectWhereGet()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
->where('id >', 1)
|
->where('id >', 1)
|
||||||
->where('id <', 900)
|
->where('id <', 900)
|
||||||
->get('create_test', 2, 1);
|
->get('create_test', 2, 1);
|
||||||
@ -64,7 +64,7 @@
|
|||||||
|
|
||||||
function TestSelectWhereGet2()
|
function TestSelectWhereGet2()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
->where('id !=', 1)
|
->where('id !=', 1)
|
||||||
->get('create_test', 2, 1);
|
->get('create_test', 2, 1);
|
||||||
|
|
||||||
@ -73,7 +73,7 @@
|
|||||||
|
|
||||||
function TestSelectGet()
|
function TestSelectGet()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
->get('create_test', 2, 1);
|
->get('create_test', 2, 1);
|
||||||
|
|
||||||
$this->assertIsA($query, 'PDOStatement');
|
$this->assertIsA($query, 'PDOStatement');
|
||||||
@ -81,7 +81,7 @@
|
|||||||
|
|
||||||
function TestSelectFromGet()
|
function TestSelectFromGet()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
->from('create_test ct')
|
->from('create_test ct')
|
||||||
->where('id >', 1)
|
->where('id >', 1)
|
||||||
->get();
|
->get();
|
||||||
@ -91,7 +91,7 @@
|
|||||||
|
|
||||||
function TestSelectFromLimitGet()
|
function TestSelectFromLimitGet()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
->from('create_test ct')
|
->from('create_test ct')
|
||||||
->where('id >', 1)
|
->where('id >', 1)
|
||||||
->limit(3)
|
->limit(3)
|
||||||
@ -102,7 +102,7 @@
|
|||||||
|
|
||||||
function TestOrderBy()
|
function TestOrderBy()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
->from('create_test')
|
->from('create_test')
|
||||||
->where('id >', 0)
|
->where('id >', 0)
|
||||||
->where('id <', 9000)
|
->where('id <', 9000)
|
||||||
@ -116,7 +116,7 @@
|
|||||||
|
|
||||||
function TestOrderByRandom()
|
function TestOrderByRandom()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
->from('create_test')
|
->from('create_test')
|
||||||
->where('id >', 0)
|
->where('id >', 0)
|
||||||
->where('id <', 9000)
|
->where('id <', 9000)
|
||||||
@ -129,7 +129,7 @@
|
|||||||
|
|
||||||
function TestGroupBy()
|
function TestGroupBy()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
->from('create_test')
|
->from('create_test')
|
||||||
->where('id >', 0)
|
->where('id >', 0)
|
||||||
->where('id <', 9000)
|
->where('id <', 9000)
|
||||||
@ -145,7 +145,7 @@
|
|||||||
|
|
||||||
function TestOrWhere()
|
function TestOrWhere()
|
||||||
{
|
{
|
||||||
$query = $this->qb->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)
|
||||||
@ -157,7 +157,7 @@
|
|||||||
|
|
||||||
function TestLike()
|
function TestLike()
|
||||||
{
|
{
|
||||||
$query = $this->qb->from('create_test')
|
$query = $this->db->from('create_test')
|
||||||
->like('key', 'og')
|
->like('key', 'og')
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
@ -166,7 +166,7 @@
|
|||||||
|
|
||||||
function TestInsert()
|
function TestInsert()
|
||||||
{
|
{
|
||||||
$query = $this->qb->set('id', 4)
|
$query = $this->db->set('id', 4)
|
||||||
->set('key', 4)
|
->set('key', 4)
|
||||||
->set('val', 5)
|
->set('val', 5)
|
||||||
->insert('create_test');
|
->insert('create_test');
|
||||||
@ -176,7 +176,7 @@
|
|||||||
|
|
||||||
function TestUpdate()
|
function TestUpdate()
|
||||||
{
|
{
|
||||||
$query = $this->qb->set('id', 4)
|
$query = $this->db->set('id', 4)
|
||||||
->set('key', 'gogle')
|
->set('key', 'gogle')
|
||||||
->set('val', 'non-word')
|
->set('val', 'non-word')
|
||||||
->where('id', 4)
|
->where('id', 4)
|
||||||
@ -187,7 +187,7 @@
|
|||||||
|
|
||||||
function TestDelete()
|
function TestDelete()
|
||||||
{
|
{
|
||||||
$query = $this->qb->where('id', 4)->delete('create_test');
|
$query = $this->db->where('id', 4)->delete('create_test');
|
||||||
|
|
||||||
$this->assertIsA($query, 'PDOStatement');
|
$this->assertIsA($query, 'PDOStatement');
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user