refactoring and bug fixes.
This commit is contained in:
@@ -44,25 +44,25 @@ class Connection {
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a SQL query against the connection and return a scalar result.
|
||||
* Execute a SQL query against the connection and return a single column result.
|
||||
*
|
||||
* <code>
|
||||
* // Get the total number of rows on a table
|
||||
* $count = DB::connection()->scalar('select count(*) from users');
|
||||
* $count = DB::connection()->only('select count(*) from users');
|
||||
*
|
||||
* // Get the sum of payment amounts from a table
|
||||
* $sum = DB::connection()->scalar('select sum(amount) from payments')
|
||||
* $sum = DB::connection()->only('select sum(amount) from payments')
|
||||
* </code>
|
||||
*
|
||||
* @param string $sql
|
||||
* @param array $bindings
|
||||
* @return float
|
||||
* @return mixed
|
||||
*/
|
||||
public function scalar($sql, $bindings = array())
|
||||
public function only($sql, $bindings = array())
|
||||
{
|
||||
$result = (array) $this->first($sql, $bindings);
|
||||
|
||||
return (float) reset($result);
|
||||
return reset($result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -113,7 +113,7 @@ class Grammar {
|
||||
//
|
||||
// The only exception to this rule are "raw" where clauses, which are simply
|
||||
// appended to the query as-is, without any further compiling.
|
||||
foreach ($wheres as $where)
|
||||
foreach ($query->wheres as $where)
|
||||
{
|
||||
$sql[] = ($where['type'] == 'raw') ? $where['sql'] : $where['connector'].' '.$this->{$where['type']}($where);
|
||||
}
|
||||
|
||||
@@ -456,23 +456,16 @@ class Query {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an aggregate value.
|
||||
* Execute the query as a SELECT statement and return a single column.
|
||||
*
|
||||
* @param string $aggregate
|
||||
* @param string $column
|
||||
* @return mixed
|
||||
*/
|
||||
private function aggregate($aggregator, $column)
|
||||
public function only($column)
|
||||
{
|
||||
$this->aggregate = compact('aggregator', 'column');
|
||||
$this->select(array($column));
|
||||
|
||||
$result = $this->connection->scalar($this->grammar->select($this), $this->bindings);
|
||||
|
||||
// Reset the aggregate so more queries can be performed using the same instance.
|
||||
// This is helpful for getting aggregates and then getting actual results.
|
||||
$this->aggregate = null;
|
||||
|
||||
return $result;
|
||||
return $this->connection->only($this->grammar->select($this), $this->bindings);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -487,16 +480,7 @@ class Query {
|
||||
{
|
||||
$columns = (array) $columns;
|
||||
|
||||
$results = (count($results = $this->take(1)->get($columns)) > 0) ? $results[0] : null;
|
||||
|
||||
// If we have results and only a single column was selected from the database,
|
||||
// we will simply return the value of that column for convenience.
|
||||
if ( ! is_null($results) and count($columns) == 1 and $columns[0] !== '*')
|
||||
{
|
||||
return $results->{$columns[0]};
|
||||
}
|
||||
|
||||
return $results;
|
||||
return (count($results = $this->take(1)->get($columns)) > 0) ? $results[0] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -518,6 +502,26 @@ class Query {
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an aggregate value.
|
||||
*
|
||||
* @param string $aggregate
|
||||
* @param string $column
|
||||
* @return mixed
|
||||
*/
|
||||
private function aggregate($aggregator, $column)
|
||||
{
|
||||
$this->aggregate = compact('aggregator', 'column');
|
||||
|
||||
$result = $this->connection->only($this->grammar->select($this), $this->bindings);
|
||||
|
||||
// Reset the aggregate so more queries can be performed using the same instance.
|
||||
// This is helpful for getting aggregates and then getting actual results.
|
||||
$this->aggregate = null;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert an array of values into the database table.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user