20240407#1

This commit is contained in:
User
2024-04-07 21:13:28 +03:00
parent 6caceefc17
commit 120d795b85
2 changed files with 63 additions and 52 deletions

View File

@@ -50,63 +50,59 @@ class MysqlStorage implements MysqlStorageInterface {
/** @inheritDoc */ /** @inheritDoc */
public function insert(string $tbl, array $data, bool $update = false) : bool { public function insert(string $table, array $data, bool $update = false) : bool {
$chunks = $this->getInsertValue($data); $chunks = $this->getInsertValue($data);
$upd = $this->getUpdateValue($data); $upd = $this->getUpdateValue($data);
if (!$update) { if (!$update) {
$sql = "insert low_priority into ".$this->escapeStr($tbl)." (".implode(",", $chunks['columns']).") values (".implode(",", $chunks['values']).")"; $sql = "insert low_priority into ".$this->escapeStr($table)." (".implode(",", $chunks['columns']).") values (".implode(",", $chunks['values']).")";
} else{ } else{
$sql = "insert low_priority into ".$this->escapeStr($tbl)." (".implode(",", $chunks['columns']).") values (".implode(",", $chunks['values']).") on duplicate key update ".implode(",", $upd); $sql = "insert low_priority into ".$this->escapeStr($table)." (".implode(",", $chunks['columns']).") values (".implode(",", $chunks['values']).") on duplicate key update ".implode(",", $upd);
} }
return $this->query($sql); return $this->query($sql);
} }
/** /** @inheritDoc */
* @param string $tbl public function batchInsert(string $table, array $data) : bool {
* @param array $data
* @return bool
*/
public function batchInsert(string $tbl, array $data) : bool {
foreach($data as $insertRow){ foreach($data as $insertRow){
$chunks = $this->getInsertValue($insertRow); $chunks = $this->getInsertValue($insertRow);
$val[] = "(".implode(",", $chunks['values']).")"; $values[] = "(".implode(",", $chunks['values']).")";
} }
$sql = "insert low_priority into ".$this->escapeStr($tbl)." (".implode(",", $chunks['columns'] ?? []).") values ".implode(",", $val ?? []); $sql = "insert low_priority into ".$this->escapeStr($table)." (".implode(",", $chunks['columns'] ?? []).") values ".implode(",", $values ?? []);
return $this->query($sql); return $this->query($sql);
} }
/** @inheritDoc */ /** @inheritDoc */
public function updateById(string $tbl, array $data, int $id, array $modifier = []) : bool { public function updateById(string $table, array $data, int $id, array $modifier = []) : bool {
$chunks = $this->getUpdateValue($data); $chunks = $this->getUpdateValue($data);
$sql = "update low_priority ".implode(" ", $modifier)." ".$this->escapeStr($tbl)." set ".implode(",", $chunks)." where id = '".$id."'"; $sql = "update low_priority ".implode(" ", $modifier)." ".$this->escapeStr($table)." set ".implode(",", $chunks)." where id = '".$id."'";
return $this->query($sql); return $this->query($sql);
} }
/** @inheritDoc */ /** @inheritDoc */
public function updateByParam(string $tbl, array $data, string $case, array $modifier = []) : bool { public function updateByParam(string $table, array $data, string $case, array $modifier = []) : bool {
$chunks = $this->getUpdateValue($data); $chunks = $this->getUpdateValue($data);
$sql = "update low_priority ".implode(" ", $modifier)." ".$this->escapeStr($tbl)." set ". implode(",", $chunks)." where ".$case; $sql = "update low_priority ".implode(" ", $modifier)." ".$this->escapeStr($table)." set ". implode(",", $chunks)." where ".$case;
return $this->query($sql); return $this->query($sql);
} }
/** @inheritDoc */ /** @inheritDoc */
public function replace(string $tbl, array $data) : bool { public function replace(string $table, array $data) : bool {
$chunks = $this->getInsertValue($data); $chunks = $this->getInsertValue($data);
$sql = "replace low_priority into ".$this->escapeStr($tbl)." (".implode(",", $chunks['columns']).") values (".implode(",", $chunks['values']).")"; $sql = "replace low_priority into ".$this->escapeStr($table)." (".implode(",", $chunks['columns']).") values (".implode(",", $chunks['values']).")";
return $this->query($sql); return $this->query($sql);
} }
/** @inheritDoc */ /** @inheritDoc */
public function deleteById(string $tbl, int $id) : bool { public function deleteById(string $table, int $id) : bool {
$sql = "delete low_priority from ".$this->escapeStr($tbl)." where id='.$id.'"; $sql = "delete low_priority from ".$this->escapeStr($table)." where id='.$id.'";
// возвращаем число затронутых строк/false // возвращаем число затронутых строк/false
return $this->query($sql); return $this->query($sql);
} }
/** @inheritDoc */ /** @inheritDoc */
public function deleteByParam(string $tbl, string $case) : bool { public function deleteByParam(string $table, string $case) : bool {
$sql = "delete low_priority from ".$this->escapeStr($tbl)." where ".$case; $sql = "delete low_priority from ".$this->escapeStr($table)." where ".$case;
// возвращаем число затронутых строк/false // возвращаем число затронутых строк/false
return $this->query($sql); return $this->query($sql);
} }
@@ -136,7 +132,15 @@ class MysqlStorage implements MysqlStorageInterface {
/** @inheritDoc */ /** @inheritDoc */
public function findOne(string $sql) : bool|array { public function findOne(string $sql) : bool|array {
$result = $this->query($sql); $result = $this->query($sql." limit 0, 1");
if (!$result || $result->num_rows == 0) return false;
$data = new MysqlStorageData($result);
return $data->fetchOne();
}
/** @inheritDoc */
public function findById(string $table, int $id, string $name = 'id') : bool|array {
$result = $this->query("select * from ".$table." where `".$name."`=".$id." limit 0, 1");
if (!$result || $result->num_rows == 0) return false; if (!$result || $result->num_rows == 0) return false;
$data = new MysqlStorageData($result); $data = new MysqlStorageData($result);
return $data->fetchOne(); return $data->fetchOne();
@@ -144,15 +148,15 @@ class MysqlStorage implements MysqlStorageInterface {
/** @inheritDoc */ /** @inheritDoc */
public function escapeReg(string $var) : ?string { public function escapeReg(string $string) : ?string {
if(!isset($var)) return null; if(!isset($string)) return null;
return trim(addcslashes($this->mysqli->real_escape_string($var), "%_")); return trim(addcslashes($this->mysqli->real_escape_string($string), "%_"));
} }
/** @inheritDoc */ /** @inheritDoc */
public function escapeStr(?string $var) : ?string { public function escapeStr(?string $string) : ?string {
if(!isset($var)) return null; if(!isset($string)) return null;
return trim($this->mysqli->real_escape_string($var)); return trim($this->mysqli->real_escape_string($string));
} }
@@ -178,12 +182,12 @@ class MysqlStorage implements MysqlStorageInterface {
*/ */
private function getInsertValue(array $array) : array { private function getInsertValue(array $array) : array {
foreach ($array as $key => $value) { foreach ($array as $key => $value) {
$col[] = "`$key`"; $colunms[] = "`$key`";
$val[] = ($value !== NULL) ? "'$value'" : "NULL"; $values[] = ($value !== NULL) ? "'$value'" : "NULL";
} }
return [ return [
"columns" => $col ?? [], "columns" => $colunms ?? [],
"values" => $val ?? [] "values" => $values ?? []
]; ];
} }

View File

@@ -18,60 +18,59 @@ interface MysqlStorageInterface {
/** /**
* Метод добавления записи в текущую БД * Метод добавления записи в текущую БД
* @param string $tbl * @param string $table
* @param array $data * @param array $data
* @param array $modifier
* @param bool $update * @param bool $update
* @return bool * @return bool
*/ */
public function insert(string $tbl, array $data, bool $update = false) : bool; public function insert(string $table, array $data, bool $update = false) : bool;
/** /**
* @param string $tbl * @param string $table
* @param array $data * @param array $data
* @return bool * @return bool
*/ */
public function batchInsert(string $tbl, array $data) : bool; public function batchInsert(string $table, array $data) : bool;
/** /**
* Метод редактирования записи в текущей БД по ID * Метод редактирования записи в текущей БД по ID
* @param string $tbl * @param string $table
* @param array $data * @param array $data
* @param int $id * @param int $id
* @param array $modifier * @param array $modifier
* @return bool * @return bool
*/ */
public function updateById(string $tbl, array $data, int $id, array $modifier = []) : bool; public function updateById(string $table, array $data, int $id, array $modifier = []) : bool;
/** /**
* @param string $tbl * @param string $table
* @param array $data * @param array $data
* @param string $case * @param string $case
* @param array $modifier * @param array $modifier
* @return bool * @return bool
*/ */
public function updateByParam(string $tbl, array $data, string $case, array $modifier = []) : bool; public function updateByParam(string $table, array $data, string $case, array $modifier = []) : bool;
/** /**
* @param string $tbl * @param string $table
* @param array $data * @param array $data
* @return bool * @return bool
*/ */
public function replace(string $tbl, array $data) : bool; public function replace(string $table, array $data) : bool;
/** /**
* @param string $tbl * @param string $table
* @param int $id * @param int $id
* @return bool * @return bool
*/ */
public function deleteById(string $tbl, int $id) : bool; public function deleteById(string $table, int $id) : bool;
/** /**
* @param string $tbl * @param string $table
* @param string $case * @param string $case
* @return bool * @return bool
*/ */
public function deleteByParam(string $tbl, string $case) : bool; public function deleteByParam(string $table, string $case) : bool;
/** /**
* @param string $sql * @param string $sql
@@ -84,23 +83,31 @@ interface MysqlStorageInterface {
/** /**
* @param string $sql * @param string $sql
* @return array * @return bool|array
*/ */
public function findOne(string $sql) : bool|array; public function findOne(string $sql) : bool|array;
/**
* @param string $table
* @param int $id
* @param string $name
* @return bool|array
*/
public function findById(string $table, int $id, string $name = 'id') : bool|array;
/** /**
* Метод экранирования данных с учетом текущего подключения в т.ч для LIKE * Метод экранирования данных с учетом текущего подключения в т.ч для LIKE
* @param string $var * @param string $string
* @return string|null * @return string|null
*/ */
public function escapeReg(string $var) : ?string; public function escapeReg(string $string) : ?string;
/** /**
* Метод экранирования данных с учетом текущего подключения * Метод экранирования данных с учетом текущего подключения
* @param string|null $var * @param string|null $string
* @return string|null * @return string|null
*/ */
public function escapeStr(?string $var) : ?string; public function escapeStr(?string $string) : ?string;
/** /**
* Метод наполнения статичного массива с логами * Метод наполнения статичного массива с логами