Refactoring query class.

This commit is contained in:
Taylor Otwell 2011-08-02 08:27:01 -05:00
parent e678b77b5b
commit 320c72edbe
1 changed files with 9 additions and 22 deletions

View File

@ -170,7 +170,7 @@ public function left_join($table, $column1, $operator, $column2)
}
/**
* Reset the where clause to its initial state.
* Reset the where clause to its initial state. All bindings will be cleared.
*
* @return void
*/
@ -345,6 +345,9 @@ public function or_where_not_null($column)
/**
* Add dynamic where conditions to the query.
*
* Dynamic queries are caught by the __call magic method and are parsed here.
* They provide a convenient, expressive API for building simple conditions.
*
* @param string $method
* @param array $parameters
* @return Query
@ -466,24 +469,11 @@ private function aggregate($aggregator, $column)
*/
public function paginate($per_page, $columns = array('*'))
{
$select = $this->select;
$total = $this->count();
// Every query clears the SELECT clause, so we store the contents of the clause
// before executing the count query and then put the contents back in afterwards.
if ( ! is_null($select))
{
$this->select = $select;
}
else
{
$this->select($columns);
}
$this->select($columns);
$current_page = \System\Paginator::page($total, $per_page);
return \System\Paginator::make($this->for_page($current_page, $per_page)->get(), $total, $per_page);
return \System\Paginator::make($this->for_page(\System\Paginator::page($total, $per_page), $per_page)->get(), $total, $per_page);
}
/**
@ -557,8 +547,8 @@ public function insert_get_id($values)
{
$sql = $this->compile_insert($values);
// Use the RETURNING clause on Postgres instead of the PDO lastInsertID method.
// The PDO method is a little cumbersome using Postgres.
// 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')
{
$query = $this->connection->pdo->prepare($sql.' RETURNING '.$this->wrap('id'));
@ -583,10 +573,7 @@ private function compile_insert($values)
{
$sql = 'INSERT INTO '.$this->wrap($this->table);
foreach (array_keys($values) as $column)
{
$columns[] = $this->wrap($column);
}
$columns = array_map(array($this, 'wrap'), array_keys($values));
return $sql .= ' ('.implode(', ', $columns).') VALUES ('.$this->parameterize($values).')';
}