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(); |
| 9 | |
| 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 | * |
| 24 | * @param string Group name |
| 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); |
| 48 | |
| 49 | if ( ! empty($config['dsn'])) |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 50 | { |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 51 | $dsn = $config['dsn']; |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 52 | } |
| 53 | else |
| 54 | { |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 55 | $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password'] |
| 56 | .'@'.$config['hostname'].'/'.$config['database']; |
| 57 | |
| 58 | } |
| 59 | |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 60 | $other_params = array_slice($config, 6); |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 61 | |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame^] | 62 | return $dsn.'?'.http_build_query($other_params); |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 63 | } |
| 64 | |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame^] | 65 | /** |
| 66 | * Return a database config array |
| 67 | * |
| 68 | * @see ./config |
| 69 | * @param string Driver based configuration |
| 70 | * @return array |
| 71 | */ |
| 72 | public static function config($driver) |
| 73 | { |
| 74 | $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR; |
| 75 | |
| 76 | return include($dir.'config'.DIRECTORY_SEPARATOR.$driver.'.php'); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Main DB method wrapper |
| 81 | * |
| 82 | * @param string Group or DSN string |
| 83 | * @param bool |
| 84 | * @return object |
| 85 | */ |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 86 | public static function DB($group, $query_builder = FALSE) |
| 87 | { |
| 88 | include_once(BASEPATH.'database/DB.php'); |
| 89 | |
| 90 | try |
| 91 | { |
| 92 | $db = DB($group, $query_builder); |
| 93 | } |
| 94 | catch (Exception $e) |
| 95 | { |
| 96 | throw new InvalidArgumentException($e->getMessage()); |
| 97 | } |
| 98 | |
| 99 | return $db; |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 100 | } |
| 101 | } |