diff --git a/core/abstract/abstract_driver.php b/core/abstract/abstract_driver.php index 2cd0787..e361ef3 100644 --- a/core/abstract/abstract_driver.php +++ b/core/abstract/abstract_driver.php @@ -291,8 +291,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { // Handle comma-separated identifiers if (strpos($ident, ',') !== FALSE) { - $parts = explode(',', $ident); - $parts = array_map('mb_trim', $parts); + $parts = array_map('mb_trim', explode(',', $ident)); $parts = array_map(array($this, __METHOD__), $parts); $ident = implode(',', $parts); } @@ -623,7 +622,6 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { */ public function truncate($table) { - $sql = ($this->has_truncate) ? 'TRUNCATE ' : 'DELETE FROM '; diff --git a/core/abstract/abstract_query_builder.php b/core/abstract/abstract_query_builder.php index 4173a79..6736f02 100644 --- a/core/abstract/abstract_query_builder.php +++ b/core/abstract/abstract_query_builder.php @@ -24,6 +24,15 @@ use \Query\Driver\Driver_Interface; */ abstract class Abstract_Query_Builder implements Query_Builder_Interface { + // -------------------------------------------------------------------------- + // ! Constants + // -------------------------------------------------------------------------- + + const KEY = 0; + const VALUE = 1; + const BOTH = 2; + + // -------------------------------------------------------------------------- // ! SQL Clause Strings // -------------------------------------------------------------------------- @@ -147,36 +156,69 @@ abstract class Abstract_Query_Builder implements Query_Builder_Interface { */ 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 // -------------------------------------------------------------------------- /** - * 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 $params - * @return mixed - * @throws \BadMethodCallException + * @param array $var + * @param mixed $key + * @param mixed $val + * @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 - $snake_name = \from_camel_case($name); + $arg = (is_scalar($key) && is_scalar($val)) + ? 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)) - { - return call_user_func_array(array($object, $method_name), $params); - } - } + case self::KEY: + $var[] = $k; + break; + case self::VALUE: + $var[] = $v; + break; + + default: + case self::BOTH: + $var[$k] = $v; + // break; + } } - throw new \BadMethodCallException("Method does not exist"); + return $var; } // -------------------------------------------------------------------------- @@ -312,23 +354,8 @@ abstract class Abstract_Query_Builder implements Query_Builder_Interface { protected function _where($key, $val=array()) { $where = array(); - - // Key and value passed? Add them to the where array - 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; - } - } - + $this->_mixed_set($where, $key, $val, self::BOTH); + $this->where_values = $this->_mixed_set($this->where_values, $key, $val, self::VALUE); return $where; } @@ -344,10 +371,8 @@ abstract class Abstract_Query_Builder implements Query_Builder_Interface { */ protected function _where_string($key, $val=array(), $conj='AND') { - $where = $this->_where($key, $val); - // 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 // is an operator such as >, <, !=, etc. diff --git a/core/query_builder.php b/core/query_builder.php index e514970..0621fe7 100644 --- a/core/query_builder.php +++ b/core/query_builder.php @@ -27,34 +27,6 @@ use \Query\Driver\Driver_Interface; */ 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 * @@ -85,6 +57,36 @@ class Query_Builder extends Abstract_Query_Builder { $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 // -------------------------------------------------------------------------- @@ -441,21 +443,8 @@ class Query_Builder extends Abstract_Query_Builder { */ public function set($key, $val = NULL) { - // Plain key, value pair - if (is_scalar($key) && is_scalar($val)) - { - $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; - } - } + $this->set_array_keys = $this->_mixed_set($this->set_array_keys, $key, $val, self::KEY); + $this->values = $this->_mixed_set($this->values, $key, $val, self::VALUE); // Use the keys of the array to make the insert/update string // Escape the field names @@ -531,7 +520,8 @@ class Query_Builder extends Abstract_Query_Builder { */ 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) { $type = (($rand = $this->sql->random()) !== FALSE ) ? $rand : 'ASC'; diff --git a/drivers/firebird/firebird_driver.php b/drivers/firebird/firebird_driver.php index 875f693..9c0de26 100644 --- a/drivers/firebird/firebird_driver.php +++ b/drivers/firebird/firebird_driver.php @@ -392,7 +392,7 @@ class Firebird extends Abstract_Driver { // End the block of SQL statements $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 // doesn't work for this type of query in Firebird. return array($sql, NULL);