Files
lgp-admin-plus-api/laravel/database/grammars/grammar.php

303 lines
7.0 KiB
PHP
Raw Normal View History

2011-09-27 21:10:32 -05:00
<?php namespace Laravel\Database\Grammars; use Laravel\Arr, Laravel\Database\Query;
2011-08-22 21:56:42 -05:00
class Grammar {
2011-08-22 21:56:42 -05:00
/**
2011-09-27 21:10:32 -05:00
* All of the query componenets in the order they should be built.
*
* @var array
*/
protected $components = array('selects', 'from', 'joins', 'wheres', 'orderings', 'limit', 'offset');
/**
* The keyword identifier for the database system.
*
* @var string
*/
protected $wrapper = '"';
/**
* Compile a SQL SELECT statement from a Query instance.
*
* The query will be compiled according to the order of the elements specified
* in the "components" property. The entire query is pased into each component
* compiler for convenience.
2011-08-22 21:56:42 -05:00
*
* @param Query $query
* @return string
*/
public function select(Query $query)
{
2011-09-27 21:10:32 -05:00
$sql = array();
2011-08-22 21:56:42 -05:00
2011-09-27 21:10:32 -05:00
// Iterate through each query component, calling the compiler for that
// component, and passing the query instance into the compiler.
foreach ($this->components as $component)
2011-08-22 21:56:42 -05:00
{
2011-09-27 21:10:32 -05:00
if ( ! is_null($query->$component)) $sql[] = call_user_func(array($this, $component), $query);
2011-08-22 21:56:42 -05:00
}
2011-09-27 21:10:32 -05:00
return implode(' ', Arr::without($sql, array(null, '')));
2011-08-22 21:56:42 -05:00
}
/**
2011-09-27 21:10:32 -05:00
* Compile the SELECT clause for a query.
2011-08-22 21:56:42 -05:00
*
* @param Query $query
* @return string
*/
2011-09-27 21:10:32 -05:00
protected function selects(Query $query)
2011-08-22 21:56:42 -05:00
{
2011-09-27 21:10:32 -05:00
if ( ! is_null($query->aggregate)) return $this->aggregate($query);
return (($query->distinct) ? 'SELECT DISTINCT ' : 'SELECT ').$this->columnize($query->selects);
2011-08-22 21:56:42 -05:00
}
/**
2011-09-27 21:10:32 -05:00
* Compile an aggregating SELECT clause for a query.
2011-08-22 21:56:42 -05:00
*
2011-09-27 21:10:32 -05:00
* This method compiled the SELECT clauses for queries built using the
* count, max, min, abs, and sum methods on the fluent query builder.
*
* @param Query $query
2011-08-22 21:56:42 -05:00
* @return string
*/
2011-09-27 21:10:32 -05:00
protected function aggregate(Query $query)
2011-08-22 21:56:42 -05:00
{
2011-09-27 21:10:32 -05:00
list($aggregator, $column) = array($query->aggregate['aggregator'], $query->aggregate['column']);
return 'SELECT '.$aggregator.'('.$this->wrap($column).')';
2011-08-22 21:56:42 -05:00
}
/**
2011-09-27 21:10:32 -05:00
* Compile the FROM clause for a query.
2011-08-22 21:56:42 -05:00
*
2011-09-27 21:10:32 -05:00
* @param Query $query
2011-08-22 21:56:42 -05:00
* @return string
*/
2011-09-27 21:10:32 -05:00
protected function from(Query $query)
2011-08-22 21:56:42 -05:00
{
2011-09-27 21:10:32 -05:00
return 'FROM '.$this->wrap($query->from);
2011-08-22 21:56:42 -05:00
}
/**
2011-09-27 21:10:32 -05:00
* Compile the JOIN clauses for a query.
2011-08-22 21:56:42 -05:00
*
2011-09-27 21:10:32 -05:00
* @param Query $query
2011-08-22 21:56:42 -05:00
* @return string
*/
2011-09-27 21:10:32 -05:00
protected function joins(Query $query)
2011-08-22 21:56:42 -05:00
{
2011-09-27 21:10:32 -05:00
foreach ($query->joins as $join)
2011-08-22 21:56:42 -05:00
{
extract($join);
$sql[] = $type.' JOIN '.$this->wrap($table).' ON '.$this->wrap($column1).' '.$operator.' '.$this->wrap($column2);
}
return implode(' ', $sql);
}
/**
2011-09-27 21:10:32 -05:00
* Compile the WHERE clause for a query.
2011-08-22 21:56:42 -05:00
*
2011-09-27 21:10:32 -05:00
* @param Query $query
2011-08-22 21:56:42 -05:00
* @return string
*/
2011-09-27 21:10:32 -05:00
protected function wheres(Query $query)
2011-08-22 21:56:42 -05:00
{
2011-09-27 21:10:32 -05:00
// Each WHERE clause array has a "type" that is assigned by the query builder, and
// each type has its own compiler function. For example, "where in" queries are
// compiled by the "where_in" function.
//
// The only exception to this rule are "raw" where clauses, which are simply
// appended to the query as-is, without any further compiling.
2011-08-22 21:56:42 -05:00
foreach ($wheres as $where)
{
2011-09-27 21:10:32 -05:00
$sql[] = ($where['type'] == 'raw') ? $where['sql'] : $where['connector'].' '.$this->{$where['type']}($where);
2011-08-22 21:56:42 -05:00
}
2011-09-27 21:10:32 -05:00
return implode(' ', array_merge(array('WHERE 1 = 1'), $sql));
2011-08-22 21:56:42 -05:00
}
/**
* Compile a simple WHERE clause.
*
2011-09-27 21:10:32 -05:00
* This method compiles the SQL for the "where" and "or_where" query functions.
*
2011-08-22 21:56:42 -05:00
* @param array $where
* @return string
*/
2011-09-27 21:10:32 -05:00
protected function where($where)
2011-08-22 21:56:42 -05:00
{
return $this->wrap($where['column']).' '.$where['operator'].' ?';
}
/**
* Compile a WHERE IN clause.
*
2011-09-27 21:10:32 -05:00
* This method compiled the SQL for all of the "where_in" style query functions.
*
2011-08-22 21:56:42 -05:00
* @param array $where
* @return string
*/
2011-09-27 21:10:32 -05:00
protected function where_in($where)
2011-08-22 21:56:42 -05:00
{
$operator = ($where['not']) ? 'NOT IN' : 'IN';
return $this->wrap($where['column']).' '.$operator.' ('.$this->parameterize($where['values']).')';
}
/**
* Compile a WHERE NULL clause.
*
2011-09-27 21:10:32 -05:00
* This method compiles the SQL for all of the "where_null" style query functions.
*
2011-08-22 21:56:42 -05:00
* @param array $where
* @return string
*/
2011-09-27 21:10:32 -05:00
protected function where_null($where)
2011-08-22 21:56:42 -05:00
{
$operator = ($where['not']) ? 'NOT NULL' : 'NULL';
return $this->wrap($where['column']).' IS '.$operator;
}
/**
2011-09-27 21:10:32 -05:00
* Compile ORDER BY clause for a query.
2011-08-22 21:56:42 -05:00
*
2011-09-27 21:10:32 -05:00
* @param Query $query
2011-08-22 21:56:42 -05:00
* @return string
*/
2011-09-27 21:10:32 -05:00
protected function orderings(Query $query)
2011-08-22 21:56:42 -05:00
{
2011-09-27 21:10:32 -05:00
foreach ($query->orderings as $ordering)
2011-08-22 21:56:42 -05:00
{
$sql[] = $this->wrap($ordering['column']).' '.strtoupper($ordering['direction']);
}
return 'ORDER BY '.implode(', ', $sql);
}
/**
2011-09-27 21:10:32 -05:00
* Compile the LIMIT clause for a query.
2011-08-22 21:56:42 -05:00
*
2011-09-27 21:10:32 -05:00
* @param Query $query
2011-08-22 21:56:42 -05:00
* @return string
*/
2011-09-27 21:10:32 -05:00
protected function limit(Query $query)
2011-08-22 21:56:42 -05:00
{
2011-09-27 21:10:32 -05:00
return 'LIMIT '.$query->limit;
2011-08-22 21:56:42 -05:00
}
/**
2011-09-27 21:10:32 -05:00
* Compile the OFFSET clause for a query.
2011-08-22 21:56:42 -05:00
*
2011-09-27 21:10:32 -05:00
* @param Query $query
2011-08-22 21:56:42 -05:00
* @return string
*/
2011-09-27 21:10:32 -05:00
protected function offset(Query $query)
2011-08-22 21:56:42 -05:00
{
2011-09-27 21:10:32 -05:00
return 'OFFSET '.$query->offset;
2011-08-22 21:56:42 -05:00
}
/**
* Compile a SQL INSERT statment from a Query instance.
*
* @param Query $query
* @param array $values
* @return string
*/
public function insert(Query $query, $values)
{
2011-09-27 21:10:32 -05:00
$columns = implode(', ', $this->columnize(array_keys($values)));
2011-08-22 21:56:42 -05:00
2011-09-27 21:10:32 -05:00
return 'INSERT INTO '.$this->wrap($query->from).' ('.$columns.') VALUES ('.$this->parameterize($values).')';
2011-08-22 21:56:42 -05:00
}
/**
2011-09-27 21:10:32 -05:00
* Compile a SQL UPDATE statment from a Query instance.
2011-08-22 21:56:42 -05:00
*
* @param Query $query
* @param array $values
* @return string
*/
2011-09-27 21:10:32 -05:00
public function update(Query $query, $values)
2011-08-22 21:56:42 -05:00
{
2011-09-27 21:10:32 -05:00
foreach (array_keys($values) as $column)
{
$sets[] = $this->wrap($column).' = ?';
}
$sql = 'UPDATE '.$this->wrap($query->from).' SET '.implode(', ', $sets);
return (count($query->wheres) > 0) ? $sql.' '.$this->wheres($query->wheres) : $sql;
2011-08-22 21:56:42 -05:00
}
/**
2011-09-27 21:10:32 -05:00
* Compile a SQL DELETE statment from a Query instance.
2011-08-22 21:56:42 -05:00
*
* @param Query $query
* @return string
*/
2011-09-27 21:10:32 -05:00
public function delete(Query $query)
2011-08-22 21:56:42 -05:00
{
2011-09-27 21:10:32 -05:00
$sql = 'DELETE FROM '.$this->wrap($query->from);
2011-08-22 21:56:42 -05:00
2011-09-27 21:10:32 -05:00
return (count($query->wheres) > 0) ? $sql.' '.$this->wheres($query->wheres) : $sql;
2011-08-22 21:56:42 -05:00
}
/**
2011-09-27 21:10:32 -05:00
* Create a comma-delimited list of wrapped column names.
2011-08-22 21:56:42 -05:00
*
2011-09-27 21:10:32 -05:00
* @param array $columns
2011-08-22 21:56:42 -05:00
* @return string
*/
2011-09-27 21:10:32 -05:00
protected function columnize($columns)
2011-08-22 21:56:42 -05:00
{
2011-09-27 21:10:32 -05:00
return implode(', ', array_map(array($this, 'wrap'), $columns));
2011-08-22 21:56:42 -05:00
}
/**
* Wrap a value in keyword identifiers.
*
2011-09-27 21:10:32 -05:00
* @param string $value
2011-08-22 21:56:42 -05:00
* @return string
*/
public function wrap($value)
{
if (strpos(strtolower($value), ' as ') !== false) return $this->wrap_alias($value);
foreach (explode('.', $value) as $segment)
{
2011-09-27 21:10:32 -05:00
$wrapped[] = ($segment != '*') ? $this->wrapper.$segment.$this->wrapper : $segment;
2011-08-22 21:56:42 -05:00
}
return implode('.', $wrapped);
}
/**
* Wrap an alias in keyword identifiers.
*
2011-09-27 21:10:32 -05:00
* @param string $value
2011-08-22 21:56:42 -05:00
* @return string
*/
public function wrap_alias($value)
{
$segments = explode(' ', $value);
return $this->wrap($segments[0]).' AS '.$this->wrap($segments[2]);
}
/**
* Create query parameters from an array of values.
*
2011-09-27 21:10:32 -05:00
* @param array $values
2011-08-22 21:56:42 -05:00
* @return string
*/
2011-09-27 21:10:32 -05:00
public function parameterize($values)
{
return implode(', ', array_fill(0, count($values), '?'));
}
2011-08-22 21:56:42 -05:00
}