diff --git a/sys/db/query_builder.php b/sys/db/query_builder.php
index 314e803..deda3da 100644
--- a/sys/db/query_builder.php
+++ b/sys/db/query_builder.php
@@ -173,9 +173,7 @@ class Query_Builder {
* @return $this
*/
public function like($field, $val, $pos='both')
- {
- // @todo Fix like syntax
-
+ {
$field = $this->db->quote_ident($field);
// 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
- * Note: this function works with key / value, or a
- * passed array with key / value pairs
- *
- * @param mixed $key
+ * Do all the repeditive stuff for where type methods
+ *
+ * @param mixed $key
* @param mixed $val
- * @return $this
+ * @return array
*/
- public function where($key, $val=array())
+ private function _where($key, $val=array())
{
$where = array();
@@ -236,23 +232,36 @@ class Query_Builder {
$this->values[] = $v;
}
}
+
+ 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
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.
$f_array = explode(' ', trim($f));
-
- // Simple key = val
- if (count($f_array) === 1)
- {
- $item = $this->db->quote_ident($f_array[0]) . '= ?';
- }
- else // Other operators
- {
- $item = $this->db->quote_ident($f_array[0]) . " {$f_array[1]} ?";
- }
+
+ $item = $this->db->quote_ident($f_array[0]);
+
+ // Simple key value, or an operator
+ $item .= (count($f_array === 1)) ? '= ?' : " {$f_array[1]} ?";
// Put in the query map for select statements
$this->query_map[] = array(
@@ -276,23 +285,7 @@ class Query_Builder {
*/
public function or_where($field, $val=array())
{
- $where = array();
-
- // 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;
- }
- }
+ $where = $this->_where($field, $val);
// Create key/value placeholders
foreach($where as $f => $val)
@@ -333,7 +326,22 @@ class Query_Builder {
*/
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;
}
diff --git a/tests/databases/firebird-qb.php b/tests/databases/firebird-qb.php
index 87c8d01..28aa318 100644
--- a/tests/databases/firebird-qb.php
+++ b/tests/databases/firebird-qb.php
@@ -30,35 +30,35 @@ class FirebirdQBTest extends UnitTestCase {
$params->host = 'localhost';
$params->user = 'sysdba';
$params->pass = 'masterkey';
- $this->qb = new Query_Builder($params);
+ $this->db = new Query_Builder($params);
- //echo '
Firebird Queries
';
+ echo '
Firebird Queries
';
}
function TestGet()
{
- $query = $this->qb->get('create_test ct');
+ $query = $this->db->get('create_test ct');
$this->assertTrue(is_resource($query));
}
function TestGetLimit()
{
- $query = $this->qb->get('create_test', 2);
+ $query = $this->db->get('create_test', 2);
$this->assertTrue(is_resource($query));
}
function TestGetLimitSkip()
{
- $query = $this->qb->get('create_test', 2, 1);
+ $query = $this->db->get('create_test', 2, 1);
$this->assertTrue(is_resource($query));
}
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 <', 800)
->get('create_test', 2, 1);
@@ -68,7 +68,7 @@ class FirebirdQBTest extends UnitTestCase {
function TestSelectWhereGet2()
{
- $query = $this->qb->select('id, key as k, val')
+ $query = $this->db->select('id, key as k, val')
->where(' id ', 1)
->get('create_test', 2, 1);
@@ -78,7 +78,7 @@ class FirebirdQBTest extends UnitTestCase {
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);
$this->assertTrue(is_resource($query));
@@ -86,7 +86,7 @@ class FirebirdQBTest extends UnitTestCase {
function TestSelectFromGet()
{
- $query = $this->qb->select('id, key as k, val')
+ $query = $this->db->select('id, key as k, val')
->from('create_test ct')
->where('id >', 1)
->get();
@@ -96,7 +96,7 @@ class FirebirdQBTest extends UnitTestCase {
function TestSelectFromLimitGet()
{
- $query = $this->qb->select('id, key as k, val')
+ $query = $this->db->select('id, key as k, val')
->from('create_test ct')
->where('id >', 1)
->limit(3)
@@ -107,7 +107,7 @@ class FirebirdQBTest extends UnitTestCase {
function TestOrderBy()
{
- $query = $this->qb->select('id, key as k, val')
+ $query = $this->db->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
@@ -121,7 +121,7 @@ class FirebirdQBTest extends UnitTestCase {
function TestOrderByRand()
{
- $query = $this->qb->select('id, key as k, val')
+ $query = $this->db->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
@@ -134,7 +134,7 @@ class FirebirdQBTest extends UnitTestCase {
function TestOrWhere()
{
- $query = $this->qb->select('id, key as k, val')
+ $query = $this->db->select('id, key as k, val')
->from('create_test')
->where(' id ', 1)
->or_where('key >', 0)
@@ -146,7 +146,7 @@ class FirebirdQBTest extends UnitTestCase {
/*function TestGroupBy()
{
- $query = $this->qb->select('id, key as k, val')
+ $query = $this->db->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
@@ -162,16 +162,25 @@ class FirebirdQBTest extends UnitTestCase {
function TestLike()
{
- $query = $this->qb->from('create_test')
+ $query = $this->db->from('create_test')
->like('key', 'og')
->get();
$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()
{
- $query = $this->qb->set('id', 4)
+ $query = $this->db->set('id', 4)
->set('key', 4)
->set('val', 5)
->insert('create_test');
@@ -181,7 +190,7 @@ class FirebirdQBTest extends UnitTestCase {
function TestUpdate()
{
- $query = $this->qb->set('id', 4)
+ $query = $this->db->set('id', 4)
->set('key', 'gogle')
->set('val', 'non-word')
->where('id', 4)
@@ -192,7 +201,7 @@ class FirebirdQBTest extends UnitTestCase {
function TestDelete()
{
- $query = $this->qb->where('id', 4)->delete('create_test');
+ $query = $this->db->where('id', 4)->delete('create_test');
$this->assertTrue($query);
}
diff --git a/tests/databases/sqlite-qb.php b/tests/databases/sqlite-qb.php
index 643aedc..4f148c6 100644
--- a/tests/databases/sqlite-qb.php
+++ b/tests/databases/sqlite-qb.php
@@ -26,35 +26,35 @@
$params->type = 'sqlite';
$params->file = $path;
$params->host = 'localhost';
- $this->qb = new Query_Builder($params);
+ $this->db = new Query_Builder($params);
- //echo '
SQLite Queries
';
+ echo '
SQLite Queries
';
}
function TestGet()
{
- $query = $this->qb->get('create_test ct');
+ $query = $this->db->get('create_test ct');
$this->assertIsA($query, 'PDOStatement');
}
function TestGetLimit()
{
- $query = $this->qb->get('create_test', 2);
+ $query = $this->db->get('create_test', 2);
$this->assertIsA($query, 'PDOStatement');
}
function TestGetLimitSkip()
{
- $query = $this->qb->get('create_test', 2, 1);
+ $query = $this->db->get('create_test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
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 <', 900)
->get('create_test', 2, 1);
@@ -64,7 +64,7 @@
function TestSelectWhereGet2()
{
- $query = $this->qb->select('id, key as k, val')
+ $query = $this->db->select('id, key as k, val')
->where('id !=', 1)
->get('create_test', 2, 1);
@@ -73,7 +73,7 @@
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);
$this->assertIsA($query, 'PDOStatement');
@@ -81,7 +81,7 @@
function TestSelectFromGet()
{
- $query = $this->qb->select('id, key as k, val')
+ $query = $this->db->select('id, key as k, val')
->from('create_test ct')
->where('id >', 1)
->get();
@@ -91,7 +91,7 @@
function TestSelectFromLimitGet()
{
- $query = $this->qb->select('id, key as k, val')
+ $query = $this->db->select('id, key as k, val')
->from('create_test ct')
->where('id >', 1)
->limit(3)
@@ -102,7 +102,7 @@
function TestOrderBy()
{
- $query = $this->qb->select('id, key as k, val')
+ $query = $this->db->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
@@ -116,7 +116,7 @@
function TestOrderByRandom()
{
- $query = $this->qb->select('id, key as k, val')
+ $query = $this->db->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
@@ -129,7 +129,7 @@
function TestGroupBy()
{
- $query = $this->qb->select('id, key as k, val')
+ $query = $this->db->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
@@ -145,7 +145,7 @@
function TestOrWhere()
{
- $query = $this->qb->select('id, key as k, val')
+ $query = $this->db->select('id, key as k, val')
->from('create_test')
->where(' id ', 1)
->or_where('key >', 0)
@@ -157,7 +157,7 @@
function TestLike()
{
- $query = $this->qb->from('create_test')
+ $query = $this->db->from('create_test')
->like('key', 'og')
->get();
@@ -166,7 +166,7 @@
function TestInsert()
{
- $query = $this->qb->set('id', 4)
+ $query = $this->db->set('id', 4)
->set('key', 4)
->set('val', 5)
->insert('create_test');
@@ -176,7 +176,7 @@
function TestUpdate()
{
- $query = $this->qb->set('id', 4)
+ $query = $this->db->set('id', 4)
->set('key', 'gogle')
->set('val', 'non-word')
->where('id', 4)
@@ -187,7 +187,7 @@
function TestDelete()
{
- $query = $this->qb->where('id', 4)->delete('create_test');
+ $query = $this->db->where('id', 4)->delete('create_test');
$this->assertIsA($query, 'PDOStatement');
}
diff --git a/tests/test_dbs/FB_TEST_DB.FDB b/tests/test_dbs/FB_TEST_DB.FDB
index 53aa271..58ec9fa 100755
Binary files a/tests/test_dbs/FB_TEST_DB.FDB and b/tests/test_dbs/FB_TEST_DB.FDB differ