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

refactoring the database layer.

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