20240406#1
This commit is contained in:
@@ -10,12 +10,12 @@ Stable version
|
|||||||
composer require rmphp/storage
|
composer require rmphp/storage
|
||||||
```
|
```
|
||||||
```bash
|
```bash
|
||||||
composer require rmphp/storage:"^2.0"
|
composer require rmphp/storage:"^3.0"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Dev version contains the latest changes
|
Dev version contains the latest changes
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
composer require rmphp/storage:"2.x-dev"
|
composer require rmphp/storage:"3.x-dev"
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -38,82 +38,87 @@ class MysqlStorage implements MysqlStorageInterface {
|
|||||||
{
|
{
|
||||||
try{
|
try{
|
||||||
$result = $this->mysqli->query($sql);
|
$result = $this->mysqli->query($sql);
|
||||||
// запись в log
|
if($this->mysqli->errno) throw new Exception();
|
||||||
($this->mysqli->errno)
|
$this->addLog("OK - ".$sql);
|
||||||
? $this->addLog("Err - SQL: ".$sql." | error: ".$this->mysqli->error)
|
|
||||||
: $this->addLog("OK - ".$sql);
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
/* 8.1.0 Теперь по умолчанию установлено значение MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT и выбрасывается исключение. Ранее оно было MYSQLI_REPORT_OFF. */
|
|
||||||
catch (Exception $exception){
|
catch (Exception $exception){
|
||||||
$this->addLog("Err - SQL: ".$sql." | error: ".$this->mysqli->error);
|
$this->addLog("Err - SQL: ".$sql." | error: ".$this->mysqli->error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function add(string $tbl, array $arr, bool $update = false) : bool {
|
|
||||||
foreach ($arr as $key => $value) {
|
|
||||||
$col[] = "`$key`";
|
|
||||||
$val[] = ($value !== NULL) ? "'$value'" : "NULL";
|
|
||||||
$upd[] = ($value !== NULL) ? "`$key`='$value'" : "`$key`=NULL";
|
|
||||||
}
|
|
||||||
// Собираем в строки для использования в запросе
|
|
||||||
$col = implode(", ", $col);
|
|
||||||
$val = implode(", ", $val);
|
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function insert(string $tbl, array $data, bool $update = false) : bool {
|
||||||
|
$chunks = $this->getInsertValue($data);
|
||||||
|
$upd = $this->getUpdateValue($data);
|
||||||
if (!$update) {
|
if (!$update) {
|
||||||
$sql = "insert low_priority into ".$this->escapeStr($tbl)." (".$col.") values (" . $val . ")";
|
$sql = "insert low_priority into ".$this->escapeStr($tbl)." (".implode(",", $chunks['columns']).") values (".implode(",", $chunks['values']).")";
|
||||||
} else{
|
} else{
|
||||||
$sql = "insert low_priority into ".$this->escapeStr($tbl)." (".$col.") values (".$val.") on duplicate key update ".implode(", ", $upd);
|
$sql = "insert low_priority into ".$this->escapeStr($tbl)." (".implode(",", $chunks['columns']).") values (".implode(",", $chunks['values']).") on duplicate key update ".implode(",", $upd);
|
||||||
}
|
}
|
||||||
return $this->query($sql);
|
return $this->query($sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritDoc */
|
/**
|
||||||
public function edit(string $tbl, array $arr, string $case, bool $ignore=false) : bool {
|
* @param string $tbl
|
||||||
foreach ($arr as $key => $value) {
|
* @param array $data
|
||||||
$isql[] = ($value !== NULL) ? "`$key`='$value'" : "`$key`=NULL";
|
* @return bool
|
||||||
}
|
*/
|
||||||
$where = (preg_match("'^[0-9]+$'",$case)) ? "id = '".(int) $case."'" : $case;
|
public function batchInsert(string $tbl, array $data) : bool {
|
||||||
if(empty($ignore)) {
|
foreach($data as $insertRow){
|
||||||
$sql = "update low_priority " . $this->escapeStr($tbl) . " set " . implode(", ", $isql) . " where " . $where;
|
$chunks = $this->getInsertValue($insertRow);
|
||||||
} else {
|
$val[] = "(".implode(",", $chunks['values']).")";
|
||||||
$sql = "update low_priority ignore " . $this->escapeStr($tbl) . " set " . implode(", ", $isql) . " where " . $where;
|
|
||||||
}
|
}
|
||||||
|
$sql = "insert low_priority into ".$this->escapeStr($tbl)." (".implode(",", $chunks['columns'] ?? []).") values ".implode(",", $val ?? []);
|
||||||
return $this->query($sql);
|
return $this->query($sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function replace(string $tbl, array $arr) : bool {
|
public function updateById(string $tbl, array $data, int $id, array $modifier = []) : bool {
|
||||||
foreach ($arr as $key => $value) {
|
$chunks = $this->getUpdateValue($data);
|
||||||
$col[] = "`$key`";
|
$sql = "update low_priority ".implode(" ", $modifier)." ".$this->escapeStr($tbl)." set ".implode(",", $chunks)." where id = '".$id."'";
|
||||||
$val[] = ($value !== NULL) ? "'$value'" : "NULL";
|
|
||||||
}
|
|
||||||
// Собираем в строки для использования в запросе
|
|
||||||
$col = implode(", ", $col);
|
|
||||||
$val = implode(", ", $val);
|
|
||||||
|
|
||||||
$sql = "replace low_priority into ".$this->escapeStr($tbl)." (".$col.") values (".$val.")";
|
|
||||||
return $this->query($sql);
|
return $this->query($sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function del(string $tbl, string $case) : bool {
|
public function updateByParam(string $tbl, array $data, string $case, array $modifier = []) : bool {
|
||||||
$where = (preg_match("'^[0-9]+$'",$case)) ? "id = '".(int) $case."'" : $case;
|
$chunks = $this->getUpdateValue($data);
|
||||||
$sql = "delete low_priority from ".$this->escapeStr($tbl)." where ".$where;
|
$sql = "update low_priority ".implode(" ", $modifier)." ".$this->escapeStr($tbl)." set ". implode(",", $chunks)." where ".$case;
|
||||||
|
return $this->query($sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function replace(string $tbl, array $data) : bool {
|
||||||
|
$chunks = $this->getInsertValue($data);
|
||||||
|
$sql = "replace low_priority into ".$this->escapeStr($tbl)." (".implode(",", $chunks['columns']).") values (".implode(",", $chunks['values']).")";
|
||||||
|
return $this->query($sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function deleteById(string $tbl, int $id) : bool {
|
||||||
|
$sql = "delete low_priority from ".$this->escapeStr($tbl)." where id='.$id.'";
|
||||||
// возвращаем число затронутых строк/false
|
// возвращаем число затронутых строк/false
|
||||||
return $this->query($sql);
|
return $this->query($sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function read(string $sql, int $ln=0, int $numPage=1, int $count=0): bool|MysqlStorageData {
|
public function deleteByParam(string $tbl, string $case) : bool {
|
||||||
|
$sql = "delete low_priority from ".$this->escapeStr($tbl)." where ".$case;
|
||||||
|
// возвращаем число затронутых строк/false
|
||||||
|
return $this->query($sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function find(string $sql, int $ln=0, int $numPage=1, int $count=0): bool|MysqlStorageData {
|
||||||
|
|
||||||
if ($ln > 1) {
|
if ($ln > 1) {
|
||||||
$cnts = (!empty($count)) ? $count : $this->query($sql)->num_rows;
|
$cnts = (!empty($count)) ? $count : $this->query($sql)->num_rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
// часть строки запроса с лимит
|
|
||||||
switch (true){
|
switch (true){
|
||||||
case ($ln > 1 || $numPage > 1) : $limit = " limit ".(($numPage * $ln) - $ln).", ".$ln; break;
|
case ($ln > 1 || $numPage > 1) : $limit = " limit ".(($numPage * $ln) - $ln).", ".$ln; break;
|
||||||
case ($ln == 1): $limit = " limit 0, 1"; break;
|
case ($ln == 1): $limit = " limit 0, 1"; break;
|
||||||
@@ -129,18 +134,14 @@ class MysqlStorage implements MysqlStorageInterface {
|
|||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function chktbl(string $tbl) : bool {
|
public function findOne(string $sql) : array {
|
||||||
$result = $this->mysqli->query("SHOW TABLES LIKE '".$this->escapeStr($tbl)."'");
|
$result = $this->query($sql);
|
||||||
if ($result->num_rows == 0) {
|
$data = new MysqlStorageData($result);
|
||||||
$this->addLog(__METHOD__.":"." Err - Table ".$tbl." doesn't exist"); return false;
|
return $data->fetchOne();
|
||||||
}
|
|
||||||
$this->addLog(__METHOD__.":"." OK - Table ".$tbl." exist");
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function escapeReg(string $var) : ?string {
|
public function escapeReg(string $var) : ?string {
|
||||||
if(!isset($var)) return null;
|
if(!isset($var)) return null;
|
||||||
@@ -153,6 +154,7 @@ class MysqlStorage implements MysqlStorageInterface {
|
|||||||
return trim($this->mysqli->real_escape_string($var));
|
return trim($this->mysqli->real_escape_string($var));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function addLog(string $log) : void {
|
public function addLog(string $log) : void {
|
||||||
if($this->logsEnabled) $this->log[] = $log;
|
if($this->logsEnabled) $this->log[] = $log;
|
||||||
@@ -168,4 +170,31 @@ class MysqlStorage implements MysqlStorageInterface {
|
|||||||
return $this->log[count($this->log)-1];
|
return $this->log[count($this->log)-1];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $array
|
||||||
|
* @return array[]
|
||||||
|
*/
|
||||||
|
private function getInsertValue(array $array) : array {
|
||||||
|
foreach ($array as $key => $value) {
|
||||||
|
$col[] = "`$key`";
|
||||||
|
$val[] = ($value !== NULL) ? "'$value'" : "NULL";
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
"columns" => $col ?? [],
|
||||||
|
"values" => $val ?? []
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $array
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getUpdateValue(array $array) : array {
|
||||||
|
foreach ($array as $key => $value) {
|
||||||
|
$out[] = ($value !== NULL) ? "`$key`='$value'" : "`$key`=NULL";
|
||||||
|
}
|
||||||
|
return $out ?? [];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -35,14 +35,14 @@ class MysqlStorageData {
|
|||||||
/**
|
/**
|
||||||
* @return iterable
|
* @return iterable
|
||||||
*/
|
*/
|
||||||
public function fatch(): iterable {
|
public function fetch(): iterable {
|
||||||
if(!empty($this->arrayData)) return $this->arrayData;
|
if(!empty($this->arrayData)) return $this->arrayData;
|
||||||
if(!$this->result) return [];
|
if(!$this->result) return [];
|
||||||
return $this->generator();
|
return $this->generator();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function fatchOne(int $index = 0) : array {
|
public function fetchOne(int $index = 0) : array {
|
||||||
if(!$this->result) return [];
|
if(!$this->result) return [];
|
||||||
$this->result->data_seek($index);
|
$this->result->data_seek($index);
|
||||||
return $this->result->fetch_assoc();
|
return $this->result->fetch_assoc();
|
||||||
|
|||||||
@@ -19,36 +19,59 @@ interface MysqlStorageInterface {
|
|||||||
/**
|
/**
|
||||||
* Метод добавления записи в текущую БД
|
* Метод добавления записи в текущую БД
|
||||||
* @param string $tbl
|
* @param string $tbl
|
||||||
* @param array $arr
|
* @param array $data
|
||||||
|
* @param array $modifier
|
||||||
* @param bool $update
|
* @param bool $update
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function add(string $tbl, array $arr, bool $update = false) : bool;
|
public function insert(string $tbl, array $data, bool $update = false) : bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $tbl
|
||||||
|
* @param array $data
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function batchInsert(string $tbl, array $data) : bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Метод редактирования записи в текущей БД по ID
|
* Метод редактирования записи в текущей БД по ID
|
||||||
* @param string $tbl
|
* @param string $tbl
|
||||||
* @param array $arr
|
* @param array $data
|
||||||
* @param string $case
|
* @param int $id
|
||||||
* @param bool $ignore
|
* @param array $modifier
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function edit(string $tbl, array $arr, string $case, bool $ignore=false) : bool;
|
public function updateById(string $tbl, array $data, int $id, array $modifier = []) : bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Метод добавления записи в текущую БД
|
|
||||||
* @param string $tbl
|
* @param string $tbl
|
||||||
* @param array $arr
|
* @param array $data
|
||||||
|
* @param string $case
|
||||||
|
* @param array $modifier
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function replace(string $tbl, array $arr) : bool;
|
public function updateByParam(string $tbl, array $data, string $case, array $modifier = []) : bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $tbl
|
||||||
|
* @param array $data
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function replace(string $tbl, array $data) : bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $tbl
|
||||||
|
* @param int $id
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function deleteById(string $tbl, int $id) : bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $tbl
|
* @param string $tbl
|
||||||
* @param string $case
|
* @param string $case
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function del(string $tbl, string $case) : bool;
|
public function deleteByParam(string $tbl, string $case) : bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
@@ -57,13 +80,13 @@ interface MysqlStorageInterface {
|
|||||||
* @param int $count
|
* @param int $count
|
||||||
* @return bool|MysqlStorageData
|
* @return bool|MysqlStorageData
|
||||||
*/
|
*/
|
||||||
public function read(string $sql, int $ln = 0, int $numPage = 1, int $count=0) : bool|MysqlStorageData;
|
public function find(string $sql, int $ln = 0, int $numPage = 1, int $count=0) : bool|MysqlStorageData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $tbl
|
* @param string $sql
|
||||||
* @return bool
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function chktbl(string $tbl) : bool;
|
public function findOne(string $sql) : array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Метод экранирования данных с учетом текущего подключения в т.ч для LIKE
|
* Метод экранирования данных с учетом текущего подключения в т.ч для LIKE
|
||||||
|
|||||||
Reference in New Issue
Block a user