2011-06-08 23:45:08 -05:00
|
|
|
<?php namespace System\DB;
|
|
|
|
|
|
|
|
|
|
class Connector {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The PDO connection options.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
public static $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,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Establish a PDO database connection.
|
|
|
|
|
*
|
|
|
|
|
* @param object $config
|
|
|
|
|
* @return PDO
|
|
|
|
|
*/
|
|
|
|
|
public static function connect($config)
|
|
|
|
|
{
|
2011-06-10 22:30:41 -05:00
|
|
|
// -----------------------------------------------------
|
|
|
|
|
// Connect to SQLite.
|
|
|
|
|
// -----------------------------------------------------
|
2011-06-08 23:45:08 -05:00
|
|
|
if ($config->driver == 'sqlite')
|
|
|
|
|
{
|
2011-06-10 22:30:41 -05:00
|
|
|
// -----------------------------------------------------
|
|
|
|
|
// Check the application/db directory first.
|
2011-06-17 11:51:53 -07:00
|
|
|
//
|
|
|
|
|
// If the database doesn't exist there, maybe the full
|
|
|
|
|
// path was specified as the database name?
|
2011-06-10 22:30:41 -05:00
|
|
|
// -----------------------------------------------------
|
2011-06-24 23:03:22 -05:00
|
|
|
if (file_exists($path = APP_PATH.'storage/db/'.$config->database.'.sqlite'))
|
2011-06-10 22:30:41 -05:00
|
|
|
{
|
|
|
|
|
return new \PDO('sqlite:'.$path, null, null, static::$options);
|
|
|
|
|
}
|
|
|
|
|
elseif (file_exists($config->database))
|
|
|
|
|
{
|
|
|
|
|
return new \PDO('sqlite:'.$config->database, null, null, static::$options);
|
|
|
|
|
}
|
2011-06-21 13:14:54 -07:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new \Exception("SQLite database [".$config->database."] could not be found.");
|
|
|
|
|
}
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
2011-06-10 22:30:41 -05:00
|
|
|
// -----------------------------------------------------
|
|
|
|
|
// Connect to MySQL or Postgres.
|
|
|
|
|
// -----------------------------------------------------
|
2011-06-08 23:45:08 -05:00
|
|
|
elseif ($config->driver == 'mysql' or $config->driver == 'pgsql')
|
|
|
|
|
{
|
2011-06-28 12:21:21 -05:00
|
|
|
// -----------------------------------------------------
|
|
|
|
|
// Build the PDO connection DSN.
|
|
|
|
|
// -----------------------------------------------------
|
|
|
|
|
$dsn = $config->driver.':host='.$config->host.';dbname='.$config->database;
|
|
|
|
|
|
|
|
|
|
if (isset($config->port))
|
|
|
|
|
{
|
|
|
|
|
$dsn .= ';port='.$config->port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$connection = new \PDO($dsn, $config->username, $config->password, static::$options);
|
2011-06-08 23:45:08 -05:00
|
|
|
|
2011-06-28 12:21:21 -05:00
|
|
|
// -----------------------------------------------------
|
|
|
|
|
// Set the appropriate character set for the datbase.
|
|
|
|
|
// -----------------------------------------------------
|
2011-06-08 23:45:08 -05:00
|
|
|
if (isset($config->charset))
|
|
|
|
|
{
|
|
|
|
|
$connection->prepare("SET NAMES '".$config->charset."'")->execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $connection;
|
|
|
|
|
}
|
2011-06-10 22:30:41 -05:00
|
|
|
|
|
|
|
|
throw new \Exception('Database driver '.$config->driver.' is not supported.');
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|