refactoring the database layer.

This commit is contained in:
Taylor Otwell 2011-08-21 11:50:53 -05:00
parent a940fc4867
commit 684bbebb5c
3 changed files with 26 additions and 72 deletions

View File

@ -11,7 +11,7 @@
| |
*/ */
'url' => 'http://localhost/laravel/public', 'url' => 'http://localhost',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------

View File

@ -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))
{
$sql .= ' '.$query->limit;
}
return implode(' ', $sql); if ( ! is_null($query->offset))
} {
$sql .= ' '.$query->offset;
}
/** return $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;
} }
/** /**

View File

@ -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 @@ private function dynamic_where($method, $parameters)
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 @@ public function order_by($column, $direction = 'asc')
*/ */
public function skip($value) public function skip($value)
{ {
$this->offset = $value; $this->offset = 'OFFSET '.$value;
return $this; return $this;
} }
@ -440,7 +450,7 @@ public function skip($value)
*/ */
public function take($value) public function take($value)
{ {
$this->limit = $value; $this->limit = 'LIMIT '.$value;
return $this; return $this;
} }