diff --git a/.gitignore b/.gitignore index c530bc8..35dadd9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ test_config.json index.html +tests/db_files/* ._* \ No newline at end of file diff --git a/README.md b/README.md index 55640d2..675acdb 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Create a connection array or object similar to this: The parameters required depend on the database. ### Running Queries -Query uses the same interface as CodeIgniter's [Active Record class](http://codeigniter.com/user_guide/database/active_record.html). However, it does not implement the `select_` methods, `count_all_results`, `distinct`, `having`, `or_having`, `get_compiled_query`, `insert_batch`, `update_batch`, or `count_all` methods. +Query uses the same interface as CodeIgniter's [Active Record class](http://codeigniter.com/user_guide/database/active_record.html). However, it does not implement the `select_` methods, `count_all_results`, `distinct`, `having`, `or_having`, `insert_batch`, `update_batch`, or `count_all` methods. #### Retrieving Results diff --git a/classes/db_sql.php b/classes/db_sql.php index f71f4cd..b85f293 100644 --- a/classes/db_sql.php +++ b/classes/db_sql.php @@ -17,6 +17,60 @@ */ abstract class DB_SQL { + // -------------------------------------------------------------------------- + // ! Methods to override + // -------------------------------------------------------------------------- + + /** + * Get the max keyword sql + * + * @return string + */ + public function max() + { + return ' MAX'; + } + + // -------------------------------------------------------------------------- + + /** + * Get the min keyword sql + * + * @return string + */ + public function min() + { + return ' MIN'; + } + + // -------------------------------------------------------------------------- + + /** + * Get the 'distinct' keyword + * + * @return string + */ + public function distinct() + { + return ' DISTINCT'; + } + + // -------------------------------------------------------------------------- + + /** + * Get the 'average' keyword + * + * @return string + */ + public function avg() + { + return ' AVG'; + } + + // -------------------------------------------------------------------------- + // ! Abstract Methods + // -------------------------------------------------------------------------- + /** * Get database-specific sql to create a new table * diff --git a/classes/query_builder.php b/classes/query_builder.php index 3b8ed3a..98f07e9 100644 --- a/classes/query_builder.php +++ b/classes/query_builder.php @@ -717,6 +717,82 @@ class Query_Builder { return $this; } + // -------------------------------------------------------------------------- + // ! Query Grouping Methods + // -------------------------------------------------------------------------- + + /** + * Adds a paren to the current query for query grouping + * + * @return $this + */ + public function group_start() + { + $this->query_map[] = array( + 'type' => 'group_start', + 'conjunction' => '', + 'string' => ' (' + ); + + return $this; + } + + // -------------------------------------------------------------------------- + + /** + * Adds a paren to the current query for query grouping, + * prefixed with 'OR' + * + * @return $this + */ + public function or_group_start() + { + $this->query_map[] = array( + 'type' => 'group_start', + 'conjunction' => '', + 'string' => ' OR (' + ); + + return $this; + } + + // -------------------------------------------------------------------------- + + /** + * Adds a paren to the current query for query grouping, + * prefixed with 'OR NOT' + * + * @return $this + */ + public function or_not_group_start() + { + $this->query_map[] = array( + 'type' => 'group_start', + 'conjunction' => '', + 'string' => ' OR NOT (' + ); + + return $this; + } + + // -------------------------------------------------------------------------- + + /** + * Ends a query group + * + * @return $this + */ + public function group_end() + { + $this->query_map[] = array( + 'type' => 'group_end', + 'conjunction' => '', + 'string' => ' ) ' + ); + + return $this; + } + // -------------------------------------------------------------------------- // ! Query execution methods // -------------------------------------------------------------------------- @@ -763,6 +839,26 @@ class Query_Builder { return $result; } + // -------------------------------------------------------------------------- + + /** + * Convience method for get() with a where clause + * + * @param string $table + * @param array $where + * @param int $limit + * @param int $offset + * @return object + */ + public function get_where($table, $where=array(), $limit=FALSE, $offset=FALSE) + { + // Create the where clause + $this->where($where); + + // Return the result + return $this->get($table, $limit, $offset); + } + // -------------------------------------------------------------------------- /** @@ -844,81 +940,6 @@ class Query_Builder { return $res; } - // -------------------------------------------------------------------------- - // ! Query Grouping Methods - // -------------------------------------------------------------------------- - - /** - * Adds a paren to the current query for query grouping - * - * @return $this - */ - public function group_start() - { - $this->query_map[] = array( - 'type' => 'group_start', - 'conjunction' => '', - 'string' => ' (' - ); - - return $this; - } - - // -------------------------------------------------------------------------- - - /** - * Adds a paren to the current query for query grouping, - * prefixed with 'OR' - * - * @return $this - */ - public function or_group_start() - { - $this->query_map[] = array( - 'type' => 'group_start', - 'conjunction' => '', - 'string' => ' OR (' - ); - - return $this; - } - - // -------------------------------------------------------------------------- - - /** - * Adds a paren to the current query for query grouping, - * prefixed with 'OR NOT' - * - * @return $this - */ - public function or_not_group_start() - { - $this->query_map[] = array( - 'type' => 'group_start', - 'conjunction' => '', - 'string' => ' OR NOT (' - ); - - return $this; - } - - // -------------------------------------------------------------------------- - - /** - * Ends a query group - * - * @return $this - */ - public function group_end() - { - $this->query_map[] = array( - 'type' => 'group_end', - 'conjunction' => '', - 'string' => ' ) ' - ); - - return $this; - } // -------------------------------------------------------------------------- // ! Miscellaneous Methods diff --git a/drivers/odbc/odbc_driver.php b/drivers/odbc/odbc_driver.php index 26a92fa..0c4d689 100644 --- a/drivers/odbc/odbc_driver.php +++ b/drivers/odbc/odbc_driver.php @@ -21,6 +21,17 @@ */ class ODBC extends DB_PDO { + // Don't define the escape char - or define it in sub-drivers in a refactor + protected $escape_char = ''; + + /** + * Use ODBC to connect to a database + * + * @param string $dsn + * @param string $username + * @param string $password + * @param array $options + */ public function __construct($dsn, $username=null, $password=null, $options=array()) { parent::__construct("odbc:$dsn", $username, $password, $options); @@ -33,6 +44,8 @@ class ODBC extends DB_PDO { /** * Doesn't apply to ODBC + * + * @return bool */ public function switch_db($name) { diff --git a/tests/databases/firebird/firebird-qb.php b/tests/databases/firebird/firebird-qb.php index 615dfe2..ba26ee5 100644 --- a/tests/databases/firebird/firebird-qb.php +++ b/tests/databases/firebird/firebird-qb.php @@ -76,6 +76,15 @@ class FirebirdQBTest extends QBTest { $this->assertIsA($query, 'Firebird_Result'); } + function TestGetWhere() + { + if (empty($this->db)) return; + + $query = $this->db->get_where('create_test', array('id !=' => 1), 2, 1); + + $this->assertIsA($query, 'Firebird_Result'); + } + function TestSelectGet() { $query = $this->db->select('id, key as k, val') diff --git a/tests/db_files/FB_TEST_DB.FDB b/tests/db_files/FB_TEST_DB.FDB index ebd17b8..9f4ec82 100755 Binary files a/tests/db_files/FB_TEST_DB.FDB and b/tests/db_files/FB_TEST_DB.FDB differ diff --git a/tests/db_files/test_sqlite.db b/tests/db_files/test_sqlite.db deleted file mode 100755 index fdc78a9..0000000 Binary files a/tests/db_files/test_sqlite.db and /dev/null differ diff --git a/tests/parent.php b/tests/parent.php index 0205121..2f644c7 100644 --- a/tests/parent.php +++ b/tests/parent.php @@ -106,6 +106,15 @@ abstract class QBTest extends UnitTestCase { $this->assertIsA($query, 'PDOStatement'); } + + function TestGetWhere() + { + if (empty($this->db)) return; + + $query = $this->db->get_where('create_test', array('id !=' => 1), 2, 1); + + $this->assertIsA($query, 'PDOStatement'); + } function TestSelectGet() {