Added order_by method to query builder
This commit is contained in:
parent
f83e93541c
commit
382664ff6d
@ -15,22 +15,35 @@
|
|||||||
/**
|
/**
|
||||||
* Convienience class for creating sql queries - also the class that
|
* Convienience class for creating sql queries - also the class that
|
||||||
* instantiates the specific db driver
|
* instantiates the specific db driver
|
||||||
|
*
|
||||||
|
* @todo Implement query queue to better match user meaning on queries
|
||||||
*/
|
*/
|
||||||
class Query_Builder {
|
class Query_Builder {
|
||||||
|
|
||||||
private $sql,
|
// Compiled query component strings
|
||||||
$select_string,
|
private $select_string,
|
||||||
$from_string,
|
$from_string,
|
||||||
$where_array,
|
|
||||||
$where_string,
|
$where_string,
|
||||||
|
$like_string,
|
||||||
$insert_string,
|
$insert_string,
|
||||||
$update_string,
|
$update_string,
|
||||||
|
$set_string,
|
||||||
|
$order_string;
|
||||||
|
|
||||||
|
// Key value pairs
|
||||||
|
private $where_array,
|
||||||
|
$like_array,
|
||||||
$set_array,
|
$set_array,
|
||||||
$set_array_keys,
|
$set_array_keys,
|
||||||
$set_string,
|
$order_array;
|
||||||
$limit,
|
|
||||||
|
// Query-global components
|
||||||
|
private $limit,
|
||||||
$offset;
|
$offset;
|
||||||
|
|
||||||
|
// Alias to $this->db->sql
|
||||||
|
private $sql;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
@ -330,7 +343,23 @@ class Query_Builder {
|
|||||||
*/
|
*/
|
||||||
public function order_by($field, $type="")
|
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;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -586,6 +615,12 @@ class Query_Builder {
|
|||||||
$sql .= $this->where_string;
|
$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
|
// Set the limit via the class variables
|
||||||
if (isset($this->limit) && is_numeric($this->limit))
|
if (isset($this->limit) && is_numeric($this->limit))
|
||||||
{
|
{
|
||||||
|
@ -35,28 +35,28 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
echo '<hr /> Firebird Queries <hr />';
|
echo '<hr /> Firebird Queries <hr />';
|
||||||
}
|
}
|
||||||
|
|
||||||
function TestQBGet()
|
function TestGet()
|
||||||
{
|
{
|
||||||
$query = $this->qb->get('create_test');
|
$query = $this->qb->get('create_test');
|
||||||
|
|
||||||
$this->assertTrue(is_resource($query));
|
$this->assertTrue(is_resource($query));
|
||||||
}
|
}
|
||||||
|
|
||||||
function TestQBGetLimit()
|
function TestGetLimit()
|
||||||
{
|
{
|
||||||
$query = $this->qb->get('create_test', 2);
|
$query = $this->qb->get('create_test', 2);
|
||||||
|
|
||||||
$this->assertTrue(is_resource($query));
|
$this->assertTrue(is_resource($query));
|
||||||
}
|
}
|
||||||
|
|
||||||
function TestQBGetLimitSkip()
|
function TestGetLimitSkip()
|
||||||
{
|
{
|
||||||
$query = $this->qb->get('create_test', 2, 1);
|
$query = $this->qb->get('create_test', 2, 1);
|
||||||
|
|
||||||
$this->assertTrue(is_resource($query));
|
$this->assertTrue(is_resource($query));
|
||||||
}
|
}
|
||||||
|
|
||||||
function TestQBSelectWhereGet()
|
function TestSelectWhereGet()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->qb->select('id, key as k, val')
|
||||||
->where('id >', 1)
|
->where('id >', 1)
|
||||||
@ -66,7 +66,7 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
$this->assertTrue(is_resource($query));
|
$this->assertTrue(is_resource($query));
|
||||||
}
|
}
|
||||||
|
|
||||||
function TestQBSelectWhereGet2()
|
function TestSelectWhereGet2()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->qb->select('id, key as k, val')
|
||||||
->where(' id ', 1)
|
->where(' id ', 1)
|
||||||
@ -77,7 +77,7 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function TestQBSelectGet()
|
function TestSelectGet()
|
||||||
{
|
{
|
||||||
$query = $this->qb->select('id, key as k, val')
|
$query = $this->qb->select('id, key as k, val')
|
||||||
->get('create_test', 2, 1);
|
->get('create_test', 2, 1);
|
||||||
@ -106,6 +106,20 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
$this->assertTrue(is_resource($query));
|
$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()
|
function TestInsert()
|
||||||
{
|
{
|
||||||
$query = $this->qb->set('id', 4)
|
$query = $this->qb->set('id', 4)
|
||||||
@ -133,4 +147,6 @@ class FirebirdQBTest extends UnitTestCase {
|
|||||||
|
|
||||||
$this->assertTrue($query);
|
$this->assertTrue($query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -100,6 +100,20 @@
|
|||||||
$this->assertIsA($query, 'PDOStatement');
|
$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()
|
function TestInsert()
|
||||||
{
|
{
|
||||||
$query = $this->qb->set('id', 4)
|
$query = $this->qb->set('id', 4)
|
||||||
|
Binary file not shown.
Reference in New Issue
Block a user