1 Commits
3.0 ... 3.1

Author SHA1 Message Date
User
448128d228 20240427#1 2024-04-27 03:02:16 +03:00
2 changed files with 25 additions and 19 deletions

View File

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

View File

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