refactoring database.
This commit is contained in:
parent
bf0aaa3cc7
commit
9d2a76b26f
|
@ -37,9 +37,9 @@ public static function connection($connection = null)
|
||||||
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 @@ public function driver()
|
||||||
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 '"'; }
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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 {
|
||||||
*/
|
*/
|
||||||
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']);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -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 @@ public static function make($driver)
|
||||||
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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -630,7 +630,7 @@ private function wrap($value)
|
||||||
|
|
||||||
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);
|
||||||
|
@ -649,6 +649,18 @@ private function wrap_alias($value)
|
||||||
return $this->wrap($segments[0]).' AS '.$this->wrap($segments[2]);
|
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.
|
* Create query parameters from an array of values.
|
||||||
*
|
*
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
* Get the keyword identifier wrapper for the connection.
|
Loading…
Reference in New Issue