Remove some variable setting logic duplication

This commit is contained in:
Timothy Warren 2014-04-24 20:14:19 -04:00
parent f5e23baf91
commit 2ae38bea88
4 changed files with 97 additions and 84 deletions

View File

@ -291,8 +291,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
// Handle comma-separated identifiers // Handle comma-separated identifiers
if (strpos($ident, ',') !== FALSE) if (strpos($ident, ',') !== FALSE)
{ {
$parts = explode(',', $ident); $parts = array_map('mb_trim', explode(',', $ident));
$parts = array_map('mb_trim', $parts);
$parts = array_map(array($this, __METHOD__), $parts); $parts = array_map(array($this, __METHOD__), $parts);
$ident = implode(',', $parts); $ident = implode(',', $parts);
} }
@ -623,7 +622,6 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
*/ */
public function truncate($table) public function truncate($table)
{ {
$sql = ($this->has_truncate) $sql = ($this->has_truncate)
? 'TRUNCATE ' ? 'TRUNCATE '
: 'DELETE FROM '; : 'DELETE FROM ';

View File

@ -24,6 +24,15 @@ use \Query\Driver\Driver_Interface;
*/ */
abstract class Abstract_Query_Builder implements Query_Builder_Interface { abstract class Abstract_Query_Builder implements Query_Builder_Interface {
// --------------------------------------------------------------------------
// ! Constants
// --------------------------------------------------------------------------
const KEY = 0;
const VALUE = 1;
const BOTH = 2;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! SQL Clause Strings // ! SQL Clause Strings
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -147,36 +156,69 @@ abstract class Abstract_Query_Builder implements Query_Builder_Interface {
*/ */
protected $explain; protected $explain;
/**
* The current database driver
* @var Driver_Interface
*/
public $db;
/**
* Query parser class instance
* @var Query_Parser
*/
protected $parser;
/**
* Alias to driver util class
* @var \Query\Driver\Abstract_Util
*/
public $util;
/**
* Alias to driver sql class
* @var \Query\Driver\SQL_Interface
*/
public $sql;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// Methods // Methods
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Calls a function further down the inheritence chain * Set values in the class, with either an array or key value pair
* *
* @param string $name * @param array $var
* @param array $params * @param mixed $key
* @return mixed * @param mixed $val
* @throws \BadMethodCallException * @param int $val_type
* @return array
*/ */
public function __call($name, $params) protected function _mixed_set(&$var, $key, $val=NULL, $val_type=self::BOTH)
{ {
// Allow camel-case method calls $arg = (is_scalar($key) && is_scalar($val))
$snake_name = \from_camel_case($name); ? array($key => $val)
: $key;
foreach(array($this, $this->db) as $object) foreach($arg as $k => $v)
{ {
foreach(array($name, $snake_name) as $method_name) switch($val_type)
{ {
if (method_exists($object, $method_name)) case self::KEY:
{ $var[] = $k;
return call_user_func_array(array($object, $method_name), $params); break;
case self::VALUE:
$var[] = $v;
break;
default:
case self::BOTH:
$var[$k] = $v;
// break;
} }
} }
} return $var;
throw new \BadMethodCallException("Method does not exist");
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -312,23 +354,8 @@ abstract class Abstract_Query_Builder implements Query_Builder_Interface {
protected function _where($key, $val=array()) protected function _where($key, $val=array())
{ {
$where = array(); $where = array();
$this->_mixed_set($where, $key, $val, self::BOTH);
// Key and value passed? Add them to the where array $this->where_values = $this->_mixed_set($this->where_values, $key, $val, self::VALUE);
if (is_scalar($key) && is_scalar($val))
{
$where[$key] = $val;
$this->where_values[] = $val;
}
// Array or object, loop through and add to the where array
elseif ( ! is_scalar($key))
{
foreach($key as $k => $v)
{
$where[$k] = $v;
$this->where_values[] = $v;
}
}
return $where; return $where;
} }
@ -344,10 +371,8 @@ abstract class Abstract_Query_Builder implements Query_Builder_Interface {
*/ */
protected function _where_string($key, $val=array(), $conj='AND') protected function _where_string($key, $val=array(), $conj='AND')
{ {
$where = $this->_where($key, $val);
// Create key/value placeholders // Create key/value placeholders
foreach($where as $f => $val) foreach($this->_where($key, $val) as $f => $val)
{ {
// Split each key by spaces, in case there // Split each key by spaces, in case there
// is an operator such as >, <, !=, etc. // is an operator such as >, <, !=, etc.

View File

@ -27,34 +27,6 @@ use \Query\Driver\Driver_Interface;
*/ */
class Query_Builder extends Abstract_Query_Builder { class Query_Builder extends Abstract_Query_Builder {
/**
* The current database driver
* @var Driver_Interface
*/
public $db;
/**
* Query parser class instance
* @var Query_Parser
*/
protected $parser;
/**
* Alias to driver util class
* @var \Query\Driver\Abstract_Util
*/
public $util;
/**
* Alias to driver sql class
* @var \Query\Driver\SQL_Interface
*/
public $sql;
// --------------------------------------------------------------------------
// ! Methods
// --------------------------------------------------------------------------
/** /**
* Constructor * Constructor
* *
@ -85,6 +57,36 @@ class Query_Builder extends Abstract_Query_Builder {
$this->db = NULL; $this->db = NULL;
} }
// --------------------------------------------------------------------------
/**
* Calls a function further down the inheritence chain
*
* @param string $name
* @param array $params
* @return mixed
* @throws \BadMethodCallException
*/
public function __call($name, $params)
{
// Allow camel-case method calls
$snake_name = \from_camel_case($name);
foreach(array($this, $this->db) as $object)
{
foreach(array($name, $snake_name) as $method_name)
{
if (method_exists($object, $method_name))
{
return call_user_func_array(array($object, $method_name), $params);
}
}
}
throw new \BadMethodCallException("Method does not exist");
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Select Queries // ! Select Queries
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -441,21 +443,8 @@ class Query_Builder extends Abstract_Query_Builder {
*/ */
public function set($key, $val = NULL) public function set($key, $val = NULL)
{ {
// Plain key, value pair $this->set_array_keys = $this->_mixed_set($this->set_array_keys, $key, $val, self::KEY);
if (is_scalar($key) && is_scalar($val)) $this->values = $this->_mixed_set($this->values, $key, $val, self::VALUE);
{
$this->set_array_keys[] = $key;
$this->values[] = $val;
}
// Object or array
elseif (is_array($key) || is_object($key))
{
foreach($key as $k => $v)
{
$this->set_array_keys[] = $k;
$this->values[] = $v;
}
}
// Use the keys of the array to make the insert/update string // Use the keys of the array to make the insert/update string
// Escape the field names // Escape the field names
@ -531,7 +520,8 @@ class Query_Builder extends Abstract_Query_Builder {
*/ */
public function order_by($field, $type="") public function order_by($field, $type="")
{ {
// Random case // When ordering by random, do an ascending order if the driver
// doesn't support random ordering
if (stripos($type, 'rand') !== FALSE) if (stripos($type, 'rand') !== FALSE)
{ {
$type = (($rand = $this->sql->random()) !== FALSE ) ? $rand : 'ASC'; $type = (($rand = $this->sql->random()) !== FALSE ) ? $rand : 'ASC';

View File

@ -392,7 +392,7 @@ class Firebird extends Abstract_Driver {
// End the block of SQL statements // End the block of SQL statements
$sql .= "END"; $sql .= "END";
// Ruturn a null array value so the query is run as it is, // Return a null array value so the query is run as it is,
// not as a prepared statement, because a prepared statement // not as a prepared statement, because a prepared statement
// doesn't work for this type of query in Firebird. // doesn't work for this type of query in Firebird.
return array($sql, NULL); return array($sql, NULL);