00001 <?php
00002
00016
00017
00018
00019 class DBConnection {
00020
00022 var $dbconn;
00023
00025 var $driver;
00026 var $host;
00027 var $username;
00028 var $password;
00029 var $databaseName;
00030 var $persistent;
00031 var $connectionCharset;
00032 var $forceNew;
00033
00035 var $connectOnInit;
00036
00037
00038 var $debug;
00039
00040
00042 var $connected;
00043
00049 function DBConnection() {
00050 $this->connected = false;
00051
00052 if (func_num_args() == 0) {
00053 $this->initDefaultDBConnection();
00054 } else {
00055 $args = func_get_args();
00056 call_user_func_array(array(&$this, 'initCustomDBConnection'), $args);
00057 }
00058 }
00059
00064 function initDefaultDBConnection() {
00065 $this->driver = Config::getVar('database', 'driver');
00066 $this->host = Config::getVar('database', 'host');
00067 $this->username = Config::getVar('database', 'username');
00068 $this->password = Config::getVar('database', 'password');
00069 $this->databaseName = Config::getVar('database', 'name');
00070 $this->persistent = Config::getVar('database', 'persistent') ? true : false;
00071 $this->connectionCharset = Config::getVar('i18n', 'connection_charset');
00072 $this->debug = Config::getVar('database', 'debug') ? true : false;
00073 $this->connectOnInit = true;
00074 $this->forceNew = false;
00075
00076 return $this->initConn();
00077 }
00078
00093 function initCustomDBConnection($driver, $host, $username, $password, $databaseName, $persistent = true, $connectionCharset = false, $connectOnInit = true, $debug = false, $forceNew = false) {
00094 $this->driver = $driver;
00095 $this->host = $host;
00096 $this->username = $username;
00097 $this->password = $password;
00098 $this->databaseName = $databaseName;
00099 $this->persistent = $persistent;
00100 $this->connectionCharset = $connectionCharset;
00101 $this->connectOnInit = $connectOnInit;
00102 $this->debug = $debug;
00103 $this->forceNew = $forceNew;
00104
00105 return $this->initConn();
00106 }
00107
00112 function initConn() {
00113 require_once('adodb/adodb.inc.php');
00114
00115 $this->dbconn = &ADONewConnection($this->driver);
00116
00117 if ($this->connectOnInit) {
00118 return $this->connect();
00119 } else {
00120 return true;
00121 }
00122 }
00123
00128 function connect() {
00129 if ($this->persistent) {
00130 $this->connected = @$this->dbconn->PConnect(
00131 $this->host,
00132 $this->username,
00133 $this->password,
00134 $this->databaseName
00135 );
00136
00137 } else {
00138 $this->connected = @$this->dbconn->Connect(
00139 $this->host,
00140 $this->username,
00141 $this->password,
00142 $this->databaseName,
00143 $this->forceNew
00144 );
00145 }
00146
00147 if ($this->debug) {
00148
00149 $this->dbconn->debug = true;
00150 }
00151
00152 if ($this->connected && $this->connectionCharset) {
00153
00154
00155 $this->dbconn->SetCharSet($this->connectionCharset);
00156 }
00157
00158 return $this->connected;
00159 }
00160
00164 function disconnect() {
00165 if ($this->connected) {
00166 $this->dbconn->Disconnect();
00167 $this->connected = false;
00168 }
00169 }
00170
00175 function reconnect($forceNew = false) {
00176 $this->disconnect();
00177 if ($forceNew) {
00178 $this->persistent = false;
00179 }
00180 $this->forceNew = $forceNew;
00181 return $this->connect();
00182 }
00183
00188 function &getDBConn() {
00189 return $this->dbconn;
00190 }
00191
00196 function isConnected() {
00197 return $this->connected;
00198 }
00199
00204 function getNumQueries() {
00205 return isset($this->dbconn) ? $this->dbconn->numQueries : 0;
00206 }
00207
00213 function &getInstance($setInstance = null) {
00214 static $instance;
00215
00216 if (isset($setInstance)) {
00217 $instance = $setInstance;
00218 } else if (!isset($instance)) {
00219 $instance = new DBConnection();
00220 }
00221
00222 return $instance;
00223 }
00224
00229 function &getConn() {
00230 $conn = &DBConnection::getInstance();
00231 return $conn->getDBConn();
00232 }
00233 }
00234
00235 ?>