PageRenderTime 24ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/ojs/ojs-2.3.2-1/lib/pkp/classes/db/DBConnection.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite
PHP | 270 lines | 139 code | 38 blank | 93 comment | 17 complexity | cf6a9e2c5433cac78d197778974142fb MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @file classes/db/DBConnection.inc.php
  4. *
  5. * Copyright (c) 2000-2009 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class DBConnection
  9. * @ingroup db
  10. *
  11. * @brief Class for accessing the low-level database connection.
  12. * Currently integrated with ADOdb (from http://adodb.sourceforge.net).
  13. */
  14. // $Id: DBConnection.inc.php,v 1.6 2009/04/24 18:50:15 mj Exp $
  15. class DBConnection {
  16. /** The underlying database connection object */
  17. var $dbconn;
  18. /** Database connection parameters */
  19. var $driver;
  20. var $host;
  21. var $username;
  22. var $password;
  23. var $databaseName;
  24. var $persistent;
  25. var $connectionCharset;
  26. var $forceNew; // Only applicable if non-persistent
  27. /** @var boolean establish connection on initiation */
  28. var $connectOnInit;
  29. /* @var boolean enable debugging output */
  30. var $debug;
  31. /** @var boolean indicate connection status */
  32. var $connected;
  33. /**
  34. * Constructor.
  35. * Calls initDefaultDBConnection if no arguments are passed,
  36. * otherwise calls initCustomDBConnection with custom connection
  37. * parameters.
  38. */
  39. function DBConnection() {
  40. $this->connected = false;
  41. if (func_num_args() == 0) {
  42. $this->initDefaultDBConnection();
  43. } else {
  44. $args = func_get_args();
  45. call_user_func_array(array(&$this, 'initCustomDBConnection'), $args);
  46. }
  47. }
  48. /**
  49. * Create new database connection with the connection parameters from
  50. * the system configuration.
  51. * @return boolean
  52. */
  53. function initDefaultDBConnection() {
  54. $this->driver = Config::getVar('database', 'driver');
  55. $this->host = Config::getVar('database', 'host');
  56. $this->username = Config::getVar('database', 'username');
  57. $this->password = Config::getVar('database', 'password');
  58. $this->databaseName = Config::getVar('database', 'name');
  59. $this->persistent = Config::getVar('database', 'persistent') ? true : false;
  60. $this->connectionCharset = Config::getVar('i18n', 'connection_charset');
  61. $this->debug = Config::getVar('database', 'debug') ? true : false;
  62. $this->connectOnInit = true;
  63. $this->forceNew = false;
  64. return $this->initConn();
  65. }
  66. /**
  67. * Create new database connection with the specified connection
  68. * parameters.
  69. * @param $driver string
  70. * @param $host string
  71. * @param $username string
  72. * @param $password string
  73. * @param $databaseName string
  74. * @param $persistent boolean use persistent connections (default true)
  75. * @param $connectionCharset string character set to use for the connection (default none)
  76. * @param $connectOnInit boolean establish database connection on initiation (default true)
  77. * @param $debug boolean enable verbose debug output (default false)
  78. * @param $forceNew boolean force a new connection (default false)
  79. * @return boolean
  80. */
  81. function initCustomDBConnection($driver, $host, $username, $password, $databaseName, $persistent = true, $connectionCharset = false, $connectOnInit = true, $debug = false, $forceNew = false) {
  82. $this->driver = $driver;
  83. $this->host = $host;
  84. $this->username = $username;
  85. $this->password = $password;
  86. $this->databaseName = $databaseName;
  87. $this->persistent = $persistent;
  88. $this->connectionCharset = $connectionCharset;
  89. $this->connectOnInit = $connectOnInit;
  90. $this->debug = $debug;
  91. $this->forceNew = $forceNew;
  92. return $this->initConn();
  93. }
  94. /**
  95. * Initialize database connection object and establish connection to the database.
  96. * @return boolean
  97. */
  98. function initConn() {
  99. require_once('adodb.inc.php');
  100. $this->dbconn =& ADONewConnection($this->driver);
  101. if ($this->connectOnInit) {
  102. return $this->connect();
  103. } else {
  104. return true;
  105. }
  106. }
  107. /**
  108. * Establish connection to the database.
  109. * @return boolean
  110. */
  111. function connect() {
  112. if ($this->persistent) {
  113. $this->connected = @$this->dbconn->PConnect(
  114. $this->host,
  115. $this->username,
  116. $this->password,
  117. $this->databaseName
  118. );
  119. } else {
  120. $this->connected = @$this->dbconn->Connect(
  121. $this->host,
  122. $this->username,
  123. $this->password,
  124. $this->databaseName,
  125. $this->forceNew
  126. );
  127. }
  128. if ($this->debug) {
  129. // Enable verbose database debugging (prints all SQL statements as they're executed)
  130. $this->dbconn->debug = true;
  131. }
  132. if ($this->connected && $this->connectionCharset) {
  133. // Set client/connection character set
  134. // NOTE: Only supported on some database servers and versions
  135. $this->dbconn->SetCharSet($this->connectionCharset);
  136. }
  137. return $this->connected;
  138. }
  139. /**
  140. * Disconnect from the database.
  141. */
  142. function disconnect() {
  143. if ($this->connected) {
  144. $this->dbconn->Disconnect();
  145. $this->connected = false;
  146. }
  147. }
  148. /**
  149. * Reconnect to the database.
  150. * @param $forceNew boolean force a new connection
  151. */
  152. function reconnect($forceNew = false) {
  153. $this->disconnect();
  154. if ($forceNew) {
  155. $this->persistent = false;
  156. }
  157. $this->forceNew = $forceNew;
  158. return $this->connect();
  159. }
  160. /**
  161. * Return the database connection object.
  162. * @return ADONewConnection
  163. */
  164. function &getDBConn() {
  165. return $this->dbconn;
  166. }
  167. /**
  168. * Check if a database connection has been established.
  169. * @return boolean
  170. */
  171. function isConnected() {
  172. return $this->connected;
  173. }
  174. /**
  175. * Get number of database queries executed.
  176. * @return int
  177. */
  178. function getNumQueries() {
  179. return isset($this->dbconn) ? $this->dbconn->numQueries : 0;
  180. }
  181. /**
  182. * Return a reference to a single static instance of the database connection manager.
  183. * @param $setInstance DBConnection
  184. * @return DBConnection
  185. */
  186. function &getInstance($setInstance = null) {
  187. $instance =& Registry::get('dbInstance', true, null);
  188. if (isset($setInstance)) {
  189. $instance = $setInstance;
  190. } else if ($instance === null) {
  191. $instance = new DBConnection();
  192. }
  193. return $instance;
  194. }
  195. /**
  196. * Return a reference to a single static instance of the database connection.
  197. * @return ADONewConnection
  198. */
  199. function &getConn() {
  200. $conn =& DBConnection::getInstance();
  201. return $conn->getDBConn();
  202. }
  203. /**
  204. * Return the name of the driver used for this connection.
  205. * @return string
  206. */
  207. function getDriver() {
  208. return $this->driver;
  209. }
  210. /**
  211. * Log a SQL query and execution time in the PKPProfiler debug log
  212. * @param $sql string SQL statement being run
  213. * @param $start string a float representing the unix microtime the query started
  214. */
  215. function logQuery($sql, $start, $params = array()) {
  216. if (!Config::getVar('debug', 'show_stats')) return;
  217. $queries =& Registry::get('queries', true, array());
  218. // re-combine the SQL into a prepared statement
  219. $preparedSql = '';
  220. foreach(explode('?',$sql) as $key => $val) {
  221. if (isset($params[$key])) {
  222. $preparedSql .= $val.$params[$key];
  223. }
  224. }
  225. $query = array(
  226. 'sql' => $preparedSql,
  227. 'time' => (Core::microtime() - $start)*1000
  228. );
  229. array_push($queries, $query);
  230. }
  231. }
  232. ?>