From 79ca1ee071295039e89ec35d1dfc73e7c3df215e Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 14 Mar 2012 13:49:28 -0400 Subject: [PATCH] Implement all the rest of the where* and like* methods in query_builder, as well as query grouping methods --- sys/db/query_builder.php | 253 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 250 insertions(+), 3 deletions(-) diff --git a/sys/db/query_builder.php b/sys/db/query_builder.php index deda3da..7f7660b 100644 --- a/sys/db/query_builder.php +++ b/sys/db/query_builder.php @@ -206,6 +206,132 @@ class Query_Builder { // -------------------------------------------------------------------------- + /** + * Generates an OR Like clause + * + * @param string $field + * @param mixed $val + * @param string $pos + * @return $this + */ + public function or_like($field, $val, $pos='both') + { + $field = $this->db->quote_ident($field); + + // Add the like string into the order map + $l = $field. ' LIKE ?'; + + if ($pos == 'before') + { + $val = "%{$val}"; + } + elseif ($pos == 'after') + { + $val = "{$val}%"; + } + else + { + $val = "%{$val}%"; + } + + $this->query_map[] = array( + 'type' => 'like', + 'conjunction' => (empty($this->query_map)) ? 'WHERE ' : ' OR ', + 'string' => $l + ); + + // Add to the values array + $this->values[] = $val; + + return $this; + } + + // -------------------------------------------------------------------------- + + /** + * Generates a NOT LIKE clause + * + * @param string $field + * @param mixed $val + * @param string $pos + * @return $this + */ + public function not_like($field, $val, $pos='both') + { + $field = $this->db->quote_ident($field); + + // Add the like string into the order map + $l = $field. ' NOT LIKE ?'; + + if ($pos == 'before') + { + $val = "%{$val}"; + } + elseif ($pos == 'after') + { + $val = "{$val}%"; + } + else + { + $val = "%{$val}%"; + } + + $this->query_map[] = array( + 'type' => 'like', + 'conjunction' => (empty($this->query_map)) ? ' WHERE ' : ' AND ', + 'string' => $l + ); + + // Add to the values array + $this->values[] = $val; + + return $this; + } + + // -------------------------------------------------------------------------- + + /** + * Generates a OR NOT LIKE clause + * + * @param string $field + * @param mixed $val + * @param string $pos + * @return $this; + */ + public function or_not_like($field, $val, $pos='both') + { + $field = $this->db->quote_ident($field); + + // Add the like string into the order map + $l = $field. ' NOT LIKE ?'; + + if ($pos == 'before') + { + $val = "%{$val}"; + } + elseif ($pos == 'after') + { + $val = "{$val}%"; + } + else + { + $val = "%{$val}%"; + } + + $this->query_map[] = array( + 'type' => 'like', + 'conjunction' => (empty($this->query_map)) ? ' WHERE ' : ' OR ', + 'string' => $l + ); + + // Add to the values array + $this->values[] = $val; + + return $this; + } + + // -------------------------------------------------------------------------- + /** * Do all the repeditive stuff for where type methods * @@ -356,7 +482,22 @@ class Query_Builder { */ public function or_where_in($field, $val=array()) { - // @todo Implement or_where_in method + $field = $this->db->quote_ident($field); + $params = array_fill(0, count($val), '?'); + + foreach($val as $v) + { + $this->values[] = $v; + } + + $string = $field.' IN ('.implode(',', $params).') '; + + $this->query_map[] = array( + 'type' => 'where_in', + 'conjunction' => ( ! empty($this->query_map)) ? ' OR ' : ' WHERE ', + 'string' => $string + ); + return $this; } @@ -371,7 +512,22 @@ class Query_Builder { */ public function where_not_in($field, $val=array()) { - // @todo Implement where_not_in method + $field = $this->db->quote_ident($field); + $params = array_fill(0, count($val), '?'); + + foreach($val as $v) + { + $this->values[] = $v; + } + + $string = $field.' NOT IN ('.implode(',', $params).') '; + + $this->query_map[] = array( + 'type' => 'where_in', + 'conjunction' => ( ! empty($this->query_map)) ? ' AND ' : ' WHERE ', + 'string' => $string + ); + return $this; } @@ -386,7 +542,22 @@ class Query_Builder { */ public function or_where_not_in($field, $val=array()) { - // @todo Implement or_where_not_in method + $field = $this->db->quote_ident($field); + $params = array_fill(0, count($val), '?'); + + foreach($val as $v) + { + $this->values[] = $v; + } + + $string = $field.' NOT IN ('.implode(',', $params).') '; + + $this->query_map[] = array( + 'type' => 'where_in', + 'conjunction' => ( ! empty($this->query_map)) ? ' OR ' : ' WHERE ', + 'string' => $string + ); + return $this; } @@ -648,6 +819,82 @@ class Query_Builder { // Delete the table rows, and return the result return $res; } + + // -------------------------------------------------------------------------- + // ! Query Grouping Methods + // -------------------------------------------------------------------------- + + /** + * Adds a paren to the current query for query grouping + * + * @return $this + */ + public function group_start() + { + $this->query_map[] = array( + 'type' => 'group_start', + 'conjunction' => '', + 'string' => ' (' + ); + + return $this; + } + + // -------------------------------------------------------------------------- + + /** + * Adds a paren to the current query for query grouping, + * prefixed with 'OR' + * + * @return $this + */ + public function or_group_start() + { + $this->query_map[] = array( + 'type' => 'group_start', + 'conjunction' => '', + 'string' => ' OR (' + ); + + return $this; + } + + // -------------------------------------------------------------------------- + + /** + * Adds a paren to the current query for query grouping, + * prefixed with 'OR NOT' + * + * @return $this + */ + public function or_not_group_start() + { + $this->query_map[] = array( + 'type' => 'group_start', + 'conjunction' => '', + 'string' => ' OR NOT (' + ); + + return $this; + } + + // -------------------------------------------------------------------------- + + /** + * Ends a query group + * + * @return $this + */ + public function group_end() + { + $this->query_map[] = array( + 'type' => 'group_end', + 'conjunction' => '', + 'string' => ' ) ' + ); + + return $this; + } // -------------------------------------------------------------------------- // ! Miscellaneous Methods