Implemented random ordering for databases that support it

This commit is contained in:
Timothy Warren 2012-03-13 18:53:32 -04:00
parent 3767956481
commit 9aad56fafe
4 changed files with 34 additions and 2 deletions

View File

@ -355,7 +355,11 @@ class Query_Builder {
*/ */
public function order_by($field, $type="") public function order_by($field, $type="")
{ {
// @todo Implement Order by Random // Random case
if (stripos($type, 'rand') !== FALSE)
{
$type = (($rand = $this->sql->random()) !== FALSE ) ? $rand : 'ASC';
}
// Set fields for later manipulation // Set fields for later manipulation
$field = $this->db->quote_ident($field); $field = $this->db->quote_ident($field);
@ -370,7 +374,9 @@ class Query_Builder {
} }
// Set the final string // Set the final string
$this->order_string = ' ORDER BY '.implode(',', $order_clauses); $this->order_string = (empty($rand))
? ' ORDER BY '.implode(',', $order_clauses)
: ' ORDER BY'.$rand;
return $this; return $this;
} }

View File

@ -120,6 +120,19 @@ class FirebirdQBTest extends UnitTestCase {
$this->assertTrue(is_resource($query)); $this->assertTrue(is_resource($query));
} }
function TestOrderByRand()
{
$query = $this->qb->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
->order_by('id', 'rand')
->limit(5,2)
->get();
$this->assertTrue(is_resource($query));
}
/*function TestGroupBy() /*function TestGroupBy()
{ {
$query = $this->qb->select('id, key as k, val') $query = $this->qb->select('id, key as k, val')

View File

@ -114,6 +114,19 @@
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
function TestOrderByRandom()
{
$query = $this->qb->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
->order_by('id', 'rand')
->limit(5,2)
->get();
$this->assertIsA($query, 'PDOStatement');
}
function TestGroupBy() function TestGroupBy()
{ {
$query = $this->qb->select('id, key as k, val') $query = $this->qb->select('id, key as k, val')

Binary file not shown.