Add seed values support for Query Builder order_by
(feature request #1987)
diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php
index c89a924..8394139 100644
--- a/system/database/drivers/cubrid/cubrid_driver.php
+++ b/system/database/drivers/cubrid/cubrid_driver.php
@@ -64,6 +64,13 @@
*/
protected $_escape_char = '`';
+ /**
+ * ORDER BY random keyword
+ *
+ * @var array
+ */
+ protected $_random_keyword = array('RANDOM()', 'RANDOM(%d)');
+
// --------------------------------------------------------------------
/**
diff --git a/system/database/drivers/ibase/ibase_driver.php b/system/database/drivers/ibase/ibase_driver.php
index fc1c28f..02ec354 100644
--- a/system/database/drivers/ibase/ibase_driver.php
+++ b/system/database/drivers/ibase/ibase_driver.php
@@ -53,9 +53,9 @@
/**
* ORDER BY random keyword
*
- * @var string
+ * @var array
*/
- protected $_random_keyword = ' Random()';
+ protected $_random_keyword = array('RAND()', 'RAND()');
/**
* IBase Transaction status flag
diff --git a/system/database/drivers/mssql/mssql_driver.php b/system/database/drivers/mssql/mssql_driver.php
index a9d53fb..53f2e55 100644
--- a/system/database/drivers/mssql/mssql_driver.php
+++ b/system/database/drivers/mssql/mssql_driver.php
@@ -53,9 +53,9 @@
/**
* ORDER BY random keyword
*
- * @var string
+ * @var array
*/
- protected $_random_keyword = ' NEWID()';
+ protected $_random_keyword = array('NEWID()', 'RAND(%d)');
/**
* Quoted identifier flag
diff --git a/system/database/drivers/oci8/oci8_driver.php b/system/database/drivers/oci8/oci8_driver.php
index eb01dd1..62f9191 100644
--- a/system/database/drivers/oci8/oci8_driver.php
+++ b/system/database/drivers/oci8/oci8_driver.php
@@ -102,9 +102,9 @@
/**
* ORDER BY random keyword
*
- * @var string
+ * @var array
*/
- protected $_random_keyword = ' ASC'; // not currently supported
+ protected $_random_keyword = array('ASC', 'ASC'); // not currently supported
/**
* COUNT string
diff --git a/system/database/drivers/odbc/odbc_driver.php b/system/database/drivers/odbc/odbc_driver.php
index 1710aaf..a01a9d6 100644
--- a/system/database/drivers/odbc/odbc_driver.php
+++ b/system/database/drivers/odbc/odbc_driver.php
@@ -73,6 +73,13 @@
*/
protected $_like_escape_str = " {escape '%s'} ";
+ /**
+ * ORDER BY random keyword
+ *
+ * @var array
+ */
+ protected $_random_keyword = array('RND()', 'RND(%d)');
+
// --------------------------------------------------------------------
/**
@@ -85,8 +92,6 @@
{
parent::__construct($params);
- $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
-
// Legacy support for DSN in the hostname field
if (empty($this->dsn))
{
diff --git a/system/database/drivers/pdo/subdrivers/pdo_cubrid_driver.php b/system/database/drivers/pdo/subdrivers/pdo_cubrid_driver.php
index 4f762b9..5a87cf0 100644
--- a/system/database/drivers/pdo/subdrivers/pdo_cubrid_driver.php
+++ b/system/database/drivers/pdo/subdrivers/pdo_cubrid_driver.php
@@ -55,6 +55,13 @@
*/
protected $_escape_char = '`';
+ /**
+ * ORDER BY random keyword
+ *
+ * @var array
+ */
+ protected $_random_keyword = array('RANDOM()', 'RANDOM(%d)');
+
// --------------------------------------------------------------------
/**
diff --git a/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php b/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php
index 7121819..a44fd3a 100644
--- a/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php
+++ b/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php
@@ -53,9 +53,9 @@
/**
* ORDER BY random keyword
*
- * @var string
+ * @var array
*/
- protected $_random_keyword = ' NEWID()';
+ protected $_random_keyword = array('NEWID()', 'RAND(%d)');
/**
* Quoted identifier flag
diff --git a/system/database/drivers/pdo/subdrivers/pdo_firebird_driver.php b/system/database/drivers/pdo/subdrivers/pdo_firebird_driver.php
index 83b9648..7c38366 100644
--- a/system/database/drivers/pdo/subdrivers/pdo_firebird_driver.php
+++ b/system/database/drivers/pdo/subdrivers/pdo_firebird_driver.php
@@ -53,9 +53,9 @@
/**
* ORDER BY random keyword
*
- * @var string
+ * @var array
*/
- protected $_random_keyword = ' RANDOM()';
+ protected $_random_keyword = array('RAND()', 'RAND()');
// --------------------------------------------------------------------
diff --git a/system/database/drivers/pdo/subdrivers/pdo_informix_driver.php b/system/database/drivers/pdo/subdrivers/pdo_informix_driver.php
index 8998670..03886f4 100644
--- a/system/database/drivers/pdo/subdrivers/pdo_informix_driver.php
+++ b/system/database/drivers/pdo/subdrivers/pdo_informix_driver.php
@@ -51,6 +51,15 @@
// --------------------------------------------------------------------
/**
+ * ORDER BY random keyword
+ *
+ * @var array
+ */
+ protected $_random_keyword = array('ASC', 'ASC'); // Currently not supported
+
+ // --------------------------------------------------------------------
+
+ /**
* Class constructor
*
* Builds the DSN if not already set.
diff --git a/system/database/drivers/pdo/subdrivers/pdo_oci_driver.php b/system/database/drivers/pdo/subdrivers/pdo_oci_driver.php
index e1beb36..051b7de 100644
--- a/system/database/drivers/pdo/subdrivers/pdo_oci_driver.php
+++ b/system/database/drivers/pdo/subdrivers/pdo_oci_driver.php
@@ -62,9 +62,9 @@
/**
* ORDER BY random keyword
*
- * @var string
+ * @var array
*/
- protected $_random_keyword = ' ASC'; // Currently not supported
+ protected $_random_keyword = array('ASC', 'ASC'); // Currently not supported
/**
* COUNT string
diff --git a/system/database/drivers/pdo/subdrivers/pdo_odbc_driver.php b/system/database/drivers/pdo/subdrivers/pdo_odbc_driver.php
index f6e0e21..05b34fa 100644
--- a/system/database/drivers/pdo/subdrivers/pdo_odbc_driver.php
+++ b/system/database/drivers/pdo/subdrivers/pdo_odbc_driver.php
@@ -73,6 +73,13 @@
*/
protected $_like_escape_str = " {escape '%s'} ";
+ /**
+ * ORDER BY random keyword
+ *
+ * @var array
+ */
+ protected $_random_keyword = array('RND()', 'RND(%d)');
+
// --------------------------------------------------------------------
/**
diff --git a/system/database/drivers/pdo/subdrivers/pdo_pgsql_driver.php b/system/database/drivers/pdo/subdrivers/pdo_pgsql_driver.php
index c3f5b76..4516521 100644
--- a/system/database/drivers/pdo/subdrivers/pdo_pgsql_driver.php
+++ b/system/database/drivers/pdo/subdrivers/pdo_pgsql_driver.php
@@ -60,9 +60,9 @@
/**
* ORDER BY random keyword
*
- * @var string
+ * @var array
*/
- protected $_random_keyword = ' RANDOM()';
+ protected $_random_keyword = array('RANDOM()', 'RANDOM()');
// --------------------------------------------------------------------
@@ -110,6 +110,41 @@
// --------------------------------------------------------------------
/**
+ * ORDER BY
+ *
+ * @param string $orderby
+ * @param string $direction ASC or DESC
+ * @param bool $escape
+ * @return object
+ */
+ public function order_by($orderby, $direction = '', $escape = NULL)
+ {
+ $direction = strtoupper(trim($direction));
+ if ($direction === 'RANDOM')
+ {
+ if ( ! is_float($orderby) && ctype_digit((string) $orderby))
+ {
+ $orderby = ($orderby > 1)
+ ? (float) '0.'.$orderby
+ : (float) $orderby;
+ }
+
+ if (is_float($orderby))
+ {
+ $this->simple_query('SET SEED '.$orderby);
+ }
+
+ $orderby = $this->_random_keyword[0];
+ $direction = '';
+ $escape = FALSE;
+ }
+
+ return parent::order_by($orderby, $direction, $escape);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Show table query
*
* Generates a platform-specific query string so that the table names can be fetched
diff --git a/system/database/drivers/pdo/subdrivers/pdo_sqlite_driver.php b/system/database/drivers/pdo/subdrivers/pdo_sqlite_driver.php
index b24b13e..f9d186f 100644
--- a/system/database/drivers/pdo/subdrivers/pdo_sqlite_driver.php
+++ b/system/database/drivers/pdo/subdrivers/pdo_sqlite_driver.php
@@ -53,7 +53,7 @@
/**
* ORDER BY random keyword
*
- * @var string
+ * @var array
*/
protected $_random_keyword = ' RANDOM()';
diff --git a/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php b/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php
index 7bb7f88..1a63072 100644
--- a/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php
+++ b/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php
@@ -53,9 +53,9 @@
/**
* ORDER BY random keyword
*
- * @var string
+ * @var array
*/
- protected $_random_keyword = ' NEWID()';
+ protected $_random_keyword = array('NEWID()', 'RAND(%d)');
/**
* Quoted identifier flag
diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php
index 40f21b1..a52777b 100644
--- a/system/database/drivers/postgre/postgre_driver.php
+++ b/system/database/drivers/postgre/postgre_driver.php
@@ -60,9 +60,9 @@
/**
* ORDER BY random keyword
*
- * @var string
+ * @var array
*/
- protected $_random_keyword = ' RANDOM()'; // database specific random keyword
+ protected $_random_keyword = array('RANDOM()', 'RANDOM()');
// --------------------------------------------------------------------
@@ -482,6 +482,41 @@
// --------------------------------------------------------------------
/**
+ * ORDER BY
+ *
+ * @param string $orderby
+ * @param string $direction ASC or DESC
+ * @param bool $escape
+ * @return object
+ */
+ public function order_by($orderby, $direction = '', $escape = NULL)
+ {
+ $direction = strtoupper(trim($direction));
+ if ($direction === 'RANDOM')
+ {
+ if ( ! is_float($orderby) && ctype_digit((string) $orderby))
+ {
+ $orderby = ($orderby > 1)
+ ? (float) '0.'.$orderby
+ : (float) $orderby;
+ }
+
+ if (is_float($orderby))
+ {
+ $this->simple_query('SET SEED '.$orderby);
+ }
+
+ $orderby = $this->_random_keyword[0];
+ $direction = '';
+ $escape = FALSE;
+ }
+
+ return parent::order_by($orderby, $direction, $escape);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Update statement
*
* Generates a platform-specific update string from the supplied data
diff --git a/system/database/drivers/sqlite/sqlite_driver.php b/system/database/drivers/sqlite/sqlite_driver.php
index 8541173..23f287b 100644
--- a/system/database/drivers/sqlite/sqlite_driver.php
+++ b/system/database/drivers/sqlite/sqlite_driver.php
@@ -53,9 +53,9 @@
/**
* ORDER BY random keyword
*
- * @var string
+ * @var array
*/
- protected $_random_keyword = ' Random()'; // database specific random keyword
+ protected $_random_keyword = array('RANDOM()', 'RANDOM()');
// --------------------------------------------------------------------
diff --git a/system/database/drivers/sqlite3/sqlite3_driver.php b/system/database/drivers/sqlite3/sqlite3_driver.php
index 9a2c1ee..f0818ae 100644
--- a/system/database/drivers/sqlite3/sqlite3_driver.php
+++ b/system/database/drivers/sqlite3/sqlite3_driver.php
@@ -54,9 +54,9 @@
/**
* ORDER BY random keyword
*
- * @var string
+ * @var array
*/
- protected $_random_keyword = ' RANDOM()';
+ protected $_random_keyword = array('RANDOM()', 'RANDOM()');
// --------------------------------------------------------------------
diff --git a/system/database/drivers/sqlsrv/sqlsrv_driver.php b/system/database/drivers/sqlsrv/sqlsrv_driver.php
index 09ccc02..a3d3177 100644
--- a/system/database/drivers/sqlsrv/sqlsrv_driver.php
+++ b/system/database/drivers/sqlsrv/sqlsrv_driver.php
@@ -53,9 +53,9 @@
/**
* ORDER BY random keyword
*
- * @var string
+ * @var array
*/
- protected $_random_keyword = ' NEWID()';
+ protected $_random_keyword = array('NEWID()', 'RAND(%d)');
/**
* Quoted identifier flag