Query builder 'where' method
This commit is contained in:
parent
913993c963
commit
6b6660585c
@ -54,11 +54,11 @@ abstract class DB_PDO extends PDO {
|
|||||||
$this->statement =& $query;
|
$this->statement =& $query;
|
||||||
|
|
||||||
|
|
||||||
if( ! (is_array($data) || is_object($data)))
|
/*if( ! (is_array($data) || is_object($data)))
|
||||||
{
|
{
|
||||||
trigger_error("Invalid data argument");
|
trigger_error("Invalid data argument");
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
// Bind the parameters
|
// Bind the parameters
|
||||||
foreach($data as $k => $value)
|
foreach($data as $k => $value)
|
||||||
@ -108,7 +108,7 @@ abstract class DB_PDO extends PDO {
|
|||||||
*/
|
*/
|
||||||
public function get_query_data($statement)
|
public function get_query_data($statement)
|
||||||
{
|
{
|
||||||
$this->statement = $statement;
|
$this->statement =& $statement;
|
||||||
|
|
||||||
// Execute the query
|
// Execute the query
|
||||||
$this->statement->execute();
|
$this->statement->execute();
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
class Query_Builder {
|
class Query_Builder {
|
||||||
|
|
||||||
private $table, $where_array, $sql, $select_string;
|
private $table, $where_array, $sql, $select_string, $where_string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
@ -86,6 +86,12 @@ class Query_Builder {
|
|||||||
// Replace the star with the selected fields
|
// Replace the star with the selected fields
|
||||||
$sql = str_replace('*', $this->select_string, $sql);
|
$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
|
// Set the limit, if it exists
|
||||||
if ($limit !== FALSE)
|
if ($limit !== FALSE)
|
||||||
@ -93,8 +99,15 @@ 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
|
||||||
|
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);
|
return $this->db->query($sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,6 +167,47 @@ class Query_Builder {
|
|||||||
*/
|
*/
|
||||||
public function where($key, $val=array())
|
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
|
// @todo Implement where method
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
@ -152,6 +152,13 @@ SQL;
|
|||||||
$this->assertIsA($query, 'PDOStatement');
|
$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()
|
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);
|
||||||
|
Loading…
Reference in New Issue
Block a user