From 8240b2ffd1ed81f5b147f86d8b6b34b64be70ee8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 6 Feb 2012 22:09:15 -0600 Subject: [PATCH] move shortcut method into grammar. --- laravel/database/connection.php | 35 +----------------- laravel/database/query/grammars/grammar.php | 39 +++++++++++++++++++-- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/laravel/database/connection.php b/laravel/database/connection.php index 6204aff8..b8504cca 100644 --- a/laravel/database/connection.php +++ b/laravel/database/connection.php @@ -206,7 +206,7 @@ protected function execute($sql, $bindings = array()) return ! $binding instanceof Expression; })); - $sql = $this->transform($sql, $bindings); + $sql = $this->grammar()->shortcut($sql, $bindings); $statement = $this->pdo->prepare($sql); @@ -229,39 +229,6 @@ protected function execute($sql, $bindings = array()) return array($statement, $result); } - /** - * Transform an SQL query into an executable query. - * - * @param string $sql - * @param array $bindings - * @return string - */ - protected function transform($sql, $bindings) - { - // Laravel provides an easy short-cut notation for writing raw - // WHERE IN statements. If (...) is in the query, it will be - // replaced with the correct number of parameters based on - // the bindings for the query. - if (strpos($sql, '(...)') !== false) - { - for ($i = 0; $i < count($bindings); $i++) - { - // If the binding is an array, we can assume it is being used - // to fill a "where in" condition, so we'll replace the next - // place-holder in the SQL query with the correct number of - // parameters based on the elements in the binding. - if (is_array($bindings[$i])) - { - $parameters = $this->grammar()->parameterize($bindings[$i]); - - $sql = preg_replace('~\(\.\.\.\)~', "({$parameters})", $sql, 1); - } - } - } - - return trim($sql); - } - /** * Log the query and fire the core query event. * diff --git a/laravel/database/query/grammars/grammar.php b/laravel/database/query/grammars/grammar.php index dc957071..d8ced03b 100644 --- a/laravel/database/query/grammars/grammar.php +++ b/laravel/database/query/grammars/grammar.php @@ -75,9 +75,10 @@ final protected function concatenate($components) */ protected function selects(Query $query) { - // Sometimes developers may set a "select" clause on the same query that - // is performing in aggregate look-up, such as during pagination. So we - // will not generate the select clause if an aggregate is present. + // Sometimes developers may set a "select" clause on the same query + // that is performing in aggregate look-up, like during pagination. + // So we will not generate the select clause if an aggregate is + // present so the aggregates work. if ( ! is_null($query->aggregate)) return; $select = ($query->distinct) ? 'SELECT DISTINCT ' : 'SELECT '; @@ -388,4 +389,36 @@ public function delete(Query $query) return trim("DELETE FROM {$table} ".$this->wheres($query)); } + /** + * Transform an SQL short-cuts into real SQL for PDO. + * + * @param string $sql + * @param array $bindings + * @return string + */ + public function shortcut($sql, $bindings) + { + // Laravel provides an easy short-cut notation for writing raw + // WHERE IN statements. If (...) is in the query, it will be + // replaced with the correct number of parameters based on + // the bindings for the query. + if (strpos($sql, '(...)') !== false) + { + for ($i = 0; $i < count($bindings); $i++) + { + // If the binding is an array, we can just assume it's + // used to fill a "where in" condition, so we'll just + // replace the next place-holder in the query. + if (is_array($bindings[$i])) + { + $parameters = $this->parameterize($bindings[$i]); + + $sql = preg_replace('~\(\.\.\.\)~', "({$parameters})", $sql, 1); + } + } + } + + return trim($sql); + } + } \ No newline at end of file