Files
kiri-core/Database/Command.php
T

351 lines
7.4 KiB
PHP
Raw Normal View History

2020-08-31 12:38:32 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 15:23
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 12:38:32 +08:00
namespace Database;
use Snowflake\Abstracts\Component;
use Exception;
use PDO;
use PDOStatement;
2020-09-17 11:15:35 +08:00
use Snowflake\Abstracts\Config;
2020-08-31 12:38:32 +08:00
/**
* Class Command
* @package Database
*/
class Command extends Component
{
const ROW_COUNT = 'ROW_COUNT';
const FETCH = 'FETCH';
const FETCH_ALL = 'FETCH_ALL';
const EXECUTE = 'EXECUTE';
const FETCH_COLUMN = 'FETCH_COLUMN';
/** @var Connection */
2020-10-29 18:17:25 +08:00
public Connection $db;
2020-08-31 12:38:32 +08:00
2020-10-30 02:08:14 +08:00
/** @var ?string */
public ?string $sql = '';
2020-08-31 12:38:32 +08:00
/** @var array */
2020-10-29 18:17:25 +08:00
public array $params = [];
2020-08-31 12:38:32 +08:00
/** @var string */
2020-10-29 18:17:25 +08:00
private string $_modelName;
2020-08-31 12:38:32 +08:00
2020-10-30 01:38:35 +08:00
private ?PDOStatement $prepare = null;
2020-08-31 12:38:32 +08:00
/**
2020-12-17 14:09:14 +08:00
* @return array|bool|int|string|PDOStatement|null
* @throws Exception
2020-08-31 12:38:32 +08:00
*/
2020-12-17 14:09:14 +08:00
public function incrOrDecr(): array|bool|int|string|PDOStatement|null
2020-08-31 12:38:32 +08:00
{
return $this->execute(static::EXECUTE);
}
/**
* @param bool $isInsert
* @param bool $hasAutoIncrement
2020-12-17 14:09:14 +08:00
* @return int|bool|array|string|null
* @throws Exception
2020-08-31 12:38:32 +08:00
*/
2021-02-22 17:44:24 +08:00
public function save($isInsert = TRUE, $hasAutoIncrement = null): int|bool|array|string|null
2020-08-31 12:38:32 +08:00
{
return $this->execute(static::EXECUTE, $isInsert, $hasAutoIncrement);
}
/**
* @param $model
* @param $attributes
* @param $condition
* @param $param
* @return Command
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function update($model, $attributes, $condition, $param): Command
2020-08-31 12:38:32 +08:00
{
$change = $this->db->getSchema()->getChange();
$sql = $change->update($model, $attributes, $condition, $param);
return $this->setSql($sql)->bindValues($param);
}
/**
* @param $tableName
* @param $attributes
* @param $condition
* @return Command
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function batchUpdate($tableName, $attributes, $condition): Command
2020-08-31 12:38:32 +08:00
{
$change = $this->db->getSchema()->getChange();
[$sql, $param] = $change->batchUpdate($tableName, $attributes, $condition);
return $this->setSql($sql)->bindValues($param);
}
/**
* @param $tableName
* @param $attributes
* @return Command
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function batchInsert($tableName, $attributes): Command
2020-08-31 12:38:32 +08:00
{
$change = $this->db->getSchema()->getChange();
$attribute_key = array_keys(current($attributes));
[$sql, $param] = $change->batchInsert($tableName, $attribute_key, $attributes);
return $this->setSql($sql)->bindValues($param);
}
/**
* @param $tableName
* @param $attributes
* @param $param
* @return Command
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function insert($tableName, $attributes, $param): Command
2020-08-31 12:38:32 +08:00
{
$change = $this->db->getSchema()->getChange();
$sql = $change->insert($tableName, $attributes, $param);
return $this->setSql($sql)->bindValues($param);
}
/**
2020-12-17 14:09:14 +08:00
* @return int|bool|array|string|null
2020-08-31 12:38:32 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function all(): int|bool|array|string|null
2020-08-31 12:38:32 +08:00
{
return $this->execute(static::FETCH_ALL);
}
/**
* @param $tableName
* @param $param
* @param $condition
* @return Command
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function mathematics($tableName, $param, $condition): static
2020-08-31 12:38:32 +08:00
{
$change = $this->db->getSchema()->getChange();
$sql = $change->mathematics($tableName, $param, $condition);
return $this->setSql($sql);
}
/**
2020-12-17 14:09:14 +08:00
* @return array|bool|int|string|null
2020-08-31 12:38:32 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function one(): null|array|bool|int|string
2020-08-31 12:38:32 +08:00
{
return $this->execute(static::FETCH);
}
/**
2020-12-17 14:09:14 +08:00
* @return int|bool|array|string|null
2020-08-31 12:38:32 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function fetchColumn(): int|bool|array|string|null
2020-08-31 12:38:32 +08:00
{
return $this->execute(static::FETCH_COLUMN);
}
/**
2020-12-17 14:09:14 +08:00
* @return int|bool|array|string|null
2020-08-31 12:38:32 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function rowCount(): int|bool|array|string|null
2020-08-31 12:38:32 +08:00
{
return $this->execute(static::ROW_COUNT);
}
/**
2020-12-17 14:09:14 +08:00
* @return int|bool|array|string|null
2020-08-31 12:38:32 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function flush(): int|bool|array|string|null
2020-08-31 12:38:32 +08:00
{
return $this->execute(static::EXECUTE);
}
/**
* @param $type
2020-12-17 14:09:14 +08:00
* @param null $isInsert
* @param bool $hasAutoIncrement
* @return int|bool|array|string|null
2020-08-31 12:38:32 +08:00
* @throws Exception
*/
2021-02-22 17:44:24 +08:00
private function execute($type, $isInsert = null, $hasAutoIncrement = null): int|bool|array|string|null
2020-08-31 12:38:32 +08:00
{
try {
2020-09-17 11:15:35 +08:00
$time = microtime(true);
2020-08-31 12:38:32 +08:00
if ($type === static::EXECUTE) {
$result = $this->insert_or_change($isInsert, $hasAutoIncrement);
} else {
$result = $this->search($type);
}
2020-10-30 01:38:35 +08:00
if ($this->prepare) {
2020-08-31 12:38:32 +08:00
$this->prepare->closeCursor();
}
2020-09-17 11:15:35 +08:00
if (Config::get('debug.enable', false, false)) {
$this->debug($this->sql . '。 Run-time: ' . (microtime(true) - $time));
}
2020-11-06 16:47:17 +08:00
} catch (\Throwable $exception) {
2020-11-26 11:37:10 +08:00
$result = $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
2020-08-31 12:38:32 +08:00
} finally {
return $result;
}
}
/**
* @param $type
2020-12-17 14:09:14 +08:00
* @return mixed
2020-08-31 12:38:32 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
private function search($type): mixed
2020-08-31 12:38:32 +08:00
{
2020-09-18 17:32:49 +08:00
$connect = $this->db->getConnect($this->sql);
if (!($connect instanceof PDO)) {
2020-09-06 04:22:52 +08:00
return null;
}
if (!($query = $connect->query($this->sql))) return null;
2020-08-31 12:38:32 +08:00
if ($type === static::ROW_COUNT) {
2020-09-06 04:22:52 +08:00
$result = $query->rowCount();
2020-08-31 12:38:32 +08:00
} else if ($type === static::FETCH_COLUMN) {
2020-09-06 04:22:52 +08:00
$result = $query->fetchColumn();
2020-08-31 12:38:32 +08:00
} else if ($type === static::FETCH_ALL) {
2020-09-06 04:22:52 +08:00
$result = $query->fetchAll(PDO::FETCH_ASSOC);
2020-08-31 12:38:32 +08:00
} else {
2020-09-06 04:22:52 +08:00
$result = $query->fetch(PDO::FETCH_ASSOC);
2020-08-31 12:38:32 +08:00
}
return $result;
}
/**
* @param $isInsert
* @param $hasAutoIncrement
* @return bool|string
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
private function insert_or_change($isInsert, $hasAutoIncrement): bool|string
2020-08-31 12:38:32 +08:00
{
2020-09-06 04:44:13 +08:00
if (!($connection = $this->initPDOStatement())) {
2020-09-06 04:22:52 +08:00
return false;
}
2020-09-06 04:45:44 +08:00
if (($result = $this->prepare->execute($this->params)) === false) {
2020-09-17 11:15:35 +08:00
return $this->addError($connection->errorInfo()[2], 'mysql');
2020-08-31 12:38:32 +08:00
}
if (!$isInsert) {
return true;
}
2020-09-06 04:44:13 +08:00
$result = $connection->lastInsertId();
2021-02-22 17:44:24 +08:00
if ($result == 0 && $hasAutoIncrement->hasAutoIncrement()) {
2020-09-17 11:15:35 +08:00
return $this->addError($connection->errorInfo()[2], 'mysql');
2020-08-31 12:38:32 +08:00
}
return $result;
}
2020-09-06 04:44:13 +08:00
/**
* 重新构建
* @throws
*/
2020-12-17 14:09:14 +08:00
private function initPDOStatement(): PDO|bool|null
2020-09-06 04:44:13 +08:00
{
if (empty($this->sql)) {
return null;
}
if (!(($connect = $this->db->getConnect($this->sql)) instanceof PDO)) {
return null;
}
$this->prepare = $connect->prepare($this->sql);
if (!($this->prepare instanceof PDOStatement)) {
2020-09-17 11:15:35 +08:00
return $this->addError($this->sql . ':' . $this->getError(), 'mysql');
2020-09-06 04:44:13 +08:00
}
return $connect;
}
2020-08-31 12:38:32 +08:00
/**
* @param $modelName
* @return $this
*/
2020-12-17 14:09:14 +08:00
public function setModelName($modelName): static
2020-08-31 12:38:32 +08:00
{
$this->_modelName = $modelName;
return $this;
}
/**
* @return string
*/
2020-12-17 14:09:14 +08:00
public function getModelName(): string
2020-08-31 12:38:32 +08:00
{
return $this->_modelName;
}
/**
2020-12-17 14:09:14 +08:00
* @return int|bool|array|string|null
2020-08-31 12:38:32 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function delete(): int|bool|array|string|null
2020-08-31 12:38:32 +08:00
{
return $this->execute(static::EXECUTE);
}
/**
2020-12-17 14:09:14 +08:00
* @return int|bool|array|string|null
2020-08-31 12:38:32 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function exec(): int|bool|array|string|null
2020-08-31 12:38:32 +08:00
{
return $this->execute(static::EXECUTE);
}
/**
* @param array|null $data
* @return $this
*/
2020-12-17 14:09:14 +08:00
public function bindValues(array $data = NULL): static
2020-08-31 12:38:32 +08:00
{
if (!is_array($this->params)) {
$this->params = [];
}
if (!empty($data)) {
$this->params = array_merge($this->params, $data);
}
return $this;
}
/**
* @param $sql
* @return $this
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function setSql($sql): static
2020-08-31 12:38:32 +08:00
{
$this->sql = $sql;
return $this;
}
/**
* @return string
*/
2020-12-17 14:09:14 +08:00
public function getError(): string
2020-08-31 12:38:32 +08:00
{
return $this->prepare->errorInfo()[2] ?? 'Db 驱动错误.';
}
}