Refactor the database query builder.

This commit is contained in:
Taylor Otwell 2011-08-08 10:56:06 -05:00
parent 73065d12c2
commit d2be60205b
1 changed files with 14 additions and 12 deletions

View File

@ -496,9 +496,7 @@ public function paginate($per_page, $columns = array('*'))
{ {
$total = $this->count(); $total = $this->count();
$this->select($columns); return Paginator::make($this->for_page(Paginator::page($total, $per_page), $per_page)->get($columns), $total, $per_page);
return Paginator::make($this->for_page(Paginator::page($total, $per_page), $per_page)->get(), $total, $per_page);
} }
/** /**
@ -527,11 +525,20 @@ public function get($columns = array('*'))
$sql = $this->select.' '.$this->from.' '.$this->where; $sql = $this->select.' '.$this->from.' '.$this->where;
if (count($this->orderings) > 0) $sql .= ' ORDER BY '.implode(', ', $this->orderings); if (count($this->orderings) > 0)
{
$sql .= ' ORDER BY '.implode(', ', $this->orderings);
}
if ( ! is_null($this->limit)) $sql .= ' LIMIT '.$this->limit; if ( ! is_null($this->limit))
{
$sql .= ' LIMIT '.$this->limit;
}
if ( ! is_null($this->offset)) $sql .= ' OFFSET '.$this->offset; if ( ! is_null($this->offset))
{
$sql .= ' OFFSET '.$this->offset;
}
$results = $this->connection->query($sql, $this->bindings); $results = $this->connection->query($sql, $this->bindings);
@ -563,8 +570,6 @@ public function insert_get_id($values)
{ {
$sql = $this->compile_insert($values); $sql = $this->compile_insert($values);
// Use the RETURNING clause on PostgreSQL so don't have to worry about sequence columns.
// MySQL and SQLite can use the PDO's lastInsertID() method.
if ($this->connection->driver() == 'pgsql') if ($this->connection->driver() == 'pgsql')
{ {
$query = $this->connection->pdo->prepare($sql.' RETURNING '.$this->wrap('id')); $query = $this->connection->pdo->prepare($sql.' RETURNING '.$this->wrap('id'));
@ -620,10 +625,7 @@ public function update($values)
*/ */
public function delete($id = null) public function delete($id = null)
{ {
if ( ! is_null($id)) if ( ! is_null($id)) $this->where('id', '=', $id);
{
$this->where('id', '=', $id);
}
return $this->connection->query('DELETE FROM '.$this->wrap($this->table).' '.$this->where, $this->bindings); return $this->connection->query('DELETE FROM '.$this->wrap($this->table).' '.$this->where, $this->bindings);
} }