From 376795648102916a2b374821285aa3299d1c0e04 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 13 Mar 2012 18:40:51 -0400 Subject: [PATCH] Added random function syntax to each driver --- sys/db/db_pdo.php | 7 +++++++ sys/db/drivers/firebird_sql.php | 16 ++++++++++++++ sys/db/drivers/mysql_sql.php | 16 ++++++++++++++ sys/db/drivers/odbc_sql.php | 24 ++++++++++++++++++++- sys/db/drivers/pgsql_sql.php | 16 ++++++++++++++ sys/db/drivers/sqlite_sql.php | 16 ++++++++++++++ sys/db/query_builder.php | 36 ++++++++++++++++++++++++-------- tests/databases/firebird-qb.php | 16 ++++++++++++++ tests/databases/sqlite-qb.php | 16 ++++++++++++++ tests/test_dbs/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 10 files changed, 153 insertions(+), 10 deletions(-) diff --git a/sys/db/db_pdo.php b/sys/db/db_pdo.php index dd52982..9da0819 100644 --- a/sys/db/db_pdo.php +++ b/sys/db/db_pdo.php @@ -277,5 +277,12 @@ abstract class DB_SQL { * @return string */ abstract public function limit($sql, $limit, $offset=FALSE); + + /** + * Get the sql for random ordering + * + * @return string + */ + abstract public function random(); } // End of db_pdo.php \ No newline at end of file diff --git a/sys/db/drivers/firebird_sql.php b/sys/db/drivers/firebird_sql.php index 6fd1a5d..3494e81 100644 --- a/sys/db/drivers/firebird_sql.php +++ b/sys/db/drivers/firebird_sql.php @@ -74,6 +74,8 @@ class Firebird_SQL extends DB_SQL { return $sql; } + + // -------------------------------------------------------------------------- /** * Drop the selected table @@ -85,6 +87,8 @@ class Firebird_SQL extends DB_SQL { { return 'DROP TABLE "'.$name.'"'; } + + // -------------------------------------------------------------------------- /** * Limit clause @@ -110,5 +114,17 @@ class Firebird_SQL extends DB_SQL { return $sql; } + + // -------------------------------------------------------------------------- + + /** + * Random ordering keyword + * + * @return string + */ + public function random() + { + return FALSE; + } } //End of firebird_sql.php \ No newline at end of file diff --git a/sys/db/drivers/mysql_sql.php b/sys/db/drivers/mysql_sql.php index 97d9810..04e2fc6 100644 --- a/sys/db/drivers/mysql_sql.php +++ b/sys/db/drivers/mysql_sql.php @@ -32,6 +32,8 @@ //TODO: implement } + // -------------------------------------------------------------------------- + /** * Convience public function for droping a MySQL table * @@ -42,6 +44,8 @@ { return "DROP TABLE `{$name}`"; } + + // -------------------------------------------------------------------------- /** * Limit clause @@ -59,6 +63,18 @@ } return $sql." LIMIT {$offset}, {$limit}"; + } + + // -------------------------------------------------------------------------- + + /** + * Random ordering keyword + * + * @return string + */ + public function random() + { + return ' RAND()'; } } //End of mysql_sql.php \ No newline at end of file diff --git a/sys/db/drivers/odbc_sql.php b/sys/db/drivers/odbc_sql.php index 6922280..60c4c6b 100644 --- a/sys/db/drivers/odbc_sql.php +++ b/sys/db/drivers/odbc_sql.php @@ -23,10 +23,20 @@ class ODBC_SQL extends DB_SQL { return FALSE; } + // -------------------------------------------------------------------------- + + /** + * Remove a table from the database + * + * @param string $name + * @return string + */ public function delete_table($name) { return "DROP TABLE {$name}"; } + + // -------------------------------------------------------------------------- /** * Limit clause @@ -38,7 +48,19 @@ class ODBC_SQL extends DB_SQL { */ public function limit($sql, $limit, $offset=FALSE) { - + return FALSE; + } + + // -------------------------------------------------------------------------- + + /** + * Random ordering keyword + * + * @return string + */ + public function random() + { + return FALSE; } } // End of odbc_sql.php \ No newline at end of file diff --git a/sys/db/drivers/pgsql_sql.php b/sys/db/drivers/pgsql_sql.php index d339c70..cc5baf6 100644 --- a/sys/db/drivers/pgsql_sql.php +++ b/sys/db/drivers/pgsql_sql.php @@ -21,11 +21,15 @@ class pgSQL_SQL extends DB_SQL { { //TODO: implement } + + // -------------------------------------------------------------------------- public function delete_table($name) { return 'DROP TABLE "'.$name.'"'; } + + // -------------------------------------------------------------------------- /** * Limit clause @@ -46,6 +50,18 @@ class pgSQL_SQL extends DB_SQL { return $sql; } + + // -------------------------------------------------------------------------- + + /** + * Random ordering keyword + * + * @return string + */ + public function random() + { + return ' RANDOM()'; + } } //End of pgsql_manip.php \ No newline at end of file diff --git a/sys/db/drivers/sqlite_sql.php b/sys/db/drivers/sqlite_sql.php index 92dfa7a..f1ad64c 100644 --- a/sys/db/drivers/sqlite_sql.php +++ b/sys/db/drivers/sqlite_sql.php @@ -73,6 +73,8 @@ class SQLite_SQL extends DB_SQL { return $sql; } + + // -------------------------------------------------------------------------- /** * SQL to drop the specified table @@ -84,6 +86,8 @@ class SQLite_SQL extends DB_SQL { { return 'DROP TABLE IF EXISTS "'.$name.'"'; } + + // -------------------------------------------------------------------------- /** * Limit clause @@ -102,5 +106,17 @@ class SQLite_SQL extends DB_SQL { return $sql." LIMIT {$offset}, {$limit}"; } + + // -------------------------------------------------------------------------- + + /** + * Random ordering keyword + * + * @return string + */ + public function random() + { + return ' RANDOM()'; + } } //End of sqlite_sql.php \ No newline at end of file diff --git a/sys/db/query_builder.php b/sys/db/query_builder.php index f424d41..6344024 100644 --- a/sys/db/query_builder.php +++ b/sys/db/query_builder.php @@ -28,14 +28,16 @@ class Query_Builder { $insert_string, $update_string, $set_string, - $order_string; + $order_string, + $group_string; // Key value pairs private $where_array, $like_array, $set_array, $set_array_keys, - $order_array; + $order_array, + $group_array; // Query-global components private $limit, @@ -233,10 +235,10 @@ class Query_Builder { * Where clause prefixed with "OR" * * @param string $field - * @param mixed $value + * @param mixed $val * @return $this */ - public function or_where($field, $value) + public function or_where($field, $val=array()) { // @todo Implement or_where method return $this; @@ -251,7 +253,7 @@ class Query_Builder { * @param mixed $val * @return $this */ - public function where_in($field, $val) + public function where_in($field, $val=array()) { // @todo Implement Where_in method return $this; @@ -266,7 +268,7 @@ class Query_Builder { * @param mixed $val * @return $this */ - public function or_where_in($field, $val) + public function or_where_in($field, $val=array()) { // @todo Implement or_where_in method return $this; @@ -281,7 +283,7 @@ class Query_Builder { * @param mixed $val * @return $this */ - public function where_not_in($field, $val) + public function where_not_in($field, $val=array()) { // @todo Implement where_not_in method return $this; @@ -296,7 +298,7 @@ class Query_Builder { * @param mixed $val * @return $this */ - public function or_where_not_in($field, $val) + public function or_where_not_in($field, $val=array()) { // @todo Implement or_where_not_in method return $this; @@ -328,7 +330,17 @@ class Query_Builder { */ public function group_by($field) { - // @todo Implement group_by method + if ( ! is_scalar($field)) + { + $this->group_array = array_map(array($this->db, 'quote_ident'), $field); + } + else + { + $this->group_array[] = $this->db->quote_ident($field); + } + + $this->group_string = ' GROUP BY '.implode(', ', $this->group_array); + return $this; } @@ -615,6 +627,12 @@ class Query_Builder { $sql .= $this->where_string; } + // Set the group_by string + if ( ! empty($this->group_string)) + { + $sql .= $this->group_string; + } + // Set the order_by string if ( ! empty($this->order_string)) { diff --git a/tests/databases/firebird-qb.php b/tests/databases/firebird-qb.php index 71fd46b..3c361f9 100644 --- a/tests/databases/firebird-qb.php +++ b/tests/databases/firebird-qb.php @@ -120,6 +120,22 @@ class FirebirdQBTest extends UnitTestCase { $this->assertTrue(is_resource($query)); } + /*function TestGroupBy() + { + $query = $this->qb->select('id, key as k, val') + ->from('create_test') + ->where('id >', 0) + ->where('id <', 9000) + ->group_by('k') + ->group_by('val') + ->order_by('id', 'DESC') + ->order_by('k', 'ASC') + ->limit(5,2) + ->get(); + + $this->assertTrue(is_resource($query)); + }*/ + function TestInsert() { $query = $this->qb->set('id', 4) diff --git a/tests/databases/sqlite-qb.php b/tests/databases/sqlite-qb.php index 73497ce..9b85515 100644 --- a/tests/databases/sqlite-qb.php +++ b/tests/databases/sqlite-qb.php @@ -114,6 +114,22 @@ $this->assertIsA($query, 'PDOStatement'); } + function TestGroupBy() + { + $query = $this->qb->select('id, key as k, val') + ->from('create_test') + ->where('id >', 0) + ->where('id <', 9000) + ->group_by('k') + ->group_by('val') + ->order_by('id', 'DESC') + ->order_by('k', 'ASC') + ->limit(5,2) + ->get(); + + $this->assertIsA($query, 'PDOStatement'); + } + function TestInsert() { $query = $this->qb->set('id', 4) diff --git a/tests/test_dbs/FB_TEST_DB.FDB b/tests/test_dbs/FB_TEST_DB.FDB index ee58f375983a8849f44d16d54eb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