Allow the registration of custom database drivers.
This commit is contained in:
parent
cf29450b99
commit
7e33ec5f34
|
@ -12,6 +12,13 @@ class Database {
|
||||||
*/
|
*/
|
||||||
public static $connections = array();
|
public static $connections = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The third-party driver registrar.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public static $registrar = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a database connection.
|
* Get a database connection.
|
||||||
*
|
*
|
||||||
|
@ -66,6 +73,11 @@ protected static function connect($config)
|
||||||
*/
|
*/
|
||||||
protected static function connector($driver)
|
protected static function connector($driver)
|
||||||
{
|
{
|
||||||
|
if (isset(static::$registrar[$driver]))
|
||||||
|
{
|
||||||
|
return static::$registrar[$driver]['connector']();
|
||||||
|
}
|
||||||
|
|
||||||
switch ($driver)
|
switch ($driver)
|
||||||
{
|
{
|
||||||
case 'sqlite':
|
case 'sqlite':
|
||||||
|
@ -120,6 +132,22 @@ public static function profile()
|
||||||
return Database\Connection::$queries;
|
return Database\Connection::$queries;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a database connector and grammars.
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param Closure $connector
|
||||||
|
* @param Closure $query
|
||||||
|
* @param Closure $schema
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function register($name, Closure $connector, $query = null, $schema = null)
|
||||||
|
{
|
||||||
|
if (is_null($query)) $query = '\Laravel\Database\Query\Grammars\Grammar';
|
||||||
|
|
||||||
|
static::$registrar[$name] = compact('connector', 'query', 'schema');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Magic Method for calling methods on the default database connection.
|
* Magic Method for calling methods on the default database connection.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
<?php namespace Laravel\Database; use PDO, PDOStatement, Laravel\Config, Laravel\Event;
|
<?php namespace Laravel\Database;
|
||||||
|
|
||||||
|
use PDO, PDOStatement, Laravel\Config, Laravel\Event;
|
||||||
|
|
||||||
class Connection {
|
class Connection {
|
||||||
|
|
||||||
|
@ -71,7 +73,12 @@ protected function grammar()
|
||||||
{
|
{
|
||||||
if (isset($this->grammar)) return $this->grammar;
|
if (isset($this->grammar)) return $this->grammar;
|
||||||
|
|
||||||
switch (isset($this->config['grammar']) ? $this->config['grammar'] : $this->driver())
|
if (isset(\Laravel\Database::$registrar[$this->driver()]))
|
||||||
|
{
|
||||||
|
\Laravel\Database::$registrar[$this->driver()]['query']();
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($this->driver())
|
||||||
{
|
{
|
||||||
case 'mysql':
|
case 'mysql':
|
||||||
return $this->grammar = new Query\Grammars\MySQL($this);
|
return $this->grammar = new Query\Grammars\MySQL($this);
|
||||||
|
@ -300,7 +307,7 @@ protected function log($sql, $bindings, $start)
|
||||||
*/
|
*/
|
||||||
public function driver()
|
public function driver()
|
||||||
{
|
{
|
||||||
return $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
|
return $this->config['driver'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -148,6 +148,11 @@ public static function grammar(Connection $connection)
|
||||||
{
|
{
|
||||||
$driver = $connection->driver();
|
$driver = $connection->driver();
|
||||||
|
|
||||||
|
if (isset(\Laravel\Database::$registrar[$driver]))
|
||||||
|
{
|
||||||
|
return \Laravel\Database::$registrar[$driver]['schema']();
|
||||||
|
}
|
||||||
|
|
||||||
switch ($driver)
|
switch ($driver)
|
||||||
{
|
{
|
||||||
case 'mysql':
|
case 'mysql':
|
||||||
|
|
Loading…
Reference in New Issue