refactoring the database layer.
This commit is contained in:
parent
b8a901c792
commit
045858d88d
|
@ -5,6 +5,13 @@
|
||||||
|
|
||||||
class Connection {
|
class Connection {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The database connector instance.
|
||||||
|
*
|
||||||
|
* @var Connector\Connector
|
||||||
|
*/
|
||||||
|
protected $connector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The connection name.
|
* The connection name.
|
||||||
*
|
*
|
||||||
|
@ -19,13 +26,6 @@ class Connection {
|
||||||
*/
|
*/
|
||||||
protected $config;
|
protected $config;
|
||||||
|
|
||||||
/**
|
|
||||||
* The database connector instance.
|
|
||||||
*
|
|
||||||
* @var Connector\Connector
|
|
||||||
*/
|
|
||||||
protected $connector;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The PDO connection.
|
* The PDO connection.
|
||||||
*
|
*
|
||||||
|
@ -44,18 +44,14 @@ class Connection {
|
||||||
* Create a new Connection instance.
|
* Create a new Connection instance.
|
||||||
*
|
*
|
||||||
* @param Connector\Connector $connector
|
* @param Connector\Connector $connector
|
||||||
* @param Query\Factory $factory
|
|
||||||
* @param Compiler\Factory $compiler
|
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct(Connector\Connector $connector, Query\Factory $query, Query\Compiler\Factory $compiler, $name, $config)
|
public function __construct(Connector\Connector $connector, $name, $config)
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
$this->query = $query;
|
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
$this->compiler = $compiler;
|
|
||||||
$this->connector = $connector;
|
$this->connector = $connector;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,14 +177,46 @@ protected function execute(PDOStatement $statement, $bindings)
|
||||||
* <code>
|
* <code>
|
||||||
* // Begin a fluent query against the "users" table
|
* // Begin a fluent query against the "users" table
|
||||||
* $query = DB::connection()->table('users');
|
* $query = DB::connection()->table('users');
|
||||||
|
*
|
||||||
|
* // Retrieve an entire table using a fluent query
|
||||||
|
* $users = DB::connection()->table('users')->get();
|
||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @return Query
|
* @return Query\Query
|
||||||
*/
|
*/
|
||||||
public function table($table)
|
public function table($table)
|
||||||
{
|
{
|
||||||
return $this->query->make($this, $this->compiler->make($this), $table);
|
switch ($this->driver())
|
||||||
|
{
|
||||||
|
case 'pgsql':
|
||||||
|
return new Query\Postgres($this, $this->compiler(), $table);
|
||||||
|
|
||||||
|
default:
|
||||||
|
return new Query\Query($this, $this->compiler(), $table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new query compiler for the connection.
|
||||||
|
*
|
||||||
|
* @return Query\Compiler
|
||||||
|
*/
|
||||||
|
protected function compiler()
|
||||||
|
{
|
||||||
|
$compiler = (isset($this->config['compiler'])) ? $this->config['compiler'] : $this->driver();
|
||||||
|
|
||||||
|
switch ($compiler)
|
||||||
|
{
|
||||||
|
case 'mysql':
|
||||||
|
return new Query\Compiler\MySQL;
|
||||||
|
|
||||||
|
case 'pgsql':
|
||||||
|
return new Query\Compiler\Postgres;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return new Query\Compiler\Compiler;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
<?php namespace Laravel\Database\Connector;
|
|
||||||
|
|
||||||
class Factory {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new database connector instance for a given driver.
|
|
||||||
*
|
|
||||||
* @param array $config
|
|
||||||
* @return Connector
|
|
||||||
*/
|
|
||||||
public function make($config)
|
|
||||||
{
|
|
||||||
if (isset($config['connector'])) return new Callback;
|
|
||||||
|
|
||||||
switch ($config['driver'])
|
|
||||||
{
|
|
||||||
case 'sqlite':
|
|
||||||
return new SQLite;
|
|
||||||
|
|
||||||
case 'mysql':
|
|
||||||
return new MySQL;
|
|
||||||
|
|
||||||
case 'pgsql':
|
|
||||||
return new Postgres;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new \Exception("Database configuration is invalid. Please verify your configuration.");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -73,14 +73,39 @@ public function connection($connection = null)
|
||||||
throw new \Exception("Database connection [$connection] is not defined.");
|
throw new \Exception("Database connection [$connection] is not defined.");
|
||||||
}
|
}
|
||||||
|
|
||||||
list($connector, $query, $compiler) = array($this->connector->make($this->config[$connection]), new Query\Factory, new Query\Compiler\Factory);
|
$config = $this->config[$connection];
|
||||||
|
|
||||||
$this->connections[$connection] = new Connection($connector, $query, $compiler, $connection, $this->config[$connection]);
|
$this->connections[$connection] = new Connection($this->connector($config), $connection, $config);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->connections[$connection];
|
return $this->connections[$connection];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new database connector instance base on a connection configuration.
|
||||||
|
*
|
||||||
|
* @param array $config
|
||||||
|
* @return Connector\Connector
|
||||||
|
*/
|
||||||
|
protected function connector($config)
|
||||||
|
{
|
||||||
|
if (isset($config['connector'])) return new Connector\Callback;
|
||||||
|
|
||||||
|
switch ($config['driver'])
|
||||||
|
{
|
||||||
|
case 'sqlite':
|
||||||
|
return new Connector\SQLite;
|
||||||
|
|
||||||
|
case 'mysql':
|
||||||
|
return new Connector\MySQL;
|
||||||
|
|
||||||
|
case 'pgsql':
|
||||||
|
return new Connector\Postgres;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \Exception("Database configuration is invalid. Please verify your configuration.");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Begin a fluent query against a table.
|
* Begin a fluent query against a table.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,34 +0,0 @@
|
||||||
<?php namespace Laravel\Database\Query\Compiler;
|
|
||||||
|
|
||||||
use Laravel\Database\Connection;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
<?php namespace Laravel\Database\Query;
|
|
||||||
|
|
||||||
use Laravel\Database\Connection;
|
|
||||||
|
|
||||||
class Factory {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new query instance based on the connection driver.
|
|
||||||
*
|
|
||||||
* @param Connection $connection
|
|
||||||
* @param Compiler $compiler
|
|
||||||
* @param string $table
|
|
||||||
* @return Query
|
|
||||||
*/
|
|
||||||
public function make(Connection $connection, Compiler $compiler, $table)
|
|
||||||
{
|
|
||||||
switch ($connection->driver())
|
|
||||||
{
|
|
||||||
case 'pgsql':
|
|
||||||
return new Postgres($connection, $compiler, $table);
|
|
||||||
|
|
||||||
default:
|
|
||||||
return new Query($connection, $compiler, $table);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -7,14 +7,14 @@ class Query {
|
||||||
/**
|
/**
|
||||||
* The database connection.
|
* The database connection.
|
||||||
*
|
*
|
||||||
* @var Connection
|
* @var Database\Connection
|
||||||
*/
|
*/
|
||||||
public $connection;
|
public $connection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The query compiler instance.
|
* The query compiler instance.
|
||||||
*
|
*
|
||||||
* @var Compiler
|
* @var Compiler\Compiler
|
||||||
*/
|
*/
|
||||||
public $compiler;
|
public $compiler;
|
||||||
|
|
||||||
|
@ -93,7 +93,7 @@ class Query {
|
||||||
* Create a new query instance.
|
* Create a new query instance.
|
||||||
*
|
*
|
||||||
* @param Database\Connection $connection
|
* @param Database\Connection $connection
|
||||||
* @param Compiler $compiler
|
* @param Compiler\Compiler $compiler
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue