Multi databases support, #12
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@113 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
65
model/Db.php
Normal file
65
model/Db.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage db
|
||||
* @since 2010-02-16
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Db
|
||||
{
|
||||
|
||||
const FETCH_ASSOC = 2;
|
||||
const FETCH_NUM = 3;
|
||||
const FETCH_BOTH = 4;
|
||||
const FETCH_OBJ = 5;
|
||||
|
||||
/**
|
||||
* Databases connections
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
static protected $connections = array();
|
||||
|
||||
/**
|
||||
* Connect to database
|
||||
*
|
||||
* @param string $name Database name. If not set 'default' will be used.
|
||||
* @param array $config Configuration array.
|
||||
*
|
||||
* @return DbDriver
|
||||
*/
|
||||
static public function connect($name = null, $config = null)
|
||||
{
|
||||
if ($name === null) {
|
||||
$name = 'default';
|
||||
}
|
||||
|
||||
if (! isset(self::$connections[$name])) {
|
||||
if (!$config) {
|
||||
$config = Config::get('databases')->$name;
|
||||
}
|
||||
|
||||
if (! is_array($config)) {
|
||||
throw new Exception('Connection parameters must be an array');
|
||||
}
|
||||
|
||||
$driver = 'MySQLiDriver';
|
||||
if (isset($config['driver'])) {
|
||||
$driver = $config['driver'];
|
||||
unset($config['driver']);
|
||||
}
|
||||
|
||||
$connection = new $driver($config);
|
||||
|
||||
if (! $connection instanceof DbDriver) {
|
||||
throw new Exception('Database driver must extends DbDriver');
|
||||
}
|
||||
self::$connections[$name] = $connection;
|
||||
}
|
||||
return self::$connections[$name];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user