Added order_by method to query builder

This commit is contained in:
Timothy Warren 2012-03-13 14:57:01 -04:00
parent f83e93541c
commit 382664ff6d
4 changed files with 77 additions and 12 deletions

View File

@ -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))
{

View File

@ -35,28 +35,28 @@ class FirebirdQBTest extends UnitTestCase {
echo '<hr /> Firebird Queries <hr />';
}
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);
}
}

View File

@ -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)

Binary file not shown.