Compare commits
36 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 | |||
| 06074c38c1 | |||
| 404b511c5f | |||
| bf4f8538ed | |||
| 8a985a65df | |||
| c7b980d969 | |||
| fea649413c | |||
| 959407a95e | |||
| 0abc7c32f7 | |||
| 9c6340d0b3 | |||
| 0f01416351 | |||
| b1d2c0fb51 | |||
| db18a2d73a | |||
| 3cb3dcf384 | |||
| fd8652ccd1 | |||
| 279151825a |
+1
-1
@@ -288,6 +288,6 @@ class ActiveQuery extends QueryTrait implements ISqlBuilder
|
|||||||
*/
|
*/
|
||||||
public function delete(): bool
|
public function delete(): bool
|
||||||
{
|
{
|
||||||
return $this->buildCommand($this->builder->delete())->delete();
|
return (bool)$this->buildCommand($this->builder->delete())->exec();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+843
-835
File diff suppressed because it is too large
Load Diff
+286
-256
@@ -1,256 +1,286 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Created by PhpStorm.
|
* Created by PhpStorm.
|
||||||
* User: whwyy
|
* User: whwyy
|
||||||
* Date: 2018/3/30 0030
|
* Date: 2018/3/30 0030
|
||||||
* Time: 15:23
|
* Time: 15:23
|
||||||
*/
|
*/
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Di\Container;
|
use Kiri\Di\Container;
|
||||||
use PDO;
|
use PDO;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Command
|
* Class Command
|
||||||
* @package Database
|
* @package Database
|
||||||
*/
|
*/
|
||||||
class Command extends Component
|
class Command extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
public Connection $connection;
|
/** @var int 数据库操作最大重试次数 */
|
||||||
public ?string $sql = '';
|
private const MAX_RETRIES = 2;
|
||||||
public array $params = [];
|
|
||||||
|
public Connection $connection;
|
||||||
/**
|
public ?string $sql = '';
|
||||||
* @param array $params
|
public array $params = [];
|
||||||
* @throws
|
|
||||||
*/
|
/**
|
||||||
public function __construct(array $params = [])
|
* @param array $params
|
||||||
{
|
* @throws
|
||||||
parent::__construct();
|
*/
|
||||||
Container::configure($this, $params);
|
public function __construct(array $params = [])
|
||||||
}
|
{
|
||||||
|
parent::__construct();
|
||||||
|
Container::configure($this, $params);
|
||||||
/**
|
}
|
||||||
* @return bool
|
|
||||||
* @throws
|
|
||||||
*/
|
/**
|
||||||
public function incrOrDecr(): bool
|
* @return bool
|
||||||
{
|
* @throws
|
||||||
return (bool)$this->_prepare();
|
*/
|
||||||
}
|
public function incrOrDecr(): bool
|
||||||
|
{
|
||||||
|
return (bool)$this->_prepare();
|
||||||
/**
|
}
|
||||||
* @return bool|array
|
|
||||||
* @throws
|
|
||||||
*/
|
/**
|
||||||
public function all(): bool|array
|
* @return bool|array
|
||||||
{
|
* @throws
|
||||||
return $this->search('fetchAll');
|
*/
|
||||||
}
|
public function all(): bool|array
|
||||||
|
{
|
||||||
/**
|
return $this->search('fetchAll');
|
||||||
* @return array|bool|null
|
}
|
||||||
* @throws
|
|
||||||
*/
|
/**
|
||||||
public function one(): array|null|bool
|
* @return array|bool|null
|
||||||
{
|
* @throws
|
||||||
return $this->search('fetch');
|
*/
|
||||||
}
|
public function one(): array|null|bool
|
||||||
|
{
|
||||||
/**
|
return $this->search('fetch');
|
||||||
* @return bool
|
}
|
||||||
* @throws Exception
|
|
||||||
*/
|
/**
|
||||||
public function exists(): bool
|
* @return bool
|
||||||
{
|
* @throws Exception
|
||||||
$data = $this->search('fetch');
|
*/
|
||||||
if (!$data) {
|
public function exists(): bool
|
||||||
return false;
|
{
|
||||||
}
|
$data = $this->search('fetch');
|
||||||
return true;
|
if (!$data) {
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
/**
|
return true;
|
||||||
* @return mixed
|
}
|
||||||
* @throws
|
|
||||||
*/
|
/**
|
||||||
public function fetchColumn(): mixed
|
* @return mixed
|
||||||
{
|
* @throws
|
||||||
return $this->search('fetchColumn');
|
*/
|
||||||
}
|
public function fetchColumn(): mixed
|
||||||
|
{
|
||||||
/**
|
return $this->search('fetchColumn');
|
||||||
* @return mixed
|
}
|
||||||
* @throws
|
|
||||||
*/
|
/**
|
||||||
public function rowCount(): int
|
* @return mixed
|
||||||
{
|
* @throws
|
||||||
$data = $this->search('fetch');
|
*/
|
||||||
|
public function rowCount(): int
|
||||||
return !$data ? 0 : +current($data);
|
{
|
||||||
}
|
$data = $this->search('fetch');
|
||||||
|
|
||||||
|
return !$data ? 0 : +current($data);
|
||||||
/**
|
}
|
||||||
* @param string $method
|
|
||||||
* @return mixed
|
|
||||||
* @throws
|
/**
|
||||||
*/
|
* 执行查询类 SQL 操作,内置断连重试机制
|
||||||
protected function search(string $method): mixed
|
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
|
||||||
{
|
* @param string $method PDO 结果获取方法名
|
||||||
$client = $this->connection->getConnection();
|
* @return mixed
|
||||||
try {
|
* @throws
|
||||||
$startTime = microtime(true);
|
*/
|
||||||
if (($prepare = $client->prepare($this->sql)) === false) {
|
protected function search(string $method): mixed
|
||||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
|
{
|
||||||
}
|
$retryCount = 0;
|
||||||
|
|
||||||
$prepare->execute($this->params);
|
while ($retryCount <= self::MAX_RETRIES) {
|
||||||
|
$client = $this->connection->getConnection();
|
||||||
$result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC);
|
try {
|
||||||
$prepare->closeCursor();
|
$startTime = microtime(true);
|
||||||
|
if (($prepare = $client->prepare($this->sql)) === false) {
|
||||||
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
|
||||||
|
}
|
||||||
return $result;
|
|
||||||
} catch (Throwable $throwable) {
|
$prepare->execute($this->params);
|
||||||
$this->getLogger()->json_log($throwable);
|
|
||||||
|
$result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC);
|
||||||
if ($this->isRefresh($throwable)) {
|
$prepare->closeCursor();
|
||||||
return $this->search($method);
|
|
||||||
}
|
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
||||||
|
|
||||||
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params);
|
$this->connection->release($client);
|
||||||
|
return $result;
|
||||||
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
|
} catch (Throwable $throwable) {
|
||||||
} finally {
|
$this->getLogger()->json_log($throwable);
|
||||||
$this->connection->release($client);
|
|
||||||
}
|
if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) {
|
||||||
}
|
$retryCount++;
|
||||||
|
$this->connection->connections->abandon($this->connection->getName());
|
||||||
|
continue;
|
||||||
/**
|
}
|
||||||
* @return bool
|
|
||||||
* @throws
|
$this->connection->release($client);
|
||||||
*/
|
|
||||||
public function flush(): bool
|
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params);
|
||||||
{
|
|
||||||
return (bool)$this->_prepare();
|
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
return $this->getLogger()->logCategory('Max retry exceeded for SQL: ' . $this->sql, 'mysql');
|
||||||
* @return int|bool
|
}
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
private function _prepare(): int|bool
|
/**
|
||||||
{
|
* @return bool
|
||||||
$client = $this->connection->getConnection();
|
* @throws
|
||||||
try {
|
*/
|
||||||
$startTime = microtime(true);
|
public function flush(): bool
|
||||||
if (($prepare = $client->prepare($this->sql)) === false) {
|
{
|
||||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
|
return (bool)$this->_prepare();
|
||||||
}
|
}
|
||||||
if ($prepare->execute($this->params) === false) {
|
|
||||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
|
|
||||||
}
|
/**
|
||||||
$prepare->closeCursor();
|
* 执行写入类 SQL 操作,内置断连重试机制
|
||||||
|
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
|
||||||
$result = $client->lastInsertId();
|
* @return int|bool
|
||||||
|
* @throws
|
||||||
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
*/
|
||||||
|
private function _prepare(): int|bool
|
||||||
return $result == 0 ? $prepare->rowCount() : (int)$result;
|
{
|
||||||
} catch (Throwable $throwable) {
|
$retryCount = 0;
|
||||||
$this->getLogger()->json_log($throwable);
|
|
||||||
|
while ($retryCount <= self::MAX_RETRIES) {
|
||||||
if ($this->isRefresh($throwable)) {
|
$client = $this->connection->getConnection();
|
||||||
return $this->_prepare();
|
try {
|
||||||
}
|
$startTime = microtime(true);
|
||||||
|
if (($prepare = $client->prepare($this->sql)) === false) {
|
||||||
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE);
|
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
|
||||||
|
}
|
||||||
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
|
if ($prepare->execute($this->params) === false) {
|
||||||
} finally {
|
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
|
||||||
$this->connection->release($client);
|
}
|
||||||
}
|
|
||||||
}
|
$prepare->closeCursor();
|
||||||
|
|
||||||
|
$result = $client->lastInsertId();
|
||||||
/**
|
|
||||||
* @param Throwable $throwable
|
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
||||||
* @return bool
|
if (str_starts_with($this->sql, 'DELETE')) {
|
||||||
*/
|
$this->connection->release($client);
|
||||||
protected function isRefresh(Throwable $throwable): bool
|
return $prepare->rowCount();
|
||||||
{
|
}
|
||||||
$message = $throwable->getMessage();
|
|
||||||
|
$this->connection->release($client);
|
||||||
// MySQL 错误处理
|
return $result == 0 ? $prepare->rowCount() : (int)$result;
|
||||||
if (str_contains($message, 'MySQL server has gone away')) {
|
} catch (Throwable $throwable) {
|
||||||
return true;
|
$this->getLogger()->json_log($throwable);
|
||||||
}
|
|
||||||
if (str_contains($message, 'Send of 14 bytes failed with errno=32 Broken pipe')) {
|
if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) {
|
||||||
return true;
|
$retryCount++;
|
||||||
}
|
$this->connection->connections->abandon($this->connection->getName());
|
||||||
if (str_contains($message, 'Lost connection to MySQL server during query')) {
|
continue;
|
||||||
return true;
|
}
|
||||||
}
|
|
||||||
|
$this->connection->release($client);
|
||||||
// PostgreSQL 错误处理
|
|
||||||
if (str_contains($message, 'server closed the connection unexpectedly')) {
|
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE);
|
||||||
return true;
|
|
||||||
}
|
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
|
||||||
if (str_contains($message, 'Connection refused')) {
|
}
|
||||||
return true;
|
}
|
||||||
}
|
|
||||||
if (str_contains($message, 'Broken pipe')) {
|
return $this->getLogger()->logCategory('Max retry exceeded for SQL: ' . $this->sql, 'mysql');
|
||||||
return true;
|
}
|
||||||
}
|
|
||||||
if (str_contains($message, 'connection to server was lost')) {
|
|
||||||
return true;
|
/**
|
||||||
}
|
* @param Throwable $throwable
|
||||||
|
* @return bool
|
||||||
return false;
|
*/
|
||||||
}
|
protected function isRefresh(Throwable $throwable): bool
|
||||||
|
{
|
||||||
|
$message = $throwable->getMessage();
|
||||||
/**
|
|
||||||
* @return bool
|
// MySQL 错误处理
|
||||||
* @throws
|
if (str_contains($message, 'MySQL server has gone away')) {
|
||||||
*/
|
return true;
|
||||||
public function delete(): bool
|
}
|
||||||
{
|
if (str_contains($message, 'Send of 14 bytes failed with errno=32 Broken pipe')) {
|
||||||
return (bool)$this->_prepare();
|
return true;
|
||||||
}
|
}
|
||||||
|
if (str_contains($message, 'Lost connection to MySQL server during query')) {
|
||||||
|
return true;
|
||||||
/**
|
}
|
||||||
* @return int|bool
|
|
||||||
*/
|
// PostgreSQL 错误处理
|
||||||
public function exec(): int|bool
|
if (str_contains($message, 'server closed the connection unexpectedly')) {
|
||||||
{
|
return true;
|
||||||
return $this->_prepare();
|
}
|
||||||
}
|
if (str_contains($message, 'Connection refused')) {
|
||||||
|
return true;
|
||||||
/**
|
}
|
||||||
* @param array $data
|
if (str_contains($message, 'Broken pipe')) {
|
||||||
* @return $this
|
return true;
|
||||||
*/
|
}
|
||||||
public function bindValues(array $data = []): static
|
if (str_contains($message, 'connection to server was lost')) {
|
||||||
{
|
return true;
|
||||||
if (count($data) > 0) {
|
}
|
||||||
$this->params = array_merge($this->params, $data);
|
|
||||||
}
|
return false;
|
||||||
return $this;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
/**
|
||||||
|
* @return bool
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function delete(): bool
|
||||||
|
{
|
||||||
|
return $this->_prepare();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int|bool
|
||||||
|
*/
|
||||||
|
public function exec(): int|bool
|
||||||
|
{
|
||||||
|
return $this->_prepare();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function bindValues(array $data = []): static
|
||||||
|
{
|
||||||
|
if (count($data) > 0) {
|
||||||
|
$this->params = array_merge($this->params, $data);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
+5
-1
@@ -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;
|
||||||
}
|
}
|
||||||
@@ -264,6 +264,7 @@ class Connection extends Component
|
|||||||
$pdo->rollback();
|
$pdo->rollback();
|
||||||
}
|
}
|
||||||
$this->release($pdo);
|
$this->release($pdo);
|
||||||
|
Context::remove($this->cds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -283,6 +284,7 @@ class Connection extends Component
|
|||||||
$pdo->commit();
|
$pdo->commit();
|
||||||
}
|
}
|
||||||
$this->release($pdo);
|
$this->release($pdo);
|
||||||
|
Context::remove($this->cds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,6 +304,8 @@ class Connection extends Component
|
|||||||
$pdo->rollback();
|
$pdo->rollback();
|
||||||
}
|
}
|
||||||
$this->release($pdo);
|
$this->release($pdo);
|
||||||
|
Context::remove($this->cds);
|
||||||
|
$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'],
|
||||||
|
|||||||
@@ -285,6 +285,7 @@ class Model extends Base\Model
|
|||||||
|
|
||||||
$primaryKey = str_replace('\\', '_', $modelName) . '_' . $foreignKey . '_' . $value;
|
$primaryKey = str_replace('\\', '_', $modelName) . '_' . $foreignKey . '_' . $value;
|
||||||
if (!$relation->hasIdentification($primaryKey)) {
|
if (!$relation->hasIdentification($primaryKey)) {
|
||||||
|
/** @var $modelName ModelInterface */
|
||||||
$relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value]));
|
$relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value]));
|
||||||
}
|
}
|
||||||
return $primaryKey;
|
return $primaryKey;
|
||||||
@@ -346,6 +347,7 @@ class Model extends Base\Model
|
|||||||
|
|
||||||
$primaryKey = str_replace('\\', '_', $modelName) . '_' . $foreignKey . '_' . implode('_', $value);
|
$primaryKey = str_replace('\\', '_', $modelName) . '_' . $foreignKey . '_' . implode('_', $value);
|
||||||
if (!$relation->hasIdentification($primaryKey)) {
|
if (!$relation->hasIdentification($primaryKey)) {
|
||||||
|
/** @var $modelName ModelInterface */
|
||||||
$relation->bindIdentification($primaryKey, $modelName::query()->whereIn($foreignKey, $value));
|
$relation->bindIdentification($primaryKey, $modelName::query()->whereIn($foreignKey, $value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+471
-462
@@ -1,462 +1,471 @@
|
|||||||
<?php
|
<?php
|
||||||
/** @noinspection ALL */
|
/** @noinspection ALL */
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
|
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Kiri;
|
use Kiri;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
|
use Database\Traits\QueryTrait;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class SqlBuilder
|
/**
|
||||||
* @package Database
|
* Class SqlBuilder
|
||||||
*/
|
* @package Database
|
||||||
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
|
/**
|
||||||
* @return $this
|
* @param ISqlBuilder|null $query
|
||||||
* @throws
|
* @return $this
|
||||||
*/
|
* @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
|
/**
|
||||||
* @throws
|
* @return string
|
||||||
*/
|
* @throws
|
||||||
public function getCondition(): string
|
*/
|
||||||
{
|
public function getCondition(): string
|
||||||
return $this->where($this->query->getWhere());
|
{
|
||||||
}
|
return $this->where($this->query->where);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $compiler
|
/**
|
||||||
* @return string
|
* @param array $compiler
|
||||||
* @throws
|
* @return string
|
||||||
*/
|
* @throws
|
||||||
public function hashCompiler(array $compiler): string
|
*/
|
||||||
{
|
public function hashCompiler(array $compiler): string
|
||||||
return $this->where($compiler);
|
{
|
||||||
}
|
return $this->where($compiler);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $attributes
|
/**
|
||||||
* @return bool|array
|
* @param array $attributes
|
||||||
* @throws
|
* @return bool|array
|
||||||
*/
|
* @throws
|
||||||
public function update(array $attributes): bool|string
|
*/
|
||||||
{
|
public function update(array $attributes): bool|string
|
||||||
$conditions = $this->query->getParams();
|
{
|
||||||
$this->query->setParams([]);
|
$params = $this->query->params;
|
||||||
$data = $this->__updateBuilder($this->makeParams($attributes));
|
$this->query->params = [];
|
||||||
foreach ($conditions as $condition) {
|
$array = $this->makeParams($attributes);
|
||||||
$this->query->pushParam($condition);
|
foreach ($params as $name => $value) {
|
||||||
}
|
$this->query->pushParam($value);
|
||||||
return $data;
|
}
|
||||||
}
|
return $this->__updateBuilder($array);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $attributes
|
/**
|
||||||
* @param string $opera
|
* @param array $attributes
|
||||||
* @return bool|array
|
* @param string $opera
|
||||||
* @throws
|
* @return bool|array
|
||||||
*/
|
* @throws
|
||||||
public function mathematics(array $attributes, string $opera = '+'): bool|string
|
*/
|
||||||
{
|
public function mathematics(array $attributes, string $opera = '+'): bool|string
|
||||||
$string = [];
|
{
|
||||||
foreach ($attributes as $attribute => $value) {
|
$string = [];
|
||||||
$string[] = $attribute . '=' . $attribute . $opera . $value;
|
foreach ($attributes as $attribute => $value) {
|
||||||
}
|
$string[] = $attribute . '=' . $attribute . $opera . $value;
|
||||||
return $this->__updateBuilder($string);
|
}
|
||||||
}
|
return $this->__updateBuilder($string);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $string
|
/**
|
||||||
* @return string|bool
|
* @param array $string
|
||||||
* @throws
|
* @return string|bool
|
||||||
*/
|
* @throws
|
||||||
private function __updateBuilder(array $string): string|bool
|
*/
|
||||||
{
|
private function __updateBuilder(array $string): string|bool
|
||||||
if (empty($string)) {
|
{
|
||||||
return Kiri::getLogger()->logCategory('None data update.');
|
if (empty($string)) {
|
||||||
}
|
return Kiri::getLogger()->logCategory('None data update.');
|
||||||
return 'UPDATE ' . $this->query->getFrom() . ' SET ' . implode(',', $string) . $this->make();
|
}
|
||||||
}
|
|
||||||
|
return 'UPDATE ' . $this->getFromAlias() . ' ' . 'SET' . ' ' . implode(', ', $string) . ' ' . $this->make();
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @param array $attributes
|
|
||||||
* @param false $isBatch
|
/**
|
||||||
* @return array
|
* @param array $attributes
|
||||||
* @throws
|
* @param false $isBatch
|
||||||
*/
|
* @return array
|
||||||
public function insert(array $attributes, bool $isBatch = false): string
|
* @throws
|
||||||
{
|
*/
|
||||||
$update = 'INSERT INTO ' . $this->query->getFrom();
|
public function insert(array $attributes, bool $isBatch = false): string
|
||||||
if ($isBatch === false) {
|
{
|
||||||
$attributes = [$attributes];
|
$update = 'INSERT INTO ' . $this->getFromAlias();
|
||||||
}
|
if ($isBatch === false) {
|
||||||
$update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES ';
|
$attributes = [$attributes];
|
||||||
|
}
|
||||||
$keys = [];
|
$update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES ';
|
||||||
foreach ($attributes as $attribute) {
|
|
||||||
$_keys = $this->makeParams($attribute, true);
|
$keys = [];
|
||||||
|
foreach ($attributes as $attribute) {
|
||||||
$keys[] = implode(',', $_keys);
|
$_keys = $this->makeParams($attribute, true);
|
||||||
}
|
|
||||||
return $update . '(' . implode('),(', $keys) . ')';
|
$keys[] = implode(',', $_keys);
|
||||||
}
|
}
|
||||||
|
return $update . '(' . implode('),(', $keys) . ')';
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
* @throws
|
/**
|
||||||
*/
|
* @return string
|
||||||
public function delete(): string
|
* @throws
|
||||||
{
|
*/
|
||||||
return 'DELETE FROM ' . $this->query->getFrom() . $this->make();
|
public function delete(): string
|
||||||
}
|
{
|
||||||
|
return 'DELETE FROM ' .
|
||||||
|
$this->getFromAlias() . ' ' .
|
||||||
/**
|
$this->make();
|
||||||
* @param $attributes
|
}
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
#[Pure] private function getFields(array $attributes): array
|
/**
|
||||||
{
|
* @param $attributes
|
||||||
return array_keys(current($attributes));
|
* @return array
|
||||||
}
|
*/
|
||||||
|
#[Pure]
|
||||||
|
private function getFields(array $attributes): array
|
||||||
/**
|
{
|
||||||
* @param array $attributes
|
return array_keys(current($attributes));
|
||||||
* @param bool $isInsert
|
}
|
||||||
* @return array[]
|
|
||||||
* a=:b,
|
|
||||||
*/
|
/**
|
||||||
private function makeParams(array $attributes, bool $isInsert = false): array
|
* @param array $attributes
|
||||||
{
|
* @param bool $isInsert
|
||||||
$keys = [];
|
* @return array[]
|
||||||
foreach ($attributes as $key => $value) {
|
* a=:b,
|
||||||
if ($isInsert === true) {
|
*/
|
||||||
$keys[] = '?';
|
private function makeParams(array $attributes, bool $isInsert = false): array
|
||||||
$this->query->pushParam($value);
|
{
|
||||||
} else {
|
$keys = [];
|
||||||
$keys = $this->resolveParams($key, $value, $keys);
|
foreach ($attributes as $key => $value) {
|
||||||
}
|
if ($isInsert === true) {
|
||||||
}
|
$keys[] = '?';
|
||||||
return $keys;
|
$this->query->pushParam($value);
|
||||||
}
|
} else {
|
||||||
|
$keys = $this->resolveParams($key, $value, $keys);
|
||||||
|
}
|
||||||
/**
|
}
|
||||||
* @param string $key
|
return $keys;
|
||||||
* @param mixed $value
|
}
|
||||||
* @param array $keys
|
|
||||||
* @return array
|
|
||||||
*/
|
/**
|
||||||
private function resolveParams(string $key, mixed $value, array $keys): array
|
* @param string $key
|
||||||
{
|
* @param mixed $value
|
||||||
if (is_null($value)) {
|
* @param array $keys
|
||||||
return $keys;
|
* @return array
|
||||||
}
|
*/
|
||||||
if (preg_match('/^[+|-]\s\d+$/', (string)$value)) {
|
private function resolveParams(string $key, mixed $value, array $keys): array
|
||||||
$keys[] = $key . '=' . $key . ' ' . $value;
|
{
|
||||||
} else {
|
if (is_null($value)) {
|
||||||
$this->query->pushParam($value);
|
return $keys;
|
||||||
$keys[] = $key . '= ?';
|
}
|
||||||
}
|
if (preg_match('/^[+|-]\s\d+$/', (string)$value)) {
|
||||||
return $keys;
|
$keys[] = $key . '=' . $key . ' ' . $value;
|
||||||
}
|
} else {
|
||||||
|
$this->query->pushParam($value);
|
||||||
|
$keys[] = $key . '= ?';
|
||||||
/**
|
}
|
||||||
* @return string
|
return $keys;
|
||||||
* @throws
|
}
|
||||||
*/
|
|
||||||
public function one(): string
|
|
||||||
{
|
/**
|
||||||
$this->query->offset(0)->limit(1);
|
* @return string
|
||||||
return $this->makeSelect($this->query->getSelect()) . $this->make() . $this->makeLimit();
|
* @throws
|
||||||
}
|
*/
|
||||||
|
public function one(): string
|
||||||
|
{
|
||||||
/**
|
$this->query->offset(0)->limit(1);
|
||||||
* @return string
|
return $this->all();
|
||||||
* @throws
|
}
|
||||||
*/
|
|
||||||
public function all(): string
|
|
||||||
{
|
/**
|
||||||
return $this->makeSelect($this->query->getSelect()) . $this->make() . $this->makeLimit();
|
* @return string
|
||||||
}
|
* @throws
|
||||||
|
*/
|
||||||
|
public function all(): string
|
||||||
/**
|
{
|
||||||
* @return string
|
return $this->makeSelect($this->query) . ' ' . $this->make() . ' ' . $this->makeLimit();
|
||||||
* @throws
|
}
|
||||||
*/
|
|
||||||
public function count(): string
|
|
||||||
{
|
/**
|
||||||
return $this->makeSelect(['COUNT(*)']) . $this->make();
|
* @return string
|
||||||
}
|
* @throws
|
||||||
|
*/
|
||||||
|
public function count(): string
|
||||||
/**
|
{
|
||||||
* @return string
|
return $this->makeSelect(['COUNT(*)']) . ' ' . $this->make();
|
||||||
* @throws
|
}
|
||||||
*/
|
|
||||||
public function exists(): string
|
|
||||||
{
|
/**
|
||||||
return $this->makeSelect(['0']) . $this->make();
|
* @return string
|
||||||
}
|
* @throws
|
||||||
|
*/
|
||||||
|
public function exists(): string
|
||||||
/**
|
{
|
||||||
* @param string $table
|
return $this->makeSelect(['0']) . ' ' . $this->make();
|
||||||
* @return string
|
}
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function columns(string $table): string
|
/**
|
||||||
{
|
* @param string $table
|
||||||
$driver = $this->getDriver();
|
* @return string
|
||||||
if (in_array($driver, ['pgsql', 'postgresql'])) {
|
* @throws
|
||||||
// PostgreSQL 使用 information_schema
|
*/
|
||||||
$tableName = trim($table, '"`');
|
public function columns(string $table): string
|
||||||
return "SELECT
|
{
|
||||||
column_name as Field,
|
$driver = $this->getDriver();
|
||||||
data_type as Type,
|
if (in_array($driver, ['pgsql', 'postgresql'])) {
|
||||||
is_nullable as Null,
|
$tableName = trim($table, '"`');
|
||||||
column_default as Default,
|
return "SELECT
|
||||||
'' as Extra,
|
column_name as Field,
|
||||||
'' as Key
|
data_type as Type,
|
||||||
FROM information_schema.columns
|
is_nullable as Null,
|
||||||
WHERE table_name = '$tableName'
|
column_default as Default,
|
||||||
ORDER BY ordinal_position";
|
'' as Extra,
|
||||||
}
|
'' as Key
|
||||||
// MySQL 使用 SHOW FULL FIELDS
|
FROM information_schema.columns
|
||||||
return 'SHOW FULL FIELDS FROM ' . $table;
|
WHERE table_name = '$tableName'
|
||||||
}
|
ORDER BY ordinal_position";
|
||||||
|
}
|
||||||
|
return 'SHOW FULL FIELDS FROM ' . $table;
|
||||||
/**
|
}
|
||||||
* @return string
|
|
||||||
* @throws
|
|
||||||
*/
|
/**
|
||||||
private function make(): string
|
* @return string
|
||||||
{
|
* @throws
|
||||||
$select = $this->makeCondition();
|
*/
|
||||||
$select .= $this->makeGroup();
|
private function make(): string
|
||||||
$select .= $this->makeOrder();
|
{
|
||||||
return $select;
|
return $this->makeCondition() . ' ' . $this->makeGroup() . ' ' . $this->makeOrder();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $select
|
* @param mixed $select 列名数组或 QueryTrait 对象
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function makeSelect(array $select = ['*']): string
|
private function makeSelect(mixed $select = ['*']): string
|
||||||
{
|
{
|
||||||
$select = "SELECT " . implode(',', $select) . " FROM " . $this->query->getFrom();
|
if (!is_array($select)) {
|
||||||
if ($this->query->getAlias() != "") {
|
$select = ['*'];
|
||||||
$select .= " AS " . $this->query->getAlias();
|
}
|
||||||
}
|
$sql = 'SELECT ' . implode(',', $select) . ' FROM ' . $this->getFromAlias();
|
||||||
if (count($this->query->getJoin()) > 0) {
|
if ($this->query->join !== []) {
|
||||||
$select .= ' ' . implode(' ', $this->query->getJoin());
|
$sql .= ' ' . implode(' ', $this->query->join);
|
||||||
}
|
}
|
||||||
return $select;
|
return $sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function makeGroup(): string
|
private function makeGroup(): string
|
||||||
{
|
{
|
||||||
if ($this->query->getGroup() != "") {
|
if ($this->query->group !== '' && $this->query->group !== '0') {
|
||||||
return ' GROUP BY ' . $this->query->getGroup();
|
return 'GROUP BY ' . $this->query->group;
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function makeOrder(): string
|
private function makeOrder(): string
|
||||||
{
|
{
|
||||||
if (count($this->query->getOrder()) > 0) {
|
if (count($this->query->order) > 0) {
|
||||||
return ' ORDER BY ' . implode(',', $this->query->getOrder());
|
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->getWhere());
|
$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->getOffset() >= 0 && $this->query->getLimit() >= 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'])) {
|
||||||
// PostgreSQL 使用 LIMIT ... OFFSET ... 语法
|
return 'LIMIT ' . $this->query->limit . ' OFFSET ' . $this->query->offset;
|
||||||
return ' LIMIT ' . $this->query->getLimit() . ' OFFSET ' . $this->query->getOffset();
|
}
|
||||||
}
|
return 'LIMIT ' . $this->query->offset . ',' . $this->query->limit;
|
||||||
// MySQL 使用 LIMIT offset,limit 语法
|
}
|
||||||
return ' LIMIT ' . $this->query->getOffset() . ',' . $this->query->getLimit();
|
return '';
|
||||||
}
|
}
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* @param false $isCount
|
||||||
/**
|
* @return string
|
||||||
* @param false $isCount
|
* @throws
|
||||||
* @return string
|
*/
|
||||||
* @throws
|
public function get(bool $isCount = false): string
|
||||||
*/
|
{
|
||||||
public function get(bool $isCount = false): string
|
if ($isCount === false) {
|
||||||
{
|
return $this->all();
|
||||||
if ($isCount === false) {
|
}
|
||||||
return $this->all();
|
return $this->count();
|
||||||
}
|
}
|
||||||
return $this->count();
|
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
/**
|
* @throws
|
||||||
* @return string
|
*/
|
||||||
* @throws
|
public function truncate(): string
|
||||||
*/
|
{
|
||||||
public function truncate(): string
|
return sprintf('TRUNCATE %s', $this->getFromAlias());
|
||||||
{
|
}
|
||||||
return sprintf('TRUNCATE %s', $this->query->getFrom());
|
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* @param array $where
|
||||||
/**
|
* @return string
|
||||||
* @param array $where
|
*/
|
||||||
* @return string
|
private function where(array $where): string
|
||||||
*/
|
{
|
||||||
private function where(array $where): string
|
if (count($where) < 1) {
|
||||||
{
|
return '';
|
||||||
if (count($where) < 1) {
|
}
|
||||||
return '';
|
$_tmp = [];
|
||||||
}
|
foreach ($where as $key => $value) {
|
||||||
$_tmp = [];
|
$_tmp[] = $this->resolveCondition($key, $value);
|
||||||
foreach ($where as $key => $value) {
|
}
|
||||||
$_tmp[] = $this->resolveCondition($key, $value);
|
return implode(' AND ', $_tmp);
|
||||||
}
|
}
|
||||||
return implode(' AND ', $_tmp);
|
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* @param string $field
|
||||||
/**
|
* @param mixed $condition
|
||||||
* @param string $field
|
* @return string
|
||||||
* @param mixed $condition
|
*/
|
||||||
* @return string
|
private function resolveCondition(string|int $field, mixed $condition): string
|
||||||
*/
|
{
|
||||||
private function resolveCondition(string|int $field, mixed $condition): string
|
if (is_string($field)) {
|
||||||
{
|
$this->query->pushParam($condition);
|
||||||
if (is_string($field)) {
|
return $field . ' = ?';
|
||||||
$this->query->pushParam($condition);
|
} else if (is_string($condition)) {
|
||||||
return $field . ' = ?';
|
return $condition;
|
||||||
} else if (is_string($condition)) {
|
} else {
|
||||||
return $condition;
|
return implode(' AND ', $this->_hashMap($condition));
|
||||||
} else {
|
}
|
||||||
return implode(' AND ', $this->_hashMap($condition));
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* @param $condition
|
||||||
/**
|
* @return array
|
||||||
* @param $condition
|
*/
|
||||||
* @return array
|
private function _hashMap(array $condition): array
|
||||||
*/
|
{
|
||||||
private function _hashMap(array $condition): array
|
$_array = [];
|
||||||
{
|
foreach ($condition as $key => $value) {
|
||||||
$_array = [];
|
$this->query->pushParam($value);
|
||||||
foreach ($condition as $key => $value) {
|
$_array[] = $key . '= ?';
|
||||||
$this->query->pushParam($value);
|
}
|
||||||
$_array[] = $key . '= ?';
|
return $_array;
|
||||||
}
|
}
|
||||||
return $_array;
|
|
||||||
}
|
/**
|
||||||
|
* 获取数据库驱动类型
|
||||||
/**
|
* @return string
|
||||||
* 获取数据库驱动类型
|
* @throws
|
||||||
* @return string
|
*/
|
||||||
* @throws
|
private function getDriver(): string
|
||||||
*/
|
{
|
||||||
private function getDriver(): string
|
try {
|
||||||
{
|
if ($this->query instanceof ActiveQuery && $this->query->modelClass !== null) {
|
||||||
try {
|
if ($this->query->modelClass instanceof Model) {
|
||||||
// 尝试从 query 对象获取 connection
|
$connection = $this->query->modelClass->getConnection();
|
||||||
if ($this->query instanceof ActiveQuery && $this->query->modelClass !== null) {
|
if ($connection instanceof Connection) {
|
||||||
if ($this->query->modelClass instanceof Model) {
|
return strtolower($connection->driver ?? 'mysql');
|
||||||
$connection = $this->query->modelClass->getConnection();
|
}
|
||||||
if ($connection instanceof Connection) {
|
}
|
||||||
return strtolower($connection->driver ?? 'mysql');
|
}
|
||||||
}
|
if (method_exists($this->query, 'getConnection')) {
|
||||||
}
|
$connection = $this->query->getConnection();
|
||||||
}
|
if ($connection instanceof Connection) {
|
||||||
// 如果是 Db 查询,尝试获取默认 connection
|
return strtolower($connection->driver ?? 'mysql');
|
||||||
if (method_exists($this->query, 'getConnection')) {
|
}
|
||||||
$connection = $this->query->getConnection();
|
}
|
||||||
if ($connection instanceof Connection) {
|
} catch (\Throwable $e) {
|
||||||
return strtolower($connection->driver ?? 'mysql');
|
$this->getLogger()->json_log($e);
|
||||||
}
|
}
|
||||||
}
|
return 'mysql';
|
||||||
} catch (\Throwable $e) {
|
}
|
||||||
// 忽略错误,返回默认值
|
|
||||||
$this->getLogger()->json_log($e);
|
|
||||||
}
|
/**
|
||||||
// 默认返回 mysql
|
* 获取 FROM 子句(表名 + 别名)
|
||||||
return 'mysql';
|
* QueryTrait 对象有 $from 和 $alias 属性
|
||||||
}
|
* @return string
|
||||||
|
*/
|
||||||
|
private function getFromAlias(): string
|
||||||
}
|
{
|
||||||
|
$from = $this->query->from;
|
||||||
|
if ($this->query->alias !== '' && $this->query->alias !== '0') {
|
||||||
|
$from .= ' AS ' . $this->query->alias;
|
||||||
|
}
|
||||||
|
return $from;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
+125
-253
@@ -27,26 +27,105 @@ use Kiri\Abstracts\Component;
|
|||||||
*/
|
*/
|
||||||
abstract class QueryTrait extends Component implements ActiveQueryInterface, ISqlBuilder
|
abstract class QueryTrait extends Component implements ActiveQueryInterface, ISqlBuilder
|
||||||
{
|
{
|
||||||
protected array $where = [];
|
public array $where = [] {
|
||||||
protected array $select = ['t1.*'];
|
&get {
|
||||||
protected array $join = [];
|
return $this->where;
|
||||||
protected array $order = [];
|
}
|
||||||
protected int $offset = -1;
|
}
|
||||||
protected int $limit = -1;
|
public array $select = ['t1.*'] {
|
||||||
protected string $group = '';
|
&get {
|
||||||
protected string $from = '';
|
return $this->select;
|
||||||
protected string $alias = '';
|
}
|
||||||
protected array $filter = [];
|
}
|
||||||
|
public array $join = [] {
|
||||||
|
&get {
|
||||||
|
return $this->join;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public array $order = [] {
|
||||||
|
&get {
|
||||||
|
return $this->order;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public int $offset = -1 {
|
||||||
|
get {
|
||||||
|
return $this->offset;
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
$this->offset = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public int $limit = -1 {
|
||||||
|
get {
|
||||||
|
return $this->limit;
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
$this->limit = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public string $group = '' {
|
||||||
|
get {
|
||||||
|
return $this->group;
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
$this->group = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public string $from = '' {
|
||||||
|
get {
|
||||||
|
return $this->from;
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
$this->from = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public string $alias = '' {
|
||||||
|
get {
|
||||||
|
return $this->alias;
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
$this->alias = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
protected array $filter = [] {
|
||||||
|
&get {
|
||||||
|
return $this->filter;
|
||||||
|
}
|
||||||
|
}
|
||||||
protected bool $lock = FALSE;
|
protected bool $lock = FALSE;
|
||||||
protected SqlBuilder $builder;
|
protected SqlBuilder $builder;
|
||||||
protected array $params = [];
|
public array $params = [] {
|
||||||
protected array $_alias = [];
|
&get {
|
||||||
|
return $this->params;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
protected array $_alias = [] {
|
||||||
|
&get {
|
||||||
|
return $this->_alias;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ModelInterface|string|null
|
* @var ModelInterface|string|null
|
||||||
*/
|
*/
|
||||||
protected ModelInterface|string|null $modelClass;
|
protected ModelInterface|string|null $modelClass {
|
||||||
|
get {
|
||||||
|
return $this->modelClass;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,29 +142,9 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getModelClass(): mixed
|
|
||||||
{
|
|
||||||
return $this->modelClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $where
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setWhere(array $where): void
|
|
||||||
{
|
|
||||||
$this->where = $where;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Closure|Query $callback
|
* @param Closure|Query $callback
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
@@ -93,7 +152,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
{
|
{
|
||||||
if ($callback instanceof Query) {
|
if ($callback instanceof Query) {
|
||||||
$this->where[] = 'NOT EXISTS(' . $callback->build() . ')';
|
$this->where[] = 'NOT EXISTS(' . $callback->build() . ')';
|
||||||
$this->mergeParams($callback->getParams());
|
$this->mergeParams($callback->params);
|
||||||
} else {
|
} else {
|
||||||
$this->where[] = 'NOT EXISTS(' . $this->makeClosureFunction($callback) . ')';
|
$this->where[] = 'NOT EXISTS(' . $this->makeClosureFunction($callback) . ')';
|
||||||
}
|
}
|
||||||
@@ -106,7 +165,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Closure|Query $callback
|
* @param Closure|Query $callback
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
@@ -114,7 +173,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
{
|
{
|
||||||
if ($callback instanceof Query) {
|
if ($callback instanceof Query) {
|
||||||
$this->where[] = 'EXISTS(' . $callback->build() . ')';
|
$this->where[] = 'EXISTS(' . $callback->build() . ')';
|
||||||
$this->mergeParams($callback->getParams());
|
$this->mergeParams($callback->params);
|
||||||
} else {
|
} else {
|
||||||
$this->where[] = 'EXISTS(' . $this->makeClosureFunction($callback) . ')';
|
$this->where[] = 'EXISTS(' . $this->makeClosureFunction($callback) . ')';
|
||||||
}
|
}
|
||||||
@@ -125,194 +184,6 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $select
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setSelect(array $select): void
|
|
||||||
{
|
|
||||||
$this->select = $select;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $join
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setJoin(array $join): void
|
|
||||||
{
|
|
||||||
$this->join = $join;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $order
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setOrder(array $order): void
|
|
||||||
{
|
|
||||||
$this->order = $order;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $offset
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setOffset(int $offset): void
|
|
||||||
{
|
|
||||||
$this->offset = $offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $limit
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setLimit(int $limit): void
|
|
||||||
{
|
|
||||||
$this->limit = $limit;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $group
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setGroup(string $group): void
|
|
||||||
{
|
|
||||||
$this->group = $group;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $from
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setFrom(string $from): void
|
|
||||||
{
|
|
||||||
$this->from = $from;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $alias
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setAlias(string $alias): void
|
|
||||||
{
|
|
||||||
$this->alias = $alias;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $params
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setParams(array $params): void
|
|
||||||
{
|
|
||||||
$this->params = $params;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getWhere(): array
|
|
||||||
{
|
|
||||||
return $this->where;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array|string[]
|
|
||||||
*/
|
|
||||||
public function getSelect(): array
|
|
||||||
{
|
|
||||||
return $this->select;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getJoin(): array
|
|
||||||
{
|
|
||||||
return $this->join;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getOrder(): array
|
|
||||||
{
|
|
||||||
return $this->order;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function getOffset(): int
|
|
||||||
{
|
|
||||||
return $this->offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function getLimit(): int
|
|
||||||
{
|
|
||||||
return $this->limit;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getGroup(): string
|
|
||||||
{
|
|
||||||
return $this->group;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getFrom(): string
|
|
||||||
{
|
|
||||||
return $this->from;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getAlias(): string
|
|
||||||
{
|
|
||||||
return $this->alias;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getParams(): array
|
|
||||||
{
|
|
||||||
return $this->params;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return void
|
* @return void
|
||||||
@@ -326,7 +197,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param callable $callable
|
* @param callable $callable
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
@@ -465,7 +336,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
{
|
{
|
||||||
if ($tableName instanceof Query) {
|
if ($tableName instanceof Query) {
|
||||||
$this->from = '(' . $tableName->build() . ')';
|
$this->from = '(' . $tableName->build() . ')';
|
||||||
$this->mergeParams($tableName->getParams());
|
$this->mergeParams($tableName->params);
|
||||||
} else if ($tableName instanceof Closure) {
|
} else if ($tableName instanceof Closure) {
|
||||||
$this->from = '(' . $this->makeClosureFunction($tableName) . ')';
|
$this->from = '(' . $this->makeClosureFunction($tableName) . ')';
|
||||||
} else if (class_exists($tableName)) {
|
} else if (class_exists($tableName)) {
|
||||||
@@ -480,8 +351,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
/**
|
/**
|
||||||
* @param string $tableName
|
* @param string $tableName
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
* @param array $on
|
* @param array $on
|
||||||
* @param array $param
|
* @param array $param
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
* $query->join([$tableName, ['userId'=>'uuvOd']], $param)
|
* $query->join([$tableName, ['userId'=>'uuvOd']], $param)
|
||||||
@@ -563,8 +434,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
/**
|
/**
|
||||||
* @param string $tableName
|
* @param string $tableName
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
* @param array $onCondition
|
* @param array $onCondition
|
||||||
* @param array $param
|
* @param array $param
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
@@ -585,8 +456,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
/**
|
/**
|
||||||
* @param string $tableName
|
* @param string $tableName
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
* @param array $onCondition
|
* @param array $onCondition
|
||||||
* @param array $param
|
* @param array $param
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
@@ -607,8 +478,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
/**
|
/**
|
||||||
* @param string $tableName
|
* @param string $tableName
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
* @param array $onCondition
|
* @param array $onCondition
|
||||||
* @param array $param
|
* @param array $param
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
@@ -651,8 +522,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $lngField
|
* @param string $lngField
|
||||||
* @param string $latField
|
* @param string $latField
|
||||||
* @param int|float $lng1
|
* @param int|float $lng1
|
||||||
* @param int|float $lat1
|
* @param int|float $lat1
|
||||||
*
|
*
|
||||||
@@ -668,7 +539,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string|array $column
|
* @param string|array $column
|
||||||
* @param string $sort
|
* @param string $sort
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*
|
*
|
||||||
@@ -736,8 +607,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|Closure|string $conditionArray
|
* @param array|Closure|string $conditionArray
|
||||||
* @param string $opera
|
* @param string $opera
|
||||||
* @param null $value
|
* @param null $value
|
||||||
*
|
*
|
||||||
* @return QueryTrait
|
* @return QueryTrait
|
||||||
* @throws
|
* @throws
|
||||||
@@ -816,7 +687,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $columns
|
* @param string $columns
|
||||||
* @param array|Closure|Query $value
|
* @param array|Closure|Query $value
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
@@ -825,12 +696,13 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
public function whereIn(string $columns, array|Closure|Query $value): static
|
public function whereIn(string $columns, array|Closure|Query $value): static
|
||||||
{
|
{
|
||||||
if (is_array($value)) {
|
if (is_array($value)) {
|
||||||
if (count($value) < 1) throw new Exception('Value must be an not empty array');
|
if (count($value) < 1)
|
||||||
|
throw new Exception('Value must be an not empty array');
|
||||||
|
|
||||||
$this->where[] = $columns . ' IN (' . implode(',', $value) . ')';
|
$this->where[] = $columns . ' IN (' . implode(',', $value) . ')';
|
||||||
} else if ($value instanceof Query) {
|
} else if ($value instanceof Query) {
|
||||||
$this->where[] = $columns . ' IN (' . $value->build() . ')';
|
$this->where[] = $columns . ' IN (' . $value->build() . ')';
|
||||||
$this->mergeParams($value->getParams());
|
$this->mergeParams($value->params);
|
||||||
} else {
|
} else {
|
||||||
$this->where[] = $columns . ' IN (' . $this->makeClosureFunction($value) . ')';
|
$this->where[] = $columns . ' IN (' . $this->makeClosureFunction($value) . ')';
|
||||||
}
|
}
|
||||||
@@ -844,13 +716,13 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
*/
|
*/
|
||||||
public function queryInstance(): Query
|
public function queryInstance(): Query
|
||||||
{
|
{
|
||||||
return new Query();
|
return new Query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $columns
|
* @param string $columns
|
||||||
* @param array $value
|
* @param array $value
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
@@ -865,7 +737,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param int|float $start
|
* @param int|float $start
|
||||||
* @param int|float $end
|
* @param int|float $end
|
||||||
*
|
*
|
||||||
@@ -886,7 +758,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param int|float $start
|
* @param int|float $start
|
||||||
* @param int|float $end
|
* @param int|float $end
|
||||||
*
|
*
|
||||||
@@ -920,7 +792,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|string $column
|
* @param array|string $column
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
@@ -940,7 +812,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param string $opera
|
* @param string $opera
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
@@ -978,13 +850,13 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
} else {
|
} else {
|
||||||
$generate->addArray($closure);
|
$generate->addArray($closure);
|
||||||
}
|
}
|
||||||
$this->mergeParams($generate->getParams());
|
$this->mergeParams($generate->params);
|
||||||
return $generate->build();
|
return $generate->build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string|null $having
|
* @param string|null $having
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
@@ -1060,7 +932,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param string $opera
|
* @param string $opera
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
@@ -1084,7 +956,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $querySql
|
* @param string $querySql
|
||||||
* @param array $params
|
* @param array $params
|
||||||
*
|
*
|
||||||
* @return Command
|
* @return Command
|
||||||
*/
|
*/
|
||||||
|
|||||||
+65
-46
@@ -18,61 +18,80 @@ use JetBrains\PhpStorm\Pure;
|
|||||||
class When
|
class When
|
||||||
{
|
{
|
||||||
|
|
||||||
public ActiveQuery|ISqlBuilder $query;
|
public ActiveQuery|ISqlBuilder $query;
|
||||||
|
|
||||||
|
|
||||||
private array $_condition = [];
|
/**
|
||||||
|
* @var array
|
||||||
private string $else = '';
|
*/
|
||||||
|
private array $_condition = [] {
|
||||||
|
&get {
|
||||||
|
return $this->_condition;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CaseWhen constructor.
|
* @var string
|
||||||
* @param string $column
|
*/
|
||||||
* @param ActiveQuery|ISqlBuilder $activeQuery
|
private string $else = '' {
|
||||||
*/
|
get {
|
||||||
public function __construct(public string $column, public ActiveQuery|ISqlBuilder $activeQuery)
|
return $this->else;
|
||||||
{
|
}
|
||||||
$this->_condition[] = 'CASE ' . $column;
|
set(string $value) {
|
||||||
}
|
$this->else = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string|int $condition
|
* CaseWhen constructor.
|
||||||
* @param string $then
|
* @param string $column
|
||||||
* @return $this
|
* @param ActiveQuery|ISqlBuilder $activeQuery
|
||||||
* @throws
|
*/
|
||||||
*/
|
public function __construct(public string $column, public ActiveQuery|ISqlBuilder $activeQuery)
|
||||||
public function when(string|int $condition, string $then): static
|
{
|
||||||
{
|
$this->_condition[] = 'CASE ' . $column;
|
||||||
$this->_condition[] = sprintf('WHEN %s THEN %s', $condition, $then);
|
}
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $alias
|
* @param string|int $condition
|
||||||
*/
|
* @param string $then
|
||||||
public function else(string $alias): void
|
* @return $this
|
||||||
{
|
* @throws
|
||||||
$this->else = $alias;
|
*/
|
||||||
}
|
public function when(string|int $condition, string $then): static
|
||||||
|
{
|
||||||
|
$this->_condition[] = sprintf('WHEN %s THEN %s', $condition, $then);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @param string $alias
|
||||||
*/
|
*/
|
||||||
#[Pure] public function end(): string
|
public function else(string $alias): void
|
||||||
{
|
{
|
||||||
if (empty($this->_condition)) {
|
$this->else = $alias;
|
||||||
return '';
|
}
|
||||||
}
|
|
||||||
$prefix = implode(' ', $this->_condition);
|
|
||||||
if (!empty($this->else)) {
|
/**
|
||||||
$prefix .= ' ELSE ' . $this->else;
|
* @return string
|
||||||
}
|
*/
|
||||||
return '(' . $prefix . ' END)';
|
#[Pure]
|
||||||
}
|
public function end(): string
|
||||||
|
{
|
||||||
|
if (empty($this->_condition)) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
$prefix = implode(' ', $this->_condition);
|
||||||
|
if (!empty($this->else)) {
|
||||||
|
$prefix .= ' ELSE ' . $this->else;
|
||||||
|
}
|
||||||
|
return '(' . $prefix . ' END)';
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -9,7 +9,7 @@
|
|||||||
],
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=8.4",
|
"php": ">=8.5",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"ext-pdo": "*",
|
"ext-pdo": "*",
|
||||||
"game-worker/kiri-pool": "^v1.0"
|
"game-worker/kiri-pool": "^v1.0"
|
||||||
|
|||||||
Reference in New Issue
Block a user