Commit 8f4bcd46 authored by Taylor Otwell's avatar Taylor Otwell

continued refactoring database layer.

parent fb1acc31
<?php namespace Laravel\DB;
class Compiler {
/**
* Compile a SQL SELECT statment from a Query instance.
*
* @param Query $query
* @return string
*/
public function select(Query $query)
{
foreach (array('add_select', 'add_from', 'add_where', 'add_order', 'add_limit', 'add_offset') as $builder)
{
$sql[] = $this->$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
......@@ -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 @@ class Query {
{
$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 @@ class Query {
$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 @@ class Query {
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 @@ class Query {
*/
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 @@ class Query {
*/
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 @@ class Query {
*/
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 @@ class Query {
{
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 @@ class Query {
* @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 @@ class Query {
* @param string $value
* @return string
*/
private function wrap_alias($value)
protected function wrap_alias($value)
{
$segments = explode(' ', $value);
......@@ -662,7 +621,10 @@ class Query {
*
* @return string
*/
protected function wrapper() { return '"'; }
protected function wrapper()
{
return '"';
}
/**
* Create query parameters from an array of values.
......@@ -670,7 +632,7 @@ class Query {
* @param array $values
* @return string
*/
private function parameterize($values)
public function parameterize($values)
{
return implode(', ', array_fill(0, count($values), '?'));
}
......
<?php namespace Laravel\DB\Query;
use Laravel\DB\Query;
use Laravel\DB\Compiler;
use Laravel\DB\Connection;
class Factory {
......@@ -19,13 +20,13 @@ class Factory {
switch ($query)
{
case 'pgsql':
return new Postgres($table, $connection);
return new Postgres($table, $connection, new Compiler);
case 'mysql':
return new MySQL($table, $connection);
return new MySQL($table, $connection, new Compiler);
default:
return new Query($table, $connection);
return new Query($table, $connection, new Compiler);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment