Commit 9d2a76b2 by Taylor Otwell

refactoring database.

parent bf0aaa3c
...@@ -37,9 +37,9 @@ class DB { ...@@ -37,9 +37,9 @@ class DB {
throw new \Exception("Database connection [$connection] is not defined."); 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]; return static::$connections[$connection];
......
...@@ -167,21 +167,4 @@ class Connection { ...@@ -167,21 +167,4 @@ class Connection {
return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); 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 '"'; }
} }
\ No newline at end of file
<?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);
}
}
}
\ No newline at end of file
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
use Laravel\DB\Connector; use Laravel\DB\Connector;
class Generic extends Connector { class Callback extends Connector {
/** /**
* Establish a PDO database connection. * Establish a PDO database connection.
...@@ -12,7 +12,7 @@ class Generic extends Connector { ...@@ -12,7 +12,7 @@ class Generic extends Connector {
*/ */
public function connect($config) public function connect($config)
{ {
return new \PDO($config['driver'].':'.$config['dsn'], $config['username'], $config['password'], $this->options); return call_user_func($config['connector']);
} }
} }
\ No newline at end of file
...@@ -5,12 +5,14 @@ class Factory { ...@@ -5,12 +5,14 @@ class Factory {
/** /**
* Create a new database connector instance for a given driver. * Create a new database connector instance for a given driver.
* *
* @param string $driver * @param array $config
* @return Connector * @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': case 'sqlite':
return new SQLite; return new SQLite;
...@@ -18,12 +20,11 @@ class Factory { ...@@ -18,12 +20,11 @@ class Factory {
case 'mysql': case 'mysql':
return new MySQL; return new MySQL;
case 'postgres': case 'pgsql':
return new Postgres; return new Postgres;
default:
return new Generic;
} }
throw new \Exception("Database configuration is invalid. Please verify your configuration.");
} }
} }
\ No newline at end of file
...@@ -630,7 +630,7 @@ class Query { ...@@ -630,7 +630,7 @@ class Query {
foreach (explode('.', $value) as $segment) 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); return implode('.', $wrapped);
...@@ -650,6 +650,18 @@ class Query { ...@@ -650,6 +650,18 @@ class Query {
} }
/** /**
* 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. * Create query parameters from an array of values.
* *
* @param array $values * @param array $values
......
...@@ -14,11 +14,16 @@ class Factory { ...@@ -14,11 +14,16 @@ class Factory {
*/ */
public static function make($table, Connection $connection) 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); return new Postgres($table, $connection);
case 'mysql':
return new MySQL($table, $connection);
default: default:
return new Query($table, $connection); return new Query($table, $connection);
} }
......
<?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. * Get the keyword identifier wrapper for the connection.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment