mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-22 08:05:34 +08:00
add cs fixer and PHPStan and activate it (build 436)
This commit is contained in:
120
src/ZM/DB/DB.php
120
src/ZM/DB/DB.php
@@ -1,22 +1,23 @@
|
||||
<?php /** @noinspection PhpUnused */
|
||||
<?php
|
||||
|
||||
/** @noinspection PhpComposerExtensionStubsInspection */
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/** @noinspection PhpUnused */
|
||||
|
||||
namespace ZM\DB;
|
||||
|
||||
|
||||
use ZM\Console\Console;
|
||||
use ZM\MySQL\MySQLManager;
|
||||
use ZM\Store\MySQL\SqlPoolStorage;
|
||||
use PDOException;
|
||||
use PDOStatement;
|
||||
use Swoole\Database\PDOStatementProxy;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Exception\DbException;
|
||||
use ZM\MySQL\MySQLManager;
|
||||
use ZM\Store\MySQL\SqlPoolStorage;
|
||||
|
||||
/**
|
||||
* Class DB
|
||||
* @package ZM\DB
|
||||
* @deprecated This will delete in 2.6 or future version, use \ZM\MySQL\MySQLManager::getConnection() instead
|
||||
*/
|
||||
class DB
|
||||
@@ -27,9 +28,12 @@ class DB
|
||||
* @param $db_name
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function initTableList($db_name) {
|
||||
if (!extension_loaded("mysqlnd")) throw new DbException("Can not find mysqlnd PHP extension.");
|
||||
$result = MySQLManager::getWrapper()->fetchAllAssociative("select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA=?;", [$db_name]);
|
||||
public static function initTableList($db_name)
|
||||
{
|
||||
if (!extension_loaded('mysqlnd')) {
|
||||
throw new DbException('Can not find mysqlnd PHP extension.');
|
||||
}
|
||||
$result = MySQLManager::getWrapper()->fetchAllAssociative('select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA=?;', [$db_name]);
|
||||
foreach ($result as $v) {
|
||||
self::$table_list[] = $v['TABLE_NAME'];
|
||||
}
|
||||
@@ -37,18 +41,18 @@ class DB
|
||||
|
||||
/**
|
||||
* @param $table_name
|
||||
* @return Table
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function table($table_name): Table {
|
||||
public static function table($table_name): Table
|
||||
{
|
||||
if (Table::getTableInstance($table_name) === null) {
|
||||
if (in_array($table_name, self::$table_list))
|
||||
if (in_array($table_name, self::$table_list)) {
|
||||
return new Table($table_name);
|
||||
elseif (SqlPoolStorage::$sql_pool !== null) {
|
||||
throw new DbException("Table " . $table_name . " not exist in database.");
|
||||
} else {
|
||||
throw new DbException("Database connection not exist or connect failed. Please check sql configuration");
|
||||
}
|
||||
if (SqlPoolStorage::$sql_pool !== null) {
|
||||
throw new DbException('Table ' . $table_name . ' not exist in database.');
|
||||
}
|
||||
throw new DbException('Database connection not exist or connect failed. Please check sql configuration');
|
||||
}
|
||||
return Table::getTableInstance($table_name);
|
||||
}
|
||||
@@ -57,23 +61,24 @@ class DB
|
||||
* @param $line
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function statement($line) {
|
||||
public static function statement($line)
|
||||
{
|
||||
self::rawQuery($line, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $line
|
||||
* @return bool
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function unprepared($line): bool {
|
||||
public static function unprepared($line): bool
|
||||
{
|
||||
try {
|
||||
$conn = SqlPoolStorage::$sql_pool->getConnection();
|
||||
if ($conn === false) {
|
||||
SqlPoolStorage::$sql_pool->putConnection(null);
|
||||
throw new DbException("无法连接SQL!" . $line);
|
||||
throw new DbException('无法连接SQL!' . $line);
|
||||
}
|
||||
$result = $conn->query($line) === false ? false : true;
|
||||
$result = !($conn->query($line) === false);
|
||||
SqlPoolStorage::$sql_pool->putConnection($conn);
|
||||
return $result;
|
||||
} catch (DBException $e) {
|
||||
@@ -82,58 +87,58 @@ class DB
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $line
|
||||
* @param array $params
|
||||
* @param int $fetch_mode
|
||||
* @return mixed
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function rawQuery(string $line, $params = [], $fetch_mode = ZM_DEFAULT_FETCH_MODE) {
|
||||
if (!is_array($params)) $params = [$params];
|
||||
Console::debug("MySQL: " . $line . " | " . implode(", ", $params));
|
||||
public static function rawQuery(string $line, $params = [], $fetch_mode = ZM_DEFAULT_FETCH_MODE)
|
||||
{
|
||||
if (!is_array($params)) {
|
||||
$params = [$params];
|
||||
}
|
||||
Console::debug('MySQL: ' . $line . ' | ' . implode(', ', $params));
|
||||
try {
|
||||
if (SqlPoolStorage::$sql_pool === null) throw new DbException("未连接到任何数据库!");
|
||||
if (SqlPoolStorage::$sql_pool === null) {
|
||||
throw new DbException('未连接到任何数据库!');
|
||||
}
|
||||
$conn = SqlPoolStorage::$sql_pool->getConnection();
|
||||
if ($conn === false) {
|
||||
SqlPoolStorage::$sql_pool->putConnection(null);
|
||||
throw new DbException("无法连接SQL!" . $line);
|
||||
throw new DbException('无法连接SQL!' . $line);
|
||||
}
|
||||
$ps = $conn->prepare($line);
|
||||
if ($ps === false) {
|
||||
SqlPoolStorage::$sql_pool->putConnection(null);
|
||||
/** @noinspection PhpUndefinedFieldInspection */
|
||||
throw new DbException("SQL语句查询错误," . $line . ",错误信息:" . $conn->error);
|
||||
} else {
|
||||
if (!($ps instanceof PDOStatement) && !($ps instanceof PDOStatementProxy)) {
|
||||
var_dump($ps);
|
||||
SqlPoolStorage::$sql_pool->putConnection(null);
|
||||
throw new DbException("语句查询错误!返回的不是 PDOStatement" . $line);
|
||||
}
|
||||
if ($params == []) $result = $ps->execute();
|
||||
elseif (!is_array($params)) {
|
||||
$result = $ps->execute([$params]);
|
||||
} else $result = $ps->execute($params);
|
||||
if ($result !== true) {
|
||||
SqlPoolStorage::$sql_pool->putConnection(null);
|
||||
throw new DBException("语句[$line]错误!" . $ps->errorInfo()[2]);
|
||||
//echo json_encode(debug_backtrace(), 128 | 256);
|
||||
}
|
||||
SqlPoolStorage::$sql_pool->putConnection($conn);
|
||||
return $ps->fetchAll($fetch_mode);
|
||||
/* @noinspection PhpUndefinedFieldInspection */
|
||||
throw new DbException('SQL语句查询错误,' . $line . ',错误信息:' . $conn->error);
|
||||
}
|
||||
if (!($ps instanceof PDOStatement) && !($ps instanceof PDOStatementProxy)) {
|
||||
var_dump($ps);
|
||||
SqlPoolStorage::$sql_pool->putConnection(null);
|
||||
throw new DbException('语句查询错误!返回的不是 PDOStatement' . $line);
|
||||
}
|
||||
if ($params == []) {
|
||||
$result = $ps->execute();
|
||||
} elseif (!is_array($params)) {
|
||||
$result = $ps->execute([$params]);
|
||||
} else {
|
||||
$result = $ps->execute($params);
|
||||
}
|
||||
if ($result !== true) {
|
||||
SqlPoolStorage::$sql_pool->putConnection(null);
|
||||
throw new DBException("语句[{$line}]错误!" . $ps->errorInfo()[2]);
|
||||
//echo json_encode(debug_backtrace(), 128 | 256);
|
||||
}
|
||||
SqlPoolStorage::$sql_pool->putConnection($conn);
|
||||
return $ps->fetchAll($fetch_mode);
|
||||
} catch (DbException $e) {
|
||||
if (mb_strpos($e->getMessage(), "has gone away") !== false) {
|
||||
if (mb_strpos($e->getMessage(), 'has gone away') !== false) {
|
||||
zm_sleep(0.2);
|
||||
Console::warning("Gone away of MySQL! retrying!");
|
||||
Console::warning('Gone away of MySQL! retrying!');
|
||||
return self::rawQuery($line, $params);
|
||||
}
|
||||
Console::warning($e->getMessage());
|
||||
throw $e;
|
||||
} catch (PDOException $e) {
|
||||
if (mb_strpos($e->getMessage(), "has gone away") !== false) {
|
||||
if (mb_strpos($e->getMessage(), 'has gone away') !== false) {
|
||||
zm_sleep(0.2);
|
||||
Console::warning("Gone away of MySQL! retrying!");
|
||||
Console::warning('Gone away of MySQL! retrying!');
|
||||
return self::rawQuery($line, $params);
|
||||
}
|
||||
Console::warning($e->getMessage());
|
||||
@@ -141,7 +146,8 @@ class DB
|
||||
}
|
||||
}
|
||||
|
||||
public static function isTableExists($table): bool {
|
||||
public static function isTableExists($table): bool
|
||||
{
|
||||
return in_array($table, self::$table_list);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\DB;
|
||||
|
||||
|
||||
use ZM\Exception\DbException;
|
||||
|
||||
/**
|
||||
* Class DeleteBody
|
||||
* @package ZM\DB
|
||||
* @deprecated This will delete in 2.6 or future version, use \ZM\MySQL\MySQLManager::getConnection() instead
|
||||
*/
|
||||
class DeleteBody
|
||||
@@ -22,18 +21,19 @@ class DeleteBody
|
||||
|
||||
/**
|
||||
* DeleteBody constructor.
|
||||
* @param Table $table
|
||||
*/
|
||||
public function __construct(Table $table) {
|
||||
public function __construct(Table $table)
|
||||
{
|
||||
$this->table = $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws DbException
|
||||
* @return mixed
|
||||
*/
|
||||
public function save() {
|
||||
list($sql, $param) = $this->getWhereSQL();
|
||||
return DB::rawQuery("DELETE FROM " . $this->table->getTableName() . " WHERE " . $sql, $param);
|
||||
public function save()
|
||||
{
|
||||
[$sql, $param] = $this->getWhereSQL();
|
||||
return DB::rawQuery('DELETE FROM ' . $this->table->getTableName() . ' WHERE ' . $sql, $param);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\DB;
|
||||
|
||||
|
||||
use ZM\Exception\DbException;
|
||||
|
||||
/**
|
||||
* Class InsertBody
|
||||
* @package ZM\DB
|
||||
* @deprecated This will delete in 2.6 or future version, use \ZM\MySQL\MySQLManager::getConnection() instead
|
||||
*/
|
||||
class InsertBody
|
||||
@@ -17,14 +16,15 @@ class InsertBody
|
||||
* @var Table
|
||||
*/
|
||||
private $table;
|
||||
|
||||
private $row;
|
||||
|
||||
/**
|
||||
* InsertBody constructor.
|
||||
* @param Table $table
|
||||
* @param $row
|
||||
*/
|
||||
public function __construct(Table $table, $row) {
|
||||
public function __construct(Table $table, $row)
|
||||
{
|
||||
$this->table = $table;
|
||||
$this->row = $row;
|
||||
}
|
||||
@@ -32,7 +32,8 @@ class InsertBody
|
||||
/**
|
||||
* @throws DbException
|
||||
*/
|
||||
public function save() {
|
||||
public function save()
|
||||
{
|
||||
DB::rawQuery('INSERT INTO ' . $this->table->getTableName() . ' VALUES (' . implode(',', array_fill(0, count($this->row), '?')) . ')', $this->row);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\DB;
|
||||
|
||||
|
||||
use ZM\Exception\DbException;
|
||||
|
||||
/**
|
||||
* Class SelectBody
|
||||
* @package ZM\DB
|
||||
* @deprecated This will delete in 2.6 or future version, use \ZM\MySQL\MySQLManager::getConnection() instead
|
||||
*/
|
||||
class SelectBody
|
||||
@@ -20,96 +19,119 @@ class SelectBody
|
||||
|
||||
private $select_thing;
|
||||
|
||||
private $result;
|
||||
|
||||
private $result = null;
|
||||
|
||||
public function __construct($table, $select_thing) {
|
||||
public function __construct($table, $select_thing)
|
||||
{
|
||||
$this->table = $table;
|
||||
$this->select_thing = $select_thing;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
* @throws DbException
|
||||
*/
|
||||
public function get() { return $this->fetchAll(); }
|
||||
|
||||
/**
|
||||
* @throws DbException
|
||||
*/
|
||||
public function count(): int {
|
||||
$this->select_thing = ["count(*)"];
|
||||
$str = $this->queryPrepare();
|
||||
$this->result = DB::rawQuery($str[0], $str[1]);
|
||||
return intval($this->result[0]["count(*)"]);
|
||||
public function get()
|
||||
{
|
||||
return $this->fetchAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $fetch_mode
|
||||
* @return null
|
||||
* @throws DbException
|
||||
*/
|
||||
public function fetchAll($fetch_mode = ZM_DEFAULT_FETCH_MODE) {
|
||||
public function count(): int
|
||||
{
|
||||
$this->select_thing = ['count(*)'];
|
||||
$str = $this->queryPrepare();
|
||||
$this->result = DB::rawQuery($str[0], $str[1]);
|
||||
return intval($this->result[0]['count(*)']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $fetch_mode
|
||||
* @throws DbException
|
||||
*/
|
||||
public function fetchAll($fetch_mode = ZM_DEFAULT_FETCH_MODE)
|
||||
{
|
||||
$this->execute($fetch_mode);
|
||||
return $this->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|null
|
||||
* @throws DbException
|
||||
* @return null|mixed
|
||||
*/
|
||||
public function fetchFirst() {
|
||||
public function fetchFirst()
|
||||
{
|
||||
return $this->fetchAll()[0] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $key
|
||||
* @return mixed|null
|
||||
* @param null $key
|
||||
* @throws DbException
|
||||
* @return null|mixed
|
||||
*/
|
||||
public function value($key = null) {
|
||||
public function value($key = null)
|
||||
{
|
||||
$r = $this->fetchFirst();
|
||||
if ($r === null) return null;
|
||||
if ($key === null)
|
||||
if ($r === null) {
|
||||
return null;
|
||||
}
|
||||
if ($key === null) {
|
||||
return current($r);
|
||||
else return $r[$key] ?? null;
|
||||
}
|
||||
return $r[$key] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $fetch_mode
|
||||
* @param int $fetch_mode
|
||||
* @throws DbException
|
||||
*/
|
||||
public function execute($fetch_mode = ZM_DEFAULT_FETCH_MODE) {
|
||||
public function execute($fetch_mode = ZM_DEFAULT_FETCH_MODE)
|
||||
{
|
||||
$str = $this->queryPrepare();
|
||||
$this->result = DB::rawQuery($str[0], $str[1], $fetch_mode);
|
||||
}
|
||||
|
||||
public function getResult() { return $this->result; }
|
||||
public function getResult()
|
||||
{
|
||||
return $this->result;
|
||||
}
|
||||
|
||||
public function equals(SelectBody $body): bool {
|
||||
if ($this->select_thing != $body->getSelectThing()) return false;
|
||||
elseif ($this->where_thing == $body->getWhereThing()) return false;
|
||||
else return true;
|
||||
public function equals(SelectBody $body): bool
|
||||
{
|
||||
if ($this->select_thing != $body->getSelectThing()) {
|
||||
return false;
|
||||
}
|
||||
if ($this->where_thing == $body->getWhereThing()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSelectThing() { return $this->select_thing; }
|
||||
public function getSelectThing()
|
||||
{
|
||||
return $this->select_thing;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getWhereThing(): array { return $this->where_thing; }
|
||||
public function getWhereThing(): array
|
||||
{
|
||||
return $this->where_thing;
|
||||
}
|
||||
|
||||
private function queryPrepare(): array {
|
||||
$msg = "SELECT " . implode(", ", $this->select_thing) . " FROM " . $this->table->getTableName();
|
||||
private function queryPrepare(): array
|
||||
{
|
||||
$msg = 'SELECT ' . implode(', ', $this->select_thing) . ' FROM ' . $this->table->getTableName();
|
||||
$sql = $this->table->paintWhereSQL($this->where_thing['='] ?? [], '=');
|
||||
if ($sql[0] != '') {
|
||||
$msg .= " WHERE " . $sql[0];
|
||||
$msg .= ' WHERE ' . $sql[0];
|
||||
$array = $sql[1];
|
||||
$sql = $this->table->paintWhereSQL($this->where_thing['!='] ?? [], '!=');
|
||||
if ($sql[0] != '') $msg .= " AND " . $sql[0];
|
||||
if ($sql[0] != '') {
|
||||
$msg .= ' AND ' . $sql[0];
|
||||
}
|
||||
$array = array_merge($array, $sql[1]);
|
||||
}
|
||||
return [$msg, $array ?? []];
|
||||
|
||||
@@ -1,71 +1,87 @@
|
||||
<?php /** @noinspection PhpUnused */
|
||||
<?php
|
||||
|
||||
/** @noinspection PhpMissingReturnTypeInspection */
|
||||
/**
|
||||
* @noinspection PhpMissingReturnTypeInspection
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\DB;
|
||||
|
||||
/**
|
||||
* Class Table
|
||||
* @package ZM\DB
|
||||
* @deprecated This will delete in 2.6 or future version, use \ZM\MySQL\MySQLManager::getConnection() instead
|
||||
*/
|
||||
class Table
|
||||
{
|
||||
private $table_name;
|
||||
|
||||
/** @var SelectBody[] */
|
||||
public $cache = [];
|
||||
|
||||
private $table_name;
|
||||
|
||||
private static $table_instance = [];
|
||||
|
||||
public function __construct($table_name) {
|
||||
public function __construct($table_name)
|
||||
{
|
||||
$this->table_name = $table_name;
|
||||
self::$table_instance[$table_name] = $this;
|
||||
}
|
||||
|
||||
public static function getTableInstance($table_name) {
|
||||
if (isset(self::$table_instance[$table_name])) return self::$table_instance[$table_name];
|
||||
else return null;
|
||||
public static function getTableInstance($table_name)
|
||||
{
|
||||
if (isset(self::$table_instance[$table_name])) {
|
||||
return self::$table_instance[$table_name];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function select($what = []) {
|
||||
return new SelectBody($this, $what == [] ? ["*"] : $what);
|
||||
public function select($what = [])
|
||||
{
|
||||
return new SelectBody($this, $what == [] ? ['*'] : $what);
|
||||
}
|
||||
|
||||
public function where($column, $operation_or_value, $value = null) {
|
||||
return (new SelectBody($this, ["*"]))->where($column, $operation_or_value, $value);
|
||||
public function where($column, $operation_or_value, $value = null)
|
||||
{
|
||||
return (new SelectBody($this, ['*']))->where($column, $operation_or_value, $value);
|
||||
}
|
||||
|
||||
public function insert($row) {
|
||||
public function insert($row)
|
||||
{
|
||||
$this->cache = [];
|
||||
return new InsertBody($this, $row);
|
||||
}
|
||||
|
||||
public function update(array $set_value) {
|
||||
public function update(array $set_value)
|
||||
{
|
||||
$this->cache = [];
|
||||
return new UpdateBody($this, $set_value);
|
||||
}
|
||||
|
||||
public function delete() {
|
||||
public function delete()
|
||||
{
|
||||
$this->cache = [];
|
||||
return new DeleteBody($this);
|
||||
}
|
||||
|
||||
public function statement() {
|
||||
public function statement()
|
||||
{
|
||||
$this->cache = [];
|
||||
//TODO: 无返回的statement语句
|
||||
}
|
||||
|
||||
public function paintWhereSQL($rule, $operator) {
|
||||
if ($rule == []) return ["", []];
|
||||
$msg = "";
|
||||
public function paintWhereSQL($rule, $operator)
|
||||
{
|
||||
if ($rule == []) {
|
||||
return ['', []];
|
||||
}
|
||||
$msg = '';
|
||||
$param = [];
|
||||
foreach ($rule as $k => $v) {
|
||||
if ($msg == "") {
|
||||
$msg .= $k . " $operator ? ";
|
||||
if ($msg == '') {
|
||||
$msg .= $k . " {$operator} ? ";
|
||||
} else {
|
||||
$msg .= " AND " . $k . " $operator ?";
|
||||
$msg .= ' AND ' . $k . " {$operator} ?";
|
||||
}
|
||||
$param[] = $v;
|
||||
}
|
||||
@@ -75,6 +91,8 @@ class Table
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTableName() { return $this->table_name; }
|
||||
|
||||
public function getTableName()
|
||||
{
|
||||
return $this->table_name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\DB;
|
||||
|
||||
|
||||
use ZM\Exception\DbException;
|
||||
|
||||
/**
|
||||
* Class UpdateBody
|
||||
* @package ZM\DB
|
||||
* @deprecated This will delete in 2.6 or future version, use \ZM\MySQL\MySQLManager::getConnection() instead
|
||||
*/
|
||||
class UpdateBody
|
||||
@@ -19,6 +18,7 @@ class UpdateBody
|
||||
* @var Table
|
||||
*/
|
||||
private $table;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
@@ -26,10 +26,9 @@ class UpdateBody
|
||||
|
||||
/**
|
||||
* UpdateBody constructor.
|
||||
* @param Table $table
|
||||
* @param array $set_value
|
||||
*/
|
||||
public function __construct(Table $table, array $set_value) {
|
||||
public function __construct(Table $table, array $set_value)
|
||||
{
|
||||
$this->table = $table;
|
||||
$this->set_value = $set_value;
|
||||
}
|
||||
@@ -37,20 +36,23 @@ class UpdateBody
|
||||
/**
|
||||
* @throws DbException
|
||||
*/
|
||||
public function save() {
|
||||
public function save()
|
||||
{
|
||||
$arr = [];
|
||||
$msg = [];
|
||||
foreach ($this->set_value as $k => $v) {
|
||||
$msg [] = $k . ' = ?';
|
||||
$msg[] = $k . ' = ?';
|
||||
$arr[] = $v;
|
||||
}
|
||||
if (($msg ?? []) == []) throw new DbException('update value sets can not be empty!');
|
||||
if (($msg ?? []) == []) {
|
||||
throw new DbException('update value sets can not be empty!');
|
||||
}
|
||||
$line = 'UPDATE ' . $this->table->getTableName() . ' SET ' . implode(', ', $msg);
|
||||
if ($this->where_thing != []) {
|
||||
list($sql, $param) = $this->getWhereSQL();
|
||||
[$sql, $param] = $this->getWhereSQL();
|
||||
$arr = array_merge($arr, $param);
|
||||
$line .= ' WHERE ' . $sql;
|
||||
}
|
||||
return DB::rawQuery($line, $arr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +1,48 @@
|
||||
<?php /** @noinspection PhpMissingReturnTypeInspection */
|
||||
<?php
|
||||
|
||||
/** @noinspection PhpMissingReturnTypeInspection */
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\DB;
|
||||
|
||||
/**
|
||||
* Trait WhereBody
|
||||
* @package ZM\DB
|
||||
* @deprecated This will delete in 2.6 or future version, use \ZM\MySQL\MySQLManager::getConnection() instead
|
||||
*/
|
||||
trait WhereBody
|
||||
{
|
||||
protected $where_thing = [];
|
||||
|
||||
public function where($column, $operation_or_value, $value = null) {
|
||||
if ($value !== null) $this->where_thing[$operation_or_value][$column] = $value;
|
||||
elseif (!in_array($operation_or_value, ['=', '!=', '>', '<', '>=', '<=', 'IN', 'in'])) $this->where_thing['='][$column] = $operation_or_value;
|
||||
else $this->where_thing['='][$column] = $operation_or_value;
|
||||
public function where($column, $operation_or_value, $value = null)
|
||||
{
|
||||
if ($value !== null) {
|
||||
$this->where_thing[$operation_or_value][$column] = $value;
|
||||
} elseif (!in_array($operation_or_value, ['=', '!=', '>', '<', '>=', '<=', 'IN', 'in'])) {
|
||||
$this->where_thing['='][$column] = $operation_or_value;
|
||||
} else {
|
||||
$this->where_thing['='][$column] = $operation_or_value;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getWhereSQL() {
|
||||
protected function getWhereSQL()
|
||||
{
|
||||
$param = [];
|
||||
$msg = '';
|
||||
foreach ($this->where_thing as $k => $v) {
|
||||
foreach ($v as $ks => $vs) {
|
||||
if ($param != []) {
|
||||
$msg .= ' AND ' . $ks . " $k ?";
|
||||
$msg .= ' AND ' . $ks . " {$k} ?";
|
||||
} else {
|
||||
$msg .= "$ks $k ?";
|
||||
$msg .= "{$ks} {$k} ?";
|
||||
}
|
||||
$param [] = $vs;
|
||||
$param[] = $vs;
|
||||
}
|
||||
}
|
||||
if ($msg == '') $msg = 1;
|
||||
if ($msg == '') {
|
||||
$msg = 1;
|
||||
}
|
||||
return [$msg, $param];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user