Open Journal Systems  2.4.4
 All Classes Namespaces Functions Variables Groups Pages
DBConnection.inc.php
1 <?php
2 
18 class DBConnection {
19 
21  var $dbconn;
22 
24  var $driver;
25  var $host;
26  var $username;
27  var $password;
28  var $databaseName;
29  var $persistent;
30  var $connectionCharset;
31  var $forceNew; // Only applicable if non-persistent
32 
34  var $connectOnInit;
35 
36  /* @var boolean enable debugging output */
37  var $debug;
38 
40  var $connected;
41 
48  function DBConnection() {
49  $this->connected = false;
50 
51  if (func_num_args() == 0) {
52  $this->initDefaultDBConnection();
53  } else {
54  $args = func_get_args();
55  call_user_func_array(array(&$this, 'initCustomDBConnection'), $args);
56  }
57  }
58 
65  $this->driver = Config::getVar('database', 'driver');
66  $this->host = Config::getVar('database', 'host');
67  $this->username = Config::getVar('database', 'username');
68  $this->password = Config::getVar('database', 'password');
69  $this->databaseName = Config::getVar('database', 'name');
70  $this->persistent = Config::getVar('database', 'persistent') ? true : false;
71  $this->connectionCharset = Config::getVar('i18n', 'connection_charset');
72  $this->debug = Config::getVar('database', 'debug') ? true : false;
73  $this->connectOnInit = true;
74  $this->forceNew = false;
75 
76  return $this->initConn();
77  }
78 
94  function initCustomDBConnection($driver, $host, $username, $password, $databaseName, $persistent = true, $connectionCharset = false, $connectOnInit = true, $debug = false, $forceNew = false) {
95  $this->driver = $driver;
96  $this->host = $host;
97  $this->username = $username;
98  $this->password = $password;
99  $this->databaseName = $databaseName;
100  $this->persistent = $persistent;
101  $this->connectionCharset = $connectionCharset;
102  $this->connectOnInit = $connectOnInit;
103  $this->debug = $debug;
104  $this->forceNew = $forceNew;
105 
106  return $this->initConn();
107  }
108 
113  function initConn() {
114  require_once('lib/pkp/lib/adodb/adodb.inc.php');
115 
116  $this->dbconn =& ADONewConnection($this->driver);
117 
118  if ($this->connectOnInit) {
119  return $this->connect();
120  } else {
121  return true;
122  }
123  }
124 
129  function connect() {
130  if ($this->persistent) {
131  $this->connected = @$this->dbconn->PConnect(
132  $this->host,
133  $this->username,
134  $this->password,
135  $this->databaseName
136  );
137 
138  } else {
139  $this->connected = @$this->dbconn->Connect(
140  $this->host,
141  $this->username,
142  $this->password,
143  $this->databaseName,
144  $this->forceNew
145  );
146  }
147 
148  if ($this->debug) {
149  // Enable verbose database debugging (prints all SQL statements as they're executed)
150  $this->dbconn->debug = true;
151  }
152 
153  if ($this->connected && $this->connectionCharset) {
154  // Set client/connection character set
155  // NOTE: Only supported on some database servers and versions
156  $this->dbconn->SetCharSet($this->connectionCharset);
157  }
158 
159  return $this->connected;
160  }
161 
165  function disconnect() {
166  if ($this->connected) {
167  $this->dbconn->Disconnect();
168  $this->connected = false;
169  }
170  }
171 
176  function reconnect($forceNew = false) {
177  $this->disconnect();
178  if ($forceNew) {
179  $this->persistent = false;
180  }
181  $this->forceNew = $forceNew;
182  return $this->connect();
183  }
184 
189  function &getDBConn() {
190  return $this->dbconn;
191  }
192 
197  function isConnected() {
198  return $this->connected;
199  }
200 
205  function getNumQueries() {
206  return isset($this->dbconn) ? $this->dbconn->numQueries : 0;
207  }
208 
214  function &getInstance($setInstance = null) {
215  $instance =& Registry::get('dbInstance', true, null);
216 
217  if (isset($setInstance)) {
218  $instance = $setInstance;
219  } else if ($instance === null) {
220  $instance = new DBConnection();
221  }
222 
223  return $instance;
224  }
225 
230  function &getConn() {
231  $conn =& DBConnection::getInstance();
232  return $conn->getDBConn();
233  }
234 
239  function getDriver() {
240  return $this->driver;
241  }
242 
248  function logQuery($sql, $start, $params = array()) {
249  if (!Config::getVar('debug', 'show_stats')) return;
250 
251  $queries =& Registry::get('queries', true, array());
252 
253  // re-combine the SQL into a prepared statement
254  $preparedSql = '';
255  foreach(explode('?',$sql) as $key => $val) {
256  if (isset($params[$key])) {
257  $preparedSql .= $val.$params[$key];
258  }
259  }
260 
261  $query = array(
262  'sql' => $preparedSql,
263  'time' => (Core::microtime() - $start)*1000
264  );
265  array_push($queries, $query);
266  }
267 }
268 
269 ?>
initCustomDBConnection($driver, $host, $username, $password, $databaseName, $persistent=true, $connectionCharset=false, $connectOnInit=true, $debug=false, $forceNew=false)
reconnect($forceNew=false)
microtime()
Definition: Core.inc.php:102
& getInstance($setInstance=null)
getVar($section, $key, $default=null)
Definition: Config.inc.php:34
logQuery($sql, $start, $params=array())
& get($key, $createIfEmpty=false, $createWithDefault=null)
Class for accessing the low-level database connection. Currently integrated with ADOdb (from http://a...