Added limit method to query builder

This commit is contained in:
Timothy Warren 2012-03-09 15:55:56 -05:00
parent ab194c0ee8
commit 6e454f4585
5 changed files with 113 additions and 46 deletions

View File

@ -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."<br />";
// 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;
}
}
}
// End of query_builder.php

View File

@ -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));
}
}

View File

@ -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)
{
}
}
}

View File

@ -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');
}
}

Binary file not shown.