20240427#1

This commit is contained in:
User
2024-04-27 03:02:16 +03:00
parent 120d795b85
commit 448128d228
2 changed files with 25 additions and 19 deletions

View File

@@ -39,11 +39,11 @@ class MysqlStorage implements MysqlStorageInterface {
try{
$result = $this->mysqli->query($sql);
if($this->mysqli->errno) throw new Exception();
$this->addLog("OK - ".$sql);
$this->addLog("OK - $sql");
return $result;
}
catch (Exception $exception){
$this->addLog("Err - SQL: ".$sql." | error: ".$this->mysqli->error);
$this->addLog("Err - SQL: $sql | error: ".$this->mysqli->error);
return false;
}
}
@@ -72,9 +72,10 @@ class MysqlStorage implements MysqlStorageInterface {
}
/** @inheritDoc */
public function updateById(string $table, array $data, int $id, array $modifier = []) : bool {
public function updateById(string $table, array $data, mixed $id, array $modifier = []) : bool {
$chunks = $this->getUpdateValue($data);
$sql = "update low_priority ".implode(" ", $modifier)." ".$this->escapeStr($table)." set ".implode(",", $chunks)." where id = '".$id."'";
$id = (is_numeric($id)) ? (int) $id : $this->escapeStr($id);
$sql = "update low_priority ".implode(" ", $modifier)." ".$this->escapeStr($table)." set ".implode(",", $chunks)." where id = '$id'";
return $this->query($sql);
}
@@ -94,8 +95,9 @@ class MysqlStorage implements MysqlStorageInterface {
/** @inheritDoc */
public function deleteById(string $table, int $id) : bool {
$sql = "delete low_priority from ".$this->escapeStr($table)." where id='.$id.'";
public function deleteById(string $table, mixed $id) : bool {
$id = (is_numeric($id)) ? (int) $id : $this->escapeStr($id);
$sql = "delete low_priority from ".$this->escapeStr($table)." where id='$id'";
// возвращаем число затронутых строк/false
return $this->query($sql);
}
@@ -139,8 +141,9 @@ class MysqlStorage implements MysqlStorageInterface {
}
/** @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");
public function findById(string $table, mixed $id, string $name = 'id') : bool|array {
$id = (is_numeric($id)) ? (int) $id : $this->escapeStr($id);
$result = $this->query("select * from ".$this->escapeStr($table)." where `$name`='$id' limit 0, 1");
if (!$result || $result->num_rows == 0) return false;
$data = new MysqlStorageData($result);
return $data->fetchOne();

View File

@@ -2,19 +2,22 @@
namespace Rmphp\Storage\Mysql;
use Mysqli;
use mysqli_result;
interface MysqlStorageInterface {
/**
* @return \Mysqli
* @return Mysqli
*/
public function mysql() : \Mysqli;
public function mysql() : Mysqli;
/**
* Метод прямого запроса к текущей БД
* @param string $sql
* @return bool|\mysqli_result
* @return bool|mysqli_result
*/
public function query(string $sql) : bool|\mysqli_result;
public function query(string $sql) : bool|mysqli_result;
/**
* Метод добавления записи в текущую БД
@@ -36,11 +39,11 @@ interface MysqlStorageInterface {
* Метод редактирования записи в текущей БД по ID
* @param string $table
* @param array $data
* @param int $id
* @param mixed $id
* @param array $modifier
* @return bool
*/
public function updateById(string $table, array $data, int $id, array $modifier = []) : bool;
public function updateById(string $table, array $data, mixed $id, array $modifier = []) : bool;
/**
* @param string $table
@@ -60,10 +63,10 @@ interface MysqlStorageInterface {
/**
* @param string $table
* @param int $id
* @param mixed $id
* @return bool
*/
public function deleteById(string $table, int $id) : bool;
public function deleteById(string $table, mixed $id) : bool;
/**
* @param string $table
@@ -89,11 +92,11 @@ interface MysqlStorageInterface {
/**
* @param string $table
* @param int $id
* @param mixed $id
* @param string $name
* @return bool|array
*/
public function findById(string $table, int $id, string $name = 'id') : bool|array;
public function findById(string $table, mixed $id, string $name = 'id') : bool|array;
/**
* Метод экранирования данных с учетом текущего подключения в т.ч для LIKE