From ab194c0ee84c07244e89743c5e0ef99c60ab24d4 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 9 Mar 2012 14:27:13 -0500 Subject: [PATCH] Added 'from' method to query_builder --- sys/db/query_builder.php | 40 ++++++++++++++++++++++++---------- tests/databases/firebird.php | 24 ++++++++++++++++---- tests/databases/sqlite.php | 22 ++++++++++++++++--- tests/test_dbs/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 4 files changed, 67 insertions(+), 19 deletions(-) diff --git a/sys/db/query_builder.php b/sys/db/query_builder.php index ad45499..fe061c6 100644 --- a/sys/db/query_builder.php +++ b/sys/db/query_builder.php @@ -18,7 +18,12 @@ */ class Query_Builder { - private $table, $where_array, $sql, $select_string, $where_string; + private $table, + $sql, + $select_string, + $from_string, + $where_array, + $where_string; /** * Constructor @@ -70,14 +75,14 @@ class Query_Builder { */ public function get($table='', $limit=FALSE, $offset=FALSE) { - // @todo Only add in the table name when using the select method - // @todo Only execute combined query when using other query methods and empty parameters - - $sql = 'SELECT * FROM ' . $this->db->quote_ident($table); - - if ( ! empty($table) && $limit === FALSE && $offset === FALSE) + // The simplest case, just get the data from the table + if ( ! empty($table) && empty($this->from_string)) { - $result = $this->db->query($sql); + $sql = 'SELECT * FROM ' . $this->db->quote_ident($table); + } + elseif(empty($table) && ! empty($this->from_string)) + { + $sql = 'SELECT * FROM ' . $this->from_string; } // Set the select string @@ -99,7 +104,7 @@ class Query_Builder { $sql = $this->sql->limit($sql, $limit, $offset); } - // echo $sql."
"; + echo $sql."
"; // Do prepared statements for anything involving a "where" clause if ( ! empty($this->where_string)) @@ -215,7 +220,7 @@ class Query_Builder { } // Create the where portion of the string - $this->where_string = ' WHERE '.implode(', ', $kv_array); + $this->where_string = ' WHERE '.implode(' AND ', $kv_array); unset($kv_array); unset($fields); @@ -249,7 +254,16 @@ class Query_Builder { */ public function from($dbname) { - // @todo Implement from method + // Split identifiers on spaces + $ident_array = explode(' ', trim($dbname)); + $ident_array = array_map('trim', $ident_array); + + // Quote the identifiers + $ident_array = array_map(array($this->db, 'quote_ident'), $ident_array); + + // Paste it back together + $this->from_string = implode(' ', $ident_array); + return $this; } @@ -264,6 +278,7 @@ class Query_Builder { unset($this->where_array); unset($this->where_string); unset($this->select_string); + unset($this->from_string); } // -------------------------------------------------------------------------- @@ -271,9 +286,10 @@ class Query_Builder { /** * String together the sql statements for sending to the db * + * @param $type * @return $string */ - private function _compile() + private function _compile($type) { // @todo Implement _compile method } diff --git a/tests/databases/firebird.php b/tests/databases/firebird.php index 090a0c8..5a2e49a 100644 --- a/tests/databases/firebird.php +++ b/tests/databases/firebird.php @@ -233,14 +233,20 @@ class FirebirdQBTest extends UnitTestCase { function TestQBSelectWhereGet() { - $query = $this->qb->select('id, key as k, val')->where('id >', 1)->get('create_test', 2, 1); + $query = $this->qb->select('id, key as k, val') + ->where('id >', 1) + ->where('id <', 800) + ->get('create_test', 2, 1); $this->assertTrue(is_resource($query)); } function TestQBSelectWhereGet2() { - $query = $this->qb->select('id, key as k, val')->where(' id ', 1)->get('create_test', 2, 1); + $query = $this->qb->select('id, key as k, val') + ->where(' id ', 1) + + ->get('create_test', 2, 1); $this->assertTrue(is_resource($query)); } @@ -248,9 +254,19 @@ class FirebirdQBTest extends UnitTestCase { function TestQBSelectGet() { - $query = $this->qb->select('id, key as k, val')->get('create_test', 2, 1); + $query = $this->qb->select('id, key as k, val') + ->get('create_test', 2, 1); $this->assertTrue(is_resource($query)); - + } + + function TestSelectFromGet() + { + $query = $this->qb->select('id, key as k, val') + ->from('create_test ct') + ->where('id >', 1) + ->get(); + + $this->assertTrue(is_resource($query)); } } \ No newline at end of file diff --git a/tests/databases/sqlite.php b/tests/databases/sqlite.php index b49edb3..c6d9a55 100644 --- a/tests/databases/sqlite.php +++ b/tests/databases/sqlite.php @@ -194,22 +194,38 @@ SQL; function TestSelectWhereGet() { - $query = $this->qb->select('id, key as k, val')->where('id >', 1)->get('create_test', 2, 1); + $query = $this->qb->select('id, key as k, val') + ->where('id >', 1) + ->where('id <', 900) + ->get('create_test', 2, 1); $this->assertIsA($query, 'PDOStatement'); } function TestSelectWhereGet2() { - $query = $this->qb->select('id, key as k, val')->where('id', 1)->get('create_test', 2, 1); + $query = $this->qb->select('id, key as k, val') + ->where('id !=', 1) + ->get('create_test', 2, 1); $this->assertIsA($query, 'PDOStatement'); } function TestSelectGet() { - $query = $this->qb->select('id, key as k, val')->get('create_test', 2, 1); + $query = $this->qb->select('id, key as k, val') + ->get('create_test', 2, 1); $this->assertIsA($query, 'PDOStatement'); } + + function TestSelectFromGet() + { + $query = $this->qb->select('id, key as k, val') + ->from('create_test ct') + ->where('id >', 1) + ->get(); + + $this->assertIsA($query, 'PDOStatement'); + } } \ No newline at end of file diff --git a/tests/test_dbs/FB_TEST_DB.FDB b/tests/test_dbs/FB_TEST_DB.FDB index d960203809ea27583d50f53eb927da2ca324b09f..f9bd05403fc829302574cdf32c645253ad7db2ec 100755 GIT binary patch delta 739 zcmY+A&ubGw6vyA2>~4~s{dH=D&@#zx>P8e2{6%j;iUbL@0m)72(L*iq;?*E!Pl6jG z-Ge9Zg2aZVr<(tu9y}BYZF5Nsf&>vMVyv?}0plz)Z@-Vv%zN+KXcQZb;u^wA;R%I* z%c70gOocJ80$k%8fZ*%T0QYVlUffOyzyiX*jYuFr^XhKKe{a6DB>m0ojUG?;n$`4f z(K<9+B};zBJcb@49*cQQ@|f%~#ba@gB|MhgVX8!~t6hbu+l138jJ2x|bg!W96{xh{ zl6niYhGD4#zCX09UyYXaZnEc7Twfq7g&RCOziN25n>wXQyjoWnRFo{ZiVdK+(3frK z$T@f=GpNg1sBrDbE&z{5QV#Yd)~-H^1=^Tiz)8>?(hOFD7bBYI>hYL5cqcd>(PjJ= z48G7MEboN5fdFp{a3H`{0iFo(RtR?eA+0B$%G`TvuHSI58m6FcW?|p7!8cvl;2Da0_qF}2BJ z^ajH}(cGcrw{83#4NlV0l|}p&PEL#&dnQ;eBlf3YwHf;(vh!Tn`CZs~F6^8LJExJI VWNq5bl<>st7E1W>zq7f~`3ouby5#@> delta 269 zcmZo@Fl=Zr^kZbOG?3wDKmY;m24*M^L|@=!V7LXu3=B++vu`mlgqWXKx8Z;&5MW>e z!UQmf5lCL(+^m?eiGPv+3+Do;8T+RzHgL-^Hf%N&IL|-XL4dVMp-q9YO@XORfw@hA zrA>jgO@XaVfxS(EqfLQxngSOK({0Y}Yzka0OpNv$150_^47j!#aI*wVWnmIy4+Gi4 zun%a