diff --git a/sys/db/query_builder.php b/sys/db/query_builder.php
index ff8a286..f424d41 100644
--- a/sys/db/query_builder.php
+++ b/sys/db/query_builder.php
@@ -15,21 +15,34 @@
/**
* Convienience class for creating sql queries - also the class that
* instantiates the specific db driver
+ *
+ * @todo Implement query queue to better match user meaning on queries
*/
class Query_Builder {
- private $sql,
- $select_string,
+ // Compiled query component strings
+ private $select_string,
$from_string,
- $where_array,
$where_string,
+ $like_string,
$insert_string,
$update_string,
+ $set_string,
+ $order_string;
+
+ // Key value pairs
+ private $where_array,
+ $like_array,
$set_array,
$set_array_keys,
- $set_string,
- $limit,
+ $order_array;
+
+ // Query-global components
+ private $limit,
$offset;
+
+ // Alias to $this->db->sql
+ private $sql;
/**
* Constructor
@@ -330,7 +343,23 @@ class Query_Builder {
*/
public function order_by($field, $type="")
{
- // @todo implement order_by method
+ // @todo Implement Order by Random
+
+ // Set fields for later manipulation
+ $field = $this->db->quote_ident($field);
+ $this->order_array[$field] = $type;
+
+ $order_clauses = array();
+
+ // Flatten key/val pairs into an array of space-separated pairs
+ foreach($this->order_array as $k => $v)
+ {
+ $order_clauses[] = $k . ' ' . strtoupper($v);
+ }
+
+ // Set the final string
+ $this->order_string = ' ORDER BY '.implode(',', $order_clauses);
+
return $this;
}
@@ -586,6 +615,12 @@ class Query_Builder {
$sql .= $this->where_string;
}
+ // Set the order_by string
+ if ( ! empty($this->order_string))
+ {
+ $sql .= $this->order_string;
+ }
+
// Set the limit via the class variables
if (isset($this->limit) && is_numeric($this->limit))
{
diff --git a/tests/databases/firebird-qb.php b/tests/databases/firebird-qb.php
index fd543ce..71fd46b 100644
--- a/tests/databases/firebird-qb.php
+++ b/tests/databases/firebird-qb.php
@@ -35,28 +35,28 @@ class FirebirdQBTest extends UnitTestCase {
echo '
Firebird Queries
';
}
- function TestQBGet()
+ function TestGet()
{
$query = $this->qb->get('create_test');
$this->assertTrue(is_resource($query));
}
- function TestQBGetLimit()
+ function TestGetLimit()
{
$query = $this->qb->get('create_test', 2);
$this->assertTrue(is_resource($query));
}
- function TestQBGetLimitSkip()
+ function TestGetLimitSkip()
{
$query = $this->qb->get('create_test', 2, 1);
$this->assertTrue(is_resource($query));
}
- function TestQBSelectWhereGet()
+ function TestSelectWhereGet()
{
$query = $this->qb->select('id, key as k, val')
->where('id >', 1)
@@ -66,7 +66,7 @@ class FirebirdQBTest extends UnitTestCase {
$this->assertTrue(is_resource($query));
}
- function TestQBSelectWhereGet2()
+ function TestSelectWhereGet2()
{
$query = $this->qb->select('id, key as k, val')
->where(' id ', 1)
@@ -77,7 +77,7 @@ class FirebirdQBTest extends UnitTestCase {
}
- function TestQBSelectGet()
+ function TestSelectGet()
{
$query = $this->qb->select('id, key as k, val')
->get('create_test', 2, 1);
@@ -106,6 +106,20 @@ class FirebirdQBTest extends UnitTestCase {
$this->assertTrue(is_resource($query));
}
+ function TestOrderBy()
+ {
+ $query = $this->qb->select('id, key as k, val')
+ ->from('create_test')
+ ->where('id >', 0)
+ ->where('id <', 9000)
+ ->order_by('id', 'DESC')
+ ->order_by('k', 'ASC')
+ ->limit(5,2)
+ ->get();
+
+ $this->assertTrue(is_resource($query));
+ }
+
function TestInsert()
{
$query = $this->qb->set('id', 4)
@@ -133,4 +147,6 @@ class FirebirdQBTest extends UnitTestCase {
$this->assertTrue($query);
}
+
+
}
\ No newline at end of file
diff --git a/tests/databases/sqlite-qb.php b/tests/databases/sqlite-qb.php
index 3a6e7f3..73497ce 100644
--- a/tests/databases/sqlite-qb.php
+++ b/tests/databases/sqlite-qb.php
@@ -100,6 +100,20 @@
$this->assertIsA($query, 'PDOStatement');
}
+ function TestOrderBy()
+ {
+ $query = $this->qb->select('id, key as k, val')
+ ->from('create_test')
+ ->where('id >', 0)
+ ->where('id <', 9000)
+ ->order_by('id', 'DESC')
+ ->order_by('k', 'ASC')
+ ->limit(5,2)
+ ->get();
+
+ $this->assertIsA($query, 'PDOStatement');
+ }
+
function TestInsert()
{
$query = $this->qb->set('id', 4)
diff --git a/tests/test_dbs/FB_TEST_DB.FDB b/tests/test_dbs/FB_TEST_DB.FDB
index b176515..ee58f37 100755
Binary files a/tests/test_dbs/FB_TEST_DB.FDB and b/tests/test_dbs/FB_TEST_DB.FDB differ