Commit 74a2bc49 authored by Alex Ne's avatar Alex Ne

FIX function delete_in

parent e9800dba
......@@ -2,8 +2,7 @@
/*
НЕ ТРОЖ!!! :)
*/
class X_DB_MySQLi
{
class X_DB_MySQLi {
/**
* @var array
*/
......@@ -46,88 +45,65 @@ class X_DB_MySQLi
// $DBHOST, $DBNAME, $DBUSER, $DBPASS, $DBCHARSET
// OR:
// $DBC - name of configured database
public function __construct()
{
public function __construct() {
$cfg = func_get_args();
if ( ! (count($cfg) > 0))
{
if ( ! (count($cfg) > 0)) {
throw new Exception("DB_MySQLi_NULL_CONSTRUCT_ARGUMENT", X_Exception_List::DB_MySQLi_NULL_CONSTRUCT_ARGUMENT);
}
if (is_array($cfg[0]))
{
if (isset($cfg[0]["DBHOST"]))
{
if (is_array($cfg[0])) {
if (isset($cfg[0]["DBHOST"])) {
$this->DBHOST = $cfg[0]["DBHOST"];
}
if (isset($cfg[0]["DBNAME"]))
{
if (isset($cfg[0]["DBNAME"])) {
$this->DBNAME = $cfg[0]["DBNAME"];
}
if (isset($cfg[0]["DBUSER"]))
{
if (isset($cfg[0]["DBUSER"])) {
$this->DBUSER = $cfg[0]["DBUSER"];
}
if (isset($cfg[0]["DBPASS"]))
{
if (isset($cfg[0]["DBPASS"])) {
$this->DBPASS = $cfg[0]["DBPASS"];
}
if (isset($cfg[0]["DBCHARSET"]))
{
if (isset($cfg[0]["DBCHARSET"])) {
$this->DBCHARSET = $cfg[0]["DBCHARSET"];
}
}
else if (count($cfg) == 1)
{
if (isset(self::$DBC_LIST[$cfg[0]]["DBHOST"]))
{
} 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"]))
{
if (isset(self::$DBC_LIST[$cfg[0]]["DBNAME"])) {
$this->DBNAME = self::$DBC_LIST[$cfg[0]]["DBNAME"];
}
if (isset(self::$DBC_LIST[$cfg[0]]["DBUSER"]))
{
if (isset(self::$DBC_LIST[$cfg[0]]["DBUSER"])) {
$this->DBUSER = self::$DBC_LIST[$cfg[0]]["DBUSER"];
}
if (isset(self::$DBC_LIST[$cfg[0]]["DBPASS"]))
{
if (isset(self::$DBC_LIST[$cfg[0]]["DBPASS"])) {
$this->DBPASS = self::$DBC_LIST[$cfg[0]]["DBPASS"];
}
if (isset(self::$DBC_LIST[$cfg[0]]["DBCHARSET"]))
{
if (isset(self::$DBC_LIST[$cfg[0]]["DBCHARSET"])) {
$this->DBCHARSET = self::$DBC_LIST[$cfg[0]]["DBCHARSET"];
}
}
else if (count($cfg) == 3)
{
} else if (count($cfg) == 3) {
$this->DBNAME = $cfg[0];
$this->DBUSER = $cfg[1];
$this->DBPASS = $cfg[2];}
else if (count($cfg) == 4)
{
$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->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
{
$this->DBCHARSET = $cfg[4];} else {
throw new Exception("DB_MySQLi_WRONG_CONSTRUCT_ARGUMENT", X_Exception_List::DB_MySQLi_WRONG_CONSTRUCT_ARGUMENT);
}
......@@ -142,51 +118,38 @@ class X_DB_MySQLi
// $DBC, $DBHOST, $DBNAME, $DBUSER, $DBPASS | Defauts: $DBCHARSET = "utf8";
// OR:
// $DBC, $DBHOST, $DBNAME, $DBUSER, $DBPASS, $DBCHARSET
public static function SetConfig()
{
public static function SetConfig() {
$cfg = func_get_args();
if (isset($cfg[1]) && is_array($cfg[1]))
{
if (isset($cfg[1]["DBHOST"]))
{
if (isset($cfg[1]) && is_array($cfg[1])) {
if (isset($cfg[1]["DBHOST"])) {
self::$DBC_LIST[$cfg[0]]["DBHOST"] = $cfg[1]["DBHOST"];
}
if (isset($cfg[1]["DBNAME"]))
{
if (isset($cfg[1]["DBNAME"])) {
self::$DBC_LIST[$cfg[0]]["DBNAME"] = $cfg[1]["DBNAME"];
}
if (isset($cfg[1]["DBUSER"]))
{
if (isset($cfg[1]["DBUSER"])) {
self::$DBC_LIST[$cfg[0]]["DBUSER"] = $cfg[1]["DBUSER"];
}
if (isset($cfg[1]["DBPASS"]))
{
if (isset($cfg[1]["DBPASS"])) {
self::$DBC_LIST[$cfg[0]]["DBPASS"] = $cfg[1]["DBPASS"];
}
if (isset($cfg[1]["DBCHARSET"]))
{
if (isset($cfg[1]["DBCHARSET"])) {
self::$DBC_LIST[$cfg[0]]["DBCHARSET"] = $cfg[1]["DBCHARSET"];
}
}
else if (count($cfg) == 4)
{
} 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)
{
} 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)
{
} 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];
......@@ -197,16 +160,13 @@ class X_DB_MySQLi
/**
* @return mixed
*/
public function Connect()
{
public function Connect() {
if ( ! isset(self::$LINK[$this->DBLN])
|| ! (self::$LINK[$this->DBLN] instanceof mysqli)
|| ! self::$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)
{
if (self::$LINK[$this->DBLN]->connect_errno) {
throw new Exception(self::$LINK[$this->DBLN]->connect_error, self::$LINK[$this->DBLN]->connect_errno);
}
}
......@@ -219,20 +179,15 @@ class X_DB_MySQLi
* @param $replace
* @return mixed
*/
public function add($tableName, $arrayParams, $replace = false)
{
public function add($tableName, $arrayParams, $replace = false) {
$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);
}, array_values($arrayParams));
$keys = array_keys($arrayParams);
if ($replace)
{
if ($replace) {
$type = "REPLACE";
}
else
{
} else {
$type = "INSERT";
}
......@@ -245,36 +200,25 @@ class X_DB_MySQLi
* @param $whereParams
* @return mixed
*/
public function update($tableName, $arrayParams, $whereParams = null)
{
if ( ! is_array($arrayParams) || count($arrayParams) == 0)
{
public function update($tableName, $arrayParams, $whereParams = null) {
if ( ! is_array($arrayParams) || count($arrayParams) == 0) {
return false;
}
$params = "";
foreach ($arrayParams as $key => $value)
{
foreach ($arrayParams as $key => $value) {
$params .= "`" . $key . "` = '" . $this->esc($value) . "',";
}
$params = trim($params, ",");
$WHERE = "";
if ($whereParams != null)
{
if (is_array($whereParams) && count($whereParams) > 0 && is_string($whereParams[0]))
{
if ($whereParams != null) {
if (is_array($whereParams) && count($whereParams) > 0 && is_string($whereParams[0])) {
$WHERE = "WHERE " . $this->construct_where_param($whereParams);
}
else if (is_array($whereParams) && count($whereParams) > 0 && is_array($whereParams[0]) && count($whereParams[0]) > 0)
{
foreach ($whereParams as $value)
{
if (strlen($WHERE) == 0)
{
} else if (is_array($whereParams) && count($whereParams) > 0 && is_array($whereParams[0]) && count($whereParams[0]) > 0) {
foreach ($whereParams as $value) {
if (strlen($WHERE) == 0) {
$WHERE = "WHERE ";
}
else
{
} else {
$WHERE .= " AND ";
}
......@@ -288,15 +232,12 @@ class X_DB_MySQLi
/**
* @param $whereParams
*/
private function construct_where_param($whereParams)
{
if (count($whereParams) == 2)
{
private function construct_where_param($whereParams) {
if (count($whereParams) == 2) {
return "`" . $whereParams[0] . "`='" . $this->esc($whereParams[1]) . "'";
}
if (count($whereParams) == 3)
{
if (count($whereParams) == 3) {
return "`" . $whereParams[0] . "`" . $whereParams[1] . "'" . $this->esc($whereParams[2]) . "'";
}
}
......@@ -304,14 +245,10 @@ class X_DB_MySQLi
/**
* @param $SQL
*/
public function insert($SQL)
{
if (self::$LINK[$this->Connect()->DBLN]->real_query($SQL))
{
public function insert($SQL) {
if (self::$LINK[$this->Connect()->DBLN]->real_query($SQL)) {
return self::$LINK[$this->DBLN]->insert_id;
}
else
{
} else {
throw new Exception(self::$LINK[$this->DBLN]->error, self::$LINK[$this->DBLN]->errno);
}
}
......@@ -319,8 +256,7 @@ class X_DB_MySQLi
/**
* @param $value
*/
public function esc($value)
{
public function esc($value) {
return self::$LINK[$this->Connect()->DBLN]->real_escape_string($value);
}
......@@ -328,14 +264,10 @@ class X_DB_MySQLi
* Real Query
* @param $SQL
*/
public function rq($SQL)
{
if (self::$LINK[$this->Connect()->DBLN]->real_query($SQL))
{
public function rq($SQL) {
if (self::$LINK[$this->Connect()->DBLN]->real_query($SQL)) {
return true;
}
else
{
} else {
throw new Exception(self::$LINK[$this->DBLN]->error, self::$LINK[$this->DBLN]->errno);
}
}
......@@ -344,14 +276,10 @@ class X_DB_MySQLi
* Simple Query (Sample: For "Call Procedure()")
* @param $SQL
*/
public function sq($SQL)
{
if (self::$LINK[$this->Connect()->DBLN]->query($SQL))
{
public function sq($SQL) {
if (self::$LINK[$this->Connect()->DBLN]->query($SQL)) {
return true;
}
else
{
} else {
throw new Exception(self::$LINK[$this->DBLN]->error, self::$LINK[$this->DBLN]->errno);
}
}
......@@ -364,58 +292,38 @@ class X_DB_MySQLi
* @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))
{
public function get($SQL, $ID_COL = false, $ID_COL2 = false, $ID_COL3 = false) {
if ($this->rq($SQL)) {
$result = self::$LINK[$this->DBLN]->store_result();
if ($result)
{
if ($result->num_rows > 0)
{
if ($result) {
if ($result->num_rows > 0) {
$DATA = [];
while ($row = $result->fetch_assoc())
{
if (isset($row[$ID_COL]) && $ID_COL)
{
if (isset($row[$ID_COL2]) && $ID_COL2)
{
if (isset($row[$ID_COL3]) && $ID_COL3)
{
while ($row = $result->fetch_assoc()) {
if (isset($row[$ID_COL]) && $ID_COL) {
if (isset($row[$ID_COL2]) && $ID_COL2) {
if (isset($row[$ID_COL3]) && $ID_COL3) {
$DATA[$row[$ID_COL]][$row[$ID_COL2]][$row[$ID_COL3]] = $row;
}
else
{
} else {
$DATA[$row[$ID_COL]][$row[$ID_COL2]] = $row;
}
}
else
{
} else {
$DATA[$row[$ID_COL]] = $row;
}
}
else
{
} else {
$DATA[] = $row;
}
}
$result->close();
return $DATA;
}
else
{
} else {
$result->close();
return [];
}
}
else
{
} else {
//throw new Exception(X_DB_MySQLi::$LINK[$this->DBLN]->error, X_DB_MySQLi::$LINK[$this->DBLN]->errno);
return false;
}
}
else
{
} else {
return false;
}
}
......@@ -426,10 +334,8 @@ class X_DB_MySQLi
* @param $data
* @return mixed
*/
public function delete_in($table, $column, $data)
{
if ( ! (count($data) > 0))
{
public function delete_in($table, $column, $data) {
if ( ! (count($data) > 0)) {
return false;
}
......@@ -437,18 +343,16 @@ class X_DB_MySQLi
$SQL .= " `" . $table . "` ";
$SQL .= "WHERE";
$SQL .= " `" . $column . "` in('";
$SQL .= implode("','", $data);
$SQL .= (count($data) > 1) ? implode("','", $data) : (count($data) == 1) ? array_shift($data) : "";
$SQL .= "')";
return $this->rq($SQL);
}
public function __destruct()
{
public function __destruct() {
if (isset(self::$LINK[$this->DBLN])
&& self::$LINK[$this->DBLN] instanceof mysqli
&& self::$LINK[$this->DBLN]->ping()
)
{
) {
self::$LINK[$this->DBLN]->close();
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