diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-09-11 02:47:11 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-09-11 02:47:11 +0000 |
commit | d4a597fd4bb2713b8f828211db6caa1b44c46faf (patch) | |
tree | 9def01bad8f51c27a002ae7d0d160777d0c9db4d /includes/database/select.inc | |
parent | f24f709e0f7d59643ca076fb00066e93fc81da01 (diff) | |
download | brdo-d4a597fd4bb2713b8f828211db6caa1b44c46faf.tar.gz brdo-d4a597fd4bb2713b8f828211db6caa1b44c46faf.tar.bz2 |
#561104 by Crell: Added support for random ordering in dynamic select queries.
Diffstat (limited to 'includes/database/select.inc')
-rw-r--r-- | includes/database/select.inc | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/includes/database/select.inc b/includes/database/select.inc index a4c4ed3e4..55eecf586 100644 --- a/includes/database/select.inc +++ b/includes/database/select.inc @@ -348,6 +348,28 @@ interface SelectQueryInterface extends QueryConditionInterface, QueryAlterableIn public function orderBy($field, $direction = 'ASC'); /** + * Orders the result set by a random value. + * + * This may be stacked with other orderBy() calls. If so, the query will order + * by each specified field, including this one, in the order called. Although + * this method may be called multiple times on the same query, doing so + * is not particularly useful. + * + * Note: The method used by most drivers may not scale to very large result + * sets. If you need to work with extremely large data sets, you may create + * your own database driver by subclassing off of an existing driver and + * implementing your own randomization mechanism. See + * + * http://jan.kneschke.de/projects/mysql/order-by-rand/ + * + * for an example of such an alternate sorting mechanism. + * + * @return + * The called object + */ + public function orderRandom(); + + /** * Restricts a query to a given range in the result set. * * If this method is called with no parameters, will remove any range @@ -643,6 +665,11 @@ class SelectQueryExtender implements SelectQueryInterface { return $this; } + public function orderRandom() { + $this->query->orderRandom(); + return $this; + } + public function range($start = NULL, $length = NULL) { $this->query->range($start, $length); return $this; @@ -1182,6 +1209,12 @@ class SelectQuery extends Query implements SelectQueryInterface { return $this; } + public function orderRandom() { + $alias = $this->addExpression('RAND()', 'random_field'); + $this->orderBy($alias); + return $this; + } + public function range($start = NULL, $length = NULL) { $this->range = func_num_args() ? array('start' => $start, 'length' => $length) : array(); return $this; |