From 6ecfb40c87c0f1d1f1d5104e1a7a0be274ef3734 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 31 Oct 2012 20:21:15 +0000 Subject: [PATCH] More cleanup/ refactoring --- autoload.php | 51 +------------ classes/db_sql.php | 66 ----------------- classes/query_builder.php | 131 ++++++++++++++-------------------- common.php | 92 ++++++++++++++++++++++++ tests/db_files/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 5 files changed, 147 insertions(+), 193 deletions(-) create mode 100644 common.php diff --git a/autoload.php b/autoload.php index d8ef8e2..28d0d0b 100644 --- a/autoload.php +++ b/autoload.php @@ -27,34 +27,8 @@ define('QBASE_PATH', dirname(__FILE__).'/'); */ define('QDRIVER_PATH', QBASE_PATH.'drivers/'); -if ( ! function_exists('do_include')) -{ - /** - * Bulk directory loading workaround for use - * with array_map and glob - * - * @param string $path - * @return void - */ - function do_include($path) - { - require_once($path); - } -} - -if ( ! function_exists('mb_trim')) -{ - /** - * Multibyte-safe trim function - * - * @param string - * @return string - */ - function mb_trim($string) - { - return preg_replace("/(^\s+)|(\s+$)/us", "", $string); - } -} +// Require some common functions +require(QBASE_PATH.'common.php'); /** * Load a Query class @@ -92,25 +66,4 @@ function query_autoload($class) // Set up autoloader spl_autoload_register('query_autoload'); -// -------------------------------------------------------------------------- - -/** - * Filter out db rows into one array - * - * @param array $array - * @param mixed $index - * @return array - */ -function db_filter($array, $index) -{ - $new_array = array(); - - foreach($array as $a) - { - $new_array[] = $a[$index]; - } - - return $new_array; -} - // End of autoload.php \ No newline at end of file diff --git a/classes/db_sql.php b/classes/db_sql.php index 016b530..b41e046 100644 --- a/classes/db_sql.php +++ b/classes/db_sql.php @@ -21,72 +21,6 @@ */ abstract class DB_SQL { - // -------------------------------------------------------------------------- - // ! Methods to override - // -------------------------------------------------------------------------- - - /** - * Get the max keyword sql - * - * @return string - */ - public function max() - { - return ' MAX'; - } - - // -------------------------------------------------------------------------- - - /** - * Get the min keyword sql - * - * @return string - */ - public function min() - { - return ' MIN'; - } - - // -------------------------------------------------------------------------- - - /** - * Get the 'distinct' keyword - * - * @return string - */ - public function distinct() - { - return ' DISTINCT '; - } - - // -------------------------------------------------------------------------- - - /** - * Get the 'average' keyword - * - * @return string - */ - public function avg() - { - return ' AVG'; - } - - // -------------------------------------------------------------------------- - - /** - * Get the 'sum' keyword - * - * @return string - */ - public function sum() - { - return ' SUM'; - } - - // -------------------------------------------------------------------------- - // ! Abstract Methods - // -------------------------------------------------------------------------- - /** * Get database specific sql for limit clause * diff --git a/classes/query_builder.php b/classes/query_builder.php index f494a87..6e060e1 100644 --- a/classes/query_builder.php +++ b/classes/query_builder.php @@ -13,26 +13,6 @@ // -------------------------------------------------------------------------- -/** - * Generic exception for bad drivers - * - * @package Query - * @subpackage Query - */ -class BadDBDriverException extends InvalidArgumentException {} - -// -------------------------------------------------------------------------- - -/** - * Generic exception for bad connection strings - * - * @package Query - * @subpackage Query - */ -class BadConnectionException extends UnexpectedValueException {} - -// -------------------------------------------------------------------------- - /** * Convienience class for creating sql queries - also the class that * instantiates the specific db driver @@ -130,33 +110,59 @@ class Query_Builder { // Convert array to object if (is_array($params)) { - $p = new stdClass(); + $params = new ArrayObject($params, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); + } + + $params->type = strtolower($params->type); + $dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql'; - foreach($params as $k => $v) - { - $p->$k = $v; - } + // Generate dsn + $dsn = $this->_connect($dbtype, $params); - $params = $p; + try + { + // Create the database connection + $this->db = ( ! empty($params->user)) + ? new $dbtype($dsn, $params->user, $params->pass) + : new $dbtype($dsn); + } + catch(Exception $e) + { + throw new BadConnectionException('Connection failed, invalid arguments', 2); } + // Set the connection name property, if applicable + if (isset($params->name)) + { + $this->conn_name = $params->name; + } + + // Instantiate the Query Parser + $this->parser = new Query_Parser(); + + // Make things just slightly shorter + $this->sql =& $this->db->sql; + } + + /** + * Create the dsn for connection to the database + * + * @param string $dbtype + * @param object $params + * @return string + */ + private function _connect($dbtype, &$params) + { // Let the connection work with 'conn_db' or 'database' if (isset($params->database)) { $params->conn_db = $params->database; } - - $params->type = strtolower($params->type); - $dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql'; - - $dsn = ''; - // Add the driver type to the dsn - if ($dbtype !== 'firebird' && $dbtype !== 'sqlite') - { - $dsn = strtolower($dbtype).':'.$dsn; - } + $dsn = ($dbtype !== 'firebird' && $dbtype !== 'sqlite') + ? strtolower($dbtype).':' + : ''; // Make sure the class exists if ( ! class_exists($dbtype)) @@ -196,31 +202,8 @@ class Query_Builder { $dsn = "{$params->host}:{$params->file}"; break; } - - - try - { - // Create the database connection - $this->db = ( ! empty($params->user)) - ? new $dbtype($dsn, $params->user, $params->pass) - : new $dbtype($dsn); - } - catch(Exception $e) - { - throw new BadConnectionException('Connection failed, invalid arguments', 2); - } - - // Set the connection name property, if applicable - if (isset($params->name)) - { - $this->conn_name = $params->name; - } - - // Instantiate the Query Parser - $this->parser = new Query_Parser(); - - // Make things just slightly shorter - $this->sql =& $this->db->sql; + + return $dsn; } // -------------------------------------------------------------------------- @@ -303,7 +286,7 @@ class Query_Builder { public function select_max($field, $as=FALSE) { // Create the select string - $this->select_string .= $this->sql->max().$this->_select($field, $as); + $this->select_string .= ' MAX'.$this->_select($field, $as); return $this; } @@ -319,7 +302,7 @@ class Query_Builder { public function select_min($field, $as=FALSE) { // Create the select string - $this->select_string .= $this->sql->min().$this->_select($field, $as); + $this->select_string .= ' MIN'.$this->_select($field, $as); return $this; } @@ -335,7 +318,7 @@ class Query_Builder { public function select_avg($field, $as=FALSE) { // Create the select string - $this->select_string .= $this->sql->avg().$this->_select($field, $as); + $this->select_string .= ' AVG'.$this->_select($field, $as); return $this; } @@ -351,7 +334,7 @@ class Query_Builder { public function select_sum($field, $as=FALSE) { // Create the select string - $this->select_string .= $this->sql->sum().$this->_select($field, $as); + $this->select_string .= ' SUM'.$this->_select($field, $as); return $this; } @@ -365,7 +348,7 @@ class Query_Builder { public function distinct() { // Prepend the keyword to the select string - $this->select_string = $this->sql->distinct() . $this->select_string; + $this->select_string = ' DISTINCT '.$this->select_string; return $this; } @@ -807,10 +790,8 @@ class Query_Builder { { $table = implode(' ', array_map(array($this->db, 'quote_ident'), explode(' ', trim($table)))); - $parser = new query_parser(); - // Parse out the join condition - $parts = $parser->parse_join($condition); + $parts = $this->parser->parse_join($condition); $count = count($parts['identifiers']); // Go through and quote the identifiers @@ -1253,12 +1234,9 @@ class Query_Builder { */ public function reset_query() { - // Only unset class variables that - // are not callable. Otherwise, we'll - // delete class methods! foreach($this as $name => $var) { - $skip = array('db','sql','queries','table_prefix'); + $skip = array('db','sql','queries','table_prefix','parser','conn_name'); // Skip properties that are needed for every query if (in_array($name, $skip)) @@ -1267,10 +1245,7 @@ class Query_Builder { } // Nothing query-generation related is safe! - if ( ! is_callable($this->$name)) - { - $this->$name = NULL; - } + $this->$name = NULL; // Set empty arrays $this->values = array(); @@ -1412,7 +1387,7 @@ class Query_Builder { // Set the last query to get rowcounts properly $this->db->last_query = $sql; - // echo $sql . '
'; + //echo $sql . '
'; return $sql; } diff --git a/common.php b/common.php new file mode 100644 index 0000000..f54b3c2 --- /dev/null +++ b/common.php @@ -0,0 +1,92 @@ +47g7}3t_ocg#D#X@LfklstwxZf&VUXgz0agE3CcwBLdH1Aev zh{vD2N&3XA(t=l_zZ{)C-KGI*QTemCU(boA2R3VycYJeSO}Rwz7yIhm*(gn$(dJ7# z(bn2{vRI10-&e&HW6x|TvxS4s*@q{weuHN}I8ZN^Oa^wxfGFdV>}x-cLziTsn}4cs zS|t=+eFX77vA6Rq5l`PlZ27dhu^nCIYR6x-R52d=O;y~bQv6R#g=+}p5KD!W%o@q=Ski}Qynw}kAa;11Pi!9ubqSe6%_w`yy`{Qo>33#%QTruRXphjKx(c(Ff_rjyaXFR>15l*^A z*jtOANl2P;@kqq8QH!58;$PL`$96At?y6gXSC_>)TLEIvi&&lmmxcX?11@&Whh03@ z#Rb}x1$V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