Commit 5ca2e2b7 authored by Taylor Otwell's avatar Taylor Otwell

db refactoring.

parent 07009102
<?php namespace Laravel\DB;
class Compiler {
/**
* Compile a SQL SELECT statment from a Query instance.
*
* @param Query $query
* @return string
*/
public function select(Query $query)
{
$sql = $query->select.' '.$query->from.' '.$query->where;
if ( ! is_null($query->order)) $sql .= ' '.$query->order;
if ( ! is_null($query->limit)) $sql .= ' '.$query->limit;
if ( ! is_null($query->offset)) $sql .= ' '.$query->offset;
return $sql;
}
/**
* Compile a SQL INSERT statment from a Query instance.
*
* @param Query $query
* @param array $values
* @return string
*/
public function insert(Query $query, $values)
{
$sql = 'INSERT INTO '.$query->wrap($query->table);
$columns = array_map(array($query, 'wrap'), array_keys($values));
return $sql .= ' ('.implode(', ', $columns).') VALUES ('.$query->parameterize($values).')';
}
/**
* Compile a SQL UPDATE statment from a Query instance.
*
* @param Query $query
* @param array $values
* @return string
*/
public function update(Query $query, $values)
{
$sql = 'UPDATE '.$query->wrap($query->table).' SET ';
foreach (array_keys($values) as $column)
{
$sets[] = $query->wrap($column).' = ?';
}
return $sql.implode(', ', $sets).' '.$query->where;
}
/**
* Compile a SQL DELETE statment from a Query instance.
*
* @param Query $query
* @return string
*/
public function delete(Query $query)
{
return 'DELETE FROM '.$query->wrap($query->table).' '.$query->where;
}
}
\ No newline at end of file
......@@ -152,7 +152,7 @@ class Connection {
*/
public function table($table)
{
return Query\Factory::make($table, $this);
return Query\Factory::make($table, $this, Query\Compiler\Factory::make($this));
}
/**
......
......@@ -19,6 +19,11 @@ class MySQL extends Connector {
$dsn .= ';port='.$config['port'];
}
if (isset($config['socket']))
{
$dsn .= ';unix_socket='.$config['socket'];
}
$connection = new \PDO($dsn, $config['username'], $config['password'], $this->options);
if (isset($config['charset']))
......
This diff is collapsed.
<?php namespace Laravel\DB\Query;
use Laravel\DB\Query;
class Compiler {
/**
* Compile a SQL SELECT statment from a Query instance.
*
* This query instance will be examined and the proper SQL syntax will be returned as a string.
* This class may be overridden to accommodate syntax differences between various database systems.
*
* @param Query $query
* @return string
*/
public function select(Query $query)
{
if ( ! is_null($query->aggregate))
{
$sql[] = $this->compile_aggregate($query->aggregate['aggregator'], $query->aggregate['column']);
}
else
{
$sql[] = $this->compile_select($query);
}
$sql[] = $this->compile_from($query->table);
foreach (array('joins', 'wheres', 'orderings', 'limit', 'offset') as $clause)
{
if ( ! is_null($query->$clause)) $sql[] = call_user_func(array($this, 'compile_'.$clause), $query->$clause);
}
foreach ($sql as $key => $value) { if (is_null($value) or (string) $value === '') unset($sql[$key]); }
return implode(' ', $sql);
}
/**
* Compile the query SELECT clause.
*
* For convenience, the entire query object is passed to the method. This to account for
* database systems who put the LIMIT amount in the SELECT clause.
*
* @param Query $query
* @return string
*/
public function compile_select(Query $query)
{
return (($query->distinct) ? 'SELECT DISTINCT ' : 'SELECT ').$this->wrap_columns($query->select);
}
/**
* Wrap and comma-delimit a set of SELECT columns.
*
* @param array $columns
* @return string
*/
public function wrap_columns($columns)
{
return implode(', ', array_map(array($this, 'wrap'), $columns));
}
/**
* Compile the query SELECT clause with an aggregate function.
*
* @param string $aggregator
* @param string $column
* @return string
*/
public function compile_aggregate($aggregator, $column)
{
return 'SELECT '.$aggregator.'('.$this->wrap($column).') AS '.$this->wrap('aggregate');
}
/**
* Compile the query FROM clause.
*
* Note: This method does not compile any JOIN clauses. Joins are compiled by the compile_joins method.
*
* @param string $table
* @return string
*/
public function compile_from($table)
{
return 'FROM '.$this->wrap($table);
}
/**
* Compile the query JOIN clauses.
*
* @param array $joins
* @return string
*/
public function compile_joins($joins)
{
foreach ($joins as $join)
{
extract($join);
$sql[] = $type.' JOIN '.$this->wrap($table).' ON '.$this->wrap($column1).' '.$operator.' '.$this->wrap($column2);
}
return implode(' ', $sql);
}
/**
* Compile the query WHERE clauses.
*
* @param array $wheres
* @return string
*/
public function compile_wheres($wheres)
{
$sql = array('WHERE 1 = 1');
foreach ($wheres as $where)
{
if (is_string($where))
{
$sql[] = $where;
}
elseif ($where['type'] === 'where')
{
$sql[] = $where['connector'].' '.$this->compile_where($where);
}
elseif ($where['type'] === 'where_in')
{
$sql[] = $where['connector'].' '.$this->compile_where_in($where);
}
elseif ($where['type'] === 'where_null')
{
$sql[] = $where['connector'].' '.$this->compile_where_null($where);
}
}
return implode(' ', $sql);
}
/**
* Compile a simple WHERE clause.
*
* @param array $where
* @return string
*/
public function compile_where($where)
{
return $this->wrap($where['column']).' '.$where['operator'].' ?';
}
/**
* Compile a WHERE IN clause.
*
* @param array $where
* @return string
*/
public function compile_where_in($where)
{
$operator = ($where['not']) ? 'NOT IN' : 'IN';
return $this->wrap($where['column']).' '.$operator.' ('.$this->parameterize($where['values']).')';
}
/**
* Compile a WHERE NULL clause.
*
* @param array $where
* @return string
*/
public function compile_where_null($where)
{
$operator = ($where['not']) ? 'NOT NULL' : 'NULL';
return $this->wrap($where['column']).' IS '.$operator;
}
/**
* Compile the query ORDER BY clause.
*
* @param array $orderings
* @return string
*/
public function compile_orderings($orderings)
{
foreach ($orderings as $ordering)
{
$sql[] = $this->wrap($ordering['column']).' '.strtoupper($ordering['direction']);
}
return 'ORDER BY '.implode(', ', $sql);
}
/**
* Compile the query LIMIT.
*
* @param int $limit
* @return string
*/
public function compile_limit($limit)
{
return 'LIMIT '.$limit;
}
/**
* Compile the query OFFSET.
*
* @param int $offset
* @return string
*/
public function compile_offset($offset)
{
return 'OFFSET '.$offset;
}
/**
* Compile a SQL INSERT statment from a Query instance.
*
* @param Query $query
* @param array $values
* @return string
*/
public function insert(Query $query, $values)
{
$columns = array_map(array($this, 'wrap'), array_keys($values));
return 'INSERT INTO '.$this->wrap($query->table).' ('.implode(', ', $columns).') VALUES ('.$this->parameterize($values).')';
}
/**
* Compile a SQL INSERT statment that returns an auto-incrementing ID from a Query instance.
*
* @param Query $query
* @param array $values
* @return string
*/
public function insert_get_id(Query $query, $values)
{
return $this->insert($query, $values);
}
/**
* Compile a SQL UPDATE statment from a Query instance.
*
* @param Query $query
* @param array $values
* @return string
*/
public function update(Query $query, $values)
{
foreach (array_keys($values) as $column) { $sets[] = $this->wrap($column).' = ?'; }
$sql = 'UPDATE '.$this->wrap($query->table).' SET '.implode(', ', $sets);
return (count($query->wheres) > 0) ? $sql.' '.$this->compile_wheres($query->wheres) : $sql;
}
/**
* Compile a SQL DELETE statment from a Query instance.
*
* @param Query $query
* @return string
*/
public function delete(Query $query)
{
$sql = 'DELETE FROM '.$this->wrap($query->table);
return (count($query->wheres) > 0) ? $sql.' '.$this->compile_wheres($query->wheres) : $sql;
}
/**
* Wrap a value in keyword identifiers.
*
* @param string $value
* @return string
*/
public function wrap($value)
{
if (strpos(strtolower($value), ' as ') !== false) return $this->wrap_alias($value);
foreach (explode('.', $value) as $segment)
{
$wrapped[] = ($segment != '*') ? $this->wrapper().$segment.$this->wrapper() : $segment;
}
return implode('.', $wrapped);
}
/**
* Wrap an alias in keyword identifiers.
*
* @param string $value
* @return string
*/
public function wrap_alias($value)
{
$segments = explode(' ', $value);
return $this->wrap($segments[0]).' AS '.$this->wrap($segments[2]);
}
/**
* Get the keyword identifier wrapper for the connection.
*
* @return string
*/
public function wrapper() { return '"'; }
/**
* Create query parameters from an array of values.
*
* @param array $values
* @return string
*/
public function parameterize($values) { return implode(', ', array_fill(0, count($values), '?')); }
}
\ No newline at end of file
<?php namespace Laravel\DB\Query\Compiler;
use Laravel\DB\Connection;
use Laravel\DB\Query\Compiler;
class Factory {
/**
* Create a new query compiler for a given connection.
*
* Using driver-based compilers allows us to provide the proper syntax to different database
* systems using a common API. A core set of functions is provided through the base Compiler
* class, which can be extended and overridden for various database systems.
*
* @param Connection $connection
* @return Compiler
*/
public static function make(Connection $connection)
{
$compiler = (isset($connection->config['compiler'])) ? $connection->config['compiler'] : $connection->driver();
switch ($compiler)
{
case 'mysql':
return new MySQL;
case 'pgsql':
return new Postgres;
default:
return new Compiler;
}
}
}
\ No newline at end of file
<?php namespace Laravel\DB\Query;
<?php namespace Laravel\DB\Query\Compiler;
use Laravel\DB\Query;
use Laravel\DB\Query\Compiler;
class MySQL extends Query {
class MySQL extends Compiler {
/**
* Get the keyword identifier wrapper for the connection.
*
* MySQL uses a non-standard wrapper
*
* @return string
*/
public function wrapper()
{
return '`';
}
public function wrapper() { return '`'; }
}
\ No newline at end of file
<?php namespace Laravel\DB\Query\Compiler;
use Laravel\DB\Query\Compiler;
class Postgres extends Compiler {
/**
* Compile a SQL INSERT statment that returns an auto-incrementing ID from a Query instance.
*
* @param Query $query
* @param array $values
* @return string
*/
public function insert_get_id(Query $query, $values)
{
return $this->insert($query, $values).' RETURNING '.$this->wrap('id');
}
}
\ No newline at end of file
<?php namespace Laravel\DB\Query;
use Laravel\DB\Query;
use Laravel\DB\Compiler;
use Laravel\DB\Connection;
class Factory {
/**
* Create a new query instance for a given driver.
* Create a new query instance based on the connection driver.
*
* @param string $table
* @param string $table
* @param Connection $connection
* @param Compiler $compiler
* @return Query
*/
public static function make($table, Connection $connection)
public static function make($table, Connection $connection, Compiler $compiler)
{
$query = (isset($connection->config['query'])) ? $connection->config['query'] : $connection->driver();
switch ($query)
switch ($connection->driver())
{
case 'pgsql':
return new Postgres($table, $connection, new Compiler);
case 'mysql':
return new MySQL($table, $connection, new Compiler);
return new Postgres($table, $connection, $compiler);
default:
return new Query($table, $connection, new Compiler);
return new Query($table, $connection, $compiler);
}
}
......
......@@ -5,20 +5,23 @@ use Laravel\DB\Query;
class Postgres extends Query {
/**
* Execute an INSERT statement and get the insert ID.
* Insert an array of values into the database table and return the value of the ID column.
*
* <code>
* // Insert into the "users" table and get the auto-incrementing ID
* $id = DB::table('users')->insert_get_id(array('email' => 'example@gmail.com'));
* </code>
*
* @param array $values
* @return int
*/
public function insert_get_id($values)
{
$sql = $this->compile_insert($values);
$query = $this->connection->pdo->prepare($sql.' RETURNING '.$this->wrap('id'));
$query = $this->connection->pdo->prepare($this->compiler->insert_get_id($this, $values));
$query->execute(array_values($values));
return $query->fetch(\PDO::FETCH_CLASS, 'stdClass')->id;
return (int) $query->fetch(\PDO::FETCH_CLASS, 'stdClass')->id;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment