Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- final class MySqliDB
- {
- private $Debug = false;
- private $Calls = Array();
- private $FetchesPerCall = 0;
- private $TotalRuntime = 0;
- private $Hits = 0;
- private $Host = null;
- private $Database = null;
- private $User = null;
- private $Pass = null;
- private $Port = null;
- private $Opened = false;
- private $InTrans = false;
- private $Conn = null; //the connection or link
- private $Result = null; //holds the resultset
- //ctor
- public function __construct($host, $user, $pass, $db, $port, $debug){
- $this->Debug = $debug;
- $this->Host = $host;
- $this->Database = $db;
- $this->User = $user;
- $this->Pass = $pass;
- $this->Port = $port;
- }
- //open the database connection
- public final function Open(){
- if($this->Opened) return;
- $this->Opened = true;
- $this->Conn = mysqli_init();
- $this->Conn->options(MYSQLI_OPT_CONNECT_TIMEOUT, CONST_DB_TIMEOUT);
- $this->Conn->options(MYSQLI_SET_CHARSET_NAME, CONST_DB_CHARSET);
- $this->Conn->real_connect($this->Host, $this->User, $this->Pass, $this->Database, $this->Port);
- if ($this->Conn->connect_errno)
- throw new xbException("Could not connect: " . $this->Conn->connect_error);
- }
- public final function GetConnectionStats(){
- if($this->Opened) $s = $this->Conn->get_connection_stats();
- if ($this->Conn->error)
- throw new xbException("Could not get client connection stats (get_connection_stats): " . $this->Conn->error);
- return $s;
- }
- public final function GetClientStats(){
- if($this->Opened) $s = mysqli_get_client_stats();
- if ($this->Conn->error)
- throw new xbException("Could not get client per-process stats (get_client_stats): " . $this->Conn->error);
- return $s;
- }
- public final function GetStatus(){
- if($this->Opened) $s = $this->Conn->stat();
- if ($this->Conn->error)
- throw new xbException("Could not get current system status (stat): " . $this->Conn->error);
- return $s;
- }
- public final function GetVersion(){
- if($this->Opened) $s = sprintf("%s : %s", $this->Conn->server_info, $this->Conn->server_version);
- if ($this->Conn->error)
- throw new xbException("Could not get server version (server_info/server_version): " . $this->Conn->error);
- return $s;
- }
- public final function GetThreadID(){
- if($this->Opened) $s = $this->Conn->thread_id;
- if ($this->Conn->error)
- throw new xbException("Could not get thread id (thread_id): " . $this->Conn->error);
- return $s;
- }
- public final function GetClientVersion(){
- if($this->Opened) $s = sprintf("%s : %s", $this->Conn->client_info, $this->Conn->client_version);
- if ($this->Conn->error)
- throw new xbException("Could not get client version (client_info/client_version): " . $this->Conn->error);
- return $s;
- }
- public final function GetProtocolVersion(){
- if($this->Opened) $s = $this->Conn->protocol_version;
- if ($this->Conn->error)
- throw new xbException("Could not get protocol version (protocol_version): " . $this->Conn->error);
- return $s;
- }
- public final function GetHostInfo(){
- if($this->Opened) $s = $this->Conn->host_info;
- if ($this->Conn->error)
- throw new xbException("Could not get host info set (host_info): " . $this->Conn->error);
- return $s;
- }
- public final function GetCharset(){
- if($this->Opened) $s = $this->Conn->get_charset();
- if ($this->Conn->error)
- throw new xbException("Could not get character set (get_charset): " . $this->Conn->error);
- return $s;
- }
- public final function GetCharsetName(){
- if($this->Opened) $s = $this->Conn->character_set_name();
- if ($this->Conn->error)
- throw new xbException("Could not get character set name: " . $this->Conn->error);
- return $s;
- }
- public final static function EscapeQuotes($s){
- if($s == null) return "null";
- return sprintf("'%s'", addslashes($s));
- }
- public final function InTransaction(){
- return $this->InTrans;
- }
- public final function BeginTran(){
- if($this->InTrans) return;
- $this->InTrans = true;
- $this->Conn->autocommit(FALSE);
- }
- public final function Commit(){
- if(!$this->InTrans) return;
- $this->Conn->commit();
- $this->InTrans = false;
- }
- public final function Rollback(){
- if(!$this->InTrans) return;
- $this->Conn->rollback();
- $this->InTrans = false;
- }
- //close the database connection
- public final function Close(){
- if(!$this->Conn) return;
- $this->Conn->close();
- }
- //just the number of times we've called into the database
- public final function GetHits(){
- return $this->Hits;
- }
- //just the number of times we've called into the database
- public final function GetTotalRuntime(){
- return $this->TotalRuntime;
- }
- //just a list of sql calls we made
- public final function GetCalls(){
- return $this->Calls;
- }
- //call a stored proc - only stored procs pls !!
- public final function Query($sqlCmd){
- ++$this->Hits;
- $this->FetchesPerCall = 0;
- $startTime = microtime(true);
- $this->Result = $this->Conn->query($sqlCmd);
- $runtime = microtime(true) - $startTime;
- $this->TotalRuntime += $runtime;
- if($this->Debug){
- $this->Calls[] = sprintf("<span class='lime'>%s</span><br/>runtime = <span class='lime'>%s</span>",
- $sqlCmd, number_format($runtime, 8, '.', ''));
- }
- if ($this->Conn->error)
- throw new xbException("Could not query: " . $this->Conn->error);
- $rows = $this->Conn->affected_rows;
- return ($this->Result && $rows > 0 ? $rows : 0);
- }
- //returns a record as a disconnected array - close the connection after this call
- public final function FetchAssocArray(){
- $row = $this->Result->fetch_array(MYSQLI_ASSOC);
- if($this->Conn->error)
- throw new xbException("Could not fetch: " . $this->Conn->error);
- ++$this->FetchesPerCall;
- return $row;
- }
- //reads the next record from the database - persist the connection until u finish looping data
- public final function FetchAssoc(){
- $row = $this->Result->fetch_assoc();
- if($this->Conn->error)
- throw new xbException("Could not fetch: " . $this->Conn->error);
- if($row) ++$this->FetchesPerCall;
- return $row;
- }
- //when calling stored procs be aware that u get 2 resultsets. One data one and an additional sp return resultset
- public final function CloseResult(){
- if(!$this->Result) return;
- if($this->Debug) $this->Calls[] = sprintf("<span>%d fetches</span> - <span>closed result OK</span><br/>", $this->FetchesPerCall);
- $this->Result->close();
- try{
- $this->Conn->next_result(); //interesting - each stored proc returns 2 resultsets.
- }
- catch(Exception $e)
- {
- //ignore
- }
- }
- }
- ?>
Add Comment
Please, Sign In to add comment