Query builder 'where' method

This commit is contained in:
Timothy Warren 2012-03-09 11:56:14 -05:00
parent 913993c963
commit 6b6660585c
3 changed files with 66 additions and 5 deletions

View File

@ -54,11 +54,11 @@ abstract class DB_PDO extends PDO {
$this->statement =& $query;
if( ! (is_array($data) || is_object($data)))
/*if( ! (is_array($data) || is_object($data)))
{
trigger_error("Invalid data argument");
return FALSE;
}
}*/
// Bind the parameters
foreach($data as $k => $value)
@ -108,7 +108,7 @@ abstract class DB_PDO extends PDO {
*/
public function get_query_data($statement)
{
$this->statement = $statement;
$this->statement =& $statement;
// Execute the query
$this->statement->execute();

View File

@ -18,7 +18,7 @@
*/
class Query_Builder {
private $table, $where_array, $sql, $select_string;
private $table, $where_array, $sql, $select_string, $where_string;
/**
* Constructor
@ -87,14 +87,27 @@ class Query_Builder {
$sql = str_replace('*', $this->select_string, $sql);
}
// 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);
}
//echo $sql."<br />";
echo $sql."<br />";
// Do prepared statements for anything involving a "where" clause
if ( ! empty($this->where_string))
{
return $this->db->prepare_execute($sql, array_values($this->where_array));
}
// Otherwise, a simple query will do.
return $this->db->query($sql);
}
@ -154,6 +167,47 @@ class Query_Builder {
*/
public function where($key, $val=array())
{
// Key and value passed? Add them to the where array
if (is_scalar($key) && is_scalar($val))
{
$this->where_array[$key] = $val;
}
// Array or object, loop through and add to the where array
elseif ( ! is_scalar($key))
{
foreach($key as $k => $v)
{
$this->where_array[$k] = $v;
}
}
// The values are irrelevant until the query is actually run
$fields = array_keys($this->where_array);
// Array of conditions
$kv_array = array();
// Create key/value placeholders
foreach($fields as $f)
{
// Split each key by spaces, incase there
// is an operator such as >, <, !=, etc.
$f_array = explode(' ', trim($f));
// Simple key = val
if (count($f_array) === 1)
{
$kv_array[] = $this->db->quote_ident($f_array[0]) . '= ?';
}
else // Other operators
{
$kv_array[] = $this->db->quote_ident($f_array[0]) . " {$f_array[1]} ?";
}
}
// Create the where portion of the string
$this->where_string = ' WHERE '.implode(', ', $kv_array);
// @todo Implement where method
return $this;
}

View File

@ -152,6 +152,13 @@ SQL;
$this->assertIsA($query, 'PDOStatement');
}
function TestQBSelectWhereGet()
{
$query = $this->qb->select('id, key as k, val')->where('id >', 1)->get('create_test, 2, 1');
$this->assertIsA($query, 'PDOStatement');
}
function TestQBSelectGet()
{
$query = $this->qb->select('id, key as k, val')->get('create_test', 2, 1);