Added vendor/ directory for Composer's installed files

This commit is contained in:
Ascendings
2015-08-30 12:33:20 -04:00
parent 45df179c49
commit b66a773ed8
1162 changed files with 112457 additions and 0 deletions

1995
vendor/illuminate/database/Query/Builder.php vendored Executable file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,43 @@
<?php namespace Illuminate\Database\Query;
class Expression {
/**
* The value of the expression.
*
* @var mixed
*/
protected $value;
/**
* Create a new raw query expression.
*
* @param mixed $value
* @return void
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* Get the value of the expression.
*
* @return mixed
*/
public function getValue()
{
return $this->value;
}
/**
* Get the value of the expression.
*
* @return string
*/
public function __toString()
{
return (string) $this->getValue();
}
}

View File

@ -0,0 +1,758 @@
<?php namespace Illuminate\Database\Query\Grammars;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Grammar as BaseGrammar;
class Grammar extends BaseGrammar {
/**
* The components that make up a select clause.
*
* @var array
*/
protected $selectComponents = array(
'aggregate',
'columns',
'from',
'joins',
'wheres',
'groups',
'havings',
'orders',
'limit',
'offset',
'unions',
'lock',
);
/**
* Compile a select query into SQL.
*
* @param \Illuminate\Database\Query\Builder
* @return string
*/
public function compileSelect(Builder $query)
{
if (is_null($query->columns)) $query->columns = array('*');
return trim($this->concatenate($this->compileComponents($query)));
}
/**
* Compile the components necessary for a select clause.
*
* @param \Illuminate\Database\Query\Builder
* @return array
*/
protected function compileComponents(Builder $query)
{
$sql = array();
foreach ($this->selectComponents as $component)
{
// To compile the query, we'll spin through each component of the query and
// see if that component exists. If it does we'll just call the compiler
// function for the component which is responsible for making the SQL.
if ( ! is_null($query->$component))
{
$method = 'compile'.ucfirst($component);
$sql[$component] = $this->$method($query, $query->$component);
}
}
return $sql;
}
/**
* Compile an aggregated select clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $aggregate
* @return string
*/
protected function compileAggregate(Builder $query, $aggregate)
{
$column = $this->columnize($aggregate['columns']);
// If the query has a "distinct" constraint and we're not asking for all columns
// we need to prepend "distinct" onto the column name so that the query takes
// it into account when it performs the aggregating operations on the data.
if ($query->distinct && $column !== '*')
{
$column = 'distinct '.$column;
}
return 'select '.$aggregate['function'].'('.$column.') as aggregate';
}
/**
* Compile the "select *" portion of the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $columns
* @return string
*/
protected function compileColumns(Builder $query, $columns)
{
// If the query is actually performing an aggregating select, we will let that
// compiler handle the building of the select clauses, as it will need some
// more syntax that is best handled by that function to keep things neat.
if ( ! is_null($query->aggregate)) return;
$select = $query->distinct ? 'select distinct ' : 'select ';
return $select.$this->columnize($columns);
}
/**
* Compile the "from" portion of the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param string $table
* @return string
*/
protected function compileFrom(Builder $query, $table)
{
return 'from '.$this->wrapTable($table);
}
/**
* Compile the "join" portions of the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $joins
* @return string
*/
protected function compileJoins(Builder $query, $joins)
{
$sql = array();
$query->setBindings(array(), 'join');
foreach ($joins as $join)
{
$table = $this->wrapTable($join->table);
// First we need to build all of the "on" clauses for the join. There may be many
// of these clauses so we will need to iterate through each one and build them
// separately, then we'll join them up into a single string when we're done.
$clauses = array();
foreach ($join->clauses as $clause)
{
$clauses[] = $this->compileJoinConstraint($clause);
}
foreach ($join->bindings as $binding)
{
$query->addBinding($binding, 'join');
}
// Once we have constructed the clauses, we'll need to take the boolean connector
// off of the first clause as it obviously will not be required on that clause
// because it leads the rest of the clauses, thus not requiring any boolean.
$clauses[0] = $this->removeLeadingBoolean($clauses[0]);
$clauses = implode(' ', $clauses);
$type = $join->type;
// Once we have everything ready to go, we will just concatenate all the parts to
// build the final join statement SQL for the query and we can then return the
// final clause back to the callers as a single, stringified join statement.
$sql[] = "$type join $table on $clauses";
}
return implode(' ', $sql);
}
/**
* Create a join clause constraint segment.
*
* @param array $clause
* @return string
*/
protected function compileJoinConstraint(array $clause)
{
$first = $this->wrap($clause['first']);
$second = $clause['where'] ? '?' : $this->wrap($clause['second']);
return "{$clause['boolean']} $first {$clause['operator']} $second";
}
/**
* Compile the "where" portions of the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @return string
*/
protected function compileWheres(Builder $query)
{
$sql = array();
if (is_null($query->wheres)) return '';
// Each type of where clauses has its own compiler function which is responsible
// for actually creating the where clauses SQL. This helps keep the code nice
// and maintainable since each clause has a very small method that it uses.
foreach ($query->wheres as $where)
{
$method = "where{$where['type']}";
$sql[] = $where['boolean'].' '.$this->$method($query, $where);
}
// If we actually have some where clauses, we will strip off the first boolean
// operator, which is added by the query builders for convenience so we can
// avoid checking for the first clauses in each of the compilers methods.
if (count($sql) > 0)
{
$sql = implode(' ', $sql);
return 'where '.$this->removeLeadingBoolean($sql);
}
return '';
}
/**
* Compile a nested where clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereNested(Builder $query, $where)
{
$nested = $where['query'];
return '('.substr($this->compileWheres($nested), 6).')';
}
/**
* Compile a where condition with a sub-select.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereSub(Builder $query, $where)
{
$select = $this->compileSelect($where['query']);
return $this->wrap($where['column']).' '.$where['operator']." ($select)";
}
/**
* Compile a basic where clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereBasic(Builder $query, $where)
{
$value = $this->parameter($where['value']);
return $this->wrap($where['column']).' '.$where['operator'].' '.$value;
}
/**
* Compile a "between" where clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereBetween(Builder $query, $where)
{
$between = $where['not'] ? 'not between' : 'between';
return $this->wrap($where['column']).' '.$between.' ? and ?';
}
/**
* Compile a where exists clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereExists(Builder $query, $where)
{
return 'exists ('.$this->compileSelect($where['query']).')';
}
/**
* Compile a where exists clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereNotExists(Builder $query, $where)
{
return 'not exists ('.$this->compileSelect($where['query']).')';
}
/**
* Compile a "where in" clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereIn(Builder $query, $where)
{
if (empty($where['values'])) return '0 = 1';
$values = $this->parameterize($where['values']);
return $this->wrap($where['column']).' in ('.$values.')';
}
/**
* Compile a "where not in" clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereNotIn(Builder $query, $where)
{
if (empty($where['values'])) return '1 = 1';
$values = $this->parameterize($where['values']);
return $this->wrap($where['column']).' not in ('.$values.')';
}
/**
* Compile a where in sub-select clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereInSub(Builder $query, $where)
{
$select = $this->compileSelect($where['query']);
return $this->wrap($where['column']).' in ('.$select.')';
}
/**
* Compile a where not in sub-select clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereNotInSub(Builder $query, $where)
{
$select = $this->compileSelect($where['query']);
return $this->wrap($where['column']).' not in ('.$select.')';
}
/**
* Compile a "where null" clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereNull(Builder $query, $where)
{
return $this->wrap($where['column']).' is null';
}
/**
* Compile a "where not null" clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereNotNull(Builder $query, $where)
{
return $this->wrap($where['column']).' is not null';
}
/**
* Compile a "where date" clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereDate(Builder $query, $where)
{
return $this->dateBasedWhere('date', $query, $where);
}
/**
* Compile a "where day" clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereDay(Builder $query, $where)
{
return $this->dateBasedWhere('day', $query, $where);
}
/**
* Compile a "where month" clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereMonth(Builder $query, $where)
{
return $this->dateBasedWhere('month', $query, $where);
}
/**
* Compile a "where year" clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereYear(Builder $query, $where)
{
return $this->dateBasedWhere('year', $query, $where);
}
/**
* Compile a date based where clause.
*
* @param string $type
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function dateBasedWhere($type, Builder $query, $where)
{
$value = $this->parameter($where['value']);
return $type.'('.$this->wrap($where['column']).') '.$where['operator'].' '.$value;
}
/**
* Compile a raw where clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereRaw(Builder $query, $where)
{
return $where['sql'];
}
/**
* Compile the "group by" portions of the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $groups
* @return string
*/
protected function compileGroups(Builder $query, $groups)
{
return 'group by '.$this->columnize($groups);
}
/**
* Compile the "having" portions of the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $havings
* @return string
*/
protected function compileHavings(Builder $query, $havings)
{
$sql = implode(' ', array_map(array($this, 'compileHaving'), $havings));
return 'having '.$this->removeLeadingBoolean($sql);
}
/**
* Compile a single having clause.
*
* @param array $having
* @return string
*/
protected function compileHaving(array $having)
{
// If the having clause is "raw", we can just return the clause straight away
// without doing any more processing on it. Otherwise, we will compile the
// clause into SQL based on the components that make it up from builder.
if ($having['type'] === 'raw')
{
return $having['boolean'].' '.$having['sql'];
}
return $this->compileBasicHaving($having);
}
/**
* Compile a basic having clause.
*
* @param array $having
* @return string
*/
protected function compileBasicHaving($having)
{
$column = $this->wrap($having['column']);
$parameter = $this->parameter($having['value']);
return $having['boolean'].' '.$column.' '.$having['operator'].' '.$parameter;
}
/**
* Compile the "order by" portions of the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $orders
* @return string
*/
protected function compileOrders(Builder $query, $orders)
{
return 'order by '.implode(', ', array_map(function($order)
{
if (isset($order['sql'])) return $order['sql'];
return $this->wrap($order['column']).' '.$order['direction'];
}, $orders));
}
/**
* Compile the "limit" portions of the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param int $limit
* @return string
*/
protected function compileLimit(Builder $query, $limit)
{
return 'limit '.(int) $limit;
}
/**
* Compile the "offset" portions of the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param int $offset
* @return string
*/
protected function compileOffset(Builder $query, $offset)
{
return 'offset '.(int) $offset;
}
/**
* Compile the "union" queries attached to the main query.
*
* @param \Illuminate\Database\Query\Builder $query
* @return string
*/
protected function compileUnions(Builder $query)
{
$sql = '';
foreach ($query->unions as $union)
{
$sql .= $this->compileUnion($union);
}
if (isset($query->unionOrders))
{
$sql .= ' '.$this->compileOrders($query, $query->unionOrders);
}
if (isset($query->unionLimit))
{
$sql .= ' '.$this->compileLimit($query, $query->unionLimit);
}
if (isset($query->unionOffset))
{
$sql .= ' '.$this->compileOffset($query, $query->unionOffset);
}
return ltrim($sql);
}
/**
* Compile a single union statement.
*
* @param array $union
* @return string
*/
protected function compileUnion(array $union)
{
$joiner = $union['all'] ? ' union all ' : ' union ';
return $joiner.$union['query']->toSql();
}
/**
* Compile an insert statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $values
* @return string
*/
public function compileInsert(Builder $query, array $values)
{
// Essentially we will force every insert to be treated as a batch insert which
// simply makes creating the SQL easier for us since we can utilize the same
// basic routine regardless of an amount of records given to us to insert.
$table = $this->wrapTable($query->from);
if ( ! is_array(reset($values)))
{
$values = array($values);
}
$columns = $this->columnize(array_keys(reset($values)));
// We need to build a list of parameter place-holders of values that are bound
// to the query. Each insert should have the exact same amount of parameter
// bindings so we can just go off the first list of values in this array.
$parameters = $this->parameterize(reset($values));
$value = array_fill(0, count($values), "($parameters)");
$parameters = implode(', ', $value);
return "insert into $table ($columns) values $parameters";
}
/**
* Compile an insert and get ID statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $values
* @param string $sequence
* @return string
*/
public function compileInsertGetId(Builder $query, $values, $sequence)
{
return $this->compileInsert($query, $values);
}
/**
* Compile an update statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $values
* @return string
*/
public function compileUpdate(Builder $query, $values)
{
$table = $this->wrapTable($query->from);
// Each one of the columns in the update statements needs to be wrapped in the
// keyword identifiers, also a place-holder needs to be created for each of
// the values in the list of bindings so we can make the sets statements.
$columns = array();
foreach ($values as $key => $value)
{
$columns[] = $this->wrap($key).' = '.$this->parameter($value);
}
$columns = implode(', ', $columns);
// If the query has any "join" clauses, we will setup the joins on the builder
// and compile them so we can attach them to this update, as update queries
// can get join statements to attach to other tables when they're needed.
if (isset($query->joins))
{
$joins = ' '.$this->compileJoins($query, $query->joins);
}
else
{
$joins = '';
}
// Of course, update queries may also be constrained by where clauses so we'll
// need to compile the where clauses and attach it to the query so only the
// intended records are updated by the SQL statements we generate to run.
$where = $this->compileWheres($query);
return trim("update {$table}{$joins} set $columns $where");
}
/**
* Compile a delete statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @return string
*/
public function compileDelete(Builder $query)
{
$table = $this->wrapTable($query->from);
$where = is_array($query->wheres) ? $this->compileWheres($query) : '';
return trim("delete from $table ".$where);
}
/**
* Compile a truncate table statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @return array
*/
public function compileTruncate(Builder $query)
{
return array('truncate '.$this->wrapTable($query->from) => array());
}
/**
* Compile the lock into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param bool|string $value
* @return string
*/
protected function compileLock(Builder $query, $value)
{
return is_string($value) ? $value : '';
}
/**
* Concatenate an array of segments, removing empties.
*
* @param array $segments
* @return string
*/
protected function concatenate($segments)
{
return implode(' ', array_filter($segments, function($value)
{
return (string) $value !== '';
}));
}
/**
* Remove the leading boolean from a statement.
*
* @param string $value
* @return string
*/
protected function removeLeadingBoolean($value)
{
return preg_replace('/and |or /', '', $value, 1);
}
}

View File

@ -0,0 +1,144 @@
<?php namespace Illuminate\Database\Query\Grammars;
use Illuminate\Database\Query\Builder;
class MySqlGrammar extends Grammar {
/**
* The components that make up a select clause.
*
* @var array
*/
protected $selectComponents = array(
'aggregate',
'columns',
'from',
'joins',
'wheres',
'groups',
'havings',
'orders',
'limit',
'offset',
'lock',
);
/**
* Compile a select query into SQL.
*
* @param \Illuminate\Database\Query\Builder
* @return string
*/
public function compileSelect(Builder $query)
{
$sql = parent::compileSelect($query);
if ($query->unions)
{
$sql = '('.$sql.') '.$this->compileUnions($query);
}
return $sql;
}
/**
* Compile a single union statement.
*
* @param array $union
* @return string
*/
protected function compileUnion(array $union)
{
$joiner = $union['all'] ? ' union all ' : ' union ';
return $joiner.'('.$union['query']->toSql().')';
}
/**
* Compile the lock into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param bool|string $value
* @return string
*/
protected function compileLock(Builder $query, $value)
{
if (is_string($value)) return $value;
return $value ? 'for update' : 'lock in share mode';
}
/**
* Compile an update statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $values
* @return string
*/
public function compileUpdate(Builder $query, $values)
{
$sql = parent::compileUpdate($query, $values);
if (isset($query->orders))
{
$sql .= ' '.$this->compileOrders($query, $query->orders);
}
if (isset($query->limit))
{
$sql .= ' '.$this->compileLimit($query, $query->limit);
}
return rtrim($sql);
}
/**
* Compile a delete statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @return string
*/
public function compileDelete(Builder $query)
{
$table = $this->wrapTable($query->from);
$where = is_array($query->wheres) ? $this->compileWheres($query) : '';
if (isset($query->joins))
{
$joins = ' '.$this->compileJoins($query, $query->joins);
$sql = trim("delete $table from {$table}{$joins} $where");
}
else
{
$sql = trim("delete from $table $where");
}
if (isset($query->orders))
{
$sql .= ' '.$this->compileOrders($query, $query->orders);
}
if (isset($query->limit))
{
$sql .= ' '.$this->compileLimit($query, $query->limit);
}
return $sql;
}
/**
* Wrap a single string in keyword identifiers.
*
* @param string $value
* @return string
*/
protected function wrapValue($value)
{
if ($value === '*') return $value;
return '`'.str_replace('`', '``', $value).'`';
}
}

View File

@ -0,0 +1,174 @@
<?php namespace Illuminate\Database\Query\Grammars;
use Illuminate\Database\Query\Builder;
class PostgresGrammar extends Grammar {
/**
* All of the available clause operators.
*
* @var array
*/
protected $operators = array(
'=', '<', '>', '<=', '>=', '<>', '!=',
'like', 'not like', 'between', 'ilike',
'&', '|', '#', '<<', '>>',
);
/**
* Compile the lock into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param bool|string $value
* @return string
*/
protected function compileLock(Builder $query, $value)
{
if (is_string($value)) return $value;
return $value ? 'for update' : 'for share';
}
/**
* Compile an update statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $values
* @return string
*/
public function compileUpdate(Builder $query, $values)
{
$table = $this->wrapTable($query->from);
// Each one of the columns in the update statements needs to be wrapped in the
// keyword identifiers, also a place-holder needs to be created for each of
// the values in the list of bindings so we can make the sets statements.
$columns = $this->compileUpdateColumns($values);
$from = $this->compileUpdateFrom($query);
$where = $this->compileUpdateWheres($query);
return trim("update {$table} set {$columns}{$from} $where");
}
/**
* Compile the columns for the update statement.
*
* @param array $values
* @return string
*/
protected function compileUpdateColumns($values)
{
$columns = array();
// When gathering the columns for an update statement, we'll wrap each of the
// columns and convert it to a parameter value. Then we will concatenate a
// list of the columns that can be added into this update query clauses.
foreach ($values as $key => $value)
{
$columns[] = $this->wrap($key).' = '.$this->parameter($value);
}
return implode(', ', $columns);
}
/**
* Compile the "from" clause for an update with a join.
*
* @param \Illuminate\Database\Query\Builder $query
* @return string
*/
protected function compileUpdateFrom(Builder $query)
{
if ( ! isset($query->joins)) return '';
$froms = array();
// When using Postgres, updates with joins list the joined tables in the from
// clause, which is different than other systems like MySQL. Here, we will
// compile out the tables that are joined and add them to a from clause.
foreach ($query->joins as $join)
{
$froms[] = $this->wrapTable($join->table);
}
if (count($froms) > 0) return ' from '.implode(', ', $froms);
}
/**
* Compile the additional where clauses for updates with joins.
*
* @param \Illuminate\Database\Query\Builder $query
* @return string
*/
protected function compileUpdateWheres(Builder $query)
{
$baseWhere = $this->compileWheres($query);
if ( ! isset($query->joins)) return $baseWhere;
// Once we compile the join constraints, we will either use them as the where
// clause or append them to the existing base where clauses. If we need to
// strip the leading boolean we will do so when using as the only where.
$joinWhere = $this->compileUpdateJoinWheres($query);
if (trim($baseWhere) == '')
{
return 'where '.$this->removeLeadingBoolean($joinWhere);
}
return $baseWhere.' '.$joinWhere;
}
/**
* Compile the "join" clauses for an update.
*
* @param \Illuminate\Database\Query\Builder $query
* @return string
*/
protected function compileUpdateJoinWheres(Builder $query)
{
$joinWheres = array();
// Here we will just loop through all of the join constraints and compile them
// all out then implode them. This should give us "where" like syntax after
// everything has been built and then we will join it to the real wheres.
foreach ($query->joins as $join)
{
foreach ($join->clauses as $clause)
{
$joinWheres[] = $this->compileJoinConstraint($clause);
}
}
return implode(' ', $joinWheres);
}
/**
* Compile an insert and get ID statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $values
* @param string $sequence
* @return string
*/
public function compileInsertGetId(Builder $query, $values, $sequence)
{
if (is_null($sequence)) $sequence = 'id';
return $this->compileInsert($query, $values).' returning '.$this->wrap($sequence);
}
/**
* Compile a truncate table statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @return array
*/
public function compileTruncate(Builder $query)
{
return array('truncate '.$this->wrapTable($query->from).' restart identity' => array());
}
}

View File

@ -0,0 +1,130 @@
<?php namespace Illuminate\Database\Query\Grammars;
use Illuminate\Database\Query\Builder;
class SQLiteGrammar extends Grammar {
/**
* All of the available clause operators.
*
* @var array
*/
protected $operators = array(
'=', '<', '>', '<=', '>=', '<>', '!=',
'like', 'not like', 'between', 'ilike',
'&', '|', '<<', '>>',
);
/**
* Compile an insert statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $values
* @return string
*/
public function compileInsert(Builder $query, array $values)
{
// Essentially we will force every insert to be treated as a batch insert which
// simply makes creating the SQL easier for us since we can utilize the same
// basic routine regardless of an amount of records given to us to insert.
$table = $this->wrapTable($query->from);
if ( ! is_array(reset($values)))
{
$values = array($values);
}
// If there is only one record being inserted, we will just use the usual query
// grammar insert builder because no special syntax is needed for the single
// row inserts in SQLite. However, if there are multiples, we'll continue.
if (count($values) == 1)
{
return parent::compileInsert($query, reset($values));
}
$names = $this->columnize(array_keys(reset($values)));
$columns = array();
// SQLite requires us to build the multi-row insert as a listing of select with
// unions joining them together. So we'll build out this list of columns and
// then join them all together with select unions to complete the queries.
foreach (array_keys(reset($values)) as $column)
{
$columns[] = '? as '.$this->wrap($column);
}
$columns = array_fill(0, count($values), implode(', ', $columns));
return "insert into $table ($names) select ".implode(' union all select ', $columns);
}
/**
* Compile a truncate table statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @return array
*/
public function compileTruncate(Builder $query)
{
$sql = array('delete from sqlite_sequence where name = ?' => array($query->from));
$sql['delete from '.$this->wrapTable($query->from)] = array();
return $sql;
}
/**
* Compile a "where day" clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereDay(Builder $query, $where)
{
return $this->dateBasedWhere('%d', $query, $where);
}
/**
* Compile a "where month" clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereMonth(Builder $query, $where)
{
return $this->dateBasedWhere('%m', $query, $where);
}
/**
* Compile a "where year" clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereYear(Builder $query, $where)
{
return $this->dateBasedWhere('%Y', $query, $where);
}
/**
* Compile a date based where clause.
*
* @param string $type
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function dateBasedWhere($type, Builder $query, $where)
{
$value = str_pad($where['value'], 2, '0', STR_PAD_LEFT);
$value = $this->parameter($value);
return 'strftime(\''.$type.'\', '.$this->wrap($where['column']).') '.$where['operator'].' '.$value;
}
}

View File

@ -0,0 +1,224 @@
<?php namespace Illuminate\Database\Query\Grammars;
use Illuminate\Database\Query\Builder;
class SqlServerGrammar extends Grammar {
/**
* All of the available clause operators.
*
* @var array
*/
protected $operators = array(
'=', '<', '>', '<=', '>=', '!<', '!>', '<>', '!=',
'like', 'not like', 'between', 'ilike',
'&', '&=', '|', '|=', '^', '^=',
);
/**
* Compile a select query into SQL.
*
* @param \Illuminate\Database\Query\Builder
* @return string
*/
public function compileSelect(Builder $query)
{
$components = $this->compileComponents($query);
// If an offset is present on the query, we will need to wrap the query in
// a big "ANSI" offset syntax block. This is very nasty compared to the
// other database systems but is necessary for implementing features.
if ($query->offset > 0)
{
return $this->compileAnsiOffset($query, $components);
}
return $this->concatenate($components);
}
/**
* Compile the "select *" portion of the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $columns
* @return string
*/
protected function compileColumns(Builder $query, $columns)
{
if ( ! is_null($query->aggregate)) return;
$select = $query->distinct ? 'select distinct ' : 'select ';
// If there is a limit on the query, but not an offset, we will add the top
// clause to the query, which serves as a "limit" type clause within the
// SQL Server system similar to the limit keywords available in MySQL.
if ($query->limit > 0 && $query->offset <= 0)
{
$select .= 'top '.$query->limit.' ';
}
return $select.$this->columnize($columns);
}
/**
* Compile the "from" portion of the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param string $table
* @return string
*/
protected function compileFrom(Builder $query, $table)
{
$from = parent::compileFrom($query, $table);
if (is_string($query->lock)) return $from.' '.$query->lock;
if ( ! is_null($query->lock))
{
return $from.' with(rowlock,'.($query->lock ? 'updlock,' : '').'holdlock)';
}
return $from;
}
/**
* Create a full ANSI offset clause for the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $components
* @return string
*/
protected function compileAnsiOffset(Builder $query, $components)
{
// An ORDER BY clause is required to make this offset query work, so if one does
// not exist we'll just create a dummy clause to trick the database and so it
// does not complain about the queries for not having an "order by" clause.
if ( ! isset($components['orders']))
{
$components['orders'] = 'order by (select 0)';
}
// We need to add the row number to the query so we can compare it to the offset
// and limit values given for the statements. So we will add an expression to
// the "select" that will give back the row numbers on each of the records.
$orderings = $components['orders'];
$components['columns'] .= $this->compileOver($orderings);
unset($components['orders']);
// Next we need to calculate the constraints that should be placed on the query
// to get the right offset and limit from our query but if there is no limit
// set we will just handle the offset only since that is all that matters.
$constraint = $this->compileRowConstraint($query);
$sql = $this->concatenate($components);
// We are now ready to build the final SQL query so we'll create a common table
// expression from the query and get the records with row numbers within our
// given limit and offset value that we just put on as a query constraint.
return $this->compileTableExpression($sql, $constraint);
}
/**
* Compile the over statement for a table expression.
*
* @param string $orderings
* @return string
*/
protected function compileOver($orderings)
{
return ", row_number() over ({$orderings}) as row_num";
}
/**
* Compile the limit / offset row constraint for a query.
*
* @param \Illuminate\Database\Query\Builder $query
* @return string
*/
protected function compileRowConstraint($query)
{
$start = $query->offset + 1;
if ($query->limit > 0)
{
$finish = $query->offset + $query->limit;
return "between {$start} and {$finish}";
}
return ">= {$start}";
}
/**
* Compile a common table expression for a query.
*
* @param string $sql
* @param string $constraint
* @return string
*/
protected function compileTableExpression($sql, $constraint)
{
return "select * from ({$sql}) as temp_table where row_num {$constraint}";
}
/**
* Compile the "limit" portions of the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param int $limit
* @return string
*/
protected function compileLimit(Builder $query, $limit)
{
return '';
}
/**
* Compile the "offset" portions of the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param int $offset
* @return string
*/
protected function compileOffset(Builder $query, $offset)
{
return '';
}
/**
* Compile a truncate table statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @return array
*/
public function compileTruncate(Builder $query)
{
return array('truncate table '.$this->wrapTable($query->from) => array());
}
/**
* Get the format for database stored dates.
*
* @return string
*/
public function getDateFormat()
{
return 'Y-m-d H:i:s.000';
}
/**
* Wrap a single string in keyword identifiers.
*
* @param string $value
* @return string
*/
protected function wrapValue($value)
{
if ($value === '*') return $value;
return '['.str_replace(']', ']]', $value).']';
}
}

View File

@ -0,0 +1,151 @@
<?php namespace Illuminate\Database\Query;
class JoinClause {
/**
* The type of join being performed.
*
* @var string
*/
public $type;
/**
* The table the join clause is joining to.
*
* @var string
*/
public $table;
/**
* The "on" clauses for the join.
*
* @var array
*/
public $clauses = array();
/**
* The "on" bindings for the join.
*
* @var array
*/
public $bindings = array();
/**
* Create a new join clause instance.
*
* @param string $type
* @param string $table
* @return void
*/
public function __construct($type, $table)
{
$this->type = $type;
$this->table = $table;
}
/**
* Add an "on" clause to the join.
*
* @param string $first
* @param string $operator
* @param string $second
* @param string $boolean
* @param bool $where
* @return $this
*/
public function on($first, $operator, $second, $boolean = 'and', $where = false)
{
$this->clauses[] = compact('first', 'operator', 'second', 'boolean', 'where');
if ($where) $this->bindings[] = $second;
return $this;
}
/**
* Add an "or on" clause to the join.
*
* @param string $first
* @param string $operator
* @param string $second
* @return \Illuminate\Database\Query\JoinClause
*/
public function orOn($first, $operator, $second)
{
return $this->on($first, $operator, $second, 'or');
}
/**
* Add an "on where" clause to the join.
*
* @param string $first
* @param string $operator
* @param string $second
* @param string $boolean
* @return \Illuminate\Database\Query\JoinClause
*/
public function where($first, $operator, $second, $boolean = 'and')
{
return $this->on($first, $operator, $second, $boolean, true);
}
/**
* Add an "or on where" clause to the join.
*
* @param string $first
* @param string $operator
* @param string $second
* @return \Illuminate\Database\Query\JoinClause
*/
public function orWhere($first, $operator, $second)
{
return $this->on($first, $operator, $second, 'or', true);
}
/**
* Add an "on where is null" clause to the join.
*
* @param string $column
* @param string $boolean
* @return \Illuminate\Database\Query\JoinClause
*/
public function whereNull($column, $boolean = 'and')
{
return $this->on($column, 'is', new Expression('null'), $boolean, false);
}
/**
* Add an "or on where is null" clause to the join.
*
* @param string $column
* @return \Illuminate\Database\Query\JoinClause
*/
public function orWhereNull($column)
{
return $this->whereNull($column, 'or');
}
/**
* Add an "on where is not null" clause to the join.
*
* @param string $column
* @param string $boolean
* @return \Illuminate\Database\Query\JoinClause
*/
public function whereNotNull($column, $boolean = 'and')
{
return $this->on($column, 'is', new Expression('not null'), $boolean, false);
}
/**
* Add an "or on where is not null" clause to the join.
*
* @param string $column
* @return \Illuminate\Database\Query\JoinClause
*/
public function orWhereNotNull($column)
{
return $this->whereNotNull($column, 'or');
}
}

View File

@ -0,0 +1,23 @@
<?php namespace Illuminate\Database\Query\Processors;
class MySqlProcessor extends Processor {
/**
* Process the results of a column listing query.
*
* @param array $results
* @return array
*/
public function processColumnListing($results)
{
$mapping = function($r)
{
$r = (object) $r;
return $r->column_name;
};
return array_map($mapping, $results);
}
}

View File

@ -0,0 +1,47 @@
<?php namespace Illuminate\Database\Query\Processors;
use Illuminate\Database\Query\Builder;
class PostgresProcessor extends Processor {
/**
* Process an "insert get ID" query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param string $sql
* @param array $values
* @param string $sequence
* @return int
*/
public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
{
$results = $query->getConnection()->selectFromWriteConnection($sql, $values);
$sequence = $sequence ?: 'id';
$result = (array) $results[0];
$id = $result[$sequence];
return is_numeric($id) ? (int) $id : $id;
}
/**
* Process the results of a column listing query.
*
* @param array $results
* @return array
*/
public function processColumnListing($results)
{
$mapping = function($r)
{
$r = (object) $r;
return $r->column_name;
};
return array_map($mapping, $results);
}
}

View File

@ -0,0 +1,48 @@
<?php namespace Illuminate\Database\Query\Processors;
use Illuminate\Database\Query\Builder;
class Processor {
/**
* Process the results of a "select" query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $results
* @return array
*/
public function processSelect(Builder $query, $results)
{
return $results;
}
/**
* Process an "insert get ID" query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param string $sql
* @param array $values
* @param string $sequence
* @return int
*/
public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
{
$query->getConnection()->insert($sql, $values);
$id = $query->getConnection()->getPdo()->lastInsertId($sequence);
return is_numeric($id) ? (int) $id : $id;
}
/**
* Process the results of a column listing query.
*
* @param array $results
* @return array
*/
public function processColumnListing($results)
{
return $results;
}
}

View File

@ -0,0 +1,23 @@
<?php namespace Illuminate\Database\Query\Processors;
class SQLiteProcessor extends Processor {
/**
* Process the results of a column listing query.
*
* @param array $results
* @return array
*/
public function processColumnListing($results)
{
$mapping = function($r)
{
$r = (object) $r;
return $r->name;
};
return array_map($mapping, $results);
}
}

View File

@ -0,0 +1,41 @@
<?php namespace Illuminate\Database\Query\Processors;
use Illuminate\Database\Query\Builder;
class SqlServerProcessor extends Processor {
/**
* Process an "insert get ID" query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param string $sql
* @param array $values
* @param string $sequence
* @return int
*/
public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
{
$query->getConnection()->insert($sql, $values);
$id = $query->getConnection()->getPdo()->lastInsertId();
return is_numeric($id) ? (int) $id : $id;
}
/**
* Process the results of a column listing query.
*
* @param array $results
* @return array
*/
public function processColumnListing($results)
{
$mapping = function($r)
{
return $r->name;
};
return array_map($mapping, $results);
}
}