Added 'from' method to query_builder

This commit is contained in:
Timothy Warren 2012-03-09 14:27:13 -05:00
parent 0654b94793
commit ab194c0ee8
4 changed files with 67 additions and 19 deletions

View File

@ -18,7 +18,12 @@
*/ */
class Query_Builder { class Query_Builder {
private $table, $where_array, $sql, $select_string, $where_string; private $table,
$sql,
$select_string,
$from_string,
$where_array,
$where_string;
/** /**
* Constructor * Constructor
@ -70,14 +75,14 @@ class Query_Builder {
*/ */
public function get($table='', $limit=FALSE, $offset=FALSE) public function get($table='', $limit=FALSE, $offset=FALSE)
{ {
// @todo Only add in the table name when using the select method // The simplest case, just get the data from the table
// @todo Only execute combined query when using other query methods and empty parameters if ( ! empty($table) && empty($this->from_string))
$sql = 'SELECT * FROM ' . $this->db->quote_ident($table);
if ( ! empty($table) && $limit === FALSE && $offset === FALSE)
{ {
$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 // Set the select string
@ -99,7 +104,7 @@ class Query_Builder {
$sql = $this->sql->limit($sql, $limit, $offset); $sql = $this->sql->limit($sql, $limit, $offset);
} }
// echo $sql."<br />"; echo $sql."<br />";
// Do prepared statements for anything involving a "where" clause // Do prepared statements for anything involving a "where" clause
if ( ! empty($this->where_string)) if ( ! empty($this->where_string))
@ -215,7 +220,7 @@ class Query_Builder {
} }
// Create the where portion of the string // 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($kv_array);
unset($fields); unset($fields);
@ -249,7 +254,16 @@ class Query_Builder {
*/ */
public function from($dbname) 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; return $this;
} }
@ -264,6 +278,7 @@ class Query_Builder {
unset($this->where_array); unset($this->where_array);
unset($this->where_string); unset($this->where_string);
unset($this->select_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 * String together the sql statements for sending to the db
* *
* @param $type
* @return $string * @return $string
*/ */
private function _compile() private function _compile($type)
{ {
// @todo Implement _compile method // @todo Implement _compile method
} }

View File

@ -233,14 +233,20 @@ class FirebirdQBTest extends UnitTestCase {
function TestQBSelectWhereGet() 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)); $this->assertTrue(is_resource($query));
} }
function TestQBSelectWhereGet2() 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)); $this->assertTrue(is_resource($query));
} }
@ -248,9 +254,19 @@ class FirebirdQBTest extends UnitTestCase {
function TestQBSelectGet() 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)); $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));
} }
} }

View File

@ -194,21 +194,37 @@ SQL;
function TestSelectWhereGet() 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'); $this->assertIsA($query, 'PDOStatement');
} }
function TestSelectWhereGet2() 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'); $this->assertIsA($query, 'PDOStatement');
} }
function TestSelectGet() 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'); $this->assertIsA($query, 'PDOStatement');
} }

Binary file not shown.