blob: 75658530b7e2e858e5d181584f348d4b99d06750 [file] [log] [blame]
Taufan Aditya655a89f2012-03-29 03:00:56 +07001<?php
2
3class Mock_Database_DB {
4
Taufan Adityaee2f5d02012-03-30 06:29:11 +07005 /**
6 * @var array DB configuration
7 */
Taufan Aditya655a89f2012-03-29 03:00:56 +07008 private $config = array();
Andrey Andreevf243ce12012-06-09 23:34:21 +03009
Taufan Aditya655a89f2012-03-29 03:00:56 +070010 /**
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 Aditya655a89f2012-03-29 03:00:56 +070018 $this->config = $config;
19 }
20
Taufan Adityaee2f5d02012-03-30 06:29:11 +070021 /**
22 * Build DSN connection string for DB driver instantiate process
23 *
Andrey Andreevf243ce12012-06-09 23:34:21 +030024 * @param string Group name
Taufan Adityaee2f5d02012-03-30 06:29:11 +070025 * @return string DSN Connection string
26 */
Taufan Adityaa8a2e332012-03-29 03:56:46 +070027 public function set_dsn($group = 'default')
Taufan Aditya655a89f2012-03-29 03:00:56 +070028 {
29 if ( ! isset($this->config[$group]))
30 {
31 throw new InvalidArgumentException('Group '.$group.' not exists');
32 }
33
Taufan Adityaa8a2e332012-03-29 03:56:46 +070034 $params = array(
35 'dbprefix' => '',
36 'pconnect' => FALSE,
Taufan Adityaee2f5d02012-03-30 06:29:11 +070037 'db_debug' => FALSE,
Taufan Adityaa8a2e332012-03-29 03:56:46 +070038 'cache_on' => FALSE,
39 'cachedir' => '',
40 'char_set' => 'utf8',
41 'dbcollat' => 'utf8_general_ci',
42 'swap_pre' => '',
43 'autoinit' => TRUE,
44 'stricton' => FALSE,
Taufan Adityaa8a2e332012-03-29 03:56:46 +070045 );
46
47 $config = array_merge($this->config[$group], $params);
Andrey Andreevfbba54e2012-06-23 20:26:31 +030048 $dsnstring = empty($config['dsn']) ? FALSE : $config['dsn'];
49 $subdriver = empty($config['subdriver']) ? FALSE: $config['subdriver'];
50 $failover = empty($config['failover']) ? FALSE : $config['failover'];
Taufan Adityaa8a2e332012-03-29 03:56:46 +070051
Taufan Aditya19725142012-04-04 23:46:23 +070052 $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password']
Taufan Aditya655a89f2012-03-29 03:00:56 +070053 .'@'.$config['hostname'].'/'.$config['database'];
54
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070055 // Build the parameter
Taufan Adityaa8a2e332012-03-29 03:56:46 +070056 $other_params = array_slice($config, 6);
Taufan Aditya19725142012-04-04 23:46:23 +070057 if ($dsnstring) $other_params['dsn'] = $dsnstring;
Andrey Andreevfbba54e2012-06-23 20:26:31 +030058 if ($subdriver) $other_params['subdriver'] = $subdriver;
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070059 if ($failover) $other_params['failover'] = $failover;
Taufan Aditya655a89f2012-03-29 03:00:56 +070060
Taufan Adityaee2f5d02012-03-30 06:29:11 +070061 return $dsn.'?'.http_build_query($other_params);
Taufan Adityaa8a2e332012-03-29 03:56:46 +070062 }
63
Taufan Adityaee2f5d02012-03-30 06:29:11 +070064 /**
65 * Return a database config array
66 *
67 * @see ./config
Andrey Andreevf243ce12012-06-09 23:34:21 +030068 * @param string Driver based configuration
69 * @return array
Taufan Adityaee2f5d02012-03-30 06:29:11 +070070 */
71 public static function config($driver)
72 {
73 $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR;
Taufan Adityaee2f5d02012-03-30 06:29:11 +070074 return include($dir.'config'.DIRECTORY_SEPARATOR.$driver.'.php');
75 }
76
77 /**
78 * Main DB method wrapper
79 *
Andrey Andreevf243ce12012-06-09 23:34:21 +030080 * @param string Group or DSN string
81 * @param bool
82 * @return object
Taufan Adityaee2f5d02012-03-30 06:29:11 +070083 */
Taufan Adityaa8a2e332012-03-29 03:56:46 +070084 public static function DB($group, $query_builder = FALSE)
85 {
86 include_once(BASEPATH.'database/DB.php');
87
Andrey Andreevf243ce12012-06-09 23:34:21 +030088 try
Taufan Adityaa8a2e332012-03-29 03:56:46 +070089 {
90 $db = DB($group, $query_builder);
91 }
92 catch (Exception $e)
93 {
94 throw new InvalidArgumentException($e->getMessage());
95 }
96
97 return $db;
Taufan Aditya655a89f2012-03-29 03:00:56 +070098 }
Andrey Andreevf243ce12012-06-09 23:34:21 +030099
Taufan Aditya655a89f2012-03-29 03:00:56 +0700100}