Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 39905b77e3 | |||
| fce82ec56c | |||
| 2970c3bc6b | |||
| a35d0bb921 | |||
| fce2b8bcdb | |||
| ce92f310a5 | |||
| 28fa17ecfb | |||
| 9fc910f10f | |||
| 0a95e722d6 | |||
| 9f2611972f | |||
| b7e59d1c96 | |||
| 9b0ee54608 | |||
| 763a4baa57 | |||
| fe639bbd1f | |||
| e40de4ee4a | |||
| ffc2af69da | |||
| 218a38da48 | |||
| 5acb2f4600 | |||
| d6a07446bb | |||
| 6c936c3896 | |||
| 14448d824a |
+45
-47
@@ -29,7 +29,7 @@ class Command extends Component
|
|||||||
public Connection $connection;
|
public Connection $connection;
|
||||||
public ?string $sql = '';
|
public ?string $sql = '';
|
||||||
public array $params = [];
|
public array $params = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @throws
|
* @throws
|
||||||
@@ -39,8 +39,8 @@ class Command extends Component
|
|||||||
parent::__construct();
|
parent::__construct();
|
||||||
Container::configure($this, $params);
|
Container::configure($this, $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws
|
* @throws
|
||||||
@@ -49,8 +49,8 @@ class Command extends Component
|
|||||||
{
|
{
|
||||||
return (bool)$this->_prepare();
|
return (bool)$this->_prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool|array
|
* @return bool|array
|
||||||
* @throws
|
* @throws
|
||||||
@@ -59,7 +59,7 @@ class Command extends Component
|
|||||||
{
|
{
|
||||||
return $this->search('fetchAll');
|
return $this->search('fetchAll');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|bool|null
|
* @return array|bool|null
|
||||||
* @throws
|
* @throws
|
||||||
@@ -68,7 +68,7 @@ class Command extends Component
|
|||||||
{
|
{
|
||||||
return $this->search('fetch');
|
return $this->search('fetch');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
@@ -81,7 +81,7 @@ class Command extends Component
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws
|
* @throws
|
||||||
@@ -90,7 +90,7 @@ class Command extends Component
|
|||||||
{
|
{
|
||||||
return $this->search('fetchColumn');
|
return $this->search('fetchColumn');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws
|
* @throws
|
||||||
@@ -98,11 +98,11 @@ class Command extends Component
|
|||||||
public function rowCount(): int
|
public function rowCount(): int
|
||||||
{
|
{
|
||||||
$data = $this->search('fetch');
|
$data = $this->search('fetch');
|
||||||
|
|
||||||
return !$data ? 0 : +current($data);
|
return !$data ? 0 : +current($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行查询类 SQL 操作,内置断连重试机制
|
* 执行查询类 SQL 操作,内置断连重试机制
|
||||||
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
|
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
|
||||||
@@ -121,38 +121,37 @@ class Command extends Component
|
|||||||
if (($prepare = $client->prepare($this->sql)) === false) {
|
if (($prepare = $client->prepare($this->sql)) === false) {
|
||||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
|
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$prepare->execute($this->params);
|
$prepare->execute($this->params);
|
||||||
|
|
||||||
$result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC);
|
$result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC);
|
||||||
$prepare->closeCursor();
|
$prepare->closeCursor();
|
||||||
|
|
||||||
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
||||||
|
|
||||||
$this->connection->release($client);
|
$this->connection->release($client);
|
||||||
return $result;
|
return $result;
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
$this->getLogger()->json_log($throwable);
|
$this->getLogger()->json_log($throwable);
|
||||||
|
|
||||||
if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) {
|
if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) {
|
||||||
$retryCount++;
|
$retryCount++;
|
||||||
$this->connection->connections->abandon($this->connection->getName());
|
$this->connection->connections->abandon($this->connection->getName());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->connection->release($client);
|
$this->connection->release($client);
|
||||||
|
|
||||||
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params);
|
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params);
|
||||||
|
|
||||||
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
|
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$errorMsg = 'Max retry exceeded for SQL: ' . $this->sql;
|
return $this->getLogger()->logCategory('Max retry exceeded for SQL: ' . $this->sql, 'mysql');
|
||||||
return $this->getLogger()->logCategory($errorMsg, 'mysql');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws
|
* @throws
|
||||||
@@ -161,8 +160,8 @@ class Command extends Component
|
|||||||
{
|
{
|
||||||
return (bool)$this->_prepare();
|
return (bool)$this->_prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行写入类 SQL 操作,内置断连重试机制
|
* 执行写入类 SQL 操作,内置断连重试机制
|
||||||
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
|
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
|
||||||
@@ -183,41 +182,40 @@ class Command extends Component
|
|||||||
if ($prepare->execute($this->params) === false) {
|
if ($prepare->execute($this->params) === false) {
|
||||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
|
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$prepare->closeCursor();
|
$prepare->closeCursor();
|
||||||
|
|
||||||
$result = $client->lastInsertId();
|
$result = $client->lastInsertId();
|
||||||
|
|
||||||
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
||||||
if (str_starts_with($this->sql, 'DELETE')) {
|
if (str_starts_with($this->sql, 'DELETE')) {
|
||||||
$this->connection->release($client);
|
$this->connection->release($client);
|
||||||
return $prepare->rowCount();
|
return $prepare->rowCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->connection->release($client);
|
$this->connection->release($client);
|
||||||
return $result == 0 ? $prepare->rowCount() : (int)$result;
|
return $result == 0 ? $prepare->rowCount() : (int)$result;
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
$this->getLogger()->json_log($throwable);
|
$this->getLogger()->json_log($throwable);
|
||||||
|
|
||||||
if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) {
|
if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) {
|
||||||
$retryCount++;
|
$retryCount++;
|
||||||
$this->connection->connections->abandon($this->connection->getName());
|
$this->connection->connections->abandon($this->connection->getName());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->connection->release($client);
|
$this->connection->release($client);
|
||||||
|
|
||||||
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE);
|
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE);
|
||||||
|
|
||||||
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
|
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$errorMsg = 'Max retry exceeded for SQL: ' . $this->sql;
|
return $this->getLogger()->logCategory('Max retry exceeded for SQL: ' . $this->sql, 'mysql');
|
||||||
return $this->getLogger()->logCategory($errorMsg, 'mysql');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Throwable $throwable
|
* @param Throwable $throwable
|
||||||
* @return bool
|
* @return bool
|
||||||
@@ -225,7 +223,7 @@ class Command extends Component
|
|||||||
protected function isRefresh(Throwable $throwable): bool
|
protected function isRefresh(Throwable $throwable): bool
|
||||||
{
|
{
|
||||||
$message = $throwable->getMessage();
|
$message = $throwable->getMessage();
|
||||||
|
|
||||||
// MySQL 错误处理
|
// MySQL 错误处理
|
||||||
if (str_contains($message, 'MySQL server has gone away')) {
|
if (str_contains($message, 'MySQL server has gone away')) {
|
||||||
return true;
|
return true;
|
||||||
@@ -236,7 +234,7 @@ class Command extends Component
|
|||||||
if (str_contains($message, 'Lost connection to MySQL server during query')) {
|
if (str_contains($message, 'Lost connection to MySQL server during query')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostgreSQL 错误处理
|
// PostgreSQL 错误处理
|
||||||
if (str_contains($message, 'server closed the connection unexpectedly')) {
|
if (str_contains($message, 'server closed the connection unexpectedly')) {
|
||||||
return true;
|
return true;
|
||||||
@@ -250,11 +248,11 @@ class Command extends Component
|
|||||||
if (str_contains($message, 'connection to server was lost')) {
|
if (str_contains($message, 'connection to server was lost')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws
|
* @throws
|
||||||
@@ -263,8 +261,8 @@ class Command extends Component
|
|||||||
{
|
{
|
||||||
return $this->_prepare();
|
return $this->_prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int|bool
|
* @return int|bool
|
||||||
*/
|
*/
|
||||||
@@ -272,7 +270,7 @@ class Command extends Component
|
|||||||
{
|
{
|
||||||
return $this->_prepare();
|
return $this->_prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @return $this
|
* @return $this
|
||||||
@@ -284,5 +282,5 @@ class Command extends Component
|
|||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+60
-60
@@ -154,7 +154,7 @@ class Connection extends Component
|
|||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function getName(): string
|
public function getName(): string
|
||||||
{
|
{
|
||||||
return strtolower($this->driver) . '.' . $this->cds;
|
return strtolower($this->driver) . '.' . $this->cds;
|
||||||
}
|
}
|
||||||
@@ -248,65 +248,65 @@ class Connection extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws
|
* @throws
|
||||||
* 事务回滚
|
* 事务回滚
|
||||||
*/
|
*/
|
||||||
public function rollback(): void
|
public function rollback(): void
|
||||||
{
|
{
|
||||||
$this->storey--;
|
$this->storey--;
|
||||||
if ($this->storey == 0) {
|
if ($this->storey == 0) {
|
||||||
if (!Context::exists($this->cds)) {
|
if (!Context::exists($this->cds)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$pdo = $this->getTransactionClient();
|
$pdo = $this->getTransactionClient();
|
||||||
if ($pdo->inTransaction()) {
|
if ($pdo->inTransaction()) {
|
||||||
$pdo->rollback();
|
$pdo->rollback();
|
||||||
}
|
}
|
||||||
$this->release($pdo);
|
$this->release($pdo);
|
||||||
Context::remove($this->cds);
|
Context::remove($this->cds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws
|
* @throws
|
||||||
* 事务提交
|
* 事务提交
|
||||||
*/
|
*/
|
||||||
public function commit(): void
|
public function commit(): void
|
||||||
{
|
{
|
||||||
$this->storey--;
|
$this->storey--;
|
||||||
if ($this->storey == 0) {
|
if ($this->storey == 0) {
|
||||||
if (!Context::exists($this->cds)) {
|
if (!Context::exists($this->cds)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$pdo = $this->getTransactionClient();
|
$pdo = $this->getTransactionClient();
|
||||||
if ($pdo->inTransaction()) {
|
if ($pdo->inTransaction()) {
|
||||||
$pdo->commit();
|
$pdo->commit();
|
||||||
}
|
}
|
||||||
$this->release($pdo);
|
$this->release($pdo);
|
||||||
Context::remove($this->cds);
|
Context::remove($this->cds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function clear(): void
|
public function clear(): void
|
||||||
{
|
{
|
||||||
/** @var PDO $pdo */
|
/** @var PDO $pdo */
|
||||||
$pdo = Context::get($this->cds);
|
$pdo = Context::get($this->cds);
|
||||||
if ($pdo === null) {
|
if ($pdo === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ($this->inTransaction()) {
|
if ($this->inTransaction()) {
|
||||||
$pdo->rollback();
|
$pdo->rollback();
|
||||||
}
|
}
|
||||||
$this->release($pdo);
|
$this->release($pdo);
|
||||||
Context::remove($this->cds);
|
Context::remove($this->cds);
|
||||||
$this->storey = 0;
|
$this->storey = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class DatabasesProviders extends Providers
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
foreach ($databases as $key => $database) {
|
foreach ($databases as $key => $database) {
|
||||||
$this->set($key, $this->_settings($database));
|
$this->set($key, $this->_settings($key, $database));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,15 +63,16 @@ class DatabasesProviders extends Providers
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $database
|
* @param string $key
|
||||||
* @return array
|
* @param array $database
|
||||||
*/
|
* @return array
|
||||||
private function _settings(array $database): array
|
*/
|
||||||
|
private function _settings(string $key, array $database): array
|
||||||
{
|
{
|
||||||
$clientPool = $database['pool'] ?? ['min' => 1, 'max' => 5, 'tick' => 60];
|
$clientPool = $database['pool'] ?? ['min' => 1, 'max' => 5, 'tick' => 60];
|
||||||
return [
|
return [
|
||||||
'id' => $database['id'],
|
'id' => $key,
|
||||||
'cds' => $database['cds'],
|
'cds' => $database['cds'],
|
||||||
'class' => Connection::class,
|
'class' => Connection::class,
|
||||||
'username' => $database['username'],
|
'username' => $database['username'],
|
||||||
|
|||||||
+362
-361
@@ -19,240 +19,244 @@ use Database\Traits\QueryTrait;
|
|||||||
class SqlBuilder extends Component
|
class SqlBuilder extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ActiveQuery|Query|ISqlBuilder|null
|
* @var ActiveQuery|Query|ISqlBuilder|null
|
||||||
*/
|
*/
|
||||||
public ActiveQuery|Query|ISqlBuilder|null $query;
|
public ActiveQuery|Query|ISqlBuilder|null $query;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ActiveQuery|Query|ISqlBuilder|null $config
|
* @param ActiveQuery|Query|ISqlBuilder|null $config
|
||||||
*/
|
*/
|
||||||
public function __construct(ActiveQuery|Query|null|ISqlBuilder $config)
|
public function __construct(ActiveQuery|Query|null|ISqlBuilder $config)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->query = $config;
|
$this->query = $config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ISqlBuilder|null $query
|
* @param ISqlBuilder|null $query
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public static function builder(ISqlBuilder|null $query): static
|
public static function builder(ISqlBuilder|null $query): static
|
||||||
{
|
{
|
||||||
return new static($query);
|
return new static($query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function getCondition(): string
|
public function getCondition(): string
|
||||||
{
|
{
|
||||||
return $this->where($this->query->where);
|
return $this->where($this->query->where);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $compiler
|
* @param array $compiler
|
||||||
* @return string
|
* @return string
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function hashCompiler(array $compiler): string
|
public function hashCompiler(array $compiler): string
|
||||||
{
|
{
|
||||||
return $this->where($compiler);
|
return $this->where($compiler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @return bool|array
|
* @return bool|array
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function update(array $attributes): bool|string
|
public function update(array $attributes): bool|string
|
||||||
{
|
{
|
||||||
$conditions = $this->query;
|
$params = $this->query->params;
|
||||||
$this->query = [];
|
$this->query->params = [];
|
||||||
$data = $this->__updateBuilder($this->makeParams($attributes));
|
$array = $this->makeParams($attributes);
|
||||||
foreach ($conditions as $condition) {
|
foreach ($params as $name => $value) {
|
||||||
$this->query->pushParam($condition);
|
$this->query->pushParam($value);
|
||||||
}
|
}
|
||||||
return $data;
|
return $this->__updateBuilder($array);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @param string $opera
|
* @param string $opera
|
||||||
* @return bool|array
|
* @return bool|array
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function mathematics(array $attributes, string $opera = '+'): bool|string
|
public function mathematics(array $attributes, string $opera = '+'): bool|string
|
||||||
{
|
{
|
||||||
$string = [];
|
$string = [];
|
||||||
foreach ($attributes as $attribute => $value) {
|
foreach ($attributes as $attribute => $value) {
|
||||||
$string[] = $attribute . '=' . $attribute . $opera . $value;
|
$string[] = $attribute . '=' . $attribute . $opera . $value;
|
||||||
}
|
}
|
||||||
return $this->__updateBuilder($string);
|
return $this->__updateBuilder($string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $string
|
* @param array $string
|
||||||
* @return string|bool
|
* @return string|bool
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
private function __updateBuilder(array $string): string|bool
|
private function __updateBuilder(array $string): string|bool
|
||||||
{
|
{
|
||||||
if (empty($string)) {
|
if (empty($string)) {
|
||||||
return Kiri::getLogger()->logCategory('None data update.');
|
return Kiri::getLogger()->logCategory('None data update.');
|
||||||
}
|
}
|
||||||
return 'UPDATE ' . $this->getFromAlias() . ' SET ' . implode(',', $string) . $this->make();
|
|
||||||
}
|
return 'UPDATE ' . $this->getFromAlias() . ' ' . 'SET' . ' ' . implode(', ', $string) . ' ' . $this->make();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @param false $isBatch
|
* @param false $isBatch
|
||||||
* @return array
|
* @return array
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function insert(array $attributes, bool $isBatch = false): string
|
public function insert(array $attributes, bool $isBatch = false): string
|
||||||
{
|
{
|
||||||
$update = 'INSERT INTO ' . $this->getFromAlias();
|
$update = 'INSERT INTO ' . $this->getFromAlias();
|
||||||
if ($isBatch === false) {
|
if ($isBatch === false) {
|
||||||
$attributes = [$attributes];
|
$attributes = [$attributes];
|
||||||
}
|
}
|
||||||
$update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES ';
|
$update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES ';
|
||||||
|
|
||||||
$keys = [];
|
$keys = [];
|
||||||
foreach ($attributes as $attribute) {
|
foreach ($attributes as $attribute) {
|
||||||
$_keys = $this->makeParams($attribute, true);
|
$_keys = $this->makeParams($attribute, true);
|
||||||
|
|
||||||
$keys[] = implode(',', $_keys);
|
$keys[] = implode(',', $_keys);
|
||||||
}
|
}
|
||||||
return $update . '(' . implode('),(', $keys) . ')';
|
return $update . '(' . implode('),(', $keys) . ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function delete(): string
|
public function delete(): string
|
||||||
{
|
{
|
||||||
return 'DELETE FROM ' . $this->getFromAlias() . $this->make();
|
return 'DELETE FROM ' .
|
||||||
}
|
$this->getFromAlias() . ' ' .
|
||||||
|
$this->make();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $attributes
|
* @param $attributes
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
#[Pure] private function getFields(array $attributes): array
|
#[Pure]
|
||||||
{
|
private function getFields(array $attributes): array
|
||||||
return array_keys(current($attributes));
|
{
|
||||||
}
|
return array_keys(current($attributes));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @param bool $isInsert
|
* @param bool $isInsert
|
||||||
* @return array[]
|
* @return array[]
|
||||||
* a=:b,
|
* a=:b,
|
||||||
*/
|
*/
|
||||||
private function makeParams(array $attributes, bool $isInsert = false): array
|
private function makeParams(array $attributes, bool $isInsert = false): array
|
||||||
{
|
{
|
||||||
$keys = [];
|
$keys = [];
|
||||||
foreach ($attributes as $key => $value) {
|
foreach ($attributes as $key => $value) {
|
||||||
if ($isInsert === true) {
|
if ($isInsert === true) {
|
||||||
$keys[] = '?';
|
$keys[] = '?';
|
||||||
$this->query->pushParam($value);
|
$this->query->pushParam($value);
|
||||||
} else {
|
} else {
|
||||||
$keys = $this->resolveParams($key, $value, $keys);
|
$keys = $this->resolveParams($key, $value, $keys);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $keys;
|
return $keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $keys
|
* @param array $keys
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function resolveParams(string $key, mixed $value, array $keys): array
|
private function resolveParams(string $key, mixed $value, array $keys): array
|
||||||
{
|
{
|
||||||
if (is_null($value)) {
|
if (is_null($value)) {
|
||||||
return $keys;
|
return $keys;
|
||||||
}
|
}
|
||||||
if (preg_match('/^[+|-]\s\d+$/', (string)$value)) {
|
if (preg_match('/^[+|-]\s\d+$/', (string)$value)) {
|
||||||
$keys[] = $key . '=' . $key . ' ' . $value;
|
$keys[] = $key . '=' . $key . ' ' . $value;
|
||||||
} else {
|
} else {
|
||||||
$this->query->pushParam($value);
|
$this->query->pushParam($value);
|
||||||
$keys[] = $key . '= ?';
|
$keys[] = $key . '= ?';
|
||||||
}
|
}
|
||||||
return $keys;
|
return $keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function one(): string
|
public function one(): string
|
||||||
{
|
{
|
||||||
$this->query->offset(0)->limit(1);
|
$this->query->offset(0)->limit(1);
|
||||||
return $this->makeSelect($this->query) . $this->make() . $this->makeLimit();
|
return $this->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function all(): string
|
public function all(): string
|
||||||
{
|
{
|
||||||
return $this->makeSelect($this->query) . $this->make() . $this->makeLimit();
|
return $this->makeSelect($this->query) . ' ' . $this->make() . ' ' . $this->makeLimit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function count(): string
|
public function count(): string
|
||||||
{
|
{
|
||||||
return $this->makeSelect(['COUNT(*)']) . $this->make();
|
return $this->makeSelect(['COUNT(*)']) . ' ' . $this->make();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function exists(): string
|
public function exists(): string
|
||||||
{
|
{
|
||||||
return $this->makeSelect(['0']) . $this->make();
|
return $this->makeSelect(['0']) . ' ' . $this->make();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @return string
|
* @return string
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function columns(string $table): string
|
public function columns(string $table): string
|
||||||
{
|
{
|
||||||
$driver = $this->getDriver();
|
$driver = $this->getDriver();
|
||||||
if (in_array($driver, ['pgsql', 'postgresql'])) {
|
if (in_array($driver, ['pgsql', 'postgresql'])) {
|
||||||
$tableName = trim($table, '"`');
|
$tableName = trim($table, '"`');
|
||||||
return "SELECT
|
return "SELECT
|
||||||
column_name as Field,
|
column_name as Field,
|
||||||
data_type as Type,
|
data_type as Type,
|
||||||
is_nullable as Null,
|
is_nullable as Null,
|
||||||
@@ -262,194 +266,191 @@ class SqlBuilder extends Component
|
|||||||
FROM information_schema.columns
|
FROM information_schema.columns
|
||||||
WHERE table_name = '$tableName'
|
WHERE table_name = '$tableName'
|
||||||
ORDER BY ordinal_position";
|
ORDER BY ordinal_position";
|
||||||
}
|
}
|
||||||
return 'SHOW FULL FIELDS FROM ' . $table;
|
return 'SHOW FULL FIELDS FROM ' . $table;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
private function make(): string
|
private function make(): string
|
||||||
{
|
{
|
||||||
$select = $this->makeCondition();
|
return $this->makeCondition() . ' ' . $this->makeGroup() . ' ' . $this->makeOrder();
|
||||||
$select .= $this->makeGroup();
|
}
|
||||||
$select .= $this->makeOrder();
|
|
||||||
return $select;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $select 列名数组或 QueryTrait 对象
|
* @param mixed $select 列名数组或 QueryTrait 对象
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function makeSelect(mixed $select = ['*']): string
|
private function makeSelect(mixed $select = ['*']): string
|
||||||
{
|
{
|
||||||
if (!is_array($select)) {
|
if (!is_array($select)) {
|
||||||
$select = ['*'];
|
$select = ['*'];
|
||||||
}
|
}
|
||||||
$sql = "SELECT " . implode(',', $select) . " FROM " . $this->getFromAlias();
|
$sql = 'SELECT ' . implode(',', $select) . ' FROM ' . $this->getFromAlias();
|
||||||
if ($this->query->join !== []) {
|
if ($this->query->join !== []) {
|
||||||
$sql .= ' ' . implode(' ', $this->query->join);
|
$sql .= ' ' . implode(' ', $this->query->join);
|
||||||
}
|
}
|
||||||
return $sql;
|
return $sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function makeGroup(): string
|
private function makeGroup(): string
|
||||||
{
|
{
|
||||||
if ($this->query->group !== '' && $this->query->group !== '0') {
|
if ($this->query->group !== '' && $this->query->group !== '0') {
|
||||||
return ' GROUP BY ' . $this->query->group;
|
return 'GROUP BY ' . $this->query->group;
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function makeOrder(): string
|
private function makeOrder(): string
|
||||||
{
|
{
|
||||||
if (count($this->query->order) > 0) {
|
if (count($this->query->order) > 0) {
|
||||||
return ' ORDER BY ' . implode(',', $this->query->order);
|
return 'ORDER BY ' . implode(',', $this->query->order);
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function makeCondition(): string
|
private function makeCondition(): string
|
||||||
{
|
{
|
||||||
$condition = $this->where($this->query->where);
|
$condition = $this->where($this->query->where);
|
||||||
if (empty($condition)) {
|
if (empty($condition)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
return ' WHERE ' . $condition;
|
return 'WHERE ' . $condition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
private function makeLimit(): string
|
private function makeLimit(): string
|
||||||
{
|
{
|
||||||
if ($this->query->offset >= 0 && $this->query->limit >= 1) {
|
if ($this->query->offset >= 0 && $this->query->limit >= 1) {
|
||||||
$driver = $this->getDriver();
|
$driver = $this->getDriver();
|
||||||
if (in_array($driver, ['pgsql', 'postgresql'])) {
|
if (in_array($driver, ['pgsql', 'postgresql'])) {
|
||||||
return ' LIMIT ' . $this->query->limit . ' OFFSET ' . $this->query->offset;
|
return 'LIMIT ' . $this->query->limit . ' OFFSET ' . $this->query->offset;
|
||||||
}
|
}
|
||||||
return ' LIMIT ' . $this->query->offset . ',' . $this->query->limit;
|
return 'LIMIT ' . $this->query->offset . ',' . $this->query->limit;
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param false $isCount
|
* @param false $isCount
|
||||||
* @return string
|
* @return string
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function get(bool $isCount = false): string
|
public function get(bool $isCount = false): string
|
||||||
{
|
{
|
||||||
if ($isCount === false) {
|
if ($isCount === false) {
|
||||||
return $this->all();
|
return $this->all();
|
||||||
}
|
}
|
||||||
return $this->count();
|
return $this->count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function truncate(): string
|
public function truncate(): string
|
||||||
{
|
{
|
||||||
return sprintf('TRUNCATE %s', $this->getFromAlias());
|
return sprintf('TRUNCATE %s', $this->getFromAlias());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $where
|
* @param array $where
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function where(array $where): string
|
private function where(array $where): string
|
||||||
{
|
{
|
||||||
if (count($where) < 1) {
|
if (count($where) < 1) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
$_tmp = [];
|
$_tmp = [];
|
||||||
foreach ($where as $key => $value) {
|
foreach ($where as $key => $value) {
|
||||||
$_tmp[] = $this->resolveCondition($key, $value);
|
$_tmp[] = $this->resolveCondition($key, $value);
|
||||||
}
|
}
|
||||||
return implode(' AND ', $_tmp);
|
return implode(' AND ', $_tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $condition
|
* @param mixed $condition
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function resolveCondition(string|int $field, mixed $condition): string
|
private function resolveCondition(string|int $field, mixed $condition): string
|
||||||
{
|
{
|
||||||
if (is_string($field)) {
|
if (is_string($field)) {
|
||||||
$this->query->pushParam($condition);
|
$this->query->pushParam($condition);
|
||||||
return $field . ' = ?';
|
return $field . ' = ?';
|
||||||
} else if (is_string($condition)) {
|
} else if (is_string($condition)) {
|
||||||
return $condition;
|
return $condition;
|
||||||
} else {
|
} else {
|
||||||
return implode(' AND ', $this->_hashMap($condition));
|
return implode(' AND ', $this->_hashMap($condition));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $condition
|
* @param $condition
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function _hashMap(array $condition): array
|
private function _hashMap(array $condition): array
|
||||||
{
|
{
|
||||||
$_array = [];
|
$_array = [];
|
||||||
foreach ($condition as $key => $value) {
|
foreach ($condition as $key => $value) {
|
||||||
$this->query->pushParam($value);
|
$this->query->pushParam($value);
|
||||||
$_array[] = $key . '= ?';
|
$_array[] = $key . '= ?';
|
||||||
}
|
}
|
||||||
return $_array;
|
return $_array;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取数据库驱动类型
|
* 获取数据库驱动类型
|
||||||
* @return string
|
* @return string
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
private function getDriver(): string
|
private function getDriver(): string
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if ($this->query instanceof ActiveQuery && $this->query->modelClass !== null) {
|
if ($this->query instanceof ActiveQuery && $this->query->modelClass !== null) {
|
||||||
if ($this->query->modelClass instanceof Model) {
|
if ($this->query->modelClass instanceof Model) {
|
||||||
$connection = $this->query->modelClass->getConnection();
|
$connection = $this->query->modelClass->getConnection();
|
||||||
if ($connection instanceof Connection) {
|
if ($connection instanceof Connection) {
|
||||||
return strtolower($connection->driver ?? 'mysql');
|
return strtolower($connection->driver ?? 'mysql');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (method_exists($this->query, 'getConnection')) {
|
if (method_exists($this->query, 'getConnection')) {
|
||||||
$connection = $this->query->getConnection();
|
$connection = $this->query->getConnection();
|
||||||
if ($connection instanceof Connection) {
|
if ($connection instanceof Connection) {
|
||||||
return strtolower($connection->driver ?? 'mysql');
|
return strtolower($connection->driver ?? 'mysql');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
$this->getLogger()->json_log($e);
|
$this->getLogger()->json_log($e);
|
||||||
}
|
}
|
||||||
return 'mysql';
|
return 'mysql';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -116,6 +116,18 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function __get(string $name)
|
||||||
|
{
|
||||||
|
if (property_exists($this, $name)) {
|
||||||
|
return $this->$name;
|
||||||
|
}
|
||||||
|
if (method_exists($this, $name)) {
|
||||||
|
return $this->$name();
|
||||||
|
}
|
||||||
|
return parent::__get($name); // TODO: Change the autogenerated stub
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Comply constructor.
|
* Comply constructor.
|
||||||
* @throws
|
* @throws
|
||||||
|
|||||||
Reference in New Issue
Block a user