Permalink
Browse files

Made it possible to use multiple named database connections

  • Loading branch information...
1 parent 582f20c commit e7ee476c9ab3e2e38e529469338b95864dd17e79 @troelskn committed Apr 4, 2012
View
@@ -12,10 +12,12 @@
$GLOBALS['WEBWORK_LOGGING']['dispatch'] = true;
// Database connection settings
-$GLOBALS['DATABASE_CONNECTION'] = array(
- 'constructor' => 'create_pdo',
- 'username' => null,
- 'password' => null
+$GLOBALS['DATABASE_CONNECTIONS'] = array(
+ 'default' => array(
+ 'constructor' => 'create_pdo',
+ 'username' => null,
+ 'password' => null
+ )
);
// If the public directory is a subdir on the web server where it's served, set the HREF_BASE below
@@ -1,4 +1,4 @@
<?php
-$GLOBALS['DATABASE_CONNECTION']['dsn'] = "mysql:host=localhost;dbname=application_development;charset=UTF-8";
-$GLOBALS['DATABASE_CONNECTION']['user'] = "root";
-$GLOBALS['DATABASE_CONNECTION']['pass'] = null;
+$GLOBALS['DATABASE_CONNECTIONS']['default']['dsn'] = "mysql:host=localhost;dbname=application_development;charset=UTF-8";
+$GLOBALS['DATABASE_CONNECTIONS']['default']['user'] = "root";
+$GLOBALS['DATABASE_CONNECTIONS']['default']['pass'] = null;
@@ -1,2 +1,2 @@
<?php
-$GLOBALS['DATABASE_CONNECTION']['dsn'] = "sqlite::memory:";
+$GLOBALS['DATABASE_CONNECTIONS']['default']['dsn'] = "sqlite::memory:";
@@ -5,12 +5,20 @@
/**
* Returns a database connection object.
*/
-function db() {
- if (!isset($GLOBALS['DATABASE_CONNECTION']['instance'])) {
- $ctor = $GLOBALS['DATABASE_CONNECTION']['constructor'];
- $GLOBALS['DATABASE_CONNECTION']['instance'] = call_user_func($ctor, $GLOBALS['DATABASE_CONNECTION']);
+function db($connection_name = 'default') {
+ if ($connection_name == 'default' && isset($GLOBALS['DATABASE_CONNECTION'])) {
+ if (!isset($GLOBALS['DATABASE_CONNECTION']['instance'])) {
+ $ctor = $GLOBALS['DATABASE_CONNECTION']['constructor'];
+ $GLOBALS['DATABASE_CONNECTION']['instance'] = call_user_func($ctor, $GLOBALS['DATABASE_CONNECTION']);
+ }
+ return $GLOBALS['DATABASE_CONNECTION']['instance'];
+ }
+ $config =& $GLOBALS['DATABASE_CONNECTIONS'][$connection_name];
+ if (!isset($config['instance'])) {
+ $ctor = $config['constructor'];
+ $config['instance'] = call_user_func($ctor, $config);
}
- return $GLOBALS['DATABASE_CONNECTION']['instance'];
+ return $config['instance'];
}
/**

0 comments on commit e7ee476

Please sign in to comment.