Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | class Mock_Database_DB { |
| 4 | |
| 5 | private $config = array(); |
| 6 | |
| 7 | /** |
| 8 | * Prepare database configuration skeleton |
| 9 | * |
| 10 | * @param array DB configuration to set |
| 11 | * @return void |
| 12 | */ |
| 13 | public function __construct($config = array()) |
| 14 | { |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 15 | $this->config = $config; |
| 16 | } |
| 17 | |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame^] | 18 | public function set_dsn($group = 'default') |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 19 | { |
| 20 | if ( ! isset($this->config[$group])) |
| 21 | { |
| 22 | throw new InvalidArgumentException('Group '.$group.' not exists'); |
| 23 | } |
| 24 | |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame^] | 25 | $params = array( |
| 26 | 'dbprefix' => '', |
| 27 | 'pconnect' => FALSE, |
| 28 | 'db_debug' => TRUE, |
| 29 | 'cache_on' => FALSE, |
| 30 | 'cachedir' => '', |
| 31 | 'char_set' => 'utf8', |
| 32 | 'dbcollat' => 'utf8_general_ci', |
| 33 | 'swap_pre' => '', |
| 34 | 'autoinit' => TRUE, |
| 35 | 'stricton' => FALSE, |
| 36 | 'failover' => array() |
| 37 | ); |
| 38 | |
| 39 | $config = array_merge($this->config[$group], $params); |
| 40 | |
| 41 | if ( ! empty($config['dsn'])) |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 42 | { |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame^] | 43 | $dsn = $config['dsn']; |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 44 | } |
| 45 | else |
| 46 | { |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 47 | $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password'] |
| 48 | .'@'.$config['hostname'].'/'.$config['database']; |
| 49 | |
| 50 | } |
| 51 | |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame^] | 52 | $other_params = array_slice($config, 6); |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 53 | |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame^] | 54 | return $dsn.http_build_query($other_params); |
| 55 | } |
| 56 | |
| 57 | public static function DB($group, $query_builder = FALSE) |
| 58 | { |
| 59 | include_once(BASEPATH.'database/DB.php'); |
| 60 | |
| 61 | try |
| 62 | { |
| 63 | $db = DB($group, $query_builder); |
| 64 | } |
| 65 | catch (Exception $e) |
| 66 | { |
| 67 | throw new InvalidArgumentException($e->getMessage()); |
| 68 | } |
| 69 | |
| 70 | return $db; |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 71 | } |
| 72 | } |