From 045858d88d3a490594346533052be02c523a7882 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 12 Sep 2011 21:48:48 -0500 Subject: [PATCH] refactoring the database layer. --- laravel/database/connection.php | 58 +++++++++++++++------ laravel/database/connector/factory.php | 30 ----------- laravel/database/manager.php | 29 ++++++++++- laravel/database/query/compiler/factory.php | 34 ------------ laravel/database/query/factory.php | 27 ---------- laravel/database/query/query.php | 6 +-- 6 files changed, 73 insertions(+), 111 deletions(-) delete mode 100644 laravel/database/connector/factory.php delete mode 100644 laravel/database/query/compiler/factory.php delete mode 100644 laravel/database/query/factory.php diff --git a/laravel/database/connection.php b/laravel/database/connection.php index e6ec8792..c0aec562 100644 --- a/laravel/database/connection.php +++ b/laravel/database/connection.php @@ -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) * * // 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(); * * - * @param string $table - * @return Query + * @param string $table + * @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; + } } /** diff --git a/laravel/database/connector/factory.php b/laravel/database/connector/factory.php deleted file mode 100644 index 796896ac..00000000 --- a/laravel/database/connector/factory.php +++ /dev/null @@ -1,30 +0,0 @@ -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. * diff --git a/laravel/database/query/compiler/factory.php b/laravel/database/query/compiler/factory.php deleted file mode 100644 index 1b3d3500..00000000 --- a/laravel/database/query/compiler/factory.php +++ /dev/null @@ -1,34 +0,0 @@ -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 diff --git a/laravel/database/query/factory.php b/laravel/database/query/factory.php deleted file mode 100644 index 477937ab..00000000 --- a/laravel/database/query/factory.php +++ /dev/null @@ -1,27 +0,0 @@ -driver()) - { - case 'pgsql': - return new Postgres($connection, $compiler, $table); - - default: - return new Query($connection, $compiler, $table); - } - } - -} \ No newline at end of file diff --git a/laravel/database/query/query.php b/laravel/database/query/query.php index 494047fb..0641781a 100644 --- a/laravel/database/query/query.php +++ b/laravel/database/query/query.php @@ -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 */