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

FIX function delete_in

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