This commit is contained in:
as2252258@163.com
2021-02-23 02:13:16 +08:00
parent 0ef0c77b3d
commit 82a83d1afc
2 changed files with 295 additions and 297 deletions
+295 -293
View File
@@ -6,6 +6,7 @@
* Time: 15:23 * Time: 15:23
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace Database; namespace Database;
@@ -21,330 +22,331 @@ use Snowflake\Abstracts\Config;
*/ */
class Command extends Component class Command extends Component
{ {
const ROW_COUNT = 'ROW_COUNT'; const ROW_COUNT = 'ROW_COUNT';
const FETCH = 'FETCH'; const FETCH = 'FETCH';
const FETCH_ALL = 'FETCH_ALL'; const FETCH_ALL = 'FETCH_ALL';
const EXECUTE = 'EXECUTE'; const EXECUTE = 'EXECUTE';
const FETCH_COLUMN = 'FETCH_COLUMN'; const FETCH_COLUMN = 'FETCH_COLUMN';
/** @var Connection */ /** @var Connection */
public Connection $db; public Connection $db;
/** @var ?string */ /** @var ?string */
public ?string $sql = ''; public ?string $sql = '';
/** @var array */ /** @var array */
public array $params = []; public array $params = [];
/** @var string */ /** @var string */
private string $_modelName; private string $_modelName;
private ?PDOStatement $prepare = null; private ?PDOStatement $prepare = null;
/** /**
* @return array|bool|int|string|PDOStatement|null * @return array|bool|int|string|PDOStatement|null
* @throws Exception * @throws Exception
*/ */
public function incrOrDecr(): array|bool|int|string|PDOStatement|null public function incrOrDecr(): array|bool|int|string|PDOStatement|null
{ {
return $this->execute(static::EXECUTE); return $this->execute(static::EXECUTE);
} }
/** /**
* @param bool $isInsert * @param bool $isInsert
* @param bool $hasAutoIncrement * @param bool $hasAutoIncrement
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
public function save($isInsert = TRUE, $hasAutoIncrement = null): int|bool|array|string|null public function save($isInsert = TRUE, $hasAutoIncrement = null): int|bool|array|string|null
{ {
return $this->execute(static::EXECUTE, $isInsert, $hasAutoIncrement); return $this->execute(static::EXECUTE, $isInsert, $hasAutoIncrement);
} }
/** /**
* @param $model * @param $model
* @param $attributes * @param $attributes
* @param $condition * @param $condition
* @param $param * @param $param
* @return Command * @return Command
* @throws Exception * @throws Exception
*/ */
public function update($model, $attributes, $condition, $param): Command public function update($model, $attributes, $condition, $param): Command
{ {
$change = $this->db->getSchema()->getChange(); $change = $this->db->getSchema()->getChange();
$sql = $change->update($model, $attributes, $condition, $param); $sql = $change->update($model, $attributes, $condition, $param);
return $this->setSql($sql)->bindValues($param); return $this->setSql($sql)->bindValues($param);
} }
/** /**
* @param $tableName * @param $tableName
* @param $attributes * @param $attributes
* @param $condition * @param $condition
* @return Command * @return Command
* @throws Exception * @throws Exception
*/ */
public function batchUpdate($tableName, $attributes, $condition): Command public function batchUpdate($tableName, $attributes, $condition): Command
{ {
$change = $this->db->getSchema()->getChange(); $change = $this->db->getSchema()->getChange();
[$sql, $param] = $change->batchUpdate($tableName, $attributes, $condition); [$sql, $param] = $change->batchUpdate($tableName, $attributes, $condition);
return $this->setSql($sql)->bindValues($param); return $this->setSql($sql)->bindValues($param);
} }
/** /**
* @param $tableName * @param $tableName
* @param $attributes * @param $attributes
* @return Command * @return Command
* @throws Exception * @throws Exception
*/ */
public function batchInsert($tableName, $attributes): Command public function batchInsert($tableName, $attributes): Command
{ {
$change = $this->db->getSchema()->getChange(); $change = $this->db->getSchema()->getChange();
$attribute_key = array_keys(current($attributes)); $attribute_key = array_keys(current($attributes));
[$sql, $param] = $change->batchInsert($tableName, $attribute_key, $attributes); [$sql, $param] = $change->batchInsert($tableName, $attribute_key, $attributes);
return $this->setSql($sql)->bindValues($param); return $this->setSql($sql)->bindValues($param);
} }
/** /**
* @param $tableName * @param $tableName
* @param $attributes * @param $attributes
* @param $param * @param $param
* @return Command * @return Command
* @throws Exception * @throws Exception
*/ */
public function insert($tableName, $attributes, $param): Command public function insert($tableName, $attributes, $param): Command
{ {
$change = $this->db->getSchema()->getChange(); $change = $this->db->getSchema()->getChange();
$sql = $change->insert($tableName, $attributes, $param); $sql = $change->insert($tableName, $attributes, $param);
return $this->setSql($sql)->bindValues($param); return $this->setSql($sql)->bindValues($param);
} }
/** /**
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
public function all(): int|bool|array|string|null public function all(): int|bool|array|string|null
{ {
return $this->execute(static::FETCH_ALL); return $this->execute(static::FETCH_ALL);
} }
/** /**
* @param $tableName * @param $tableName
* @param $param * @param $param
* @param $condition * @param $condition
* @return Command * @return Command
* @throws Exception * @throws Exception
*/ */
public function mathematics($tableName, $param, $condition): static public function mathematics($tableName, $param, $condition): static
{ {
$change = $this->db->getSchema()->getChange(); $change = $this->db->getSchema()->getChange();
$sql = $change->mathematics($tableName, $param, $condition); $sql = $change->mathematics($tableName, $param, $condition);
return $this->setSql($sql); return $this->setSql($sql);
} }
/** /**
* @return array|bool|int|string|null * @return array|bool|int|string|null
* @throws Exception * @throws Exception
*/ */
public function one(): null|array|bool|int|string public function one(): null|array|bool|int|string
{ {
return $this->execute(static::FETCH); return $this->execute(static::FETCH);
} }
/** /**
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
public function fetchColumn(): int|bool|array|string|null public function fetchColumn(): int|bool|array|string|null
{ {
return $this->execute(static::FETCH_COLUMN); return $this->execute(static::FETCH_COLUMN);
} }
/** /**
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
public function rowCount(): int|bool|array|string|null public function rowCount(): int|bool|array|string|null
{ {
return $this->execute(static::ROW_COUNT); return $this->execute(static::ROW_COUNT);
} }
/** /**
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
public function flush(): int|bool|array|string|null public function flush(): int|bool|array|string|null
{ {
return $this->execute(static::EXECUTE); return $this->execute(static::EXECUTE);
} }
/** /**
* @param $type * @param $type
* @param null $isInsert * @param null $isInsert
* @param bool $hasAutoIncrement * @param bool $hasAutoIncrement
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
private function execute($type, $isInsert = null, $hasAutoIncrement = null): int|bool|array|string|null private function execute($type, $isInsert = null, $hasAutoIncrement = null): int|bool|array|string|null
{ {
try { try {
$time = microtime(true); $time = microtime(true);
if ($type === static::EXECUTE) { if ($type === static::EXECUTE) {
$result = $this->insert_or_change($isInsert, $hasAutoIncrement); $result = $this->insert_or_change($isInsert, $hasAutoIncrement);
} else { } else {
$result = $this->search($type); $result = $this->search($type);
} }
if ($this->prepare) { if ($this->prepare) {
$this->prepare->closeCursor(); $this->prepare->closeCursor();
} }
if (Config::get('debug.enable', false, false)) { if (Config::get('debug.enable', false, false)) {
$this->debug($this->sql . '。 Run-time: ' . (microtime(true) - $time)); $this->debug($this->sql . '。 Run-time: ' . (microtime(true) - $time));
} }
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$result = $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql'); $result = $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
} finally { } finally {
return $result; return $result;
} }
} }
/** /**
* @param $type * @param $type
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
private function search($type): mixed private function search($type): mixed
{ {
$connect = $this->db->getConnect($this->sql); $connect = $this->db->getConnect($this->sql);
if (!($connect instanceof PDO)) { if (!($connect instanceof PDO)) {
return null; return null;
} }
if (!($query = $connect->query($this->sql))) return null; var_dump($connect, $this->sql);
if ($type === static::ROW_COUNT) { if (!($query = $connect->query($this->sql))) return null;
$result = $query->rowCount(); if ($type === static::ROW_COUNT) {
} else if ($type === static::FETCH_COLUMN) { $result = $query->rowCount();
$result = $query->fetchColumn(); } else if ($type === static::FETCH_COLUMN) {
} else if ($type === static::FETCH_ALL) { $result = $query->fetchColumn();
$result = $query->fetchAll(PDO::FETCH_ASSOC); } else if ($type === static::FETCH_ALL) {
} else { $result = $query->fetchAll(PDO::FETCH_ASSOC);
$result = $query->fetch(PDO::FETCH_ASSOC); } else {
} $result = $query->fetch(PDO::FETCH_ASSOC);
return $result; }
} return $result;
}
/** /**
* @param $isInsert * @param $isInsert
* @param $hasAutoIncrement * @param $hasAutoIncrement
* @return bool|string * @return bool|string
* @throws Exception * @throws Exception
*/ */
private function insert_or_change($isInsert, $hasAutoIncrement): bool|string private function insert_or_change($isInsert, $hasAutoIncrement): bool|string
{ {
if (!($connection = $this->initPDOStatement())) { if (!($connection = $this->initPDOStatement())) {
return false; return false;
} }
if (($result = $this->prepare->execute($this->params)) === false) { if (($result = $this->prepare->execute($this->params)) === false) {
return $this->addError($connection->errorInfo()[2], 'mysql'); return $this->addError($connection->errorInfo()[2], 'mysql');
} }
if (!$isInsert) { if (!$isInsert) {
return true; return true;
} }
$result = $connection->lastInsertId(); $result = $connection->lastInsertId();
if ($result == 0 && $hasAutoIncrement->hasAutoIncrement()) { if ($result == 0 && $hasAutoIncrement->hasAutoIncrement()) {
return $this->addError($connection->errorInfo()[2], 'mysql'); return $this->addError($connection->errorInfo()[2], 'mysql');
} }
return $result; return $result;
} }
/** /**
* 重新构建 * 重新构建
* @throws * @throws
*/ */
private function initPDOStatement(): PDO|bool|null private function initPDOStatement(): PDO|bool|null
{ {
if (empty($this->sql)) { if (empty($this->sql)) {
return null; return null;
} }
if (!(($connect = $this->db->getConnect($this->sql)) instanceof PDO)) { if (!(($connect = $this->db->getConnect($this->sql)) instanceof PDO)) {
return null; return null;
} }
$this->prepare = $connect->prepare($this->sql); $this->prepare = $connect->prepare($this->sql);
if (!($this->prepare instanceof PDOStatement)) { if (!($this->prepare instanceof PDOStatement)) {
return $this->addError($this->sql . ':' . $this->getError(), 'mysql'); return $this->addError($this->sql . ':' . $this->getError(), 'mysql');
} }
return $connect; return $connect;
} }
/** /**
* @param $modelName * @param $modelName
* @return $this * @return $this
*/ */
public function setModelName($modelName): static public function setModelName($modelName): static
{ {
$this->_modelName = $modelName; $this->_modelName = $modelName;
return $this; return $this;
} }
/** /**
* @return string * @return string
*/ */
public function getModelName(): string public function getModelName(): string
{ {
return $this->_modelName; return $this->_modelName;
} }
/** /**
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
public function delete(): int|bool|array|string|null public function delete(): int|bool|array|string|null
{ {
return $this->execute(static::EXECUTE); return $this->execute(static::EXECUTE);
} }
/** /**
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
public function exec(): int|bool|array|string|null public function exec(): int|bool|array|string|null
{ {
return $this->execute(static::EXECUTE); return $this->execute(static::EXECUTE);
} }
/** /**
* @param array|null $data * @param array|null $data
* @return $this * @return $this
*/ */
public function bindValues(array $data = NULL): static public function bindValues(array $data = NULL): static
{ {
if (!is_array($this->params)) { if (!is_array($this->params)) {
$this->params = []; $this->params = [];
} }
if (!empty($data)) { if (!empty($data)) {
$this->params = array_merge($this->params, $data); $this->params = array_merge($this->params, $data);
} }
return $this; return $this;
} }
/** /**
* @param $sql * @param $sql
* @return $this * @return $this
* @throws Exception * @throws Exception
*/ */
public function setSql($sql): static public function setSql($sql): static
{ {
$this->sql = $sql; $this->sql = $sql;
return $this; return $this;
} }
/** /**
* @return string * @return string
*/ */
public function getError(): string public function getError(): string
{ {
return $this->prepare->errorInfo()[2] ?? 'Db 驱动错误.'; return $this->prepare->errorInfo()[2] ?? 'Db 驱动错误.';
} }
} }
-4
View File
@@ -148,10 +148,6 @@ class Container extends BaseObject
} else { } else {
$this->_constructs[$class] = $constrict; $this->_constructs[$class] = $constrict;
} }
if ($class == HttpHeaders::class) {
var_dump($constrict, $this->_constructs[$class]);
}
return $reflection; return $reflection;
} }