Guest User

Untitled

a guest
Jul 24th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.77 KB | None | 0 0
  1. <?php
  2. final class MySqliDB
  3. {
  4. private $Debug = false;
  5.  
  6. private $Calls = Array();
  7. private $FetchesPerCall = 0;
  8. private $TotalRuntime = 0;
  9.  
  10. private $Hits = 0;
  11.  
  12. private $Host = null;
  13. private $Database = null;
  14. private $User = null;
  15. private $Pass = null;
  16. private $Port = null;
  17.  
  18. private $Opened = false;
  19. private $InTrans = false;
  20.  
  21. private $Conn = null; //the connection or link
  22. private $Result = null; //holds the resultset
  23.  
  24. //ctor
  25. public function __construct($host, $user, $pass, $db, $port, $debug){
  26. $this->Debug = $debug;
  27. $this->Host = $host;
  28. $this->Database = $db;
  29. $this->User = $user;
  30. $this->Pass = $pass;
  31. $this->Port = $port;
  32. }
  33.  
  34. //open the database connection
  35. public final function Open(){
  36.  
  37. if($this->Opened) return;
  38.  
  39. $this->Opened = true;
  40.  
  41. $this->Conn = mysqli_init();
  42.  
  43. $this->Conn->options(MYSQLI_OPT_CONNECT_TIMEOUT, CONST_DB_TIMEOUT);
  44. $this->Conn->options(MYSQLI_SET_CHARSET_NAME, CONST_DB_CHARSET);
  45.  
  46. $this->Conn->real_connect($this->Host, $this->User, $this->Pass, $this->Database, $this->Port);
  47.  
  48. if ($this->Conn->connect_errno)
  49. throw new xbException("Could not connect: " . $this->Conn->connect_error);
  50. }
  51.  
  52.  
  53. public final function GetConnectionStats(){
  54.  
  55. if($this->Opened) $s = $this->Conn->get_connection_stats();
  56.  
  57. if ($this->Conn->error)
  58. throw new xbException("Could not get client connection stats (get_connection_stats): " . $this->Conn->error);
  59.  
  60. return $s;
  61. }
  62.  
  63. public final function GetClientStats(){
  64.  
  65. if($this->Opened) $s = mysqli_get_client_stats();
  66.  
  67. if ($this->Conn->error)
  68. throw new xbException("Could not get client per-process stats (get_client_stats): " . $this->Conn->error);
  69.  
  70. return $s;
  71. }
  72.  
  73. public final function GetStatus(){
  74.  
  75. if($this->Opened) $s = $this->Conn->stat();
  76.  
  77. if ($this->Conn->error)
  78. throw new xbException("Could not get current system status (stat): " . $this->Conn->error);
  79.  
  80. return $s;
  81. }
  82.  
  83. public final function GetVersion(){
  84.  
  85. if($this->Opened) $s = sprintf("%s : %s", $this->Conn->server_info, $this->Conn->server_version);
  86.  
  87. if ($this->Conn->error)
  88. throw new xbException("Could not get server version (server_info/server_version): " . $this->Conn->error);
  89.  
  90. return $s;
  91. }
  92.  
  93. public final function GetThreadID(){
  94.  
  95. if($this->Opened) $s = $this->Conn->thread_id;
  96.  
  97. if ($this->Conn->error)
  98. throw new xbException("Could not get thread id (thread_id): " . $this->Conn->error);
  99.  
  100. return $s;
  101. }
  102.  
  103. public final function GetClientVersion(){
  104.  
  105. if($this->Opened) $s = sprintf("%s : %s", $this->Conn->client_info, $this->Conn->client_version);
  106.  
  107. if ($this->Conn->error)
  108. throw new xbException("Could not get client version (client_info/client_version): " . $this->Conn->error);
  109.  
  110. return $s;
  111. }
  112.  
  113. public final function GetProtocolVersion(){
  114.  
  115. if($this->Opened) $s = $this->Conn->protocol_version;
  116.  
  117. if ($this->Conn->error)
  118. throw new xbException("Could not get protocol version (protocol_version): " . $this->Conn->error);
  119.  
  120. return $s;
  121.  
  122. }
  123.  
  124. public final function GetHostInfo(){
  125.  
  126. if($this->Opened) $s = $this->Conn->host_info;
  127.  
  128. if ($this->Conn->error)
  129. throw new xbException("Could not get host info set (host_info): " . $this->Conn->error);
  130.  
  131. return $s;
  132. }
  133.  
  134. public final function GetCharset(){
  135.  
  136. if($this->Opened) $s = $this->Conn->get_charset();
  137.  
  138. if ($this->Conn->error)
  139. throw new xbException("Could not get character set (get_charset): " . $this->Conn->error);
  140.  
  141. return $s;
  142. }
  143.  
  144. public final function GetCharsetName(){
  145.  
  146. if($this->Opened) $s = $this->Conn->character_set_name();
  147.  
  148. if ($this->Conn->error)
  149. throw new xbException("Could not get character set name: " . $this->Conn->error);
  150.  
  151. return $s;
  152. }
  153.  
  154. public final static function EscapeQuotes($s){
  155.  
  156. if($s == null) return "null";
  157. return sprintf("'%s'", addslashes($s));
  158. }
  159.  
  160. public final function InTransaction(){
  161. return $this->InTrans;
  162. }
  163.  
  164. public final function BeginTran(){
  165. if($this->InTrans) return;
  166. $this->InTrans = true;
  167.  
  168. $this->Conn->autocommit(FALSE);
  169. }
  170.  
  171. public final function Commit(){
  172. if(!$this->InTrans) return;
  173. $this->Conn->commit();
  174. $this->InTrans = false;
  175. }
  176.  
  177. public final function Rollback(){
  178. if(!$this->InTrans) return;
  179. $this->Conn->rollback();
  180. $this->InTrans = false;
  181. }
  182.  
  183. //close the database connection
  184. public final function Close(){
  185. if(!$this->Conn) return;
  186. $this->Conn->close();
  187. }
  188.  
  189. //just the number of times we've called into the database
  190. public final function GetHits(){
  191. return $this->Hits;
  192. }
  193.  
  194. //just the number of times we've called into the database
  195. public final function GetTotalRuntime(){
  196. return $this->TotalRuntime;
  197. }
  198.  
  199. //just a list of sql calls we made
  200. public final function GetCalls(){
  201. return $this->Calls;
  202. }
  203.  
  204. //call a stored proc - only stored procs pls !!
  205. public final function Query($sqlCmd){
  206.  
  207. ++$this->Hits;
  208.  
  209. $this->FetchesPerCall = 0;
  210. $startTime = microtime(true);
  211.  
  212. $this->Result = $this->Conn->query($sqlCmd);
  213.  
  214. $runtime = microtime(true) - $startTime;
  215.  
  216. $this->TotalRuntime += $runtime;
  217.  
  218. if($this->Debug){
  219. $this->Calls[] = sprintf("<span class='lime'>%s</span><br/>runtime = <span class='lime'>%s</span>",
  220. $sqlCmd, number_format($runtime, 8, '.', ''));
  221. }
  222.  
  223. if ($this->Conn->error)
  224. throw new xbException("Could not query: " . $this->Conn->error);
  225.  
  226. $rows = $this->Conn->affected_rows;
  227.  
  228. return ($this->Result && $rows > 0 ? $rows : 0);
  229. }
  230.  
  231. //returns a record as a disconnected array - close the connection after this call
  232. public final function FetchAssocArray(){
  233.  
  234. $row = $this->Result->fetch_array(MYSQLI_ASSOC);
  235.  
  236. if($this->Conn->error)
  237. throw new xbException("Could not fetch: " . $this->Conn->error);
  238.  
  239. ++$this->FetchesPerCall;
  240.  
  241. return $row;
  242. }
  243.  
  244. //reads the next record from the database - persist the connection until u finish looping data
  245. public final function FetchAssoc(){
  246.  
  247. $row = $this->Result->fetch_assoc();
  248.  
  249. if($this->Conn->error)
  250. throw new xbException("Could not fetch: " . $this->Conn->error);
  251.  
  252. if($row) ++$this->FetchesPerCall;
  253.  
  254. return $row;
  255. }
  256.  
  257. //when calling stored procs be aware that u get 2 resultsets. One data one and an additional sp return resultset
  258. public final function CloseResult(){
  259.  
  260. if(!$this->Result) return;
  261.  
  262. if($this->Debug) $this->Calls[] = sprintf("<span>%d fetches</span> - <span>closed result OK</span><br/>", $this->FetchesPerCall);
  263.  
  264. $this->Result->close();
  265.  
  266. try{
  267. $this->Conn->next_result(); //interesting - each stored proc returns 2 resultsets.
  268. }
  269. catch(Exception $e)
  270. {
  271. //ignore
  272. }
  273. }
  274. }
  275.  
  276. ?>
Add Comment
Please, Sign In to add comment