Commit bde3f2bd authored by Alex Ne's avatar Alex Ne

Add Simple Query For "CALL PROCEDURE()"

parent 7247d92b
<?php <?php
/* /*
НЕ ТРОЖ!!! :) НЕ ТРОЖ!!! :)
*/ */
class X_DB_MySQLi class X_DB_MySQLi
{ {
/**
* @var array
*/
static $DBC_LIST = []; static $DBC_LIST = [];
/**
* @var array
*/
static $LINK = []; static $LINK = [];
/**
* @var mixed
*/
private $DBLN = false; private $DBLN = false;
/**
* @var string
*/
private $DBHOST = "localhost"; private $DBHOST = "localhost";
/**
* @var string
*/
private $DBNAME = "test"; private $DBNAME = "test";
/**
* @var string
*/
private $DBUSER = "root"; private $DBUSER = "root";
/**
* @var string
*/
private $DBPASS = ""; private $DBPASS = "";
/**
* @var string
*/
private $DBCHARSET = "utf8"; private $DBCHARSET = "utf8";
// ARGV: // ARGV:
// array( "DBHOST" => , "DBNAME" => , "DBUSER" => , "DBPASS" => , "DBCHARSET" => ) // array( "DBHOST" => , "DBNAME" => , "DBUSER" => , "DBPASS" => , "DBCHARSET" => )
...@@ -22,30 +46,94 @@ class X_DB_MySQLi ...@@ -22,30 +46,94 @@ class X_DB_MySQLi
// $DBHOST, $DBNAME, $DBUSER, $DBPASS, $DBCHARSET // $DBHOST, $DBNAME, $DBUSER, $DBPASS, $DBCHARSET
// OR: // OR:
// $DBC - name of configured database // $DBC - name of configured database
function __construct() public function __construct()
{ $cfg = func_get_args(); {
if( !(count($cfg)>0) ) throw new Exception("DB_MySQLi_NULL_CONSTRUCT_ARGUMENT", X_Exception_List::DB_MySQLi_NULL_CONSTRUCT_ARGUMENT); $cfg = func_get_args();
if(is_array($cfg[0])) { if ( ! (count($cfg) > 0))
if(isset($cfg[0]["DBHOST"])) $this->DBHOST = $cfg[0]["DBHOST"]; {
if(isset($cfg[0]["DBNAME"])) $this->DBNAME = $cfg[0]["DBNAME"]; throw new Exception("DB_MySQLi_NULL_CONSTRUCT_ARGUMENT", X_Exception_List::DB_MySQLi_NULL_CONSTRUCT_ARGUMENT);
if(isset($cfg[0]["DBUSER"])) $this->DBUSER = $cfg[0]["DBUSER"]; }
if(isset($cfg[0]["DBPASS"])) $this->DBPASS = $cfg[0]["DBPASS"];
if(isset($cfg[0]["DBCHARSET"])) $this->DBCHARSET = $cfg[0]["DBCHARSET"]; if (is_array($cfg[0]))
} else if( count($cfg) == 1 ) { {
if(isset(X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBHOST"])) $this->DBHOST = X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBHOST"]; if (isset($cfg[0]["DBHOST"]))
if(isset(X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBNAME"])) $this->DBNAME = X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBNAME"]; {
if(isset(X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBUSER"])) $this->DBUSER = X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBUSER"]; $this->DBHOST = $cfg[0]["DBHOST"];
if(isset(X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBPASS"])) $this->DBPASS = X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBPASS"]; }
if(isset(X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBCHARSET"])) $this->DBCHARSET = X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBCHARSET"];
} else if( count($cfg) == 3 ) if (isset($cfg[0]["DBNAME"]))
{ $this->DBNAME = $cfg[0]; $this->DBUSER = $cfg[1]; $this->DBPASS = $cfg[2]; } {
else if( count($cfg) == 4 ) $this->DBNAME = $cfg[0]["DBNAME"];
{ $this->DBHOST = $cfg[0]; $this->DBNAME = $cfg[1]; $this->DBUSER = $cfg[2]; $this->DBPASS = $cfg[3]; } }
else if( count($cfg) == 5 )
{ $this->DBHOST = $cfg[0]; $this->DBNAME = $cfg[1]; $this->DBUSER = $cfg[2]; $this->DBPASS = $cfg[3]; $this->DBCHARSET = $cfg[4]; } if (isset($cfg[0]["DBUSER"]))
else throw new Exception("DB_MySQLi_WRONG_CONSTRUCT_ARGUMENT", X_Exception_List::DB_MySQLi_WRONG_CONSTRUCT_ARGUMENT); {
$this->DBLN = md5($this->DBHOST.$this->DBNAME.$this->DBUSER.$this->DBPASS.$this->DBCHARSET); $this->DBUSER = $cfg[0]["DBUSER"];
}
if (isset($cfg[0]["DBPASS"]))
{
$this->DBPASS = $cfg[0]["DBPASS"];
}
if (isset($cfg[0]["DBCHARSET"]))
{
$this->DBCHARSET = $cfg[0]["DBCHARSET"];
}
}
else if (count($cfg) == 1)
{
if (isset(self::$DBC_LIST[$cfg[0]]["DBHOST"]))
{
$this->DBHOST = self::$DBC_LIST[$cfg[0]]["DBHOST"];
}
if (isset(self::$DBC_LIST[$cfg[0]]["DBNAME"]))
{
$this->DBNAME = self::$DBC_LIST[$cfg[0]]["DBNAME"];
}
if (isset(self::$DBC_LIST[$cfg[0]]["DBUSER"]))
{
$this->DBUSER = self::$DBC_LIST[$cfg[0]]["DBUSER"];
}
if (isset(self::$DBC_LIST[$cfg[0]]["DBPASS"]))
{
$this->DBPASS = self::$DBC_LIST[$cfg[0]]["DBPASS"];
}
if (isset(self::$DBC_LIST[$cfg[0]]["DBCHARSET"]))
{
$this->DBCHARSET = self::$DBC_LIST[$cfg[0]]["DBCHARSET"];
}
}
else if (count($cfg) == 3)
{
$this->DBNAME = $cfg[0];
$this->DBUSER = $cfg[1];
$this->DBPASS = $cfg[2];}
else if (count($cfg) == 4)
{
$this->DBHOST = $cfg[0];
$this->DBNAME = $cfg[1];
$this->DBUSER = $cfg[2];
$this->DBPASS = $cfg[3];}
else if (count($cfg) == 5)
{
$this->DBHOST = $cfg[0];
$this->DBNAME = $cfg[1];
$this->DBUSER = $cfg[2];
$this->DBPASS = $cfg[3];
$this->DBCHARSET = $cfg[4];}
else
{
throw new Exception("DB_MySQLi_WRONG_CONSTRUCT_ARGUMENT", X_Exception_List::DB_MySQLi_WRONG_CONSTRUCT_ARGUMENT);
} }
$this->DBLN = md5($this->DBHOST . $this->DBNAME . $this->DBUSER . $this->DBPASS . $this->DBCHARSET);
}
// ARGV: // ARGV:
// $DBC, array( "DBHOST" => , "DBNAME" => , "DBUSER" => , "DBPASS" => , "DBCHARSET" => ) // $DBC, array( "DBHOST" => , "DBNAME" => , "DBUSER" => , "DBPASS" => , "DBCHARSET" => )
// OR: // OR:
...@@ -54,157 +142,315 @@ class X_DB_MySQLi ...@@ -54,157 +142,315 @@ class X_DB_MySQLi
// $DBC, $DBHOST, $DBNAME, $DBUSER, $DBPASS | Defauts: $DBCHARSET = "utf8"; // $DBC, $DBHOST, $DBNAME, $DBUSER, $DBPASS | Defauts: $DBCHARSET = "utf8";
// OR: // OR:
// $DBC, $DBHOST, $DBNAME, $DBUSER, $DBPASS, $DBCHARSET // $DBC, $DBHOST, $DBNAME, $DBUSER, $DBPASS, $DBCHARSET
static function SetConfig() public static function SetConfig()
{ $cfg = func_get_args(); {
if(isset($cfg[1]) && is_array($cfg[1])) { $cfg = func_get_args();
if(isset($cfg[1]["DBHOST"])) X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBHOST"] = $cfg[1]["DBHOST"]; if (isset($cfg[1]) && is_array($cfg[1]))
if(isset($cfg[1]["DBNAME"])) X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBNAME"] = $cfg[1]["DBNAME"]; {
if(isset($cfg[1]["DBUSER"])) X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBUSER"] = $cfg[1]["DBUSER"]; if (isset($cfg[1]["DBHOST"]))
if(isset($cfg[1]["DBPASS"])) X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBPASS"] = $cfg[1]["DBPASS"]; {
if(isset($cfg[1]["DBCHARSET"])) X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBCHARSET"] = $cfg[1]["DBCHARSET"]; self::$DBC_LIST[$cfg[0]]["DBHOST"] = $cfg[1]["DBHOST"];
} else if( count($cfg) == 4 ) { X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBNAME"] = $cfg[1]; X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBUSER"] = $cfg[2]; X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBPASS"] = $cfg[3]; }
} else if( count($cfg) == 5 ) { X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBHOST"] = $cfg[1]; X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBNAME"] = $cfg[2];
X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBUSER"] = $cfg[3]; X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBPASS"] = $cfg[4]; if (isset($cfg[1]["DBNAME"]))
} else if( count($cfg) == 6 ) { X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBHOST"] = $cfg[1]; X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBNAME"] = $cfg[2]; X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBUSER"] = $cfg[3]; {
X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBPASS"] = $cfg[4]; X_DB_MySQLi::$DBC_LIST[$cfg[0]]["DBCHARSET"] = $cfg[5]; } self::$DBC_LIST[$cfg[0]]["DBNAME"] = $cfg[1]["DBNAME"];
}
if (isset($cfg[1]["DBUSER"]))
{
self::$DBC_LIST[$cfg[0]]["DBUSER"] = $cfg[1]["DBUSER"];
}
if (isset($cfg[1]["DBPASS"]))
{
self::$DBC_LIST[$cfg[0]]["DBPASS"] = $cfg[1]["DBPASS"];
}
if (isset($cfg[1]["DBCHARSET"]))
{
self::$DBC_LIST[$cfg[0]]["DBCHARSET"] = $cfg[1]["DBCHARSET"];
}
}
else if (count($cfg) == 4)
{
self::$DBC_LIST[$cfg[0]]["DBNAME"] = $cfg[1];
self::$DBC_LIST[$cfg[0]]["DBUSER"] = $cfg[2];
self::$DBC_LIST[$cfg[0]]["DBPASS"] = $cfg[3];
}
else if (count($cfg) == 5)
{
self::$DBC_LIST[$cfg[0]]["DBHOST"] = $cfg[1];
self::$DBC_LIST[$cfg[0]]["DBNAME"] = $cfg[2];
self::$DBC_LIST[$cfg[0]]["DBUSER"] = $cfg[3];
self::$DBC_LIST[$cfg[0]]["DBPASS"] = $cfg[4];
}
else if (count($cfg) == 6)
{
self::$DBC_LIST[$cfg[0]]["DBHOST"] = $cfg[1];
self::$DBC_LIST[$cfg[0]]["DBNAME"] = $cfg[2];
self::$DBC_LIST[$cfg[0]]["DBUSER"] = $cfg[3];
self::$DBC_LIST[$cfg[0]]["DBPASS"] = $cfg[4];
self::$DBC_LIST[$cfg[0]]["DBCHARSET"] = $cfg[5];}
} }
/**
* @return mixed
*/
public function Connect() public function Connect()
{ {
if(!isset(X_DB_MySQLi::$LINK[$this->DBLN]) if ( ! isset(self::$LINK[$this->DBLN])
|| !(X_DB_MySQLi::$LINK[$this->DBLN] instanceof mysqli) || ! (self::$LINK[$this->DBLN] instanceof mysqli)
|| !X_DB_MySQLi::$LINK[$this->DBLN]->ping() ) || ! self::$LINK[$this->DBLN]->ping())
{
self::$LINK[$this->DBLN] = new mysqli($this->DBHOST, $this->DBUSER, $this->DBPASS, $this->DBNAME);
self::$LINK[$this->DBLN]->set_charset($this->DBCHARSET);
if (self::$LINK[$this->DBLN]->connect_errno)
{ {
X_DB_MySQLi::$LINK[$this->DBLN] = new mysqli( $this->DBHOST, $this->DBUSER, $this->DBPASS, $this->DBNAME ); throw new Exception(self::$LINK[$this->DBLN]->connect_error, self::$LINK[$this->DBLN]->connect_errno);
X_DB_MySQLi::$LINK[$this->DBLN]->set_charset( $this->DBCHARSET ); }
if ( X_DB_MySQLi::$LINK[$this->DBLN]->connect_errno )
throw new Exception(X_DB_MySQLi::$LINK[$this->DBLN]->connect_error, X_DB_MySQLi::$LINK[$this->DBLN]->connect_errno);
} }
return $this; return $this;
} }
public function add($tableName, $arrayParams, $replace=false) /**
* @param $tableName
* @param $arrayParams
* @param $replace
* @return mixed
*/
public function add($tableName, $arrayParams, $replace = false)
{ {
$link_id = $this->Connect()->DBLN; $link_id = $this->Connect()->DBLN;
$values = array_map( function($string) use ($link_id) { $values = array_map(function ($string) use ($link_id)
{
return X_DB_MySQLi::$LINK[$link_id]->real_escape_string($string); return X_DB_MySQLi::$LINK[$link_id]->real_escape_string($string);
}, array_values($arrayParams)); }, array_values($arrayParams));
$keys = array_keys($arrayParams); $keys = array_keys($arrayParams);
if($replace) $type = "REPLACE"; if ($replace)
else $type = "INSERT"; {
return $this->insert($type.' INTO `'.$tableName.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $values).'\')'); $type = "REPLACE";
}
else
{
$type = "INSERT";
} }
return $this->insert($type . ' INTO `' . $tableName . '` (`' . implode('`,`', $keys) . '`) VALUES (\'' . implode('\',\'', $values) . '\')');
}
/**
* @param $tableName
* @param $arrayParams
* @param $whereParams
* @return mixed
*/
public function update($tableName, $arrayParams, $whereParams = null) public function update($tableName, $arrayParams, $whereParams = null)
{ {
if( !is_array($arrayParams) || count($arrayParams) == 0 ) return false; if ( ! is_array($arrayParams) || count($arrayParams) == 0)
{
return false;
}
$params = ""; $params = "";
foreach ($arrayParams as $key => $value) { foreach ($arrayParams as $key => $value)
$params .= "`".$key."` = '".$this->esc($value)."',"; {
$params .= "`" . $key . "` = '" . $this->esc($value) . "',";
} }
$params = trim($params, ","); $params = trim($params, ",");
$WHERE = ""; $WHERE = "";
if($whereParams != null) if ($whereParams != null)
{ {
if(is_array($whereParams) && count($whereParams)>0 && is_string($whereParams[0])) if (is_array($whereParams) && count($whereParams) > 0 && is_string($whereParams[0]))
{ {
$WHERE = "WHERE ".$this->construct_where_param($whereParams); $WHERE = "WHERE " . $this->construct_where_param($whereParams);
} }
else if(is_array($whereParams) && count($whereParams)>0 && is_array($whereParams[0]) && count($whereParams[0])>0 ) else if (is_array($whereParams) && count($whereParams) > 0 && is_array($whereParams[0]) && count($whereParams[0]) > 0)
{ {
foreach ($whereParams as $value) foreach ($whereParams as $value)
{ {
if(strlen($WHERE) == 0 ) $WHERE = "WHERE "; else $WHERE .= " AND "; if (strlen($WHERE) == 0)
{
$WHERE = "WHERE ";
}
else
{
$WHERE .= " AND ";
}
$WHERE .= $this->construct_where_param($value); $WHERE .= $this->construct_where_param($value);
} }
} }
} }
return $this->rq('UPDATE `'.$tableName.'` SET '.$params.' '.$WHERE); return $this->rq('UPDATE `' . $tableName . '` SET ' . $params . ' ' . $WHERE);
} }
/**
* @param $whereParams
*/
private function construct_where_param($whereParams) private function construct_where_param($whereParams)
{ {
if(count($whereParams) == 2 ) if (count($whereParams) == 2)
return "`".$whereParams[0]."`='".$this->esc($whereParams[1])."'"; {
if(count($whereParams) == 3 ) return "`" . $whereParams[0] . "`='" . $this->esc($whereParams[1]) . "'";
return "`".$whereParams[0]."`".$whereParams[1]."'".$this->esc($whereParams[2])."'";
} }
if (count($whereParams) == 3)
{
return "`" . $whereParams[0] . "`" . $whereParams[1] . "'" . $this->esc($whereParams[2]) . "'";
}
}
/**
* @param $SQL
*/
public function insert($SQL) public function insert($SQL)
{ {
if( X_DB_MySQLi::$LINK[$this->Connect()->DBLN]->real_query($SQL) ) return X_DB_MySQLi::$LINK[$this->DBLN]->insert_id; if (self::$LINK[$this->Connect()->DBLN]->real_query($SQL))
else throw new Exception(X_DB_MySQLi::$LINK[$this->DBLN]->error, X_DB_MySQLi::$LINK[$this->DBLN]->errno); {
return self::$LINK[$this->DBLN]->insert_id;
}
else
{
throw new Exception(self::$LINK[$this->DBLN]->error, self::$LINK[$this->DBLN]->errno);
}
}
/**
* @param $value
*/
public function esc($value)
{
return self::$LINK[$this->Connect()->DBLN]->real_escape_string($value);
} }
public function esc( $value ) /**
* Real Query
* @param $SQL
*/
public function rq($SQL)
{ {
return X_DB_MySQLi::$LINK[$this->Connect()->DBLN]->real_escape_string( $value ); if (self::$LINK[$this->Connect()->DBLN]->real_query($SQL))
{
return true;
}
else
{
throw new Exception(self::$LINK[$this->DBLN]->error, self::$LINK[$this->DBLN]->errno);
}
} }
public function rq( $SQL ) /**
* Simple Query (Sample: For "Call Procedure()")
* @param $SQL
*/
public function sq($SQL)
{
if (self::$LINK[$this->Connect()->DBLN]->query($SQL))
{ {
if( X_DB_MySQLi::$LINK[$this->Connect()->DBLN]->real_query($SQL) ) return true; return true;
else throw new Exception(X_DB_MySQLi::$LINK[$this->DBLN]->error, X_DB_MySQLi::$LINK[$this->DBLN]->errno); }
else
{
throw new Exception(self::$LINK[$this->DBLN]->error, self::$LINK[$this->DBLN]->errno);
}
} }
public function get($SQL,$ID_COL=false,$ID_COL2=false,$ID_COL3=false) /**
* Analod SQL SELECT
* @param $SQL
* @param $ID_COL
* @param false $ID_COL2
* @param false $ID_COL3
* @return Array or false
*/
public function get($SQL, $ID_COL = false, $ID_COL2 = false, $ID_COL3 = false)
{
if ($this->rq($SQL))
{ {
if($this->rq($SQL)) $result = self::$LINK[$this->DBLN]->store_result();
if ($result)
{ {
$result = X_DB_MySQLi::$LINK[$this->DBLN]->store_result(); if ($result->num_rows > 0)
if($result)
{ {
if( $result->num_rows > 0 ) $DATA = [];
while ($row = $result->fetch_assoc())
{ {
$DATA = array(); if (isset($row[$ID_COL]) && $ID_COL)
while ( $row = $result->fetch_assoc() )
{ {
if(isset($row[$ID_COL]) && $ID_COL) if (isset($row[$ID_COL2]) && $ID_COL2)
{ {
if(isset($row[$ID_COL2]) && $ID_COL2) if (isset($row[$ID_COL3]) && $ID_COL3)
{ {
if(isset($row[$ID_COL3]) && $ID_COL3) $DATA[$row[$ID_COL]][$row[$ID_COL2]][$row[$ID_COL3]] = $row; $DATA[$row[$ID_COL]][$row[$ID_COL2]][$row[$ID_COL3]] = $row;
else $DATA[$row[$ID_COL]][$row[$ID_COL2]] = $row;
} }
else $DATA[$row[$ID_COL]] = $row; else
{
$DATA[$row[$ID_COL]][$row[$ID_COL2]] = $row;
}
}
else
{
$DATA[$row[$ID_COL]] = $row;
}
}
else
{
$DATA[] = $row;
} }
else $DATA[] = $row;
} }
$result->close(); $result->close();
return $DATA; return $DATA;
} else { }
else
{
$result->close(); $result->close();
return array(); return [];
} }
} else { }
else
{
//throw new Exception(X_DB_MySQLi::$LINK[$this->DBLN]->error, X_DB_MySQLi::$LINK[$this->DBLN]->errno); //throw new Exception(X_DB_MySQLi::$LINK[$this->DBLN]->error, X_DB_MySQLi::$LINK[$this->DBLN]->errno);
return false; return false;
} }
} }
else return false; else
{
return false;
}
} }
/**
* @param $table
* @param $column
* @param $data
* @return mixed
*/
public function delete_in($table, $column, $data) public function delete_in($table, $column, $data)
{ {
if(!(count($data)>0)) return false; if ( ! (count($data) > 0))
{
return false;
}
$SQL = "DELETE FROM"; $SQL = "DELETE FROM";
$SQL .= " `".$table."` "; $SQL .= " `" . $table . "` ";
$SQL .= "WHERE"; $SQL .= "WHERE";
$SQL .= " `".$column."` in('"; $SQL .= " `" . $column . "` in('";
$SQL .= implode("','", $data); $SQL .= implode("','", $data);
$SQL .= "')"; $SQL .= "')";
return $this->rq($SQL); return $this->rq($SQL);
} }
function __destruct() public function __destruct()
{ {
if ( isset( X_DB_MySQLi::$LINK[$this->DBLN] ) if (isset(self::$LINK[$this->DBLN])
&& X_DB_MySQLi::$LINK[$this->DBLN] instanceof mysqli && self::$LINK[$this->DBLN] instanceof mysqli
&& X_DB_MySQLi::$LINK[$this->DBLN]->ping() && self::$LINK[$this->DBLN]->ping()
) )
{ {
X_DB_MySQLi::$LINK[$this->DBLN]->close(); self::$LINK[$this->DBLN]->close();
unset(X_DB_MySQLi::$LINK[$this->DBLN]); unset(self::$LINK[$this->DBLN]);
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment