From 5f4838726e9629d45e7ed6d17169f6fa8e341de2 Mon Sep 17 00:00:00 2001 From: Stan Bondi Date: Wed, 17 Oct 2012 23:36:18 +0200 Subject: [PATCH] Ref #649 - Added query builder support for BETWEEN clauses Signed-off-by: Stan Bondi --- laravel/database/query.php | 61 +++++++++++++++++++++ laravel/database/query/grammars/grammar.php | 27 +++++++++ 2 files changed, 88 insertions(+) mode change 100644 => 100755 laravel/database/query.php mode change 100644 => 100755 laravel/database/query/grammars/grammar.php diff --git a/laravel/database/query.php b/laravel/database/query.php old mode 100644 new mode 100755 index fa6a4c8c..0b2029c1 --- a/laravel/database/query.php +++ b/laravel/database/query.php @@ -342,6 +342,67 @@ public function or_where_not_in($column, $values) { return $this->where_not_in($column, $values, 'OR'); } + + /** + * Add a BETWEEN condition to the query + * + * @param string $column + * @param mixed $min + * @param mixed $max + * @param string $connector + * @param boolean $not + * @return Query + */ + public function where_between($column, $min, $max, $connector = 'AND', $not = false) + { + $type = ($not) ? 'where_not_between' : 'where_between'; + + $this->wheres[] = compact('type', 'column', 'min', 'max', 'connector'); + + $this->bindings[] = $min; + $this->bindings[] = $max; + + return $this; + } + + /** + * Add a OR BETWEEN condition to the query + * + * @param string $column + * @param mixed $min + * @param mixed $max + * @return Query + */ + public function or_where_between($column, $min, $max) + { + return $this->where_between($column, $min, $max, 'OR'); + } + + /** + * Add a NOT BETWEEN condition to the query + * + * @param string $column + * @param mixed $min + * @param mixed $max + * @return Query + */ + public function where_not_between($column, $min, $max, $connector = 'AND') + { + return $this->where_between($column, $min, $max, $connector, true); + } + + /** + * Add a OR NOT BETWEEN condition to the query + * + * @param string $column + * @param mixed $min + * @param mixed $max + * @return Query + */ + public function or_where_not_between($column, $min, $max) + { + return $this->where_not_between($column, $min, $max, 'OR'); + } /** * Add a where null condition to the query. diff --git a/laravel/database/query/grammars/grammar.php b/laravel/database/query/grammars/grammar.php old mode 100644 new mode 100755 index 3e828cff..d5d8a2e7 --- a/laravel/database/query/grammars/grammar.php +++ b/laravel/database/query/grammars/grammar.php @@ -242,6 +242,33 @@ protected function where_not_in($where) return $this->wrap($where['column']).' NOT IN ('.$parameters.')'; } + /** + * Compile a WHERE BETWEEN clause + * + * @param array $where + * @return string + */ + protected function where_between($where) + { + $min = $this->parameter($where['min']); + $max = $this->parameter($where['max']); + + return $this->wrap($where['column']).' BETWEEN '.$min.' AND '.$max; + } + + /** + * Compile a WHERE NOT BETWEEN clause + * @param array $where + * @return string + */ + protected function where_not_between($where) + { + $min = $this->parameter($where['min']); + $max = $this->parameter($where['max']); + + return $this->wrap($where['column']).' NOT BETWEEN '.$min.' AND '.$max; + } + /** * Compile a WHERE NULL clause. *