Advertisement
Guest User

Untitled

a guest
Sep 14th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Lazy Database connection class - API version
  5. *
  6. * The database is not actually opened until we execute a query
  7. *
  8. * @author David Knape
  9. */
  10. class DBConnection
  11. {
  12.  
  13. public $databaseHost = "localhost";
  14. public $databaseUser = "root";
  15. public $databasePass = "";
  16. public $databaseName = "";
  17.  
  18. protected $connected = false;
  19.  
  20. protected $memcache;
  21. protected $memcacheHost;
  22. protected $memcachePort;
  23. protected $memcacheConnected = false;
  24.  
  25. public $debugLogEnabled = false;
  26. public $debugLogFile = "/tmp/dbconnection.log";
  27.  
  28. /**
  29. * Creates a new database connection
  30. *
  31. * @param $db_host
  32. * @param $db_name
  33. * @param $db_user
  34. * @param $db_pass
  35. * @param $memcache_host
  36. * @param $memcache_port
  37. */
  38. public function __construct($db_host = "localhost", $db_name = "", $db_user = "root", $db_pass = "", $memcache_host = "", $memcache_port = 11211)
  39. {
  40. $this->databaseHost = $db_host;
  41. $this->databaseUser = $db_user;
  42. $this->databasePass = $db_pass;
  43. $this->databaseName = $db_name;
  44. $this->memcacheHost = $memcache_host;
  45. $this->memcachePort = $memcache_port;
  46. }
  47.  
  48. public function __wakeup()
  49. {
  50. $this->connected = false;
  51. $this->memcacheConnected = false;
  52. }
  53.  
  54. public function isConnected()
  55. {
  56. return $this->connected;
  57. }
  58.  
  59. public function getDateTimeString($date_string)
  60. {
  61. return date('Y/M/d h:m:s', strtotime($date_string));
  62. }
  63.  
  64. /**
  65. * Runs DB Query using mysql function
  66. *
  67. * Returns results as an array of objects.
  68. * Optional param ('explicitType') is used to pass a class type to AMF
  69. *
  70. * @access private
  71. */
  72. public function query($sql, $ignoreError = false, $explicitType = "")
  73. {
  74. $rs = $this->execute($sql, $ignoreError);
  75. $results = array();
  76. if (is_resource($rs)) {
  77. while ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
  78. // convert ints and float to actual ints and floats
  79. $col_n = 0;
  80. foreach ($row as $col => $val) {
  81. switch (mysql_field_type($rs, $col_n)) {
  82. case 'int':
  83. $row[$col] = (int)$val;
  84. break;
  85. case 'real':
  86. $row[$col] = (float)$val;
  87. break;
  88. }
  89. $col_n++;
  90. }
  91. $obj = (object)$row;
  92. if ($explicitType) $obj->_explicitType = $explicitType;
  93. $results[] = $obj;
  94. }
  95. }
  96. //if(count($results)==1) return $results[0];
  97. return $results;
  98. }
  99.  
  100. /**
  101. * Default cache time - 60 minutes (3600 seconds)
  102. */
  103. public function queryCached($sql, $seconds = 3600, $explicitType = null)
  104. {
  105. if (!$this->memcacheConnected && $this->memcacheHost && $this->memcachePort) {
  106. $this->connectMemcache();
  107. }
  108. $key = MD5($this->databaseHost . '_' . $this->databaseName . '_' . $this->databaseUser) . '_' . MD5($sql);
  109. if ($this->memcacheConnected) {
  110. $result = $this->memcache->get($key);
  111. }
  112. if (!$result) {
  113. $result = $this->query($sql, false, $explicitType);
  114. if ($this->memcacheConnected) {
  115. $this->memcache->set($key, $result, MEMCACHE_COMPRESSED, $seconds);
  116. }
  117. }
  118. return $result;
  119. }
  120.  
  121. /**
  122. * Shortcut to return first record
  123. *
  124. * @param $sql
  125. * @param $ignoreError
  126. * @param $explicitType
  127. */
  128. public function getOne($sql, $ignoreError = false, $explicitType = "")
  129. {
  130. return @array_shift($this->query($sql, $ignoreError, $explicitType));
  131. }
  132.  
  133. /**
  134. * Execute a query and return mysql resource on success
  135. *
  136. * Throws an exception on errors unless ignoreError is true
  137. */
  138. public function execute($sql, $ignoreError = false)
  139. {
  140. if (!$this->connected) $this->connect();
  141. $this->trace("[DBConnection] execute: " . $sql);
  142. $rs = mysql_query($sql);
  143.  
  144. if (!$rs) {
  145. if ($ignoreError !== true) throw new Exception(mysql_error());
  146. return false;
  147. } else {
  148. return $rs;
  149. }
  150. }
  151.  
  152. public function connect()
  153. {
  154. if ($this->connected) return;
  155. $this->connected = @mysql_connect($this->databaseHost, $this->databaseUser, $this->databasePass) && @mysql_select_db($this->databaseName);
  156.  
  157. // try again in 1 second
  158. if (!$this->connected) {
  159. sleep(1);
  160. $this->connected = @mysql_connect($this->databaseHost, $this->databaseUser, $this->databasePass) && @mysql_select_db($this->databaseName);
  161. }
  162.  
  163. if (!$this->connected) {
  164. $this->trace("[DBConnection] Failed to Connect");
  165. throw new Exception("Unable to connect to the database {$this->databaseName} at {$this->databaseHost}");
  166. }
  167. }
  168.  
  169. /**
  170. * Initialize memcache connection
  171. */
  172. protected function connectMemcache()
  173. {
  174.  
  175. $host = $this->memcacheHost;
  176. $port = $this->memcachePort;
  177.  
  178. $this->memcacheConnected = false;
  179. // init memcache
  180. if (@class_exists('Memcache') && $host != null && $port != null) {
  181. $this->memcache = new Memcache();
  182. if (@$this->memcache->connect($host, $port)) {
  183. $this->memcacheConnected = true;
  184. } else if (@$this->memcache->connect($host, $port)) {
  185. // try again to avoid intermittent connection issues
  186. $this->memcacheConnected = true;
  187. }
  188. }
  189. }
  190.  
  191. protected function trace($s)
  192. {
  193. if (!$this->debugLogEnabled) return;
  194. if (!file_exists($this->debugLogFile)) {
  195. touch($this->debugLogFile);
  196. }
  197. $fh = @fopen($this->debugLogFile, 'a');
  198. if (is_resource($fh)) {
  199. fwrite($fh, "$s\n");
  200. fclose($fh);
  201. }
  202. }
  203.  
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement