From 15e7ef0832aae9170c091d6a9301acb762e820f6 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 13 Apr 2012 11:53:11 -0400 Subject: [PATCH] Added 'get_where' method to query builder Also added prep work for select_* methods --- .gitignore | 1 + README.md | 2 +- classes/db_sql.php | 54 +++++++ classes/query_builder.php | 171 +++++++++++++---------- drivers/odbc/odbc_driver.php | 13 ++ tests/databases/firebird/firebird-qb.php | 9 ++ tests/db_files/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes tests/db_files/test_sqlite.db | Bin 3072 -> 0 bytes tests/parent.php | 9 ++ 9 files changed, 183 insertions(+), 76 deletions(-) delete mode 100755 tests/db_files/test_sqlite.db 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 ebd17b8f453dd90714818796ccd47e580b02f124..9f4ec8271361ac5add1ac78e56f1b8904e4329e8 100755 GIT binary patch delta 958 zcmZWnOK4L;6utA3*QB}mOxr{P+VJu-RZ$YOh#+Xdt^`piuDeK4Q7ARIHW+-N#QNEI z7w%n%;J?|3An3-8AZ`>FAxS|rFi2{g|7K4+#o{uwlr+%v8C6R zrY*;8>9eKZmg8$O;FaP|!?XfRzQ%GcaLaG!18aHoKD}NthjI0IUQEGf%tA9_!SP56 z;^8d34$nX)WI-tu0tltxTQCisa2n=<-eFkrd!V$c zzO@OEyBw@Hc|Iaw7HYKF#_Z&XZ-Qi^0BWvdRMcp$gQvv`-Roe&6a(I*D}A(zH8$-H z6Fp27m&@-pHWlV|jl(Xkx_lK{ES-QVEw(W;g#$;G8vM@19RKlmjEgE=`+@l=um7*O zN~?WLj;WKZ^5ic}8_a&&!VG=sVouy7(ZYmb3~0HD>4W-WB};|U*b%-3OSMnu+W3s_ z_c1|M2M^JQ7Vf4?9V}3wg|Wty4#s>Fs%F&Fou};Kx^SLCn?Lb@sMD)9S~S}ejzzJmJWW!nhpD0t>4>_pcvWMsD_Ud2#oAp*Lb-gnrX*zPQ4jZX z%2+}&J*#iyBPydijjSmnvmOCM$704P7XaJJK5-ghm71t;u(Kq#B~n$DQ(6x<@a#X)48N%W delta 1076 zcmaJ0nni6S-jtv!_ z2qJE0aB~oG()yD{L>+fh6o% zqwvT|!hFz%)4^eQZrX6q9ELqJ0t3M~l+8GtHAkVv_@fmEoAD=px7k*$=|BYT1V((f zqU|cSf<_fvA}ws9ofz;V zsZ)UDGc)Oh^sxGug(CfGV)n>NMF~{ee7hDCs8*&}SlGpcKsvsKcIe!4rCL3)KpPDl z%7rK7|E6)i>=*%`I#iB|fNq4t#gx_4g6uoM24VS#K2|@M_ zCUQEEmw0JmS>ryh5FEHeq_=~0_q}d&6H0V=lcPM|!DFJNdL4^rH8nQjg6ZkvzfA5O zSCeWM0$UXdlSAGM15Y2J` diff --git a/tests/db_files/test_sqlite.db b/tests/db_files/test_sqlite.db deleted file mode 100755 index fdc78a932ef284562811c45e8ae6ac74f4f538e6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3072 zcmWFz^vNtqRY=P(%1ta$FlJz3U}R))P*7lCU=m|sU|>wNfF(H6lboN1-e+ z2g20MVZ>rvNosKk)JzbQ39D@&8B%Qno?7VR6~? z{PdjEy!<@f^8BI{4lWi3K|w3etvptQL%eb WesM7<{{t)iQ7{?;qaiQ?LjVARNMHs4 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() {