Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
448128d228 | ||
|
|
120d795b85 | ||
|
|
6caceefc17 | ||
|
|
c681de9bfb | ||
|
|
2e384e2071 | ||
|
|
368ccfa44b | ||
|
|
f0f862d979 | ||
|
|
c43f3a4768 | ||
|
|
19305af62f | ||
|
|
23485cd0e3 |
@@ -7,15 +7,15 @@ DB component for **Rmphp**
|
|||||||
Stable version
|
Stable version
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
composer require rmphp/kernel
|
composer require rmphp/storage
|
||||||
```
|
```
|
||||||
```bash
|
```bash
|
||||||
composer require rmphp/kernel:"^1.0"
|
composer require rmphp/storage:"^3.0"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Dev version contains the latest changes
|
Dev version contains the latest changes
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
composer require rmphp/kernel:"1.0.x-dev"
|
composer require rmphp/storage:"3.x-dev"
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Rmphp\Storage\Exception;
|
namespace Rmphp\Storage\Mysql\Exception;
|
||||||
|
|
||||||
|
|
||||||
class MysqlException extends \Exception {
|
class MysqlException extends \Exception {
|
||||||
208
src/Mysql/MysqlStorage.php
Normal file
208
src/Mysql/MysqlStorage.php
Normal file
@@ -0,0 +1,208 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rmphp\Storage\Mysql;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Mysqli;
|
||||||
|
use mysqli_result;
|
||||||
|
|
||||||
|
class MysqlStorage implements MysqlStorageInterface {
|
||||||
|
|
||||||
|
public array $log = array();
|
||||||
|
public bool $logsEnabled = false;
|
||||||
|
private Mysqli $mysqli;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Внутренний конструктор подключения к БД
|
||||||
|
* Mysql constructor.
|
||||||
|
* @param array $params
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function __construct(array $params) {
|
||||||
|
$this->mysqli = new mysqli($params['host'], $params['user'], $params['pass'], $params['base']);
|
||||||
|
// выводим ошибку при неудачном подключении
|
||||||
|
if ($this->mysqli->connect_errno) {
|
||||||
|
throw new Exception($this->mysqli->connect_errno);
|
||||||
|
}
|
||||||
|
$this->mysqli->set_charset("utf8");
|
||||||
|
if(!empty($params['logsEnable'])) $this->logsEnabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function mysql() : Mysqli {
|
||||||
|
return $this->mysqli;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function query(string $sql) : bool|mysqli_result
|
||||||
|
{
|
||||||
|
try{
|
||||||
|
$result = $this->mysqli->query($sql);
|
||||||
|
if($this->mysqli->errno) throw new Exception();
|
||||||
|
$this->addLog("OK - $sql");
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
catch (Exception $exception){
|
||||||
|
$this->addLog("Err - SQL: $sql | error: ".$this->mysqli->error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function insert(string $table, array $data, bool $update = false) : bool {
|
||||||
|
$chunks = $this->getInsertValue($data);
|
||||||
|
$upd = $this->getUpdateValue($data);
|
||||||
|
if (!$update) {
|
||||||
|
$sql = "insert low_priority into ".$this->escapeStr($table)." (".implode(",", $chunks['columns']).") values (".implode(",", $chunks['values']).")";
|
||||||
|
} else{
|
||||||
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function batchInsert(string $table, array $data) : bool {
|
||||||
|
foreach($data as $insertRow){
|
||||||
|
$chunks = $this->getInsertValue($insertRow);
|
||||||
|
$values[] = "(".implode(",", $chunks['values']).")";
|
||||||
|
}
|
||||||
|
$sql = "insert low_priority into ".$this->escapeStr($table)." (".implode(",", $chunks['columns'] ?? []).") values ".implode(",", $values ?? []);
|
||||||
|
return $this->query($sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function updateById(string $table, array $data, mixed $id, array $modifier = []) : bool {
|
||||||
|
$chunks = $this->getUpdateValue($data);
|
||||||
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function updateByParam(string $table, array $data, string $case, array $modifier = []) : bool {
|
||||||
|
$chunks = $this->getUpdateValue($data);
|
||||||
|
$sql = "update low_priority ".implode(" ", $modifier)." ".$this->escapeStr($table)." set ". implode(",", $chunks)." where ".$case;
|
||||||
|
return $this->query($sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function replace(string $table, array $data) : bool {
|
||||||
|
$chunks = $this->getInsertValue($data);
|
||||||
|
$sql = "replace low_priority into ".$this->escapeStr($table)." (".implode(",", $chunks['columns']).") values (".implode(",", $chunks['values']).")";
|
||||||
|
return $this->query($sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function deleteByParam(string $table, string $case) : bool {
|
||||||
|
$sql = "delete low_priority from ".$this->escapeStr($table)." 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) {
|
||||||
|
$cnts = (!empty($count)) ? $count : $this->query($sql)->num_rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (true){
|
||||||
|
case ($ln > 1 || $numPage > 1) : $limit = " limit ".(($numPage * $ln) - $ln).", ".$ln; break;
|
||||||
|
case ($ln == 1): $limit = " limit 0, 1"; break;
|
||||||
|
default: $limit = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = $this->query($sql.$limit);
|
||||||
|
if (!$result || $result->num_rows == 0) return false;
|
||||||
|
|
||||||
|
$data = new MysqlStorageData($result);
|
||||||
|
$data->count = $cnts ?? 0;
|
||||||
|
$data->hex = md5($sql);
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function findOne(string $sql) : bool|array {
|
||||||
|
$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, 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function escapeReg(string $string) : ?string {
|
||||||
|
if(!isset($string)) return null;
|
||||||
|
return trim(addcslashes($this->mysqli->real_escape_string($string), "%_"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function escapeStr(?string $string) : ?string {
|
||||||
|
if(!isset($string)) return null;
|
||||||
|
return trim($this->mysqli->real_escape_string($string));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function addLog(string $log) : void {
|
||||||
|
if($this->logsEnabled) $this->log[] = $log;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function getLogs() : array {
|
||||||
|
return $this->log;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
public function getLastLog() : string {
|
||||||
|
return $this->log[count($this->log)-1];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $array
|
||||||
|
* @return array[]
|
||||||
|
*/
|
||||||
|
private function getInsertValue(array $array) : array {
|
||||||
|
foreach ($array as $key => $value) {
|
||||||
|
$colunms[] = "`$key`";
|
||||||
|
$values[] = ($value !== NULL) ? "'$value'" : "NULL";
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
"columns" => $colunms ?? [],
|
||||||
|
"values" => $values ?? []
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 ?? [];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Rmphp\Storage;
|
namespace Rmphp\Storage\Mysql;
|
||||||
|
|
||||||
|
|
||||||
class MysqlStorageData {
|
class MysqlStorageData {
|
||||||
@@ -8,6 +8,7 @@ class MysqlStorageData {
|
|||||||
private ?\mysqli_result $result;
|
private ?\mysqli_result $result;
|
||||||
private array $arrayData = [];
|
private array $arrayData = [];
|
||||||
public int $count;
|
public int $count;
|
||||||
|
public string $hex = "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MysqlDataObject constructor.
|
* MysqlDataObject constructor.
|
||||||
@@ -34,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();
|
||||||
@@ -50,7 +51,7 @@ class MysqlStorageData {
|
|||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getData() : array {
|
public function getData() : iterable {
|
||||||
if(!empty($this->arrayData)) return $this->arrayData;
|
if(!empty($this->arrayData)) return $this->arrayData;
|
||||||
if(!$this->result) return [];
|
if(!$this->result) return [];
|
||||||
$this->result->data_seek(0);
|
$this->result->data_seek(0);
|
||||||
130
src/Mysql/MysqlStorageInterface.php
Normal file
130
src/Mysql/MysqlStorageInterface.php
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rmphp\Storage\Mysql;
|
||||||
|
|
||||||
|
use Mysqli;
|
||||||
|
use mysqli_result;
|
||||||
|
|
||||||
|
interface MysqlStorageInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Mysqli
|
||||||
|
*/
|
||||||
|
public function mysql() : Mysqli;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Метод прямого запроса к текущей БД
|
||||||
|
* @param string $sql
|
||||||
|
* @return bool|mysqli_result
|
||||||
|
*/
|
||||||
|
public function query(string $sql) : bool|mysqli_result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Метод добавления записи в текущую БД
|
||||||
|
* @param string $table
|
||||||
|
* @param array $data
|
||||||
|
* @param bool $update
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function insert(string $table, array $data, bool $update = false) : bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $table
|
||||||
|
* @param array $data
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function batchInsert(string $table, array $data) : bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Метод редактирования записи в текущей БД по ID
|
||||||
|
* @param string $table
|
||||||
|
* @param array $data
|
||||||
|
* @param mixed $id
|
||||||
|
* @param array $modifier
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function updateById(string $table, array $data, mixed $id, array $modifier = []) : bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $table
|
||||||
|
* @param array $data
|
||||||
|
* @param string $case
|
||||||
|
* @param array $modifier
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function updateByParam(string $table, array $data, string $case, array $modifier = []) : bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $table
|
||||||
|
* @param array $data
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function replace(string $table, array $data) : bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $table
|
||||||
|
* @param mixed $id
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function deleteById(string $table, mixed $id) : bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $table
|
||||||
|
* @param string $case
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function deleteByParam(string $table, string $case) : bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $sql
|
||||||
|
* @param int $ln
|
||||||
|
* @param int $numPage
|
||||||
|
* @param int $count
|
||||||
|
* @return bool|MysqlStorageData
|
||||||
|
*/
|
||||||
|
public function find(string $sql, int $ln = 0, int $numPage = 1, int $count=0) : bool|MysqlStorageData;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $sql
|
||||||
|
* @return bool|array
|
||||||
|
*/
|
||||||
|
public function findOne(string $sql) : bool|array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $table
|
||||||
|
* @param mixed $id
|
||||||
|
* @param string $name
|
||||||
|
* @return bool|array
|
||||||
|
*/
|
||||||
|
public function findById(string $table, mixed $id, string $name = 'id') : bool|array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Метод экранирования данных с учетом текущего подключения в т.ч для LIKE
|
||||||
|
* @param string $string
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
public function escapeReg(string $string) : ?string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Метод экранирования данных с учетом текущего подключения
|
||||||
|
* @param string|null $string
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
public function escapeStr(?string $string) : ?string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Метод наполнения статичного массива с логами
|
||||||
|
* @param string $log
|
||||||
|
*/
|
||||||
|
public function addLog(string $log) : void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getLogs() : array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getLastLog() : string;
|
||||||
|
}
|
||||||
@@ -1,166 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Storage;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Mysqli;
|
|
||||||
use mysqli_result;
|
|
||||||
|
|
||||||
class MysqlStorage implements MysqlStorageInterface {
|
|
||||||
|
|
||||||
public array $log = array();
|
|
||||||
public bool $logsEnabled = false;
|
|
||||||
private Mysqli $mysqli;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Внутренний конструктор подключения к БД
|
|
||||||
* Mysql constructor.
|
|
||||||
* @param array $params
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function __construct(array $params) {
|
|
||||||
$this->mysqli = new mysqli($params['host'], $params['user'], $params['pass'], $params['base']);
|
|
||||||
// выводим ошибку при неудачном подключении
|
|
||||||
if ($this->mysqli->connect_errno) {
|
|
||||||
throw new Exception($this->mysqli->connect_errno);
|
|
||||||
}
|
|
||||||
$this->mysqli->set_charset("utf8");
|
|
||||||
if(!empty($params['logsEnable'])) $this->logsEnabled = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function mysql() : Mysqli {
|
|
||||||
return $this->mysqli;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function query(string $sql) : bool|mysqli_result
|
|
||||||
{
|
|
||||||
try{
|
|
||||||
$result = $this->mysqli->query($sql);
|
|
||||||
// запись в log
|
|
||||||
($this->mysqli->errno)
|
|
||||||
? $this->addLog("Err - SQL: ".$sql." | error: ".$this->mysqli->error)
|
|
||||||
: $this->addLog("OK - ".$sql);
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
/* 8.1.0 Теперь по умолчанию установлено значение MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT и выбрасывается исключение. Ранее оно было MYSQLI_REPORT_OFF. */
|
|
||||||
catch (Exception $exception){
|
|
||||||
$this->addLog("Err - SQL: ".$sql." | error: ".$this->mysqli->error);
|
|
||||||
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);
|
|
||||||
|
|
||||||
if (!$update) {
|
|
||||||
$sql = "insert low_priority into ".$this->escapeStr($tbl)." (".$col.") values (" . $val . ")";
|
|
||||||
} else{
|
|
||||||
$sql = "insert low_priority into ".$this->escapeStr($tbl)." (".$col.") values (".$val.") on duplicate key update ".implode(", ", $upd);
|
|
||||||
}
|
|
||||||
return $this->query($sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function edit(string $tbl, array $arr, string $case, bool $ignore=false) : bool {
|
|
||||||
foreach ($arr as $key => $value) {
|
|
||||||
$isql[] = ($value !== NULL) ? "`$key`='$value'" : "`$key`=NULL";
|
|
||||||
}
|
|
||||||
$where = (preg_match("'^[0-9]+$'",$case)) ? "id = '".(int) $case."'" : $case;
|
|
||||||
if(empty($ignore)) {
|
|
||||||
$sql = "update low_priority " . $this->escapeStr($tbl) . " set " . implode(", ", $isql) . " where " . $where;
|
|
||||||
} else {
|
|
||||||
$sql = "update low_priority ignore " . $this->escapeStr($tbl) . " set " . implode(", ", $isql) . " where " . $where;
|
|
||||||
}
|
|
||||||
return $this->query($sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function replace(string $tbl, array $arr) : bool {
|
|
||||||
foreach ($arr as $key => $value) {
|
|
||||||
$col[] = "`$key`";
|
|
||||||
$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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function del(string $tbl, string $case) : bool {
|
|
||||||
$where = (preg_match("'^[0-9]+$'",$case)) ? "id = '".(int) $case."'" : $case;
|
|
||||||
$sql = "delete low_priority from ".$this->escapeStr($tbl)." where ".$where;
|
|
||||||
// возвращаем число затронутых строк/false
|
|
||||||
return $this->query($sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function read(string $sql, int $ln = 0, int $numPage = 1) : bool|MysqlStorageData {
|
|
||||||
if ($ln > 1) {
|
|
||||||
$cnts = $this->query($sql)->num_rows;
|
|
||||||
}
|
|
||||||
// часть строки запроса с лимит
|
|
||||||
switch (true){
|
|
||||||
case ($ln > 1 || $numPage > 1) : $limit = " limit ".(($numPage * $ln) - $ln).", ".$ln; break;
|
|
||||||
case ($ln == 1): $limit = " limit 0, 1"; break;
|
|
||||||
default: $limit = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
$result = $this->query($sql.$limit);
|
|
||||||
if (!$result || $result->num_rows == 0) return false;
|
|
||||||
|
|
||||||
$data = new MysqlStorageData($result);
|
|
||||||
$data->count = $cnts ?? 0;
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function chktbl(string $tbl) : bool {
|
|
||||||
$result = $this->mysqli->query("SHOW TABLES LIKE '".$this->escapeStr($tbl)."'");
|
|
||||||
if ($result->num_rows == 0) {
|
|
||||||
$this->addLog(__METHOD__.":"." Err - Table ".$tbl." doesn't exist"); return false;
|
|
||||||
}
|
|
||||||
$this->addLog(__METHOD__.":"." OK - Table ".$tbl." exist");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function escapeReg(string $var) : ?string {
|
|
||||||
if(!isset($var)) return null;
|
|
||||||
return trim(addcslashes($this->mysqli->real_escape_string($var), "%_"));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function escapeStr(?string $var) : ?string {
|
|
||||||
if(!isset($var)) return null;
|
|
||||||
return trim($this->mysqli->real_escape_string($var));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function addLog(string $log) : void {
|
|
||||||
if($this->logsEnabled) $this->log[] = $log;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function getLogs() : array {
|
|
||||||
return $this->log;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function getLastLog() : string {
|
|
||||||
return $this->log[count($this->log)-1];
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Storage;
|
|
||||||
|
|
||||||
interface MysqlStorageInterface {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return \Mysqli
|
|
||||||
*/
|
|
||||||
public function mysql() : \Mysqli;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Метод прямого запроса к текущей БД
|
|
||||||
* @param string $sql
|
|
||||||
* @return bool|\mysqli_result
|
|
||||||
*/
|
|
||||||
public function query(string $sql) : bool|\mysqli_result;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Метод добавления записи в текущую БД
|
|
||||||
* @param string $tbl
|
|
||||||
* @param array $arr
|
|
||||||
* @param bool $update
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function add(string $tbl, array $arr, bool $update = false) : bool;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Метод редактирования записи в текущей БД по ID
|
|
||||||
* @param string $tbl
|
|
||||||
* @param array $arr
|
|
||||||
* @param string $case
|
|
||||||
* @param bool $ignore
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function edit(string $tbl, array $arr, string $case, bool $ignore=false) : bool;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Метод добавления записи в текущую БД
|
|
||||||
* @param string $tbl
|
|
||||||
* @param array $arr
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function replace(string $tbl, array $arr) : bool;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $tbl
|
|
||||||
* @param string $case
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function del(string $tbl, string $case) : bool;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $sql
|
|
||||||
* @param int $ln
|
|
||||||
* @param int $numPage
|
|
||||||
* @param int $count
|
|
||||||
* @return bool|MysqlStorageData
|
|
||||||
*/
|
|
||||||
public function read(string $sql, int $ln = 0, int $numPage = 1) : bool|MysqlStorageData;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $tbl
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function chktbl(string $tbl) : bool;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Метод экранирования данных с учетом текущего подключения в т.ч для LIKE
|
|
||||||
* @param string $var
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function escapeReg(string $var) : ?string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Метод экранирования данных с учетом текущего подключения
|
|
||||||
* @param string|null $var
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function escapeStr(?string $var) : ?string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Метод наполнения статичного массива с логами
|
|
||||||
* @param string $log
|
|
||||||
*/
|
|
||||||
public function addLog(string $log) : void;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getLogs() : array;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getLastLog() : string;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user