diff options
author | Derek Allard <derek.allard@ellislab.com> | 2007-12-18 18:22:50 +0100 |
---|---|---|
committer | Derek Allard <derek.allard@ellislab.com> | 2007-12-18 18:22:50 +0100 |
commit | 6ddb5a17ae1a0a75ca75f846dbb7d3a98f1902a3 (patch) | |
tree | 1192959a689fc65b6995abbedb51c7c92730fdfa | |
parent | 694b5b8ee6a40b57c91be3c5448bc8f5540d32d8 (diff) |
Added 'random' as an order_by() option in Active Record.
-rw-r--r-- | system/database/DB_active_rec.php | 11 | ||||
-rw-r--r-- | system/database/drivers/mssql/mssql_driver.php | 4 | ||||
-rw-r--r-- | system/database/drivers/mysql/mysql_driver.php | 17 | ||||
-rw-r--r-- | system/database/drivers/mysqli/mysqli_driver.php | 7 | ||||
-rw-r--r-- | system/database/drivers/oci8/oci8_driver.php | 4 | ||||
-rw-r--r-- | system/database/drivers/odbc/odbc_driver.php | 5 | ||||
-rw-r--r-- | system/database/drivers/postgre/postgre_driver.php | 7 | ||||
-rw-r--r-- | system/database/drivers/sqlite/sqlite_driver.php | 5 | ||||
-rw-r--r-- | user_guide/changelog.html | 13 | ||||
-rw-r--r-- | user_guide/database/active_record.html | 3 |
10 files changed, 45 insertions, 31 deletions
diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index cb134ea6a..e8059ab76 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -450,7 +450,12 @@ class CI_DB_active_record extends CI_DB_driver { */
function order_by($orderby, $direction = '')
{
- if (trim($direction) != '')
+ if (strtolower($direction) == 'random')
+ {
+ $orderby = ''; // Random results want or don't need a field name
+ $direction = $this->_random_keyword;
+ }
+ elseif (trim($direction) != '')
{
$direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
}
@@ -458,6 +463,7 @@ class CI_DB_active_record extends CI_DB_driver { $this->ar_orderby[] = $orderby.$direction;
return $this;
}
+
// --------------------------------------------------------------------
/**
@@ -469,6 +475,7 @@ class CI_DB_active_record extends CI_DB_driver { {
return $this->order_by($orderby, $direction);
}
+
// --------------------------------------------------------------------
/**
@@ -583,7 +590,7 @@ class CI_DB_active_record extends CI_DB_driver { $this->from($table);
}
- $sql = $this->_compile_select($this->count_string);
+ $sql = $this->_compile_select($this->_count_string);
$query = $this->query($sql);
$this->_reset_select();
diff --git a/system/database/drivers/mssql/mssql_driver.php b/system/database/drivers/mssql/mssql_driver.php index 051dd5bfe..89610455b 100644 --- a/system/database/drivers/mssql/mssql_driver.php +++ b/system/database/drivers/mssql/mssql_driver.php @@ -35,7 +35,7 @@ class CI_DB_mssql_driver extends CI_DB { * database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $count_string = "SELECT COUNT(*) AS numrows ";
+ var $_count_string = "SELECT COUNT(*) AS numrows ";
/**
* Non-persistent database connection
@@ -280,7 +280,7 @@ class CI_DB_mssql_driver extends CI_DB { if ($table == '')
return '0';
- $query = $this->query($this->count_string . "FROM ".$this->dbprefix.$table);
+ $query = $this->query($this->_count_string . "FROM ".$this->dbprefix.$table);
if ($query->num_rows() == 0)
return '0';
diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php index 69a238d94..2d91c5927 100644 --- a/system/database/drivers/mysql/mysql_driver.php +++ b/system/database/drivers/mysql/mysql_driver.php @@ -31,18 +31,19 @@ class CI_DB_mysql_driver extends CI_DB {
/**
- * The syntax to count rows is slightly different across different
- * database engines, so this string appears in each driver and is
- * used for the count_all() and count_all_results() functions.
- */
- var $count_string = "SELECT COUNT(*) AS numrows ";
-
- /**
* Whether to use the MySQL "delete hack" which allows the number
* of affected rows to be shown. Uses a preg_replace when enabled,
* adding a bit more processing to all queries.
*/
var $delete_hack = TRUE;
+
+ /**
+ * The syntax to count rows is slightly different across different
+ * database engines, so this string appears in each driver and is
+ * used for the count_all() and count_all_results() functions.
+ */
+ var $_count_string = "SELECT COUNT(*) AS numrows ";
+ var $_random_keyword = ' RAND()'; // database specific random keyword
/**
* Non-persistent database connection
@@ -296,7 +297,7 @@ class CI_DB_mysql_driver extends CI_DB { if ($table == '')
return '0';
- $query = $this->query($this->count_string . "FROM `".$this->dbprefix.$table."`");
+ $query = $this->query($this->_count_string . "FROM `".$this->dbprefix.$table."`");
if ($query->num_rows() == 0)
return '0';
diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php index 30a256e05..099117097 100644 --- a/system/database/drivers/mysqli/mysqli_driver.php +++ b/system/database/drivers/mysqli/mysqli_driver.php @@ -35,8 +35,9 @@ class CI_DB_mysqli_driver extends CI_DB { * database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $count_string = "SELECT COUNT(*) AS numrows ";
-
+ var $_count_string = "SELECT COUNT(*) AS numrows ";
+ var $_random_keyword = ' RAND()'; // database specific random keyword
+
/**
* Whether to use the MySQL "delete hack" which allows the number
* of affected rows to be shown. Uses a preg_replace when enabled,
@@ -289,7 +290,7 @@ class CI_DB_mysqli_driver extends CI_DB { if ($table == '')
return '0';
- $query = $this->query($this->count_string . "FROM `".$this->dbprefix.$table."`");
+ $query = $this->query($this->_count_string . "FROM `".$this->dbprefix.$table."`");
if ($query->num_rows() == 0)
return '0';
diff --git a/system/database/drivers/oci8/oci8_driver.php b/system/database/drivers/oci8/oci8_driver.php index 0cd04cc58..e6bcefef1 100644 --- a/system/database/drivers/oci8/oci8_driver.php +++ b/system/database/drivers/oci8/oci8_driver.php @@ -48,7 +48,7 @@ class CI_DB_oci8_driver extends CI_DB { * database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $count_string = "SELECT COUNT(1) AS numrows ";
+ var $_count_string = "SELECT COUNT(1) AS numrows ";
// Set "auto commit" by default
var $_commit = OCI_COMMIT_ON_SUCCESS;
@@ -396,7 +396,7 @@ class CI_DB_oci8_driver extends CI_DB { if ($table == '')
return '0';
- $query = $this->query($this->count_string . "FROM ".$table);
+ $query = $this->query($this->_count_string . "FROM ".$table);
if ($query == FALSE)
{
diff --git a/system/database/drivers/odbc/odbc_driver.php b/system/database/drivers/odbc/odbc_driver.php index ef26ea1c6..5e1676244 100644 --- a/system/database/drivers/odbc/odbc_driver.php +++ b/system/database/drivers/odbc/odbc_driver.php @@ -35,7 +35,8 @@ class CI_DB_odbc_driver extends CI_DB { * database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $count_string = "SELECT COUNT(*) AS numrows ";
+ var $_count_string = "SELECT COUNT(*) AS numrows ";
+ var $_random_keyword = ' RND('.time().')'; // database specific random keyword
/**
* Non-persistent database connection
@@ -258,7 +259,7 @@ class CI_DB_odbc_driver extends CI_DB { if ($table == '')
return '0';
- $query = $this->query($this->count_string . "FROM `".$this->dbprefix.$table."`");
+ $query = $this->query($this->_count_string . "FROM `".$this->dbprefix.$table."`");
if ($query->num_rows() == 0)
return '0';
diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index beaa7931c..e54f9cceb 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -35,7 +35,8 @@ class CI_DB_postgre_driver extends CI_DB { * database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $count_string = "SELECT COUNT(*) AS numrows ";
+ var $_count_string = "SELECT COUNT(*) AS numrows ";
+ var $_random_keyword = ' RANDOM()'; // database specific random keyword
/**
* Non-persistent database connection
@@ -285,8 +286,8 @@ class CI_DB_postgre_driver extends CI_DB { if ($table == '')
return '0';
- $query = $this->query($this->count_string .'FROM "'.$this->dbprefix.$table.'"');
-// original query before count_string was used. Kept for reference
+ $query = $this->query($this->_count_string .'FROM "'.$this->dbprefix.$table.'"');
+// original query before _count_string was used. Kept for reference
// $query = $this->query('SELECT COUNT(*) AS numrows FROM "'.$this->dbprefix.$table.'"');
if ($query->num_rows() == 0)
diff --git a/system/database/drivers/sqlite/sqlite_driver.php b/system/database/drivers/sqlite/sqlite_driver.php index 5f86b8ec3..aa6738c03 100644 --- a/system/database/drivers/sqlite/sqlite_driver.php +++ b/system/database/drivers/sqlite/sqlite_driver.php @@ -37,7 +37,8 @@ class CI_DB_sqlite_driver extends CI_DB { * database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $count_string = "SELECT COUNT(*) AS numrows ";
+ var $_count_string = "SELECT COUNT(*) AS numrows ";
+ var $_random_keyword = ' Random()'; // database specific random keyword
/**
* Non-persistent database connection
@@ -281,7 +282,7 @@ class CI_DB_sqlite_driver extends CI_DB { if ($table == '')
return '0';
- $query = $this->query($this->count_string . "FROM `".$this->dbprefix.$table."`");
+ $query = $this->query($this->_count_string . "FROM `".$this->dbprefix.$table."`");
if ($query->num_rows() == 0)
return '0';
diff --git a/user_guide/changelog.html b/user_guide/changelog.html index 5028032b2..612c4db62 100644 --- a/user_guide/changelog.html +++ b/user_guide/changelog.html @@ -66,10 +66,11 @@ Change Log <li>Added <dfn>$assign_to_controller</dfn> variable in the main <kbd>index.php</kbd> file. Anything that this variable contains will be passed automatically to a controller constructor when initialized.</li>
<li>Reorganized the URI and Routes classes for better clarity.</li>
<li>Javascript Calendar plugin now uses the months and days from the calendar language file, instead of hard-coded values, internationalizing it.</li>
- <li>Removed "rand()" as a listed option from orderby in the <a href="./database/active_record.html">Active Record</a>, as it was MySQL only. </li>
+ <li>Removed "rand()" as a listed option from orderby in the <a href="./database/active_record.html">Active Record</a>, as it was MySQL only.</li>
+ <li>Added 'random' as an <kbd>order_by()</kbd> option in <a href="./database/active_record.html">Active Record</a>.</li>
<li>Added titles to all user manual pages.</li>
<li>Added a check for NULL fields in the MySQL database backup utility.</li>
- <li>Documented the timezones() function in the <a href="./helpers/date_helper.html">Date Helper</a>.</li>
+ <li>Documented the <kbd>timezones()</kbd> function in the <a href="./helpers/date_helper.html">Date Helper</a>.</li>
<li>Documented unset_userdata in the <a href="./libraries/sessions.html">Session class</a>.</li>
<li>Changed the behaviour of custom callbacks so that they no longer trigger the "required" rule. </li>
<li>Added a few additional mime type variations for CSV.</li>
@@ -79,16 +80,16 @@ Change Log <li>Added <kbd>form_reset()</kbd> function to <a href="./helpers/form_helper.html">form helper</a>.</li>
<li>Added <kbd>count_all_results()</kbd> function to <a href="./database/active_record.html">Active Record</a>.</li>
<li>Added a language entry for valid_ip validation error.</li>
- <li>Added a third parameter to Active Record's like() clause to control where the wildcard goes. </li>
+ <li>Added a third parameter to Active Record's <kbd>like()</kbd> clause to control where the wildcard goes. </li>
<li>Moved the safe mode and auth checks for the Email library into the constructor. </li>
<li>Fixed a bug in <kbd>highlight_pharse()</kbd> that caused an error with slashes.</li>
<li>Fixed a bug in <kbd>word_wrap()</kbd> of the Text Helper that incorrectly referenced an object. </li>
<li>Fixed a bug in the <a href="./libraries/email.html">Email</a> library where some timezones were calculated incorrectly. </li>
- <li>Fixed a bug in <a href="./libraries/validation.html">Validation</a> where valid_ip() wasn't called properly.</li>
+ <li>Fixed a bug in <a href="./libraries/validation.html">Validation</a> where <kbd>valid_ip()</kbd> wasn't called properly.</li>
<li>Fixed a bug in the Session library where user agent matching would fail on user agents ending with a space. </li>
<li>Fixed a bug in database driver where num_rows property wasn't getting updated.</li>
<li>Fixed a bug in captcha calling an invalid PHP function.</li>
- <li>Fixed a bug in _html_entity_decode_callback() when 'global_xss_filtering' is enabled.</li>
+ <li>Fixed a bug in <kbd>_html_entity_decode_callback()</kbd> when 'global_xss_filtering' is enabled.</li>
<li>Fixed a bug in the cookie helper "set_cookie" function. It was not honoring the config settings.</li>
<li>Fixed a bug that was making validation callbacks required even when not set as such.</li>
<li>Fixed an example of comma-separated emails in the email library documentation.</li>
@@ -103,7 +104,7 @@ Change Log <li>Fixed a typo in the DocBlock comment for unset_userdata() in Session.</li>
<li>Fixed a typo in the table library docs. </li>
<li>Moved part of the userguide menu javascript to an external file. </li>
- <li>Deprecated from Active Record; getwhere() for get_where(); groupby() for group_by(); orderby() for order_by; orwhere() for or_where(); and orlike() for or_like().</li>
+ <li>Deprecated from Active Record; <kbd>getwhere()</kbd> for <kbd>get_where()</kbd>; <kbd>groupby()</kbd> for <kbd>group_by()</kbd>; <kbd>orderby()</kbd> for <kbd>order_by</kbd>; <kbd>orwhere()</kbd> for <kbd>or_where()</kbd>; and <kbd>orlike()</kbd> for <kbd>or_like()</kbd>.</li>
</ul>
diff --git a/user_guide/database/active_record.html b/user_guide/database/active_record.html index d608459e4..739d56a15 100644 --- a/user_guide/database/active_record.html +++ b/user_guide/database/active_record.html @@ -353,7 +353,7 @@ $this->db->or_like('body', $match); <h2>$this->db->order_by();</h2>
<p>Lets you set an ORDER BY clause. The first parameter contains the name of the column you would like to order by.
-The second parameter lets you set the direction of the result. Options are <kbd>asc</kbd> or <kbd>desc</kbd></p>
+The second parameter lets you set the direction of the result. Options are <kbd>asc</kbd> or <kbd>desc</kbd>, or <kbd>random</kbd>. </p>
<code>$this->db->order_by("title", "desc");
<br />
@@ -375,6 +375,7 @@ The second parameter lets you set the direction of the result. Options are <kbd // Produces: ORDER BY title DESC, name ASC
</code></p>
<p class="important">Note: order_by() was formerly known as orderby(), which has been deprecated.</p>
+<p class="important">Note: random ordering is not currently supported in Orcacle or MSSQL drivers. </p>
<h2>$this->db->limit();</h2>
<p>Lets you limit the number of rows you would like returned by the query:</p>
|