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