blob: 11e4a93bd98523f279fac6c30dcf62069036e48b [file] [log] [blame]
Taufan Aditya655a89f2012-03-29 03:00:56 +07001<?php
2
3class 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 Aditya655a89f2012-03-29 03:00:56 +070015 $this->config = $config;
16 }
17
Taufan Adityaa8a2e332012-03-29 03:56:46 +070018 public function set_dsn($group = 'default')
Taufan Aditya655a89f2012-03-29 03:00:56 +070019 {
20 if ( ! isset($this->config[$group]))
21 {
22 throw new InvalidArgumentException('Group '.$group.' not exists');
23 }
24
Taufan Adityaa8a2e332012-03-29 03:56:46 +070025 $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 Aditya655a89f2012-03-29 03:00:56 +070042 {
Taufan Adityaa8a2e332012-03-29 03:56:46 +070043 $dsn = $config['dsn'];
Taufan Aditya655a89f2012-03-29 03:00:56 +070044 }
45 else
46 {
Taufan Aditya655a89f2012-03-29 03:00:56 +070047 $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password']
48 .'@'.$config['hostname'].'/'.$config['database'];
49
50 }
51
Taufan Adityaa8a2e332012-03-29 03:56:46 +070052 $other_params = array_slice($config, 6);
Taufan Aditya655a89f2012-03-29 03:00:56 +070053
Taufan Adityaa8a2e332012-03-29 03:56:46 +070054 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 Aditya655a89f2012-03-29 03:00:56 +070071 }
72}