database refactoring.
This commit is contained in:
@@ -1,24 +1,16 @@
|
||||
<?php namespace System\DB;
|
||||
|
||||
use System\DB;
|
||||
use System\Str;
|
||||
use System\Config;
|
||||
|
||||
class Query {
|
||||
|
||||
/**
|
||||
* The database connection name.
|
||||
* The database connection.
|
||||
*
|
||||
* @var string
|
||||
* @var Connection
|
||||
*/
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* The database connection configuration.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $config;
|
||||
public $connection;
|
||||
|
||||
/**
|
||||
* The SELECT clause.
|
||||
@@ -86,24 +78,25 @@ class Query {
|
||||
/**
|
||||
* Create a new query instance.
|
||||
*
|
||||
* @param string $table
|
||||
* @param string $connection
|
||||
* @param string $table
|
||||
* @param Connection $connection
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($table, $connection = null)
|
||||
public function __construct($table, $connection)
|
||||
{
|
||||
$this->connection = (is_null($connection)) ? Config::get('db.default') : $connection;
|
||||
$this->from = 'FROM '.$this->wrap($this->table = $table);
|
||||
$this->connection = $connection;
|
||||
$this->table = $table;
|
||||
$this->from = 'FROM '.$this->wrap($table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new query instance.
|
||||
*
|
||||
* @param string $table
|
||||
* @param string $connection
|
||||
* @param string $table
|
||||
* @param Connection $connection
|
||||
* @return Query
|
||||
*/
|
||||
public static function table($table, $connection = null)
|
||||
public static function table($table, $connection)
|
||||
{
|
||||
return new static($table, $connection);
|
||||
}
|
||||
@@ -129,22 +122,9 @@ class Query {
|
||||
{
|
||||
$this->select = ($this->distinct) ? 'SELECT DISTINCT ' : 'SELECT ';
|
||||
|
||||
$wrapped = array();
|
||||
|
||||
foreach ($columns as $column)
|
||||
{
|
||||
// If the column name is being aliased, we will need to wrap the column
|
||||
// name and its alias in keyword identifiers.
|
||||
if (strpos(strtolower($column), ' as ') !== false)
|
||||
{
|
||||
$segments = explode(' ', $column);
|
||||
|
||||
$wrapped[] = $this->wrap($segments[0]).' AS '.$this->wrap($segments[2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
$wrapped[] = $this->wrap($column);
|
||||
}
|
||||
$wrapped[] = $this->wrap($column);
|
||||
}
|
||||
|
||||
$this->select .= implode(', ', $wrapped);
|
||||
@@ -194,6 +174,17 @@ class Query {
|
||||
return $this->join($table, $column1, $operator, $column2, 'LEFT');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the where clause to its initial state.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function reset_where()
|
||||
{
|
||||
$this->where = 'WHERE 1 = 1';
|
||||
$this->bindings = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a raw where condition to the query.
|
||||
*
|
||||
@@ -356,6 +347,47 @@ class Query {
|
||||
return $this->where_not_null($column, 'OR');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add dynamic where conditions to the query.
|
||||
*
|
||||
* @param string $method
|
||||
* @param array $parameters
|
||||
* @return Query
|
||||
*/
|
||||
private function dynamic_where($method, $parameters)
|
||||
{
|
||||
// Strip the "where_" off of the method.
|
||||
$finder = substr($method, 6);
|
||||
|
||||
// Split the column names from the connectors.
|
||||
$segments = preg_split('/(_and_|_or_)/i', $finder, -1, PREG_SPLIT_DELIM_CAPTURE);
|
||||
|
||||
// The connector variable will determine which connector will be used for the condition.
|
||||
// We'll change it as we come across new connectors in the dynamic method string.
|
||||
//
|
||||
// The index variable helps us get the correct parameter value for the where condition.
|
||||
// We increment it each time we add a condition.
|
||||
$connector = 'AND';
|
||||
|
||||
$index = 0;
|
||||
|
||||
foreach ($segments as $segment)
|
||||
{
|
||||
if ($segment != '_and_' and $segment != '_or_')
|
||||
{
|
||||
$this->where($segment, '=', $parameters[$index], $connector);
|
||||
|
||||
$index++;
|
||||
}
|
||||
else
|
||||
{
|
||||
$connector = trim(strtoupper($segment), '_');
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an ordering to the query.
|
||||
*
|
||||
@@ -393,6 +425,18 @@ class Query {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the limit and offset values for a given page.
|
||||
*
|
||||
* @param int $page
|
||||
* @param int $per_page
|
||||
* @return Query
|
||||
*/
|
||||
public function for_page($page, $per_page)
|
||||
{
|
||||
return $this->skip(($page - 1) * $per_page)->take($per_page);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a record by the primary key.
|
||||
*
|
||||
@@ -405,6 +449,46 @@ class Query {
|
||||
return $this->where('id', '=', $id)->first($columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an aggregate value.
|
||||
*
|
||||
* @param string $aggregate
|
||||
* @param string $column
|
||||
* @return mixed
|
||||
*/
|
||||
private function aggregate($aggregator, $column)
|
||||
{
|
||||
$this->select = 'SELECT '.$aggregator.'('.$this->wrap($column).') AS '.$this->wrap('aggregate');
|
||||
return $this->first()->aggregate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get paginated query results.
|
||||
*
|
||||
* @param int $per_page
|
||||
* @param array $columns
|
||||
* @return Paginator
|
||||
*/
|
||||
public function paginate($per_page, $columns = array('*'))
|
||||
{
|
||||
list($select, $total) = array($this->select, $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);
|
||||
}
|
||||
|
||||
$current_page = \System\Paginator::page($total, $per_page);
|
||||
|
||||
return \System\Paginator::make($this->for_page($current_page, $per_page)->get(), $total, $per_page);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the query as a SELECT statement and return the first result.
|
||||
*
|
||||
@@ -429,71 +513,32 @@ class Query {
|
||||
$this->select($columns);
|
||||
}
|
||||
|
||||
$results = DB::query(Query\Compiler::select($this), $this->bindings, $this->connection);
|
||||
$sql = $this->select.' '.$this->from.' '.$this->where;
|
||||
|
||||
if (count($this->orderings) > 0)
|
||||
{
|
||||
$sql .= ' ORDER BY '.implode(', ', $this->orderings);
|
||||
}
|
||||
|
||||
if ( ! is_null($this->limit))
|
||||
{
|
||||
$sql .= ' LIMIT '.$this->limit;
|
||||
}
|
||||
|
||||
if ( ! is_null($this->offset))
|
||||
{
|
||||
$sql .= ' OFFSET '.$this->offset;
|
||||
}
|
||||
|
||||
$results = $this->connection->query($sql, $this->bindings);
|
||||
|
||||
// Reset the SELECT clause so more queries can be performed using the same instance.
|
||||
// This is helpful for performing counts and then getting actual results, such as
|
||||
// when paginating results.
|
||||
// This is helpful for getting aggregates and then getting actual results.
|
||||
$this->select = null;
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an aggregate value.
|
||||
*
|
||||
* @param string $aggregate
|
||||
* @param string $column
|
||||
* @return mixed
|
||||
*/
|
||||
private function aggregate($aggregator, $column)
|
||||
{
|
||||
$this->select = 'SELECT '.$aggregator.'('.$this->wrap($column).') AS '.$this->wrap('aggregate');
|
||||
|
||||
return $this->first()->aggregate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get paginated query results.
|
||||
*
|
||||
* @param int $per_page
|
||||
* @param array $columns
|
||||
* @return Paginator
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
$current_page = \System\Paginator::page($total, $per_page);
|
||||
|
||||
return \System\Paginator::make($this->for_page($current_page, $per_page)->get(), $total, $per_page);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the LIMIT and OFFSET values for a given page.
|
||||
*
|
||||
* @param int $page
|
||||
* @param int $per_page
|
||||
* @return Query
|
||||
*/
|
||||
public function for_page($page, $per_page)
|
||||
{
|
||||
return $this->skip(($page - 1) * $per_page)->take($per_page);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute an INSERT statement.
|
||||
*
|
||||
@@ -502,7 +547,7 @@ class Query {
|
||||
*/
|
||||
public function insert($values)
|
||||
{
|
||||
return DB::query(Query\Compiler::insert($this, $values), array_values($values), $this->connection);
|
||||
return $this->connection->query($this->compile_insert($values), array_values($values));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -513,22 +558,40 @@ class Query {
|
||||
*/
|
||||
public function insert_get_id($values)
|
||||
{
|
||||
$sql = Query\Compiler::insert($this, $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.
|
||||
if (DB::driver($this->connection) == 'pgsql')
|
||||
if ($this->connection->driver() == 'pgsql')
|
||||
{
|
||||
$query = DB::connection($this->connection)->prepare($sql.' RETURNING '.$this->wrap('id'));
|
||||
$query = $this->connection->pdo->prepare($sql.' RETURNING '.$this->wrap('id'));
|
||||
|
||||
$query->execute(array_values($values));
|
||||
|
||||
return $query->fetch(\PDO::FETCH_CLASS, 'stdClass')->id;
|
||||
}
|
||||
|
||||
DB::query($sql, array_values($values), $this->connection);
|
||||
$this->connection->query($sql, array_values($values));
|
||||
|
||||
return DB::connection($this->connection)->lastInsertId();
|
||||
return $this->connection->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile an SQL INSERT statement.
|
||||
*
|
||||
* @param array $values
|
||||
* @return string
|
||||
*/
|
||||
private function compile_insert($values)
|
||||
{
|
||||
$sql = 'INSERT INTO '.$this->wrap($this->table);
|
||||
|
||||
foreach (array_keys($values) as $column)
|
||||
{
|
||||
$columns[] = $this->wrap($column);
|
||||
}
|
||||
|
||||
return $sql .= ' ('.implode(', ', $columns).') VALUES ('.$this->parameterize($values).')';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -539,7 +602,16 @@ class Query {
|
||||
*/
|
||||
public function update($values)
|
||||
{
|
||||
return DB::query(Query\Compiler::update($this, $values), array_merge(array_values($values), $this->bindings), $this->connection);
|
||||
$sql = 'UPDATE '.$this->wrap($this->table).' SET ';
|
||||
|
||||
foreach (array_keys($values) as $column)
|
||||
{
|
||||
$sets[] = $this->wrap($column).' = ?';
|
||||
}
|
||||
|
||||
$sql .= implode(', ', $sets).' '.$this->where;
|
||||
|
||||
return $this->connection->query($sql, array_merge(array_values($values), $this->bindings));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -555,32 +627,38 @@ class Query {
|
||||
$this->where('id', '=', $id);
|
||||
}
|
||||
|
||||
return DB::query(Query\Compiler::delete($this), $this->bindings, $this->connection);
|
||||
return $this->connection->query('DELETE FROM '.$this->wrap($this->table).' '.$this->where, $this->bindings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap a value in keyword identifiers.
|
||||
*
|
||||
* @param string $value
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public function wrap($value)
|
||||
private function wrap($value)
|
||||
{
|
||||
if (is_null($this->config))
|
||||
if (strpos(strtolower($value), ' as ') !== false)
|
||||
{
|
||||
$connections = Config::get('db.connections');
|
||||
|
||||
$this->config = $connections[$this->connection];
|
||||
return $this->wrap_alias($value, $connection);
|
||||
}
|
||||
|
||||
if (array_key_exists('wrap', $this->config) and $this->config['wrap'] === false)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
$wrap = $this->connection->wrapper();
|
||||
|
||||
$wrap = (DB::driver($this->connection) == 'mysql') ? '`' : '"';
|
||||
return implode('.', array_map(function($segment) use ($wrap) { return ($segment != '*') ? $wrap.$segment.$wrap : $segment; }, explode('.', $value)));
|
||||
}
|
||||
|
||||
return implode('.', array_map(function($segment) use ($wrap) {return ($segment != '*') ? $wrap.$segment.$wrap : $segment;}, explode('.', $value)));
|
||||
/**
|
||||
* Wrap an alias in keyword identifiers.
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
private function wrap_alias($value)
|
||||
{
|
||||
$segments = explode(' ', $value);
|
||||
|
||||
return $this->wrap($segments[0]).' AS '.$this->wrap($segments[2]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -589,7 +667,7 @@ class Query {
|
||||
* @param array $values
|
||||
* @return string
|
||||
*/
|
||||
public function parameterize($values)
|
||||
private function parameterize($values)
|
||||
{
|
||||
return implode(', ', array_fill(0, count($values), '?'));
|
||||
}
|
||||
@@ -601,7 +679,7 @@ class Query {
|
||||
{
|
||||
if (strpos($method, 'where_') === 0)
|
||||
{
|
||||
return Query\Dynamic::build($method, $parameters, $this);
|
||||
return $this->dynamic_where($method, $parameters, $this);
|
||||
}
|
||||
|
||||
if (in_array($method, array('count', 'min', 'max', 'avg', 'sum')))
|
||||
|
||||
Reference in New Issue
Block a user