refactoring the database layer.
This commit is contained in:
parent
b8a901c792
commit
045858d88d
|
@ -5,6 +5,13 @@
|
|||
|
||||
class Connection {
|
||||
|
||||
/**
|
||||
* The database connector instance.
|
||||
*
|
||||
* @var Connector\Connector
|
||||
*/
|
||||
protected $connector;
|
||||
|
||||
/**
|
||||
* The connection name.
|
||||
*
|
||||
|
@ -19,13 +26,6 @@ class Connection {
|
|||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* The database connector instance.
|
||||
*
|
||||
* @var Connector\Connector
|
||||
*/
|
||||
protected $connector;
|
||||
|
||||
/**
|
||||
* The PDO connection.
|
||||
*
|
||||
|
@ -44,18 +44,14 @@ class Connection {
|
|||
* Create a new Connection instance.
|
||||
*
|
||||
* @param Connector\Connector $connector
|
||||
* @param Query\Factory $factory
|
||||
* @param Compiler\Factory $compiler
|
||||
* @param string $name
|
||||
* @param array $config
|
||||
* @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->query = $query;
|
||||
$this->config = $config;
|
||||
$this->compiler = $compiler;
|
||||
$this->connector = $connector;
|
||||
}
|
||||
|
||||
|
@ -181,14 +177,46 @@ protected function execute(PDOStatement $statement, $bindings)
|
|||
* <code>
|
||||
* // Begin a fluent query against the "users" table
|
||||
* $query = DB::connection()->table('users');
|
||||
*
|
||||
* // Retrieve an entire table using a fluent query
|
||||
* $users = DB::connection()->table('users')->get();
|
||||
* </code>
|
||||
*
|
||||
* @param string $table
|
||||
* @return Query
|
||||
* @return Query\Query
|
||||
*/
|
||||
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.");
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
* @var Connection
|
||||
* @var Database\Connection
|
||||
*/
|
||||
public $connection;
|
||||
|
||||
/**
|
||||
* The query compiler instance.
|
||||
*
|
||||
* @var Compiler
|
||||
* @var Compiler\Compiler
|
||||
*/
|
||||
public $compiler;
|
||||
|
||||
|
@ -93,7 +93,7 @@ class Query {
|
|||
* Create a new query instance.
|
||||
*
|
||||
* @param Database\Connection $connection
|
||||
* @param Compiler $compiler
|
||||
* @param Compiler\Compiler $compiler
|
||||
* @param string $table
|
||||
* @return void
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue