Commit 320c72ed by Taylor Otwell

Refactoring query class.

parent e678b77b
...@@ -170,7 +170,7 @@ class Query { ...@@ -170,7 +170,7 @@ class Query {
} }
/** /**
* Reset the where clause to its initial state. * Reset the where clause to its initial state. All bindings will be cleared.
* *
* @return void * @return void
*/ */
...@@ -345,6 +345,9 @@ class Query { ...@@ -345,6 +345,9 @@ class Query {
/** /**
* Add dynamic where conditions to the query. * 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 string $method
* @param array $parameters * @param array $parameters
* @return Query * @return Query
...@@ -466,24 +469,11 @@ class Query { ...@@ -466,24 +469,11 @@ class Query {
*/ */
public function paginate($per_page, $columns = array('*')) public function paginate($per_page, $columns = array('*'))
{ {
$select = $this->select;
$total = $this->count(); $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 @@ class Query { ...@@ -557,8 +547,8 @@ class Query {
{ {
$sql = $this->compile_insert($values); $sql = $this->compile_insert($values);
// Use the RETURNING clause on Postgres instead of the PDO lastInsertID method. // Use the RETURNING clause on PostgreSQL so don't have to worry about sequence columns.
// The PDO method is a little cumbersome using Postgres. // 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'));
...@@ -583,10 +573,7 @@ class Query { ...@@ -583,10 +573,7 @@ class Query {
{ {
$sql = 'INSERT INTO '.$this->wrap($this->table); $sql = 'INSERT INTO '.$this->wrap($this->table);
foreach (array_keys($values) as $column) $columns = array_map(array($this, 'wrap'), array_keys($values));
{
$columns[] = $this->wrap($column);
}
return $sql .= ' ('.implode(', ', $columns).') VALUES ('.$this->parameterize($values).')'; return $sql .= ' ('.implode(', ', $columns).') VALUES ('.$this->parameterize($values).')';
} }
......
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