From 4b063ac042464840c040550627f62a247e22564f Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 7 Jul 2011 23:35:23 -0500 Subject: [PATCH] refactoring database and connector classes. --- system/db.php | 23 ++++++++--------------- system/db/connector.php | 32 +++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/system/db.php b/system/db.php index 34d9d743..1b03d851 100644 --- a/system/db.php +++ b/system/db.php @@ -7,10 +7,13 @@ class DB { * * @var array */ - private static $connections = array(); + public static $connections = array(); /** - * Get a database connection. Database connections are managed as singletons. + * Get a database connection. If no database name is specified, the default + * connection will be returned as defined in the db configuration file. + * + * Note: Database connections are managed as singletons. * * @param string $connection * @return PDO @@ -22,19 +25,9 @@ public static function connection($connection = null) $connection = Config::get('db.default'); } - if ( ! array_key_exists($connection, static::$connections)) - { - $config = Config::get('db.connections'); - - if ( ! array_key_exists($connection, $config)) - { - throw new \Exception("Database connection [$connection] is not defined."); - } - - static::$connections[$connection] = DB\Connector::connect((object) $config[$connection]); - } - - return static::$connections[$connection]; + return array_key_exists($connection, static::$connections) + ? static::$connections[$connection] + : static::$connections[$connection] = DB\Connector::connect($connection); } /** diff --git a/system/db/connector.php b/system/db/connector.php index d7c138da..bbc831d9 100644 --- a/system/db/connector.php +++ b/system/db/connector.php @@ -1,5 +1,7 @@ driver == 'sqlite') { return static::connect_to_sqlite($config); @@ -37,14 +41,14 @@ public static function connect($config) /** * Establish a PDO connection to a SQLite database. * + * SQLite database paths can be specified either relative to the application/db + * directory, or as an absolute path to any location on the file system. + * * @param array $config * @return PDO */ private static function connect_to_sqlite($config) { - // Database paths can either be specified relative to the application/storage/db - // directory, or as an absolute path. - if (file_exists($path = APP_PATH.'storage/db/'.$config->database.'.sqlite')) { return new \PDO('sqlite:'.$path, null, null, static::$options); @@ -84,4 +88,22 @@ private static function connect_to_server($config) return $connection; } + /** + * Get the configuration options for a database connection. + * + * @param string $connection + * @return object + */ + private static function configuration($connection) + { + $config = Config::get('db.connections'); + + if ( ! array_key_exists($connection, $config)) + { + throw new \Exception("Database connection [$connection] is not defined."); + } + + return (object) $config[$connection]; + } + } \ No newline at end of file