Commit 0f8f3f34 authored by Taylor Otwell's avatar Taylor Otwell

Refactor Query\Compiler.

parent 00fcaf2b
...@@ -10,30 +10,18 @@ class Compiler { ...@@ -10,30 +10,18 @@ class Compiler {
*/ */
public static function select($query) public static function select($query)
{ {
// ----------------------------------------------------
// Add the SELECT, FROM, and WHERE clause to the query.
// ----------------------------------------------------
$sql = $query->select.' '.$query->from.' '.$query->where; $sql = $query->select.' '.$query->from.' '.$query->where;
// ----------------------------------------------------
// Add the ORDER BY clause to the query.
// ----------------------------------------------------
if (count($query->orderings) > 0) if (count($query->orderings) > 0)
{ {
$sql .= ' ORDER BY '.implode(', ', $query->orderings); $sql .= ' ORDER BY '.implode(', ', $query->orderings);
} }
// ----------------------------------------------------
// Add the LIMIT clause to the query.
// ----------------------------------------------------
if ( ! is_null($query->limit)) if ( ! is_null($query->limit))
{ {
$sql .= ' LIMIT '.$query->limit; $sql .= ' LIMIT '.$query->limit;
} }
// ----------------------------------------------------
// Add the OFFSET clause to the query.
// ----------------------------------------------------
if ( ! is_null($query->offset)) if ( ! is_null($query->offset))
{ {
$sql .= ' OFFSET '.$query->offset; $sql .= ' OFFSET '.$query->offset;
...@@ -51,14 +39,8 @@ class Compiler { ...@@ -51,14 +39,8 @@ class Compiler {
*/ */
public static function insert($query, $values) public static function insert($query, $values)
{ {
// ----------------------------------------------------- $sql = 'INSERT INTO '.$query->wrap($query->table);
// Begin the INSERT statement.
// -----------------------------------------------------
$sql = 'INSERT INTO '.$query->table.' (';
// ----------------------------------------------------
// Wrap each column name in keyword identifiers.
// ----------------------------------------------------
$columns = array(); $columns = array();
foreach (array_keys($values) as $column) foreach (array_keys($values) as $column)
...@@ -66,10 +48,7 @@ class Compiler { ...@@ -66,10 +48,7 @@ class Compiler {
$columns[] = $query->wrap($column); $columns[] = $query->wrap($column);
} }
// ----------------------------------------------------- return $sql .= ' ('.implode(', ', $columns).') VALUES ('.$query->parameterize($values).')';
// Concatenante the columns and values to the statement.
// -----------------------------------------------------
return $sql .= implode(', ', $columns).') VALUES ('.$query->parameterize($values).')';
} }
/** /**
...@@ -81,25 +60,16 @@ class Compiler { ...@@ -81,25 +60,16 @@ class Compiler {
*/ */
public static function update($query, $values) public static function update($query, $values)
{ {
// ---------------------------------------------------- $sql = 'UPDATE '.$query->wrap($query->table).' SET ';
// Initialize the UPDATE statement.
// ---------------------------------------------------- $sets = array();
$sql = 'UPDATE '.$query->table.' SET ';
// ---------------------------------------------------
// Add each column set the statement.
// ---------------------------------------------------
$columns = array();
foreach (array_keys($values) as $column) foreach (array_keys($values) as $column)
{ {
$columns[] = $query->wrap($column).' = ?'; $sets[] = $query->wrap($column).' = ?';
} }
// --------------------------------------------------- return $sql .= implode(', ', $sets).' '.$query->where;
// Concatenate the SETs to the statement.
// ---------------------------------------------------
return $sql .= implode(', ', $columns).' '.$query->where;
} }
/** /**
......
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