Skip to content

Commit

Permalink
Merge pull request #899 from LuckyCyborg/master
Browse files Browse the repository at this point in the history
Introduce in the Database API the support for using PostgresSQL and SQLite Databases
  • Loading branch information
daveismynamecom committed May 29, 2016
2 parents 00e3be0 + c221c3b commit 349935a
Show file tree
Hide file tree
Showing 8 changed files with 440 additions and 38 deletions.
9 changes: 8 additions & 1 deletion app/Config/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
),
));
'custom' => array(
'driver' => 'sqlite',
'database' => APPDIR .'Storage' .DS .'database.sqlite',
'prefix' => PREFIX,
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
),
));
114 changes: 86 additions & 28 deletions system/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

use Core\Config;
use Core\Logger;
use Database\Connectors\Connector;
use Database\Connectors\MySqlConnector;
use Database\Connectors\PostgresConnector;
use Database\Connectors\SQLiteConnector;
use Database\Query\Expression;
use Database\Query\Builder as QueryBuilder;

Expand All @@ -26,6 +30,13 @@ class Connection
*/
private static $instances = array();

/**
* The Connector instance.
*
* @var
*/
protected $connector;

/**
* The active PDO Connection.
*
Expand Down Expand Up @@ -76,13 +87,16 @@ class Connection
*/
public function __construct(array $config)
{
$this->pdo = $this->createConnection($config);

$this->database = $config['dbname'];

$this->tablePrefix = $config['prefix'];

$this->config = $config;

//
$this->connector = $this->createConnector($config);

$this->pdo = $this->createConnection($config);
}

/**
Expand All @@ -94,21 +108,7 @@ public function __construct(array $config)
*/
public static function getInstance($config = 'default')
{
if (is_array($config) && ! empty($config)) {
// The given parameter is a configuration array.
$connection = implode('.', array_values($config));
} else {
$connection = (is_string($config) && ! empty($config)) ? $config : 'default';

// Retrieve the configuration with the specified name.
$config = Config::get('database');

if (isset($config[$connection]) && ! empty($config[$connection])) {
$config = $config[$connection];
} else {
throw new \Exception("Connection name '$connection' is not defined in your configuration");
}
}
$connection = (is_string($config) && ! empty($config)) ? $config : 'default';

// Prepare a Token for handling the Connection instances.
$token = md5($connection);
Expand All @@ -118,6 +118,15 @@ public static function getInstance($config = 'default')
return static::$instances[$token];
}

// Retrieve the configuration with the specified name.
$config = Config::get('database');

if (isset($config[$connection]) && ! empty($config[$connection])) {
$config = $config[$connection];
} else {
throw new \Exception("Connection name '$connection' is not defined in your configuration");
}

// Create the Connection instance and return it.
return static::$instances[$token] = new static($config);
}
Expand All @@ -132,6 +141,34 @@ public function connection()
return $this;
}

/**
* Create a connector instance based on the configuration.
*
* @param array $config
* @return \Database\Connectors\ConnectorInterface
*
* @throws \InvalidArgumentException
*/
public function createConnector(array $config)
{
if ( ! isset($config['driver'])) {
throw new \InvalidArgumentException("A driver must be specified.");
}

switch ($config['driver']) {
case 'mysql':
return new MySqlConnector();

case 'pgsql':
return new PostgresConnector();

case 'sqlite':
return new SQLiteConnector();
}

throw new \InvalidArgumentException("Unsupported driver [{$config['driver']}]");
}

/**
* Create a new PDO connection.
*
Expand All @@ -140,16 +177,7 @@ public function connection()
*/
public function createConnection(array $config)
{
extract($config);

$dsn = "$driver:host={$hostname};dbname={$database}";

$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES {$charset} COLLATE {$collation}"
);

return new PDO($dsn, $username, $password, $options);
return $this->connector->connect($config);
}

/**
Expand Down Expand Up @@ -430,6 +458,26 @@ public function setTablePrefix($prefix)
$this->tablePrefix = $prefix;
}

/**
* Get the Database Driver.
*
* @return string
*/
public function getDriver()
{
return isset($this->config['driver']) ? $this->config['driver'] : null;
}

/**
* Get the Connector instance.
*
* @return \Database\Connectors\Connector
*/
public function getConnector()
{
return $this->connector;
}

/**
* Get the PDO instance.
*
Expand Down Expand Up @@ -463,13 +511,23 @@ public function setFetchMode($fetchMode)
return $this;
}

/**
* Get the keyword identifier wrapper format.
*
* @return string
*/
public function getWrapper()
{
return $this->connector->getWrapper();
}

/**
* Get the format for database stored dates.
*
* @return string
*/
public function getDateFormat()
{
return 'Y-m-d H:i:s';
return $this->connector->getDateFormat();
}
}
112 changes: 112 additions & 0 deletions system/Database/Connectors/Connector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* Connector - A PDO based Database Connector.
*
* @author Virgil-Adrian Teaca - virgil@giulianaeassociati.com
* @version 3.0
*/

namespace Database\Connectors;

use PDO;


abstract class Connector
{
/**
* The default PDO connection options.
*
* @var array
*/
protected $options = array(
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => false,
);

/**
* The keyword identifier wrapper format.
*
* @var string
*/
protected $wrapper = '`%s`';


public function __construct()
{
}

/**
* Get the PDO options based on the configuration.
*
* @param array $config
* @return array
*/
public function getOptions(array $config)
{
$options = array_get($config, 'options', array());

return array_diff_key($this->options, $options) + $options;
}

/**
* Create a new PDO connection.
*
* @param string $dsn
* @param array $config
* @param array $options
* @return PDO
*/
public function createConnection($dsn, array $config, array $options)
{
$username = array_get($config, 'username');

$password = array_get($config, 'password');

return new PDO($dsn, $username, $password, $options);
}

/**
* Get the default PDO connection options.
*
* @return array
*/
public function getDefaultOptions()
{
return $this->options;
}

/**
* Set the default PDO connection options.
*
* @param array $options
* @return void
*/
public function setDefaultOptions(array $options)
{
$this->options = $options;
}

/**
* Get the keyword identifier wrapper format.
*
* @return string
*/
public function getWrapper()
{
return $this->wrapper;
}

/**
* Get the format for database stored dates.
*
* @return string
*/
public function getDateFormat()
{
return 'Y-m-d H:i:s';
}

}
16 changes: 16 additions & 0 deletions system/Database/Connectors/ConnectorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Database\Connectors;


interface ConnectorInterface
{
/**
* Establish a database connection.
*
* @param array $config
* @return \PDO
*/
public function connect(array $config);

}
Loading

0 comments on commit 349935a

Please sign in to comment.