Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | class Mock_Database_DB { |
| 4 | |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 5 | /** |
| 6 | * @var array DB configuration |
| 7 | */ |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 8 | private $config = array(); |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 9 | |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 10 | /** |
| 11 | * Prepare database configuration skeleton |
| 12 | * |
| 13 | * @param array DB configuration to set |
| 14 | * @return void |
| 15 | */ |
| 16 | public function __construct($config = array()) |
| 17 | { |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 18 | $this->config = $config; |
| 19 | } |
| 20 | |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 21 | /** |
| 22 | * Build DSN connection string for DB driver instantiate process |
| 23 | * |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 24 | * @param string Group name |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 25 | * @return string DSN Connection string |
| 26 | */ |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 27 | public function set_dsn($group = 'default') |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 28 | { |
| 29 | if ( ! isset($this->config[$group])) |
| 30 | { |
| 31 | throw new InvalidArgumentException('Group '.$group.' not exists'); |
| 32 | } |
| 33 | |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 34 | $params = array( |
| 35 | 'dbprefix' => '', |
| 36 | 'pconnect' => FALSE, |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 37 | 'db_debug' => FALSE, |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 38 | 'cache_on' => FALSE, |
| 39 | 'cachedir' => '', |
| 40 | 'char_set' => 'utf8', |
| 41 | 'dbcollat' => 'utf8_general_ci', |
| 42 | 'swap_pre' => '', |
| 43 | 'autoinit' => TRUE, |
| 44 | 'stricton' => FALSE, |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 45 | ); |
| 46 | |
| 47 | $config = array_merge($this->config[$group], $params); |
Andrey Andreev | fbba54e | 2012-06-23 20:26:31 +0300 | [diff] [blame] | 48 | $dsnstring = empty($config['dsn']) ? FALSE : $config['dsn']; |
| 49 | $subdriver = empty($config['subdriver']) ? FALSE: $config['subdriver']; |
| 50 | $failover = empty($config['failover']) ? FALSE : $config['failover']; |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 51 | |
Taufan Aditya | 1972514 | 2012-04-04 23:46:23 +0700 | [diff] [blame] | 52 | $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password'] |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 53 | .'@'.$config['hostname'].'/'.$config['database']; |
| 54 | |
Taufan Aditya | f4c6c9b | 2012-04-04 23:24:09 +0700 | [diff] [blame] | 55 | // Build the parameter |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 56 | $other_params = array_slice($config, 6); |
Taufan Aditya | 1972514 | 2012-04-04 23:46:23 +0700 | [diff] [blame] | 57 | if ($dsnstring) $other_params['dsn'] = $dsnstring; |
Andrey Andreev | fbba54e | 2012-06-23 20:26:31 +0300 | [diff] [blame] | 58 | if ($subdriver) $other_params['subdriver'] = $subdriver; |
Taufan Aditya | f4c6c9b | 2012-04-04 23:24:09 +0700 | [diff] [blame] | 59 | if ($failover) $other_params['failover'] = $failover; |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 60 | |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 61 | return $dsn.'?'.http_build_query($other_params); |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 62 | } |
| 63 | |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 64 | /** |
| 65 | * Return a database config array |
| 66 | * |
| 67 | * @see ./config |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 68 | * @param string Driver based configuration |
| 69 | * @return array |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 70 | */ |
| 71 | public static function config($driver) |
| 72 | { |
| 73 | $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR; |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 74 | return include($dir.'config'.DIRECTORY_SEPARATOR.$driver.'.php'); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Main DB method wrapper |
| 79 | * |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 80 | * @param string Group or DSN string |
| 81 | * @param bool |
| 82 | * @return object |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 83 | */ |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 84 | public static function DB($group, $query_builder = FALSE) |
| 85 | { |
| 86 | include_once(BASEPATH.'database/DB.php'); |
| 87 | |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 88 | try |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 89 | { |
| 90 | $db = DB($group, $query_builder); |
| 91 | } |
| 92 | catch (Exception $e) |
| 93 | { |
| 94 | throw new InvalidArgumentException($e->getMessage()); |
| 95 | } |
| 96 | |
| 97 | return $db; |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 98 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 99 | |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 100 | } |