From 8f4bcd4675eaf51a48cd8a49baf8e7dea2d4a2da Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 19 Aug 2011 22:09:17 -0500 Subject: [PATCH] continued refactoring database layer. --- laravel/db/compiler.php | 135 +++++++++++++++++++++++++++++++++++ laravel/db/query.php | 82 ++++++--------------- laravel/db/query/factory.php | 7 +- 3 files changed, 161 insertions(+), 63 deletions(-) create mode 100644 laravel/db/compiler.php diff --git a/laravel/db/compiler.php b/laravel/db/compiler.php new file mode 100644 index 00000000..32c8f78f --- /dev/null +++ b/laravel/db/compiler.php @@ -0,0 +1,135 @@ +$builder($query); + } + + foreach ($sql as $key => $value) { if (is_null($value) or $value === '') unset($sql[$key]); } + + return implode(' ', $sql); + } + + /** + * Get the SELECT clause from the Query instance. + * + * @param Query $query + * @return string + */ + protected function add_select(Query $query) + { + return $query->select; + } + + /** + * Get the FROM clause from the Query instance. + * + * @param Query $query + * @return string + */ + protected function add_from(Query $query) + { + return $query->from; + } + + /** + * Get the WHERE clause from the Query instance. + * + * @param Query $query + * @return string + */ + protected function add_where(Query $query) + { + return $query->where; + } + + /** + * Get the ORDER BY clause from the Query instance. + * + * @param Query $query + * @return string + */ + protected function add_order(Query $query) + { + if (count($query->orderings) > 0) return 'ORDER BY'.implode(', ', $query->orderings); + } + + /** + * Get the LIMIT clause from the Query instance. + * + * @param Query $query + * @return string + */ + protected function add_limit(Query $query) + { + if ( ! is_null($query->limit)) return 'LIMIT '.$query->limit; + } + + /** + * Get the OFFSET clause from the Query instance. + * + * @param Query $query + * @return string + */ + protected function add_offset(Query $query) + { + if ( ! is_null($query->offset)) return 'OFFSET '.$query->offset; + } + + /** + * Compile a SQL INSERT statment from a Query instance. + * + * @param Query $query + * @param array $values + * @return string + */ + public function insert(Query $query, $values) + { + $sql = 'INSERT INTO '.$query->wrap($query->table); + + $columns = array_map(array($query, 'wrap'), array_keys($values)); + + return $sql .= ' ('.implode(', ', $columns).') VALUES ('.$query->parameterize($values).')'; + } + + /** + * Compile a SQL UPDATE statment from a Query instance. + * + * @param Query $query + * @param array $values + * @return string + */ + public function update(Query $query, $values) + { + $sql = 'UPDATE '.$query->wrap($query->table).' SET '; + + foreach (array_keys($values) as $column) + { + $sets[] = $query->wrap($column).' = ?'; + } + + return $sql.implode(', ', $sets).' '.$query->where; + } + + /** + * Compile a SQL DELETE statment from a Query instance. + * + * @param Query $query + * @return string + */ + public function delete(Query $query) + { + return 'DELETE FROM '.$query->wrap($query->table).' '.$query->where; + } + +} \ No newline at end of file diff --git a/laravel/db/query.php b/laravel/db/query.php index e56bebc2..bab85cd2 100644 --- a/laravel/db/query.php +++ b/laravel/db/query.php @@ -13,6 +13,13 @@ class Query { */ public $connection; + /** + * The query compiler instance. + * + * @var Compiler + */ + public $compiler; + /** * The SELECT clause. * @@ -83,9 +90,10 @@ class Query { * @param Connection $connection * @return void */ - public function __construct($table, Connection $connection) + public function __construct($table, Connection $connection, Compiler $compiler) { $this->table = $table; + $this->compiler = $compiler; $this->connection = $connection; $this->from = 'FROM '.$this->wrap($table); } @@ -471,7 +479,7 @@ private function aggregate($aggregator, $column) { $this->select = 'SELECT '.$aggregator.'('.$this->wrap($column).') AS '.$this->wrap('aggregate'); - $result = $this->connection->scalar($this->compile_select(), $this->bindings); + $result = $this->connection->scalar($this->compiler->select($this), $this->bindings); // Reset the SELECT clause so more queries can be performed using the same instance. // This is helpful for getting aggregates and then getting actual results. @@ -518,7 +526,7 @@ public function get($columns = array('*')) $this->select($columns); } - $results = $this->connection->query($this->compile_select(), $this->bindings); + $results = $this->connection->query($this->compiler->select($this), $this->bindings); // Reset the SELECT clause so more queries can be performed using the same instance. // This is helpful for getting aggregates and then getting actual results. @@ -527,33 +535,6 @@ public function get($columns = array('*')) return $results; } - /** - * Compile the query into a SQL SELECT statement. - * - * @return string - */ - private function compile_select() - { - $sql = $this->select.' '.$this->from.' '.$this->where; - - if (count($this->orderings) > 0) - { - $sql .= ' ORDER BY '.implode(', ', $this->orderings); - } - - if ( ! is_null($this->limit)) - { - $sql .= ' LIMIT '.$this->limit; - } - - if ( ! is_null($this->offset)) - { - $sql .= ' OFFSET '.$this->offset; - } - - return $sql; - } - /** * Execute an INSERT statement. * @@ -562,7 +543,7 @@ private function compile_select() */ public function insert($values) { - return $this->connection->query($this->compile_insert($values), array_values($values)); + return $this->connection->query($this->compiler->insert($this, $values), array_values($values)); } /** @@ -573,26 +554,11 @@ public function insert($values) */ public function insert_get_id($values) { - $this->connection->query($this->compile_insert($values), array_values($values)); + $this->connection->query($this->compiler->insert($this, $values), array_values($values)); return $this->connection->pdo->lastInsertId(); } - /** - * Compile the query into a SQL INSERT statement. - * - * @param array $values - * @return string - */ - private function compile_insert($values) - { - $sql = 'INSERT INTO '.$this->wrap($this->table); - - $columns = array_map(array($this, 'wrap'), array_keys($values)); - - return $sql .= ' ('.implode(', ', $columns).') VALUES ('.$this->parameterize($values).')'; - } - /** * Execute the query as an UPDATE statement. * @@ -601,14 +567,7 @@ private function compile_insert($values) */ public function update($values) { - $sql = 'UPDATE '.$this->wrap($this->table).' SET '; - - foreach (array_keys($values) as $column) - { - $sets[] = $this->wrap($column).' = ?'; - } - - return $this->connection->query($sql.implode(', ', $sets).' '.$this->where, array_merge(array_values($values), $this->bindings)); + return $this->connection->query($this->compiler->update($this, $values), array_merge(array_values($values), $this->bindings)); } /** @@ -621,7 +580,7 @@ public function delete($id = null) { if ( ! is_null($id)) $this->where('id', '=', $id); - return $this->connection->query('DELETE FROM '.$this->wrap($this->table).' '.$this->where, $this->bindings); + return $this->connection->query($this->compiler->delete($this), $this->bindings); } /** @@ -630,7 +589,7 @@ public function delete($id = null) * @param string $value * @return string */ - private function wrap($value) + public function wrap($value) { if (strpos(strtolower($value), ' as ') !== false) return $this->wrap_alias($value); @@ -648,7 +607,7 @@ private function wrap($value) * @param string $value * @return string */ - private function wrap_alias($value) + protected function wrap_alias($value) { $segments = explode(' ', $value); @@ -662,7 +621,10 @@ private function wrap_alias($value) * * @return string */ - protected function wrapper() { return '"'; } + protected function wrapper() + { + return '"'; + } /** * Create query parameters from an array of values. @@ -670,7 +632,7 @@ protected function wrapper() { return '"'; } * @param array $values * @return string */ - private function parameterize($values) + public function parameterize($values) { return implode(', ', array_fill(0, count($values), '?')); } diff --git a/laravel/db/query/factory.php b/laravel/db/query/factory.php index e474b22a..77aec41b 100644 --- a/laravel/db/query/factory.php +++ b/laravel/db/query/factory.php @@ -1,6 +1,7 @@