Guest User

Untitled

a guest
Dec 12th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  4.  
  5. class DatabaseMySQL {
  6.  
  7. protected $link;
  8. private $hostname;
  9. private $username;
  10. private $password;
  11. private $database;
  12. private $onInit = array();
  13. private $queries = array();
  14. public $debug = true;
  15.  
  16. // {{{ __construct()
  17.  
  18. public function __construct($hostname, $username, $password, $database = null, array $onInit = array())
  19. {
  20. /** store values for lazy connect */
  21. $this->hostname = $hostname;
  22. $this->username = $username;
  23. $this->password = $password;
  24. $this->database = $database;
  25. $this->onInit = $onInit;
  26. }
  27.  
  28. // }}}
  29. // {{{ lazyConnect()
  30.  
  31. private function lazyConnect()
  32. {
  33. /** use stored values to connect */
  34. $this->connect(
  35. $this->hostname
  36. , $this->username
  37. , $this->password
  38. , $this->database
  39. );
  40. return $this;
  41. }
  42.  
  43. // }}}
  44. // {{{ connect()
  45.  
  46. public function connect($hostname, $username, $password, $database = null)
  47. {
  48. $mysqli = @new mysqli($hostname, $username, $password, $database);
  49. if (mysqli_connect_errno()) {
  50. throw new DatabaseException(
  51. 'Could not connect to database on: ' . $hostname . PHP_EOL
  52. . 'Server returned: ' . mysqli_connect_error() . ' (Errno. ' . mysqli_connect_errno() . ')'
  53. );
  54. }
  55. $this->link = $mysqli;
  56. /** execute init statements if any */
  57. if (count($this->onInit)) {
  58. foreach ($this->onInit as $statement) {
  59. $this->doExec($statement);
  60. }
  61. }
  62. return $this;
  63. }
  64.  
  65. // }}}
  66. // {{{ checkLink()
  67.  
  68. public function checkLink()
  69. {
  70. if (is_null($this->link)) {
  71. return false;
  72. }
  73. return true;
  74. }
  75.  
  76. // }}}
  77. // {{{ selectDatabase()
  78.  
  79. public function selectDatabase($database)
  80. {
  81. if ($this->checkLink() === false) {
  82. $this->lazyConnect();
  83. }
  84. if ($this->link->select_db($database) === false) {
  85. throw new DatabaseException(
  86. 'Could not select database: ' . $database . PHP_EOL
  87. . 'Server returned: ' . $this->link->error . ' (Errno. ' . $this->link->errno . ')'
  88. );
  89. }
  90. $this->database = $database;
  91. return $this;
  92. }
  93.  
  94. // }}}
  95. // {{{ getResult()
  96.  
  97. private function getResult($statement, $database = null)
  98. {
  99. /** check if we have no link */
  100. if ($this->checkLink() === false) {
  101. /** lazy connect */
  102. $this->lazyConnect();
  103. }
  104. /** check if we are switching database */
  105. if (!is_null($database)) {
  106. /** store name of old one and switch to new */
  107. $old = $this->database;
  108. $this->selectDatabase($database);
  109. }
  110. /** check if we debug mode is on */
  111. if ($this->debug === true) {
  112. /** get result set within timers */
  113. $start = microtime(true);
  114. $result = $this->link->query($statement);
  115. $end = microtime(true);
  116. /** add to queries array */
  117. $this->queries[] = array(
  118. $this->clean($statement)
  119. , sprintf('%.8F', $end - $start)
  120. );
  121. /** if not, just get the result set */
  122. } else {
  123. $result = $this->link->query($statement);
  124. }
  125. /** switch back to old database if applicable */
  126. if (isset($old)) {
  127. $this->selectDatabase($old);
  128. }
  129. /** if result is boolean false then query failed */
  130. if ($result === false) {
  131. throw new DatabaseException(
  132. 'Invalid query: ' . $statement . PHP_EOL
  133. . 'Server returned: ' . $this->link->error . ' (Errno. ' . $this->link->errno . ')'
  134. );
  135. }
  136. /** return result set */
  137. return $result;
  138. }
  139.  
  140. // }}}
  141. // {{{ doSelect()
  142.  
  143. public function doSelect($statement, $database = null)
  144. {
  145. return $this->getResult($statement, $database);
  146. }
  147.  
  148. // }}}
  149. // {{{ doInsert()
  150.  
  151. public function doInsert($statement, $database = null)
  152. {
  153. $this->getResult($statement, $database);
  154. return $this->link->insert_id;
  155. }
  156.  
  157. // }}}
  158. // {{{ doAlter()
  159.  
  160. public function doAlter($statement, $database = null)
  161. {
  162. $this->getResult($statement, $database);
  163. return $this->link->affected_rows;
  164. }
  165.  
  166. // }}}
  167. // {{{ doExec()
  168.  
  169. public function doExec($statement, $database = null)
  170. {
  171. $this->getResult($statement, $database);
  172. return $this;
  173. }
  174.  
  175. // }}}
  176. // {{{ escape()
  177.  
  178. public function escape($string)
  179. {
  180. /** check if we have no link */
  181. if ($this->checkLink() === false) {
  182. /** lazy connect */
  183. $this->lazyConnect();
  184. }
  185. /** return the value properly escaped */
  186. return $this->link->real_escape_string($string);
  187. }
  188.  
  189. // }}}
  190. // {{{ clean()
  191.  
  192. public function clean($string)
  193. {
  194. /** strip all linebreaks and tabs, replace with spaces */
  195. $string = preg_replace('/[\r|\n|\t]/m', ' ', $string);
  196. /** strip all excess whitespace */
  197. $string = preg_replace('/ +/m', ' ', $string);
  198. /** return string */
  199. return trim($string);
  200. }
  201.  
  202. // }}}
  203. // {{{ getQueries()
  204.  
  205. public function getQueries()
  206. {
  207. return $this->queries;
  208. }
  209.  
  210. // }}}
  211. }
Add Comment
Please, Sign In to add comment