db refactoring.
This commit is contained in:
parent
0700910270
commit
5ca2e2b7f9
|
@ -1,70 +0,0 @@
|
|||
<?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;
|
||||
}
|
||||
|
||||
}
|
|
@ -152,7 +152,7 @@ private function execute(\PDOStatement $statement, $bindings)
|
|||
*/
|
||||
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 @@ public function connect($config)
|
|||
$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']))
|
||||
|
|
|
@ -23,10 +23,18 @@ class Query {
|
|||
/**
|
||||
* The SELECT clause.
|
||||
*
|
||||
* @var string
|
||||
* @var array
|
||||
*/
|
||||
public $select;
|
||||
|
||||
/**
|
||||
* If the query is performing an aggregate function, this will contain the column
|
||||
* and and function to use when aggregating.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $aggregate;
|
||||
|
||||
/**
|
||||
* Indicates if the query should return distinct results.
|
||||
*
|
||||
|
@ -34,13 +42,6 @@ class Query {
|
|||
*/
|
||||
public $distinct = false;
|
||||
|
||||
/**
|
||||
* The FROM clause.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $from;
|
||||
|
||||
/**
|
||||
* The table name.
|
||||
*
|
||||
|
@ -49,25 +50,25 @@ class Query {
|
|||
public $table;
|
||||
|
||||
/**
|
||||
* The WHERE clause.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $where = 'WHERE 1 = 1';
|
||||
|
||||
/**
|
||||
* The ORDER BY clause.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $order;
|
||||
|
||||
/**
|
||||
* The ORDER BY columns.
|
||||
* The table joins.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $orderings = array();
|
||||
public $joins;
|
||||
|
||||
/**
|
||||
* The WHERE clauses.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $wheres;
|
||||
|
||||
/**
|
||||
* The ORDER BY clauses.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $orderings;
|
||||
|
||||
/**
|
||||
* The LIMIT value.
|
||||
|
@ -95,14 +96,14 @@ class Query {
|
|||
*
|
||||
* @param string $table
|
||||
* @param Connection $connection
|
||||
* @param Compiler $compiler
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($table, Connection $connection, Compiler $compiler)
|
||||
public function __construct($table, Connection $connection, Query\Compiler $compiler)
|
||||
{
|
||||
$this->table = $table;
|
||||
$this->compiler = $compiler;
|
||||
$this->connection = $connection;
|
||||
$this->from = 'FROM '.$this->wrap($table);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -113,38 +114,33 @@ public function __construct($table, Connection $connection, Compiler $compiler)
|
|||
public function distinct()
|
||||
{
|
||||
$this->distinct = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add columns to the SELECT clause.
|
||||
* Add an array of columns to the SELECT clause.
|
||||
*
|
||||
* <code>
|
||||
* $query->select(array('id', 'email'));
|
||||
* </code>
|
||||
*
|
||||
* @param array $columns
|
||||
* @return Query
|
||||
*/
|
||||
public function select($columns = array('*'))
|
||||
{
|
||||
$this->select = ($this->distinct) ? 'SELECT DISTINCT ' : 'SELECT ';
|
||||
|
||||
$this->select .= implode(', ', array_map(array($this, 'wrap'), $columns));
|
||||
$this->select = $columns;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the FROM clause.
|
||||
* Add a join clause to the query.
|
||||
*
|
||||
* @param string $from
|
||||
* @return Query
|
||||
*/
|
||||
public function from($from)
|
||||
{
|
||||
$this->from = $from;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a join to the query.
|
||||
* <code>
|
||||
* $query->join('users', 'users.id', '=', 'posts.user_id');
|
||||
* </code>
|
||||
*
|
||||
* @param string $table
|
||||
* @param string $column1
|
||||
|
@ -155,13 +151,18 @@ public function from($from)
|
|||
*/
|
||||
public function join($table, $column1, $operator, $column2, $type = 'INNER')
|
||||
{
|
||||
$this->from .= ' '.$type.' JOIN '.$this->wrap($table).' ON '.$this->wrap($column1).' '.$operator.' '.$this->wrap($column2);
|
||||
$this->joins[] = compact('table', 'column1', 'operator', 'column2', 'type');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a left join to the query.
|
||||
*
|
||||
* <code>
|
||||
* $query->left_join('users', 'users.id', '=', 'posts.user_id');
|
||||
* </code>
|
||||
*
|
||||
* @param string $table
|
||||
* @param string $column1
|
||||
* @param string $operator
|
||||
|
@ -180,13 +181,18 @@ public function left_join($table, $column1, $operator, $column2)
|
|||
*/
|
||||
public function reset_where()
|
||||
{
|
||||
$this->where = 'WHERE 1 = 1';
|
||||
$this->wheres = array();
|
||||
|
||||
$this->bindings = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a raw where condition to the query.
|
||||
*
|
||||
* <code>
|
||||
* $query->raw_where('user_id = ? and password = ?', array(1, 'secret'));
|
||||
* </code>
|
||||
*
|
||||
* @param string $where
|
||||
* @param array $bindings
|
||||
* @param string $connector
|
||||
|
@ -194,7 +200,8 @@ public function reset_where()
|
|||
*/
|
||||
public function raw_where($where, $bindings = array(), $connector = 'AND')
|
||||
{
|
||||
$this->where .= ' '.$connector.' '.$where;
|
||||
$this->wheres[] = ' '.$connector.' '.$where;
|
||||
|
||||
$this->bindings = array_merge($this->bindings, $bindings);
|
||||
|
||||
return $this;
|
||||
|
@ -203,6 +210,10 @@ public function raw_where($where, $bindings = array(), $connector = 'AND')
|
|||
/**
|
||||
* Add a raw or where condition to the query.
|
||||
*
|
||||
* <code>
|
||||
* $query->raw_or_where('user_id = ? and password = ?', array(1, 'secret'));
|
||||
* </code>
|
||||
*
|
||||
* @param string $where
|
||||
* @param array $bindings
|
||||
* @return Query
|
||||
|
@ -215,6 +226,10 @@ public function raw_or_where($where, $bindings = array())
|
|||
/**
|
||||
* Add a where condition to the query.
|
||||
*
|
||||
* <code>
|
||||
* $query->where('id', '=', 1);
|
||||
* </code>
|
||||
*
|
||||
* @param string $column
|
||||
* @param string $operator
|
||||
* @param mixed $value
|
||||
|
@ -223,7 +238,8 @@ public function raw_or_where($where, $bindings = array())
|
|||
*/
|
||||
public function where($column, $operator, $value, $connector = 'AND')
|
||||
{
|
||||
$this->where .= ' '.$connector.' '.$this->wrap($column).' '.$operator.' ?';
|
||||
$this->wheres[] = array_merge(array('type' => 'where'), compact('column', 'operator', 'value', 'connector'));
|
||||
|
||||
$this->bindings[] = $value;
|
||||
|
||||
return $this;
|
||||
|
@ -232,6 +248,10 @@ public function where($column, $operator, $value, $connector = 'AND')
|
|||
/**
|
||||
* Add an or where condition to the query.
|
||||
*
|
||||
* <code>
|
||||
* $query->or_where('id', '=', 1);
|
||||
* </code>
|
||||
*
|
||||
* @param string $column
|
||||
* @param string $operator
|
||||
* @param mixed $value
|
||||
|
@ -244,7 +264,10 @@ public function or_where($column, $operator, $value)
|
|||
|
||||
/**
|
||||
* Add a where condition for the primary key to the query.
|
||||
* This is simply a short-cut method for convenience.
|
||||
*
|
||||
* <code>
|
||||
* $query->where_id(1);
|
||||
* </code>
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return Query
|
||||
|
@ -256,7 +279,10 @@ public function where_id($value)
|
|||
|
||||
/**
|
||||
* Add an or where condition for the primary key to the query.
|
||||
* This is simply a short-cut method for convenience.
|
||||
*
|
||||
* <code>
|
||||
* $query->or_where_id(1);
|
||||
* </code>
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return Query
|
||||
|
@ -269,14 +295,20 @@ public function or_where_id($value)
|
|||
/**
|
||||
* Add a where in condition to the query.
|
||||
*
|
||||
* <code>
|
||||
* $query->where_in('id', array(1, 2, 3));
|
||||
* </code>
|
||||
*
|
||||
* @param string $column
|
||||
* @param array $values
|
||||
* @param string $connector
|
||||
* @param bool $not
|
||||
* @return Query
|
||||
*/
|
||||
public function where_in($column, $values, $connector = 'AND')
|
||||
public function where_in($column, $values, $connector = 'AND', $not = false)
|
||||
{
|
||||
$this->where .= ' '.$connector.' '.$this->wrap($column).' IN ('.$this->parameterize($values).')';
|
||||
$this->wheres[] = array_merge(array('type' => 'where_in'), compact('column', 'values', 'connector', 'not'));
|
||||
|
||||
$this->bindings = array_merge($this->bindings, $values);
|
||||
|
||||
return $this;
|
||||
|
@ -285,6 +317,10 @@ public function where_in($column, $values, $connector = 'AND')
|
|||
/**
|
||||
* Add an or where in condition to the query.
|
||||
*
|
||||
* <code>
|
||||
* $query->or_where_in('id', array(1, 2, 3));
|
||||
* </code>
|
||||
*
|
||||
* @param string $column
|
||||
* @param array $values
|
||||
* @return Query
|
||||
|
@ -297,6 +333,10 @@ public function or_where_in($column, $values)
|
|||
/**
|
||||
* Add a where not in condition to the query.
|
||||
*
|
||||
* <code>
|
||||
* $query->where_not_in('id', array(1, 2, 3));
|
||||
* </code>
|
||||
*
|
||||
* @param string $column
|
||||
* @param array $values
|
||||
* @param string $connector
|
||||
|
@ -304,15 +344,16 @@ public function or_where_in($column, $values)
|
|||
*/
|
||||
public function where_not_in($column, $values, $connector = 'AND')
|
||||
{
|
||||
$this->where .= ' '.$connector.' '.$this->wrap($column).' NOT IN ('.$this->parameterize($values).')';
|
||||
$this->bindings = array_merge($this->bindings, $values);
|
||||
|
||||
return $this;
|
||||
return $this->where_in($column, $values, $connector, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an or where not in condition to the query.
|
||||
*
|
||||
* <code>
|
||||
* $query->or_where_not_in('id', array(1, 2, 3));
|
||||
* </code>
|
||||
*
|
||||
* @param string $column
|
||||
* @param array $values
|
||||
* @return Query
|
||||
|
@ -327,11 +368,13 @@ public function or_where_not_in($column, $values)
|
|||
*
|
||||
* @param string $column
|
||||
* @param string $connector
|
||||
* @param bool $not
|
||||
* @return Query
|
||||
*/
|
||||
public function where_null($column, $connector = 'AND')
|
||||
public function where_null($column, $connector = 'AND', $not = false)
|
||||
{
|
||||
$this->where .= ' '.$connector.' '.$this->wrap($column).' IS NULL';
|
||||
$this->wheres[] = array_merge(array('type' => 'where_null'), compact('column', 'connector', 'not'));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -355,8 +398,7 @@ public function or_where_null($column)
|
|||
*/
|
||||
public function where_not_null($column, $connector = 'AND')
|
||||
{
|
||||
$this->where .= ' '.$connector.' '.$this->wrap($column).' IS NOT NULL';
|
||||
return $this;
|
||||
return $this->where_null($column, $connector, true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -417,15 +459,21 @@ private function dynamic_where($method, $parameters)
|
|||
/**
|
||||
* Add an ordering to the query.
|
||||
*
|
||||
* <code>
|
||||
* // Set an ascending sort on the query
|
||||
* $query->order_by('votes', 'asc');
|
||||
*
|
||||
* // Set a descending sort on the query
|
||||
* $query->order_by('votes', 'desc');
|
||||
* </code>
|
||||
*
|
||||
* @param string $column
|
||||
* @param string $direction
|
||||
* @return Query
|
||||
*/
|
||||
public function order_by($column, $direction = 'asc')
|
||||
{
|
||||
$this->orderings[] = $this->wrap($column).' '.strtoupper($direction);
|
||||
|
||||
$this->order = 'ORDER BY '.implode(', ', $this->orderings);
|
||||
$this->orderings[] = compact('column', 'direction');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -438,7 +486,8 @@ public function order_by($column, $direction = 'asc')
|
|||
*/
|
||||
public function skip($value)
|
||||
{
|
||||
$this->offset = 'OFFSET '.$value;
|
||||
$this->offset = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -450,12 +499,13 @@ public function skip($value)
|
|||
*/
|
||||
public function take($value)
|
||||
{
|
||||
$this->limit = 'LIMIT '.$value;
|
||||
$this->limit = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the limit and offset values for a given page.
|
||||
* Calculate and set the limit and offset values for a given page.
|
||||
*
|
||||
* @param int $page
|
||||
* @param int $per_page
|
||||
|
@ -469,6 +519,11 @@ public function for_page($page, $per_page)
|
|||
/**
|
||||
* Find a record by the primary key.
|
||||
*
|
||||
* <code>
|
||||
* // Get the user having an ID of 1
|
||||
* $user = DB::table('users')->find(1);
|
||||
* </code>
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $columns
|
||||
* @return object
|
||||
|
@ -487,7 +542,7 @@ public function find($id, $columns = array('*'))
|
|||
*/
|
||||
private function aggregate($aggregator, $column)
|
||||
{
|
||||
$this->select = 'SELECT '.$aggregator.'('.$this->wrap($column).') AS '.$this->wrap('aggregate');
|
||||
$this->aggregate = compact('aggregator', 'column');
|
||||
|
||||
$result = $this->connection->scalar($this->compiler->select($this), $this->bindings);
|
||||
|
||||
|
@ -501,6 +556,17 @@ private function aggregate($aggregator, $column)
|
|||
/**
|
||||
* Get paginated query results.
|
||||
*
|
||||
* A Paginator instance will be returned, and the results of the query will be stored
|
||||
* in the "results" property of the Paginator instance.
|
||||
*
|
||||
* <code>
|
||||
* // Paginate the "users" table
|
||||
* $users = DB::table('users')->paginate(15);
|
||||
*
|
||||
* // Paginate the "users" table with a where clause
|
||||
* $users = DB::table('users')->where('votes', '>', 100)->paginate(10);
|
||||
* </code>
|
||||
*
|
||||
* @param int $per_page
|
||||
* @param array $columns
|
||||
* @return Paginator
|
||||
|
@ -509,14 +575,16 @@ public function paginate($per_page, $columns = array('*'))
|
|||
{
|
||||
$total = $this->count();
|
||||
|
||||
return Paginator::make($this->for_page(Paginator::page($total, $per_page), $per_page)->get($columns), $total, $per_page);
|
||||
$results = $this->skip((Paginator::page($total, $per_page) - 1) * $per_page)->take($per_page)->get($columns);
|
||||
|
||||
return Paginator::make($results, $total, $per_page);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the query as a SELECT statement and return the first result.
|
||||
*
|
||||
* @param array $columns
|
||||
* @return object
|
||||
* @param array $columns
|
||||
* @return stdClass
|
||||
*/
|
||||
public function first($columns = array('*'))
|
||||
{
|
||||
|
@ -543,7 +611,12 @@ public function get($columns = array('*'))
|
|||
}
|
||||
|
||||
/**
|
||||
* Execute an INSERT statement.
|
||||
* Insert an array of values into the database table.
|
||||
*
|
||||
* <code>
|
||||
* // Insert into the "users" table
|
||||
* $success = DB::table('users')->insert(array('id' => 1, 'email' => 'example@gmail.com'));
|
||||
* </code>
|
||||
*
|
||||
* @param array $values
|
||||
* @return bool
|
||||
|
@ -554,7 +627,12 @@ public function insert($values)
|
|||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
@ -563,14 +641,19 @@ public function insert_get_id($values)
|
|||
{
|
||||
$this->connection->query($this->compiler->insert($this, $values), array_values($values));
|
||||
|
||||
return $this->connection->pdo->lastInsertId();
|
||||
return (int) $this->connection->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the query as an UPDATE statement.
|
||||
* Update an array of values in the database table.
|
||||
*
|
||||
* <code>
|
||||
* // Update a user's e-mail address
|
||||
* $affected = DB::table('users')->where_id(1)->update(array('email' => 'new_email@example.com'));
|
||||
* </code>
|
||||
*
|
||||
* @param array $values
|
||||
* @return bool
|
||||
* @return int
|
||||
*/
|
||||
public function update($values)
|
||||
{
|
||||
|
@ -580,8 +663,21 @@ public function update($values)
|
|||
/**
|
||||
* Execute the query as a DELETE statement.
|
||||
*
|
||||
* Optionally, an ID may be passed to the method do delete a specific row.
|
||||
*
|
||||
* <code>
|
||||
* // Delete everything from the "users" table
|
||||
* $affected = DB::table('users')->delete();
|
||||
*
|
||||
* // Delete a specific user from the table
|
||||
* $affected = DB::table('users')->delete(1);
|
||||
*
|
||||
* // Execute a delete statement with where conditions
|
||||
* $affected = DB::table('users')->where_email($email)->delete();
|
||||
* </code>
|
||||
*
|
||||
* @param int $id
|
||||
* @return bool
|
||||
* @return int
|
||||
*/
|
||||
public function delete($id = null)
|
||||
{
|
||||
|
@ -590,62 +686,18 @@ public function delete($id = null)
|
|||
return $this->connection->query($this->compiler->delete($this), $this->bindings);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
protected 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.
|
||||
*
|
||||
* MySQL uses a non-standard wrapper
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected 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), '?'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic Method for handling dynamic functions.
|
||||
*
|
||||
* This method handles all calls to aggregate functions as well as the construction of dynamic where clauses.
|
||||
*
|
||||
* <code>
|
||||
* // Get the total number of rows on the "users" table
|
||||
* $count = DB::table('users')->count();
|
||||
*
|
||||
* // Get a user using a dynamic where clause
|
||||
* $user = DB::table('users')->where_email('example@gmail.com')->first();
|
||||
* </code>
|
||||
*/
|
||||
public function __call($method, $parameters)
|
||||
{
|
||||
|
|
|
@ -0,0 +1,316 @@
|
|||
<?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), '?')); }
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
<?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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<?php namespace Laravel\DB\Query\Compiler;
|
||||
|
||||
use Laravel\DB\Query\Compiler;
|
||||
|
||||
class MySQL extends Compiler {
|
||||
|
||||
/**
|
||||
* Get the keyword identifier wrapper for the connection.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function wrapper() { return '`'; }
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?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');
|
||||
}
|
||||
|
||||
}
|
|
@ -1,32 +1,27 @@
|
|||
<?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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
<?php namespace Laravel\DB\Query;
|
||||
|
||||
use Laravel\DB\Query;
|
||||
|
||||
class MySQL extends Query {
|
||||
|
||||
/**
|
||||
* Get the keyword identifier wrapper for the connection.
|
||||
*
|
||||
* MySQL uses a non-standard wrapper
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function wrapper()
|
||||
{
|
||||
return '`';
|
||||
}
|
||||
|
||||
}
|
|
@ -5,20 +5,23 @@
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue