refactoring database.

This commit is contained in:
Taylor Otwell 2011-08-19 21:18:59 -05:00
parent bf0aaa3cc7
commit 9d2a76b26f
8 changed files with 35 additions and 64 deletions

View File

@ -37,9 +37,9 @@ public static function connection($connection = null)
throw new \Exception("Database connection [$connection] is not defined.");
}
$connector = DB\Connector\Factory::make($config['driver']);
$connector = DB\Connector\Factory::make($config);
static::$connections[$connection] = DB\Connection\Factory::make($connection, $config, $connector);
static::$connections[$connection] = new DB\Connection($connection, $config, $connector);
}
return static::$connections[$connection];

View File

@ -167,21 +167,4 @@ public function driver()
return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
}
/**
* Get the table prefix for the database connection.
*
* @return string
*/
public function prefix()
{
return (array_key_exists('prefix', $this->config)) ? $this->config['prefix'] : '';
}
/**
* Get the keyword identifier wrapper for the connection.
*
* @return string
*/
public function wrapper() { return '"'; }
}

View File

@ -1,30 +0,0 @@
<?php namespace Laravel\DB\Connection;
use Laravel\DB\Connector;
use Laravel\DB\Connection;
class Factory {
/**
* Get a connnection instance.
*
* The connection instance created depends on the driver being used.
*
* @param string $connection
* @param object $config
* @param Connector $connector
* @return Connection
*/
public static function make($connection, $config, Connector $connector)
{
switch ($config['driver'])
{
case 'mysql':
return new MySQL($connection, $config, $connector);
default:
return new Connection($connection, $config, $connector);
}
}
}

View File

@ -2,7 +2,7 @@
use Laravel\DB\Connector;
class Generic extends Connector {
class Callback extends Connector {
/**
* Establish a PDO database connection.
@ -12,7 +12,7 @@ class Generic extends Connector {
*/
public function connect($config)
{
return new \PDO($config['driver'].':'.$config['dsn'], $config['username'], $config['password'], $this->options);
return call_user_func($config['connector']);
}
}

View File

@ -5,12 +5,14 @@ class Factory {
/**
* Create a new database connector instance for a given driver.
*
* @param string $driver
* @param array $config
* @return Connector
*/
public static function make($driver)
public static function make($config)
{
switch ($driver)
if (isset($config['connector'])) return new Callback;
switch ($config['driver'])
{
case 'sqlite':
return new SQLite;
@ -18,12 +20,11 @@ public static function make($driver)
case 'mysql':
return new MySQL;
case 'postgres':
case 'pgsql':
return new Postgres;
default:
return new Generic;
}
throw new \Exception("Database configuration is invalid. Please verify your configuration.");
}
}

View File

@ -630,7 +630,7 @@ private function wrap($value)
foreach (explode('.', $value) as $segment)
{
$wrapped[] = ($segment != '*') ? $this->connection->wrapper().$segment.$this->connection->wrapper() : $segment;
$wrapped[] = ($segment != '*') ? $this->wrapper().$segment.$this->wrapper() : $segment;
}
return implode('.', $wrapped);
@ -649,6 +649,18 @@ private function wrap_alias($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.
*

View File

@ -14,11 +14,16 @@ class Factory {
*/
public static function make($table, Connection $connection)
{
switch ($connection->driver())
$query = (isset($connection->config['query'])) ? $connection->config['query'] : $connection->driver();
switch ($query)
{
case 'postgres':
case 'pgsql':
return new Postgres($table, $connection);
case 'mysql':
return new MySQL($table, $connection);
default:
return new Query($table, $connection);
}

View File

@ -1,8 +1,8 @@
<?php namespace Laravel\DB\Connection;
<?php namespace Laravel\DB\Query;
use Laravel\DB\Connection;
use Laravel\DB\Query;
class MySQL extends Connection {
class MySQL extends Query {
/**
* Get the keyword identifier wrapper for the connection.