Files
spa-api/laravel/database/query.php

674 lines
14 KiB
PHP
Raw Normal View History

2011-10-04 21:43:39 -05:00
<?php namespace Laravel\Database; use Laravel\Paginator;
2011-06-08 23:45:08 -05:00
class Query {
/**
2011-08-01 17:58:20 -05:00
* The database connection.
2011-06-08 23:45:08 -05:00
*
2011-09-12 23:33:42 -05:00
* @var Connection
2011-07-19 11:17:51 -07:00
*/
2011-08-01 17:58:20 -05:00
public $connection;
2011-07-19 11:17:51 -07:00
2011-08-19 22:09:17 -05:00
/**
* The query grammar instance.
2011-08-19 22:09:17 -05:00
*
* @var Grammars\Grammar
2011-08-19 22:09:17 -05:00
*/
public $grammar;
2011-08-19 22:09:17 -05:00
2011-06-08 23:45:08 -05:00
/**
* The SELECT clause.
*
2011-08-22 21:56:42 -05:00
* @var array
2011-06-08 23:45:08 -05:00
*/
2011-09-27 21:10:32 -05:00
public $selects;
2011-06-08 23:45:08 -05:00
/**
2011-10-15 14:04:11 -05:00
* If the query is performing an aggregate function, this will contain
* the column and and function to use when aggregating.
2011-06-08 23:45:08 -05:00
*
2011-08-22 21:56:42 -05:00
* @var array
2011-06-08 23:45:08 -05:00
*/
2011-08-22 21:56:42 -05:00
public $aggregate;
2011-06-08 23:45:08 -05:00
/**
2011-08-22 21:56:42 -05:00
* Indicates if the query should return distinct results.
2011-06-08 23:45:08 -05:00
*
2011-08-22 21:56:42 -05:00
* @var bool
2011-06-08 23:45:08 -05:00
*/
2011-08-22 21:56:42 -05:00
public $distinct = false;
2011-06-08 23:45:08 -05:00
/**
* The table name.
*
* @var string
*/
2011-09-27 21:10:32 -05:00
public $from;
2011-06-08 23:45:08 -05:00
/**
2011-08-22 21:56:42 -05:00
* The table joins.
2011-06-08 23:45:08 -05:00
*
2011-08-22 21:56:42 -05:00
* @var array
2011-06-08 23:45:08 -05:00
*/
2011-08-22 21:56:42 -05:00
public $joins;
2011-06-08 23:45:08 -05:00
2011-08-21 11:50:53 -05:00
/**
2011-08-22 21:56:42 -05:00
* The WHERE clauses.
2011-08-21 11:50:53 -05:00
*
2011-08-22 21:56:42 -05:00
* @var array
2011-08-21 11:50:53 -05:00
*/
2011-08-22 21:56:42 -05:00
public $wheres;
2011-08-21 11:50:53 -05:00
2011-06-08 23:45:08 -05:00
/**
2011-08-22 21:56:42 -05:00
* The ORDER BY clauses.
2011-06-08 23:45:08 -05:00
*
* @var array
*/
2011-08-22 21:56:42 -05:00
public $orderings;
2011-06-08 23:45:08 -05:00
/**
* The LIMIT value.
*
* @var int
*/
public $limit;
/**
* The OFFSET value.
*
* @var int
*/
public $offset;
/**
* The query value bindings.
*
* @var array
*/
public $bindings = array();
/**
* Create a new query instance.
*
2011-09-27 21:10:32 -05:00
* @param Connection $connection
* @param Grammars\Grammar $grammar
* @param string $table
2011-06-08 23:45:08 -05:00
* @return void
*/
public function __construct(Connection $connection, Grammars\Grammar $grammar, $table)
2011-06-08 23:45:08 -05:00
{
2011-09-27 21:10:32 -05:00
$this->from = $table;
$this->grammar = $grammar;
2011-08-01 19:57:48 -05:00
$this->connection = $connection;
2011-06-08 23:45:08 -05:00
}
/**
* Force the query to return distinct results.
*
* @return Query
*/
public function distinct()
{
$this->distinct = true;
return $this;
}
/**
2011-08-22 21:56:42 -05:00
* Add an array of columns to the SELECT clause.
*
* @param array $columns
2011-06-08 23:45:08 -05:00
* @return Query
*/
public function select($columns = array('*'))
2011-06-08 23:45:08 -05:00
{
2011-09-27 21:10:32 -05:00
$this->selects = (array) $columns;
return $this;
}
/**
2011-08-22 21:56:42 -05:00
* Add a join clause to the query.
*
2011-06-08 23:45:08 -05:00
* @param string $table
* @param string $column1
* @param string $operator
* @param string $column2
* @param string $type
* @return Query
*/
public function join($table, $column1, $operator, $column2, $type = 'INNER')
{
2011-09-27 21:10:32 -05:00
$this->joins[] = compact('type', 'table', 'column1', 'operator', 'column2');
2011-08-22 21:56:42 -05:00
2011-06-08 23:45:08 -05:00
return $this;
}
/**
* Add a left join to the query.
*
* @param string $table
* @param string $column1
* @param string $operator
* @param string $column2
* @return Query
*/
public function left_join($table, $column1, $operator, $column2)
{
return $this->join($table, $column1, $operator, $column2, 'LEFT');
}
2011-08-01 17:58:20 -05:00
/**
2011-08-02 08:27:01 -05:00
* Reset the where clause to its initial state. All bindings will be cleared.
2011-08-01 17:58:20 -05:00
*
* @return void
*/
public function reset_where()
{
2011-09-12 23:57:53 -05:00
list($this->wheres, $this->bindings) = array(array(), array());
2011-08-01 17:58:20 -05:00
}
2011-06-08 23:45:08 -05:00
/**
* Add a raw where condition to the query.
*
* @param string $where
* @param array $bindings
* @param string $connector
* @return Query
*/
public function raw_where($where, $bindings = array(), $connector = 'AND')
{
2011-09-27 21:10:32 -05:00
$this->wheres[] = array('type' => 'raw', 'connector' => $connector, 'sql' => $where);
2011-08-22 21:56:42 -05:00
2011-06-08 23:45:08 -05:00
$this->bindings = array_merge($this->bindings, $bindings);
return $this;
}
/**
* Add a raw or where condition to the query.
*
* @param string $where
* @param array $bindings
* @return Query
*/
public function raw_or_where($where, $bindings = array())
{
return $this->raw_where($where, $bindings, 'OR');
}
/**
* Add a where condition to the query.
*
* @param string $column
* @param string $operator
* @param mixed $value
* @param string $connector
* @return Query
*/
public function where($column, $operator, $value, $connector = 'AND')
{
2011-10-15 23:25:18 -05:00
$type = 'where';
$this->wheres[] = compact('type', 'column', 'operator', 'value', 'connector');
2011-08-22 21:56:42 -05:00
2011-06-08 23:45:08 -05:00
$this->bindings[] = $value;
return $this;
}
/**
* Add an or where condition to the query.
*
* @param string $column
* @param string $operator
* @param mixed $value
* @return Query
*/
public function or_where($column, $operator, $value)
{
return $this->where($column, $operator, $value, 'OR');
}
/**
* Add an or where condition for the primary key to the query.
2011-08-22 21:56:42 -05:00
*
* @param mixed $value
* @return Query
*/
public function or_where_id($value)
{
return $this->or_where('id', '=', $value);
}
2011-06-08 23:45:08 -05:00
/**
* Add a where in condition to the query.
*
* @param string $column
* @param array $values
* @param string $connector
2011-08-22 21:56:42 -05:00
* @param bool $not
2011-06-08 23:45:08 -05:00
* @return Query
*/
2011-08-22 21:56:42 -05:00
public function where_in($column, $values, $connector = 'AND', $not = false)
2011-06-08 23:45:08 -05:00
{
$type = ($not) ? 'where_not_in' : 'where_in';
$this->wheres[] = compact('type', 'column', 'values', 'connector');
2011-08-22 21:56:42 -05:00
2011-06-08 23:45:08 -05:00
$this->bindings = array_merge($this->bindings, $values);
return $this;
}
/**
* Add an or where in condition to the query.
*
* @param string $column
* @param array $values
* @return Query
*/
public function or_where_in($column, $values)
{
return $this->where_in($column, $values, 'OR');
}
/**
* Add a where not in condition to the query.
*
* @param string $column
* @param array $values
* @param string $connector
* @return Query
*/
public function where_not_in($column, $values, $connector = 'AND')
{
2011-08-22 21:56:42 -05:00
return $this->where_in($column, $values, $connector, true);
2011-06-08 23:45:08 -05:00
}
/**
* Add an or where not in condition to the query.
*
* @param string $column
* @param array $values
* @return Query
*/
public function or_where_not_in($column, $values)
{
return $this->where_not_in($column, $values, 'OR');
}
/**
* Add a where null condition to the query.
*
* @param string $column
* @param string $connector
2011-08-22 21:56:42 -05:00
* @param bool $not
2011-06-08 23:45:08 -05:00
* @return Query
*/
2011-08-22 21:56:42 -05:00
public function where_null($column, $connector = 'AND', $not = false)
2011-06-08 23:45:08 -05:00
{
$type = ($not) ? 'where_not_null' : 'where_null';
$this->wheres[] = compact('type', 'column', 'connector');
2011-08-22 21:56:42 -05:00
2011-06-08 23:45:08 -05:00
return $this;
}
/**
* Add an or where null condition to the query.
*
* @param string $column
* @return Query
*/
public function or_where_null($column)
{
return $this->where_null($column, 'OR');
}
/**
* Add a where not null condition to the query.
*
* @param string $column
* @param string $connector
* @return Query
*/
public function where_not_null($column, $connector = 'AND')
{
2011-08-22 21:56:42 -05:00
return $this->where_null($column, $connector, true);
2011-06-08 23:45:08 -05:00
}
/**
* Add an or where not null condition to the query.
*
* @param string $column
* @return Query
*/
public function or_where_not_null($column)
{
return $this->where_not_null($column, 'OR');
}
2011-08-01 17:58:20 -05:00
/**
* Add dynamic where conditions to the query.
*
2011-08-02 08:27:01 -05:00
* Dynamic queries are caught by the __call magic method and are parsed here.
* They provide a convenient, expressive API for building simple conditions.
*
2011-08-01 17:58:20 -05:00
* @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);
2011-10-15 14:04:11 -05:00
// 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.
2011-08-01 17:58:20 -05:00
//
2011-10-15 14:04:11 -05:00
// The index variable helps us get the correct parameter value
// for the where condition. We increment it each time we add
// a condition to the query.
2011-08-01 17:58:20 -05:00
$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;
}
2011-06-08 23:45:08 -05:00
/**
* Add an ordering to the query.
*
* @param string $column
* @param string $direction
* @return Query
*/
public function order_by($column, $direction = 'asc')
2011-06-08 23:45:08 -05:00
{
2011-08-22 21:56:42 -05:00
$this->orderings[] = compact('column', 'direction');
2011-06-08 23:45:08 -05:00
return $this;
}
/**
* Set the query offset.
*
* @param int $value
* @return Query
*/
public function skip($value)
{
2011-08-22 21:56:42 -05:00
$this->offset = $value;
2011-06-08 23:45:08 -05:00
return $this;
}
/**
* Set the query limit.
*
* @param int $value
* @return Query
*/
public function take($value)
{
2011-08-22 21:56:42 -05:00
$this->limit = $value;
2011-06-08 23:45:08 -05:00
return $this;
}
/**
* Set the query limit and offset for a given page and item per page count.
*
* @param int $page
* @param int $per_page
* @return Query
*/
2011-08-25 23:46:45 -05:00
public function for_page($page, $per_page)
{
2011-08-01 17:58:20 -05:00
return $this->skip(($page - 1) * $per_page)->take($per_page);
2011-06-08 23:45:08 -05:00
}
/**
2011-08-01 17:58:20 -05:00
* Find a record by the primary key.
2011-06-08 23:45:08 -05:00
*
2011-08-01 17:58:20 -05:00
* @param int $id
* @param array $columns
2011-06-08 23:45:08 -05:00
* @return object
*/
2011-08-01 17:58:20 -05:00
public function find($id, $columns = array('*'))
2011-06-08 23:45:08 -05:00
{
2011-08-01 17:58:20 -05:00
return $this->where('id', '=', $id)->first($columns);
2011-06-08 23:45:08 -05:00
}
/**
2011-09-29 21:22:48 -05:00
* Execute the query as a SELECT statement and return a single column.
2011-06-08 23:45:08 -05:00
*
* @param string $column
* @return mixed
*/
2011-09-29 21:22:48 -05:00
public function only($column)
2011-06-08 23:45:08 -05:00
{
2011-09-29 21:22:48 -05:00
$this->select(array($column));
2011-08-19 21:39:38 -05:00
2011-09-29 21:22:48 -05:00
return $this->connection->only($this->grammar->select($this), $this->bindings);
2011-06-08 23:45:08 -05:00
}
2011-07-21 07:17:53 -07:00
/**
2011-08-01 17:58:20 -05:00
* Execute the query as a SELECT statement and return the first result.
2011-07-21 07:17:53 -07:00
*
* If a single column is selected from the database, only the value of that column will be returned.
*
* @param array $columns
* @return mixed
2011-07-21 07:17:53 -07:00
*/
2011-08-01 17:58:20 -05:00
public function first($columns = array('*'))
2011-07-21 07:17:53 -07:00
{
$columns = (array) $columns;
2011-09-29 21:22:48 -05:00
return (count($results = $this->take(1)->get($columns)) > 0) ? $results[0] : null;
2011-08-01 17:58:20 -05:00
}
/**
* Execute the query as a SELECT statement.
*
* @param array $columns
* @return array
*/
public function get($columns = array('*'))
{
2011-09-27 21:10:32 -05:00
if (is_null($this->selects)) $this->select($columns);
2011-08-01 17:58:20 -05:00
$results = $this->connection->query($this->grammar->select($this), $this->bindings);
2011-08-08 16:09:08 -05:00
2011-10-15 14:04:11 -05:00
// Reset the SELECT clause so more queries can be performed using
// the same instance. This is helpful for getting aggregates and
// then getting actual results.
2011-09-27 21:10:32 -05:00
$this->selects = null;
2011-08-08 16:09:08 -05:00
return $results;
}
2011-09-29 21:22:48 -05:00
/**
* 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);
2011-10-15 14:04:11 -05:00
// Reset the aggregate so more queries can be performed using
// the same instance. This is helpful for getting aggregates
// and then getting actual results.
2011-09-29 21:22:48 -05:00
$this->aggregate = null;
return $result;
}
2011-10-04 21:43:39 -05:00
/**
* Get the paginated query results as a Paginator instance.
*
* @param int $per_page
* @param array $columns
* @return Paginator
*/
2011-10-05 18:32:48 -05:00
public function paginate($per_page = 20, $columns = array('*'))
2011-10-04 21:43:39 -05:00
{
2011-10-15 14:04:11 -05:00
// Calculate the current page for the request. The page number
// will be validated and adjusted by the Paginator class,
// so we can assume it is valid.
2011-10-04 21:43:39 -05:00
$page = Paginator::page($total = $this->count(), $per_page);
return Paginator::make($this->for_page($page, $per_page)->get($columns), $total, $per_page);
}
2011-06-08 23:45:08 -05:00
/**
2011-08-22 21:56:42 -05:00
* Insert an array of values into the database table.
*
2011-06-08 23:45:08 -05:00
* @param array $values
* @return bool
*/
public function insert($values)
{
2011-10-15 14:04:11 -05:00
// Force every insert to be treated like a batch insert to make creating
// the binding array simpler since we can just spin through the inserted
// rows as if there/ was more than one every time.
2011-09-28 21:43:11 -05:00
if ( ! is_array(reset($values))) $values = array($values);
$bindings = array();
foreach ($values as $value)
{
$bindings = array_merge($bindings, array_values($value));
}
return $this->connection->query($this->grammar->insert($this, $values), $bindings);
2011-06-08 23:45:08 -05:00
}
/**
2011-10-15 14:04:11 -05:00
* Insert an array of values into the database table and
* return the value of the ID column.
2011-08-22 21:56:42 -05:00
*
2011-09-12 23:33:42 -05:00
* @param array $values
* @param string $sequence
2011-06-08 23:45:08 -05:00
* @return int
*/
2011-09-12 23:33:42 -05:00
public function insert_get_id($values, $sequence = null)
2011-06-08 23:45:08 -05:00
{
$this->connection->query($this->grammar->insert($this, $values), array_values($values));
2011-06-17 13:09:50 -07:00
2011-09-12 23:33:42 -05:00
return (int) $this->connection->pdo->lastInsertId($sequence);
2011-08-01 17:58:20 -05:00
}
/**
* Increment the value of a column by a given amount.
*
* @param string $column
* @param int $amount
* @return int
*/
public function increment($column, $amount = 1)
{
return $this->adjust($column, $amount, ' + ');
}
/**
* Decrement the value of a column by a given amount.
*
* @param string $column
* @param int $amount
* @return int
*/
public function decrement($column, $amount = 1)
{
return $this->adjust($column, $amount, ' - ');
}
/**
* Adjust the value of a column up or down by a given amount.
*
* @param string $column
* @param int $amount
* @param string $operator
* @return int
*/
protected function adjust($column, $amount, $operator)
{
return $this->update(array($column => Manager::raw($this->grammar->wrap($column).$operator.$amount)));
}
2011-06-08 23:45:08 -05:00
/**
2011-08-22 21:56:42 -05:00
* Update an array of values in the database table.
*
2011-06-08 23:45:08 -05:00
* @param array $values
2011-08-22 21:56:42 -05:00
* @return int
2011-06-08 23:45:08 -05:00
*/
public function update($values)
{
2011-10-15 14:04:11 -05:00
$bindings = array_merge(array_values($values), $this->bindings);
return $this->connection->query($this->grammar->update($this, $values), $bindings);
2011-06-08 23:45:08 -05:00
}
/**
* Execute the query as a DELETE statement.
*
2011-08-22 21:56:42 -05:00
* Optionally, an ID may be passed to the method do delete a specific row.
*
2011-06-08 23:45:08 -05:00
* @param int $id
2011-08-22 21:56:42 -05:00
* @return int
2011-06-08 23:45:08 -05:00
*/
public function delete($id = null)
{
2011-08-08 10:56:06 -05:00
if ( ! is_null($id)) $this->where('id', '=', $id);
2011-06-08 23:45:08 -05:00
return $this->connection->query($this->grammar->delete($this), $this->bindings);
2011-06-08 23:45:08 -05:00
}
/**
2011-08-22 21:56:42 -05:00
* Magic Method for handling dynamic functions.
2011-08-19 21:18:59 -05:00
*
2011-10-15 14:04:11 -05:00
* This method handles all calls to aggregate functions as well
* as the construction of dynamic where clauses.
2011-06-08 23:45:08 -05:00
*/
public function __call($method, $parameters)
{
if (strpos($method, 'where_') === 0)
{
2011-08-01 17:58:20 -05:00
return $this->dynamic_where($method, $parameters, $this);
}
2011-09-27 21:10:32 -05:00
if (in_array($method, array('abs', 'count', 'min', 'max', 'avg', 'sum')))
2011-06-08 23:45:08 -05:00
{
2011-10-15 14:04:11 -05:00
if ($method == 'count')
{
return $this->aggregate(strtoupper($method), '*');
}
else
{
return $this->aggregate(strtoupper($method), $parameters[0]);
}
2011-06-08 23:45:08 -05:00
}
2011-06-17 13:09:50 -07:00
throw new \Exception("Method [$method] is not defined on the Query class.");
2011-06-08 23:45:08 -05:00
}
}