From 6e454f4585ffea4276a40112e3385eaba72be991 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 9 Mar 2012 15:55:56 -0500 Subject: [PATCH] Added limit method to query builder --- sys/db/query_builder.php | 98 +++++++++++++++++++++++++--------- tests/databases/firebird.php | 21 ++++---- tests/databases/odbc.php | 13 +++++ tests/databases/sqlite.php | 27 ++++++---- tests/test_dbs/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 5 files changed, 113 insertions(+), 46 deletions(-) diff --git a/sys/db/query_builder.php b/sys/db/query_builder.php index fe061c6..5bb4979 100644 --- a/sys/db/query_builder.php +++ b/sys/db/query_builder.php @@ -23,7 +23,9 @@ class Query_Builder { $select_string, $from_string, $where_array, - $where_string; + $where_string, + $limit, + $offset; /** * Constructor @@ -75,35 +77,20 @@ class Query_Builder { */ public function get($table='', $limit=FALSE, $offset=FALSE) { - // The simplest case, just get the data from the table - if ( ! empty($table) && empty($this->from_string)) + // Set the table + if ( ! empty($table)) { - $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 - if ( ! empty($this->select_string)) - { - // Replace the star with the selected fields - $sql = str_replace('*', $this->select_string, $sql); + $this->from_string = $this->db->quote_ident($table); } - // Set the where string - if ( ! empty($this->where_string)) - { - $sql .= $this->where_string; - } - // Set the limit, if it exists if ($limit !== FALSE) { - $sql = $this->sql->limit($sql, $limit, $offset); + $this->limit($limit, $offset); } + $sql = $this->_compile('select'); + echo $sql."
"; // Do prepared statements for anything involving a "where" clause @@ -269,6 +256,23 @@ class Query_Builder { // -------------------------------------------------------------------------- + /** + * Set a limit on the current sql statement + * + * @param int $limit + * @param int $offset + * @return string + */ + public function limit($limit, $offset=FALSE) + { + $this->limit = $limit; + $this->offset = $offset; + + return $this; + } + + // -------------------------------------------------------------------------- + /** * Clear out the class variables, so the next query can be run */ @@ -279,6 +283,8 @@ class Query_Builder { unset($this->where_string); unset($this->select_string); unset($this->from_string); + unset($this->limit); + unset($this->offset); } // -------------------------------------------------------------------------- @@ -289,8 +295,50 @@ class Query_Builder { * @param $type * @return $string */ - private function _compile($type) + private function _compile($type="select") { - // @todo Implement _compile method + $sql = ''; + + switch($type) + { + default: + case "select": + $sql = 'SELECT * FROM '.$this->from_string; + + // Set the select string + if ( ! empty($this->select_string)) + { + // Replace the star with the selected fields + $sql = str_replace('*', $this->select_string, $sql); + } + + // Set the where string + if ( ! empty($this->where_string)) + { + $sql .= $this->where_string; + } + + // Set the limit via the class variables + if (is_numeric($this->limit)) + { + $sql = $this->sql->limit($sql, $this->limit, $this->offset); + } + break; + + case "insert": + // @todo Implement insert statements + break; + + case "update": + // @todo Implement update statements + break; + + case "delete": + // @todo Implement delete statements + break; + } + + return $sql; } -} \ No newline at end of file +} +// End of query_builder.php \ No newline at end of file diff --git a/tests/databases/firebird.php b/tests/databases/firebird.php index 5a2e49a..e8a4b8b 100644 --- a/tests/databases/firebird.php +++ b/tests/databases/firebird.php @@ -36,16 +36,6 @@ class FirebirdTest extends UnitTestCase { // Test the db driver directly $this->db = new Firebird('localhost:'.$dbpath); - - // Test the query builder - $params = new Stdclass(); - $params->type = 'firebird'; - $params->file = $dbpath; - $params->host = 'localhost'; - $params->user = 'sysdba'; - $params->pass = 'masterkey'; - $this->qb = new Query_Builder($params); - $this->tables = $this->db->get_tables(); } @@ -269,4 +259,15 @@ class FirebirdQBTest extends UnitTestCase { $this->assertTrue(is_resource($query)); } + + function TestSelectFromLimitGet() + { + $query = $this->qb->select('id, key as k, val') + ->from('create_test ct') + ->where('id >', 1) + ->limit(3) + ->get(); + + $this->assertTrue(is_resource($query)); + } } \ No newline at end of file diff --git a/tests/databases/odbc.php b/tests/databases/odbc.php index ba73a49..cb0ad08 100644 --- a/tests/databases/odbc.php +++ b/tests/databases/odbc.php @@ -28,5 +28,18 @@ class ODBCTest extends UnitTestCase { function __construct() { parent::__construct(); + + // Connection Test with SQLite + $path = dirname(__FILE__)."/../test_dbs/test_sqlite.db"; + + try + { + $this->db = new ODBC("Driver=/usr/lib/libsqlite3odbc.so;Database={$path}"); + } + catch(PDOException $e) + { + + } + } } \ No newline at end of file diff --git a/tests/databases/sqlite.php b/tests/databases/sqlite.php index c6d9a55..95b20f5 100644 --- a/tests/databases/sqlite.php +++ b/tests/databases/sqlite.php @@ -26,14 +26,8 @@ class SQLiteTest extends UnitTestCase { function setUp() { - $path = dirname(__FILE__)."/../test_dbs/test_sqlite.db"; + $path = TEST_DIR.DS.'test_dbs'.DS.'test_sqlite.db'; $this->db = new SQLite($path); - - $params = new Stdclass(); - $params->type = 'sqlite'; - $params->file = $path; - $params->host = 'localhost'; - $this->qb = new Query_Builder($params); } function tearDown() @@ -149,6 +143,8 @@ SQL; } +// -------------------------------------------------------------------------- + /** * Class for testing Query Builder with SQLite */ @@ -156,9 +152,7 @@ SQL; function setUp() { - $path = dirname(__FILE__)."/../test_dbs/test_sqlite.db"; - $this->db = new SQLite($path); - + $path = TEST_DIR.DS.'test_dbs'.DS.'test_sqlite.db'; $params = new Stdclass(); $params->type = 'sqlite'; $params->file = $path; @@ -168,7 +162,7 @@ SQL; function tearDown() { - unset($this->db); + unset($this->qb); } function TestGet() @@ -228,4 +222,15 @@ SQL; $this->assertIsA($query, 'PDOStatement'); } + + function TestSelectFromLimitGet() + { + $query = $this->qb->select('id, key as k, val') + ->from('create_test ct') + ->where('id >', 1) + ->limit(3) + ->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 f9bd05403fc829302574cdf32c645253ad7db2ec..53e832c0e0f70b377c8e8dbd81fecc0f2bd34b18 100755 GIT binary patch delta 1324 zcmYk*KTH!*90%~H?_Te7`Mt}%Y_=(z zZR#V?o@foi|07DX9&uH!8#=%)20$SXKLPZgUOL7C{Dvqf_)EJN1kW4#OkMg}sJMV) za+O~9ICE<>MrNF+`?O9uL!8kPZB%A-=gT)*8`M(xF51fP1KOOG5^W&U4*5e5``tfU ziIZQa2ASlzMc_#pH@mo*;|U2T1Rm%3guvr6j=MO{@tA~<3v6@TD6lPK)5Rvo!xC-~ zc!=YAfrn(wT+BGWDdAdyGaT0loRM+0i>o=lCgGUC0~}WgJRsvr7gut8LBaggKm2d2U@dQ~6sdsE5&TXu zxrhZBtep`PScJiT7{60YA#A}2)^yl}&M<=-9PfqjTMSt+g*6*8r;|rRWRth}Q?&R) zZgJCXag(=Ll3M&O*7t$qlECj}yzb(4j^9f7tH28!uL`^%<7F2wb3E^2pSuap7Aqdp P?MRW0k`-~zr_cWdR=q?g delta 278 zcmZo@Fl=Zr^kZbOG$`X{KmY;m24*M^L~C*}Fqi-_0|OJ|>{|>BA?D}RTR0#J1Q?ir zFagYA1d^Iun-vo_@lO(9;avzd;{Z_jz{Ei5&4vQ!`6oLFur?{QDKNGvFtsT#w<)l+ zDX_LFu(c_$w<&P6DR53x;9_Ak-Oj4OHHT^YMI|nM-iZkYZ3bN147gbWCbJw6;|^m4 zS+oy`_iqe*%{N(K{fBK6xET|~fZh-QioyU$RT!iAbny?|u^WRdco=o2H}di*Za?>j Y+lYC)$OOhW{M!{KFg`#bCnl5t0GXXU9{>OV