Commit 684bbebb authored by Taylor Otwell's avatar Taylor Otwell

refactoring the database layer.

parent a940fc48
......@@ -11,7 +11,7 @@ return array(
|
*/
'url' => 'http://localhost/laravel/public',
'url' => 'http://localhost',
/*
|--------------------------------------------------------------------------
......
......@@ -10,80 +10,24 @@ class Compiler {
*/
public function select(Query $query)
{
foreach (array('add_select', 'add_from', 'add_where', 'add_order', 'add_limit', 'add_offset') as $builder)
$sql = $query->select.' '.$query->from.' '.$query->where;
if ( ! is_null($query->order))
{
$sql[] = $this->$builder($query);
$sql .= ' '.$query->order;
}
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);
}
if ( ! is_null($query->limit))
{
$sql .= ' '.$query->limit;
}
/**
* 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;
}
if ( ! is_null($query->offset))
{
$sql .= ' '.$query->offset;
}
/**
* 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;
return $sql;
}
/**
......
......@@ -55,6 +55,13 @@ class Query {
*/
public $where = 'WHERE 1 = 1';
/**
* The ORDER BY clause.
*
* @var string
*/
public $order;
/**
* The ORDER BY columns.
*
......@@ -417,6 +424,9 @@ class Query {
public function order_by($column, $direction = 'asc')
{
$this->orderings[] = $this->wrap($column).' '.strtoupper($direction);
$this->order = 'ORDER BY '.implode(', ', $this->orderings);
return $this;
}
......@@ -428,7 +438,7 @@ class Query {
*/
public function skip($value)
{
$this->offset = $value;
$this->offset = 'OFFSET '.$value;
return $this;
}
......@@ -440,7 +450,7 @@ class Query {
*/
public function take($value)
{
$this->limit = $value;
$this->limit = 'LIMIT '.$value;
return $this;
}
......
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