Refactor Query\Compiler.
This commit is contained in:
parent
00fcaf2be9
commit
0f8f3f347c
|
@ -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 @@ public static function select($query)
|
||||||
*/
|
*/
|
||||||
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 @@ public static function insert($query, $values)
|
||||||
$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 @@ public static function insert($query, $values)
|
||||||
*/
|
*/
|
||||||
public static function update($query, $values)
|
public static function update($query, $values)
|
||||||
{
|
{
|
||||||
// ----------------------------------------------------
|
$sql = 'UPDATE '.$query->wrap($query->table).' SET ';
|
||||||
// Initialize the UPDATE statement.
|
|
||||||
// ----------------------------------------------------
|
|
||||||
$sql = 'UPDATE '.$query->table.' SET ';
|
|
||||||
|
|
||||||
// ---------------------------------------------------
|
$sets = array();
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue