refactored query->select.

This commit is contained in:
Taylor Otwell 2011-08-01 20:14:30 -05:00
parent 75d96c9204
commit 82a0218011
1 changed files with 16 additions and 12 deletions

View File

@ -122,12 +122,7 @@ public function select($columns = array('*'))
{ {
$this->select = ($this->distinct) ? 'SELECT DISTINCT ' : 'SELECT '; $this->select = ($this->distinct) ? 'SELECT DISTINCT ' : 'SELECT ';
foreach ($columns as $column) $this->select .= implode(', ', array_map(array($this, 'wrap'), $columns));
{
$wrapped[] = $this->wrap($column);
}
$this->select .= implode(', ', $wrapped);
return $this; return $this;
} }
@ -517,11 +512,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);
@ -602,9 +606,7 @@ public function update($values)
$sets[] = $this->wrap($column).' = ?'; $sets[] = $this->wrap($column).' = ?';
} }
$sql .= implode(', ', $sets).' '.$this->where; return $this->connection->query($sql.implode(', ', $sets).' '.$this->where, array_merge(array_values($values), $this->bindings));
return $this->connection->query($sql, array_merge(array_values($values), $this->bindings));
} }
/** /**
@ -670,11 +672,13 @@ private function parameterize($values)
*/ */
public function __call($method, $parameters) public function __call($method, $parameters)
{ {
// Dynamically handle the addition of dynamic where clauses.
if (strpos($method, 'where_') === 0) if (strpos($method, 'where_') === 0)
{ {
return $this->dynamic_where($method, $parameters, $this); return $this->dynamic_where($method, $parameters, $this);
} }
// Dynamically handle calls to any of the aggregate query functions.
if (in_array($method, array('count', 'min', 'max', 'avg', 'sum'))) if (in_array($method, array('count', 'min', 'max', 'avg', 'sum')))
{ {
return ($method == 'count') ? $this->aggregate(strtoupper($method), '*') : $this->aggregate(strtoupper($method), $parameters[0]); return ($method == 'count') ? $this->aggregate(strtoupper($method), '*') : $this->aggregate(strtoupper($method), $parameters[0]);