added database connectors and cleaned up configuration.

This commit is contained in:
Taylor Otwell
2011-09-17 23:46:24 -05:00
parent 5387312e0d
commit cb5a426cba
8 changed files with 244 additions and 23 deletions

View File

@@ -0,0 +1,41 @@
<?php namespace Laravel\Database\Connectors; use PDO;
abstract class Connector {
/**
* The PDO connection options.
*
* @var array
*/
protected $options = array(
PDO::ATTR_CASE => PDO::CASE_LOWER,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => false,
);
/**
* Establish a PDO database connection for a given database configuration.
*
* @param array $config
* @return PDO
*/
abstract public function connect($config);
/**
* Get the PDO connection options for a given database configuration.
*
* Developer specified options will override the default connection options.
*
* @param array $config
* @return array
*/
protected function options($config)
{
$options = (isset($config['options'])) ? $config['options'] : array();
return array_merge($this->options, $options);
}
}

View File

@@ -0,0 +1,47 @@
<?php namespace Laravel\Database\Connectors; use PDO;
class MySQL extends Connector {
/**
* Establish a PDO database connection for a given database configuration.
*
* @param array $config
* @return PDO
*/
public function connect($config)
{
$connection = new PDO($this->dsn($config), $config['username'], $config['password'], $this->options($config));
if (isset($config['charset']))
{
$connection->prepare("SET NAMES '{$config['charset']}'")->execute();
}
return $connection;
}
/**
* Format the DSN connection string for a MySQL connection.
*
* @param array $config
* @return string
*/
protected function dsn($config)
{
// Format the initial MySQL PDO connection string. These options are required
// for every MySQL connection that is established. The connection strings
// have the following convention: "mysql:host=hostname;dbname=database"
$dsn = sprintf('%s:host=%s;dbname=%s', $config['driver'], $config['host'], $config['database']);
// Check for any optional MySQL PDO options. These options are not required
// to establish a PDO connection; however, may be needed in certain server
// or hosting environments used by the developer.
foreach (array('port', 'unix_socket') as $key => $value)
{
if (isset($config[$key])) $dsn .= ";{$key}={$value}";
}
return $dsn;
}
}

View File

@@ -0,0 +1,47 @@
<?php namespace Laravel\Database\Connectors; use PDO;
class Postgres extends Connector {
/**
* Establish a PDO database connection for a given database configuration.
*
* @param array $config
* @return PDO
*/
public function connect($config)
{
$connection = new PDO($this->dsn($config), $config['username'], $config['password'], $this->options($config));
if (isset($config['charset']))
{
$connection->prepare("SET NAMES '{$config['charset']}'")->execute();
}
return $connection;
}
/**
* Format the DSN connection string for a Postgres connection.
*
* @param array $config
* @return string
*/
protected function dsn($config)
{
// Format the initial Postgres PDO connection string. These options are required
// for every Postgres connection that is established. The connection strings
// have the following convention: "pgsql:host=hostname;dbname=database"
$dsn = sprintf('%s:host=%s;dbname=%s', $config['driver'], $config['host'], $config['database']);
// Check for any optional Postgres PDO options. These options are not required
// to establish a PDO connection; however, may be needed in certain server
// or hosting environments used by the developer.
foreach (array('port', 'unix_socket') as $key => $value)
{
if (isset($config[$key])) $dsn .= ";{$key}={$value}";
}
return $dsn;
}
}

View File

@@ -0,0 +1,31 @@
<?php namespace Laravel\Database\Connectors; use PDO;
class SQLite extends Connector {
/**
* Establish a PDO database connection for a given database configuration.
*
* @param array $config
* @return PDO
*/
public function connect($config)
{
$options = $this->options($config);
if ($config['database'] == ':memory:')
{
return new PDO('sqlite::memory:', null, null, $options);
}
elseif (file_exists($path = DATABASE_PATH.$config['database'].'.sqlite'))
{
return new PDO('sqlite:'.$path, null, null, $options);
}
elseif (file_exists($config['database']))
{
return new PDO('sqlite:'.$config['database'], null, null, $options);
}
throw new \Exception("SQLite database [{$config['database']}] could not be found.");
}
}