From be46ca3f9034bbda0dce218b24faf0957e33cab1 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 12 Mar 2012 16:17:30 -0400 Subject: [PATCH] Reorganize query_builder method order --- sys/db/query_builder.php | 250 +++++++++++++++++----------------- tests/test_dbs/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 2 files changed, 127 insertions(+), 123 deletions(-) diff --git a/sys/db/query_builder.php b/sys/db/query_builder.php index 05f95b2..a74fcaf 100644 --- a/sys/db/query_builder.php +++ b/sys/db/query_builder.php @@ -65,89 +65,9 @@ class Query_Builder { // Make things just slightly shorter $this->sql =& $this->db->sql; } - - // -------------------------------------------------------------------------- - - /** - * Select and retrieve all records from the current table, and/or - * execute current compiled query - * - * @param $table - * @param int $limit - * @param int $offset - * @return object - */ - public function get($table='', $limit=FALSE, $offset=FALSE) - { - // Set the table - if ( ! empty($table)) - { - $this->from_string = $this->db->quote_ident($table); - } - - // Set the limit, if it exists - if ($limit !== FALSE) - { - $this->limit($limit, $offset); - } - - $sql = $this->_compile('select'); - - // Do prepared statements for anything involving a "where" clause - if ( ! empty($this->where_string)) - { - $result = $this->db->prepare_execute($sql, array_values($this->where_array)); - } - else - { - // Otherwise, a simple query will do. - $result = $this->db->query($sql); - } - - // Reset for next query - $this->_reset(); - - return $result; - } - - // -------------------------------------------------------------------------- - - /** - * Sets values for inserts / updates / deletes - * - * @param mixed $key - * @param mixed $val - * @return $this - */ - public function set($key, $val) - { - // Plain key, value pair - if (is_scalar($key) && is_scalar($val)) - { - $this->set_array[$key] = $val; - } - // Object or array - elseif ( ! is_scalar($key)) - { - foreach($key as $k => $v) - { - $this->set_array[$k] = $v; - } - } - - // Use the keys of the array to make the insert/update string - $fields = array_keys($this->set_array); - - // Escape the field names - $fields = array_map(array($this, 'quote_ident'), $fields); - - // Generate the "set" string - $this->set_string = implode('=?, ', $fields); - $this->set_string .= '=?'; - - return $this; - } + // -------------------------------------------------------------------------- + // ! Select Queries // -------------------------------------------------------------------------- /** @@ -195,6 +115,45 @@ class Query_Builder { // -------------------------------------------------------------------------- + /** + * Specify the database table to select from + * + * @param string $dbname + * @return $this + */ + public function from($dbname) + { + // Split identifiers on spaces + $ident_array = explode(' ', trim($dbname)); + $ident_array = array_map('trim', $ident_array); + + // Quote the identifiers + $ident_array = array_map(array($this->db, 'quote_ident'), $ident_array); + + // Paste it back together + $this->from_string = implode(' ', $ident_array); + + return $this; + } + + // -------------------------------------------------------------------------- + + /** + * Creates a Like clause in the sql statement + * + * @param string $field + * @param mixed $val + * @param string $pos + * @return $this + */ + public function like($field, $val, $pos='both') + { + // @todo implement like method + return $this; + } + + // -------------------------------------------------------------------------- + /** * Specify condition(s) in the where clause of a query * Note: this function works with key / value, or a @@ -324,10 +283,10 @@ class Query_Builder { */ public function or_where_not_in($field, $val) { - // @tood Implement or_where_not_in method + // @todo Implement or_where_not_in method return $this; } - + // -------------------------------------------------------------------------- /** @@ -346,45 +305,6 @@ class Query_Builder { // -------------------------------------------------------------------------- - /** - * Specify the database table to select from - * - * @param string $dbname - * @return $this - */ - public function from($dbname) - { - // Split identifiers on spaces - $ident_array = explode(' ', trim($dbname)); - $ident_array = array_map('trim', $ident_array); - - // Quote the identifiers - $ident_array = array_map(array($this->db, 'quote_ident'), $ident_array); - - // Paste it back together - $this->from_string = implode(' ', $ident_array); - - return $this; - } - - // -------------------------------------------------------------------------- - - /** - * Creates a Like clause in the sql statement - * - * @param string $field - * @param mixed $val - * @param string $pos - * @return $this - */ - public function like($field, $val, $pos='both') - { - // @todo implement like method - return $this; - } - - // -------------------------------------------------------------------------- - /** * Set a limit on the current sql statement * @@ -399,6 +319,50 @@ class Query_Builder { return $this; } + + // -------------------------------------------------------------------------- + + /** + * Select and retrieve all records from the current table, and/or + * execute current compiled query + * + * @param $table + * @param int $limit + * @param int $offset + * @return object + */ + public function get($table='', $limit=FALSE, $offset=FALSE) + { + // Set the table + if ( ! empty($table)) + { + $this->from_string = $this->db->quote_ident($table); + } + + // Set the limit, if it exists + if ($limit !== FALSE) + { + $this->limit($limit, $offset); + } + + $sql = $this->_compile('select'); + + // Do prepared statements for anything involving a "where" clause + if ( ! empty($this->where_string)) + { + $result = $this->db->prepare_execute($sql, array_values($this->where_array)); + } + else + { + // Otherwise, a simple query will do. + $result = $this->db->query($sql); + } + + // Reset for next query + $this->_reset(); + + return $result; + } // -------------------------------------------------------------------------- @@ -414,7 +378,47 @@ class Query_Builder { // @todo implement order_by method return $this; } + + // -------------------------------------------------------------------------- + // ! Insert/Update/Delete Queries + // -------------------------------------------------------------------------- + /** + * Sets values for inserts / updates / deletes + * + * @param mixed $key + * @param mixed $val + * @return $this + */ + public function set($key, $val) + { + // Plain key, value pair + if (is_scalar($key) && is_scalar($val)) + { + $this->set_array[$key] = $val; + } + // Object or array + elseif ( ! is_scalar($key)) + { + foreach($key as $k => $v) + { + $this->set_array[$k] = $v; + } + } + + // Use the keys of the array to make the insert/update string + $fields = array_keys($this->set_array); + + // Escape the field names + $fields = array_map(array($this, 'quote_ident'), $fields); + + // Generate the "set" string + $this->set_string = implode('=?, ', $fields); + $this->set_string .= '=?'; + + return $this; + } + // -------------------------------------------------------------------------- /** diff --git a/tests/test_dbs/FB_TEST_DB.FDB b/tests/test_dbs/FB_TEST_DB.FDB index caa35b0ffed85c07933e4ed068d0f413b8653295..eddeef569d897a8c40f877152765c0db52f50aa5 100755 GIT binary patch delta 374 zcmYjMyGjF55S@FotJ%BbW{{9dVZ+KMaV@eC5)u@xqLm;K6|3}$y^vzDxL67nw@Bj; zC_W%56@S3gR_YfhSXhaZhv*D*4u>Z)okuz)%+yK9NsKh@XCVtU;M0`X>STSJ{o2Cefz0*Yg%XZ zDxC7n!&}SWcL3h>#0I7T@aL_{ch$9zK7TjV!;c7AtVWx-p~fD@EL0aAJX{W{1YUiF bY8;`jc#PYs@NixAV%$-6A6Lsf#;cWIObt5l