diff --git a/autoload.php b/autoload.php index 29414b3..9d86827 100644 --- a/autoload.php +++ b/autoload.php @@ -42,7 +42,8 @@ require(QBASE_PATH.'common.php'); */ function query_autoload($class) { - $class = strtolower($class); + $class_segments = explode('\\', $class); + $class = strtolower(array_pop($class_segments)); // Load Firebird separately if (function_exists('fbird_connect') && $class === 'firebird') diff --git a/classes/abstract_driver.php b/classes/abstract_driver.php index a438b91..38d1e4f 100644 --- a/classes/abstract_driver.php +++ b/classes/abstract_driver.php @@ -13,6 +13,8 @@ // -------------------------------------------------------------------------- +namespace Query\Driver; + /** * Base Database class * @@ -21,7 +23,7 @@ * @package Query * @subpackage Drivers */ -abstract class Abstract_Driver extends PDO implements Driver_Interface { +abstract class Abstract_Driver extends \PDO implements Driver_Interface { /** * Reference to the last executed query @@ -70,7 +72,7 @@ abstract class Abstract_Driver extends PDO implements Driver_Interface { public function __construct($dsn, $username=NULL, $password=NULL, array $driver_options=array()) { // Set PDO to display errors as exceptions, and apply driver options - $driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; + $driver_options[\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION; parent::__construct($dsn, $username, $password, $driver_options); // Load the sql and util class for the driver @@ -100,7 +102,7 @@ abstract class Abstract_Driver extends PDO implements Driver_Interface { if( ! (is_array($data) || is_object($data))) { - throw new InvalidArgumentException("Invalid data argument"); + throw new \InvalidArgumentException("Invalid data argument"); } // Bind the parameters @@ -422,10 +424,10 @@ abstract class Abstract_Driver extends PDO implements Driver_Interface { $res = $this->query($sql); - $flag = ($filtered_index) ? PDO::FETCH_NUM : PDO::FETCH_ASSOC; + $flag = ($filtered_index) ? \PDO::FETCH_NUM : \PDO::FETCH_ASSOC; $all = $res->fetchAll($flag); - return ($filtered_index) ? db_filter($all, 0) : $all; + return ($filtered_index) ? \db_filter($all, 0) : $all; } // -------------------------------------------------------------------------- diff --git a/classes/abstract_sql.php b/classes/abstract_sql.php index f2f7ca0..62e9761 100644 --- a/classes/abstract_sql.php +++ b/classes/abstract_sql.php @@ -13,6 +13,8 @@ // -------------------------------------------------------------------------- +namespace Query\Driver; + /** * parent for database manipulation subclasses * diff --git a/classes/connection_manager.php b/classes/connection_manager.php index 95097b9..bc842d0 100644 --- a/classes/connection_manager.php +++ b/classes/connection_manager.php @@ -13,13 +13,17 @@ // -------------------------------------------------------------------------- +namespace Query; + +use \Query\Driver; + /** * Generic exception for bad drivers * * @package Query * @subpackage Core */ -class BadDBDriverException extends InvalidArgumentException {} +class BadDBDriverException extends \InvalidArgumentException {} // -------------------------------------------------------------------------- @@ -69,7 +73,7 @@ final class Connection_Manager { */ private function __wakeup() { - throw new DomainException("Can't unserialize singleton"); + throw new \DomainException("Can't unserialize singleton"); } // -------------------------------------------------------------------------- @@ -115,7 +119,7 @@ final class Connection_Manager { } else { - throw new InvalidArgumentException("The specified connection does not exist"); + throw new \InvalidArgumentException("The specified connection does not exist"); } } @@ -132,10 +136,12 @@ final class Connection_Manager { { list($dsn, $dbtype, $params, $options) = $this->parse_params($params); + $driver = "\\Query\\Driver\\{$dbtype}"; + // Create the database connection $db = ( ! empty($params->user)) - ? new $dbtype($dsn, $params->user, $params->pass, $options) - : new $dbtype($dsn, '', '', $options); + ? new $driver($dsn, $params->user, $params->pass, $options) + : new $driver($dsn, '', '', $options); // -------------------------------------------------------------------------- // Save connection @@ -178,12 +184,12 @@ final class Connection_Manager { // -------------------------------------------------------------------------- // Convert array to object - $params = new ArrayObject($params, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); + $params = new \ArrayObject($params, \ArrayObject::STD_PROP_LIST | \ArrayObject::ARRAY_AS_PROPS); $params->type = strtolower($params->type); $dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql'; // Make sure the class exists - if ( ! class_exists($dbtype)) + if ( ! class_exists("Query\\Driver\\$dbtype")) { throw new BadDBDriverException('Database driver does not exist, or is not supported'); } diff --git a/classes/db_util.php b/classes/db_util.php index bdcc7c1..670ebb5 100644 --- a/classes/db_util.php +++ b/classes/db_util.php @@ -13,6 +13,10 @@ // -------------------------------------------------------------------------- +namespace Query\Driver; + +use \Query; + /** * Abstract class defining database / table creation methods * diff --git a/classes/driver_interface.php b/classes/driver_interface.php index e5f10d3..98e5788 100644 --- a/classes/driver_interface.php +++ b/classes/driver_interface.php @@ -13,6 +13,8 @@ // -------------------------------------------------------------------------- +namespace Query\Driver; + /** * PDO Interface to implement for database drivers * diff --git a/classes/query_builder.php b/classes/query_builder.php index 8e1b866..5668a28 100644 --- a/classes/query_builder.php +++ b/classes/query_builder.php @@ -13,6 +13,8 @@ // -------------------------------------------------------------------------- +namespace Query; + /** * Convienience class for creating sql queries - also the class that * instantiates the specific db driver @@ -176,14 +178,14 @@ class Query_Builder implements Query_Builder_Interface { /** * Constructor * - * @param Abstract_driver $db + * @param \Query\Driver\Driver_Interface $db */ - public function __construct(Driver_Interface $db) + public function __construct(\Query\Driver\Driver_Interface $db) { $this->db = $db; // Instantiate the Query Parser - $this->parser = new Query_Parser(); + $this->parser = new Query_Parser($this); $this->queries['total_time'] = 0; @@ -810,19 +812,7 @@ class Query_Builder implements Query_Builder_Interface { $table = implode(' ', $table); // Parse out the join condition - $parts = $this->parser->parse_join($condition); - $count = count($parts['identifiers']); - - // Go through and quote the identifiers - for($i=0; $i <= $count; $i++) - { - if (in_array($parts['combined'][$i], $parts['identifiers']) && ! is_numeric($parts['combined'][$i])) - { - $parts['combined'][$i] = $this->db->quote_ident($parts['combined'][$i]); - } - } - - $parsed_condition = implode('', $parts['combined']); + $parsed_condition = $this->parser->compile_join($condition); $condition = $table . ' ON ' . $parsed_condition; $this->_append_map("\n" . strtoupper($type) . ' JOIN ', $condition, 'join'); @@ -1251,7 +1241,7 @@ class Query_Builder implements Query_Builder_Interface { 'group_string', 'limit', 'offset', - 'explain' + 'explain', ) as $var) { $this->$var = NULL; @@ -1297,19 +1287,14 @@ class Query_Builder implements Query_Builder_Interface { $start_time = microtime(TRUE); - if (empty($vals)) - { - $res = $this->db->query($sql); - } - else - { - $res = $this->db->prepare_execute($sql, $vals); - } + $res = (empty($vals)) + ? $this->db->query($sql) + : $this->db->prepare_execute($sql, $vals); $end_time = microtime(TRUE); $total_time = number_format($end_time - $start_time, 5); - + // Add this query to the list of executed queries $this->_append_query($vals, $sql, $total_time); // Reset class state for next query @@ -1335,7 +1320,7 @@ class Query_Builder implements Query_Builder_Interface { return call_user_func_array(array($this->db, $name), $params); } - throw new BadMethodCallException("Method does not exist"); + throw new \BadMethodCallException("Method does not exist"); } /** @@ -1349,13 +1334,13 @@ class Query_Builder implements Query_Builder_Interface { protected function _append_query($vals, $sql, $total_time) { $evals = (is_array($vals)) ? $vals : array(); + $esql = str_replace('?', "%s", $sql); // Quote string values foreach($evals as &$v) { $v = ( ! is_numeric($v)) ? htmlentities($this->db->quote($v), ENT_NOQUOTES, 'utf-8', FALSE) : $v; } - $esql = str_replace('?', "%s", $sql); // Add the query onto the array of values to pass // as arguments to sprintf diff --git a/classes/query_builder_interface.php b/classes/query_builder_interface.php index d6030e0..7217e3c 100644 --- a/classes/query_builder_interface.php +++ b/classes/query_builder_interface.php @@ -13,6 +13,8 @@ // -------------------------------------------------------------------------- +namespace Query; + /** * Interface defining the Query Builder class * diff --git a/classes/query_parser.php b/classes/query_parser.php index 4fffe6f..df087a9 100644 --- a/classes/query_parser.php +++ b/classes/query_parser.php @@ -13,6 +13,8 @@ // -------------------------------------------------------------------------- +namespace Query; + /** * Utility Class to parse sql clauses for properly escaping identifiers * @@ -21,6 +23,13 @@ */ class Query_Parser { + /** + * DB Driver + * + * @var \Query\Driver\Driver_Interface + */ + private $db; + /** * Regex patterns for various syntax components * @@ -51,6 +60,12 @@ class Query_Parser { */ public function __construct($sql = '') { + if (is_object($sql)) + { + $this->db = $sql; + $sql = ''; + } + // Get sql clause components preg_match_all('`'.$this->match_patterns['function'].'`', $sql, $this->matches['functions'], PREG_SET_ORDER); preg_match_all('`'.$this->match_patterns['identifier'].'`', $sql, $this->matches['identifiers'], PREG_SET_ORDER); @@ -71,7 +86,7 @@ class Query_Parser { * * @param string $sql */ - public function parse_join($sql) + protected function parse_join($sql) { $this->__construct($sql); return $this->matches; @@ -79,13 +94,36 @@ class Query_Parser { // -------------------------------------------------------------------------- + /** + * Compiles a join condition after parsing + * + * @param string $condition + * @return string + */ + public function compile_join($condition) + { + $parts = $this->parse_join($condition); + $count = count($parts['identifiers']); + + // Go through and quote the identifiers + for($i=0; $i <= $count; $i++) + { + if (in_array($parts['combined'][$i], $parts['identifiers']) && ! is_numeric($parts['combined'][$i])) + { + $parts['combined'][$i] = $this->db->quote_ident($parts['combined'][$i]); + } + } + + return implode('', $parts['combined']); + } + /** * Returns a more useful match array * * @param array $array * @return array */ - private function filter_array($array) + protected function filter_array($array) { $new_array = array(); diff --git a/classes/sql_interface.php b/classes/sql_interface.php index 61dc59a..54b3c64 100644 --- a/classes/sql_interface.php +++ b/classes/sql_interface.php @@ -13,6 +13,8 @@ // -------------------------------------------------------------------------- +namespace Query\Driver; + /** * parent for database manipulation subclasses * diff --git a/common.php b/common.php index 7958455..86d2efa 100644 --- a/common.php +++ b/common.php @@ -82,7 +82,7 @@ function db_filter($array, $index) */ function Query($params = '') { - $cmanager = Connection_Manager::get_instance(); + $cmanager = \Query\Connection_Manager::get_instance(); // If you are getting a previously created connection if (is_scalar($params)) diff --git a/docs/classes.svg b/docs/classes.svg deleted file mode 100644 index ccfcb1e..0000000 --- a/docs/classes.svg +++ /dev/null @@ -1,262 +0,0 @@ - - - - - diff --git a/docs/classes/BadDBDriverException.html b/docs/classes/Query.BadDBDriverException.html similarity index 90% rename from docs/classes/BadDBDriverException.html rename to docs/classes/Query.BadDBDriverException.html index ec1dc1d..b0ef339 100644 --- a/docs/classes/BadDBDriverException.html +++ b/docs/classes/Query.BadDBDriverException.html @@ -3,7 +3,7 @@ -