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 ee58f37..f554a51 100755 Binary files a/tests/test_dbs/FB_TEST_DB.FDB and b/tests/test_dbs/FB_TEST_DB.FDB differ