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
|
||||
{
|
||||
return $this->buildCommand($this->builder->delete())->delete();
|
||||
return (bool)$this->buildCommand($this->builder->delete())->exec();
|
||||
}
|
||||
}
|
||||
|
||||
+17
-9
@@ -299,7 +299,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
*/
|
||||
protected static function instance(): static
|
||||
{
|
||||
return new static();
|
||||
return new static;
|
||||
}
|
||||
|
||||
|
||||
@@ -590,7 +590,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
*/
|
||||
private function resolve(array $rule): Validator
|
||||
{
|
||||
$validate = new Validator();
|
||||
$validate = new Validator;
|
||||
foreach ($rule as $val) {
|
||||
$field = array_shift($val);
|
||||
if (is_string($field)) {
|
||||
@@ -728,12 +728,20 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
*/
|
||||
public function __get(string $name): mixed
|
||||
{
|
||||
$value = $this->_attributes[$name] ?? NULL;
|
||||
if (!method_exists($this, 'get' . ucfirst($name))) {
|
||||
return $this->withPropertyOverride($name, $value);
|
||||
} else {
|
||||
return $this->withRelate($name);
|
||||
if (isset($this->_attributes[$name])) {
|
||||
return $this->withPropertyOverride($name, $this->_attributes[$name]);
|
||||
}
|
||||
|
||||
if (isset($this->_oldAttributes[$name])) {
|
||||
return $this->withPropertyOverride($name, $this->_oldAttributes[$name]);
|
||||
}
|
||||
|
||||
|
||||
if (!method_exists($this, 'get' . ucfirst($name))) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return $this->{'get' . ucfirst($name)}();
|
||||
}
|
||||
|
||||
|
||||
@@ -868,7 +876,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
*/
|
||||
public static function populate(array $data): static
|
||||
{
|
||||
$model = new static();
|
||||
$model = new static;
|
||||
$model->_attributes = $data;
|
||||
$model->_oldAttributes = $data;
|
||||
$model->setIsNowExample();
|
||||
@@ -884,7 +892,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
*/
|
||||
public static function __callStatic(string $name, array $arguments)
|
||||
{
|
||||
return (new static())->{$name}(...$arguments);
|
||||
return (new static)->{$name}(...$arguments);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+40
-10
@@ -23,6 +23,9 @@ use Throwable;
|
||||
class Command extends Component
|
||||
{
|
||||
|
||||
/** @var int 数据库操作最大重试次数 */
|
||||
private const MAX_RETRIES = 2;
|
||||
|
||||
public Connection $connection;
|
||||
public ?string $sql = '';
|
||||
public array $params = [];
|
||||
@@ -101,12 +104,17 @@ class Command extends Component
|
||||
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* 执行查询类 SQL 操作,内置断连重试机制
|
||||
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
|
||||
* @param string $method PDO 结果获取方法名
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
protected function search(string $method): mixed
|
||||
{
|
||||
$retryCount = 0;
|
||||
|
||||
while ($retryCount <= self::MAX_RETRIES) {
|
||||
$client = $this->connection->getConnection();
|
||||
try {
|
||||
$startTime = microtime(true);
|
||||
@@ -121,22 +129,28 @@ class Command extends Component
|
||||
|
||||
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
||||
|
||||
$this->connection->release($client);
|
||||
return $result;
|
||||
} catch (Throwable $throwable) {
|
||||
$this->getLogger()->json_log($throwable);
|
||||
|
||||
if ($this->isRefresh($throwable)) {
|
||||
return $this->search($method);
|
||||
if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) {
|
||||
$retryCount++;
|
||||
$this->connection->connections->abandon($this->connection->getName());
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->connection->release($client);
|
||||
|
||||
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params);
|
||||
|
||||
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
|
||||
} finally {
|
||||
$this->connection->release($client);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->getLogger()->logCategory('Max retry exceeded for SQL: ' . $this->sql, 'mysql');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
@@ -149,11 +163,16 @@ class Command extends Component
|
||||
|
||||
|
||||
/**
|
||||
* 执行写入类 SQL 操作,内置断连重试机制
|
||||
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
|
||||
* @return int|bool
|
||||
* @throws
|
||||
*/
|
||||
private function _prepare(): int|bool
|
||||
{
|
||||
$retryCount = 0;
|
||||
|
||||
while ($retryCount <= self::MAX_RETRIES) {
|
||||
$client = $this->connection->getConnection();
|
||||
try {
|
||||
$startTime = microtime(true);
|
||||
@@ -163,28 +182,39 @@ class Command extends Component
|
||||
if ($prepare->execute($this->params) === false) {
|
||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
|
||||
}
|
||||
|
||||
$prepare->closeCursor();
|
||||
|
||||
$result = $client->lastInsertId();
|
||||
|
||||
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
||||
if (str_starts_with($this->sql, 'DELETE')) {
|
||||
$this->connection->release($client);
|
||||
return $prepare->rowCount();
|
||||
}
|
||||
|
||||
$this->connection->release($client);
|
||||
return $result == 0 ? $prepare->rowCount() : (int)$result;
|
||||
} catch (Throwable $throwable) {
|
||||
$this->getLogger()->json_log($throwable);
|
||||
|
||||
if ($this->isRefresh($throwable)) {
|
||||
return $this->_prepare();
|
||||
if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) {
|
||||
$retryCount++;
|
||||
$this->connection->connections->abandon($this->connection->getName());
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->connection->release($client);
|
||||
|
||||
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
|
||||
} finally {
|
||||
$this->connection->release($client);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->getLogger()->logCategory('Max retry exceeded for SQL: ' . $this->sql, 'mysql');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Throwable $throwable
|
||||
@@ -229,7 +259,7 @@ class Command extends Component
|
||||
*/
|
||||
public function delete(): bool
|
||||
{
|
||||
return (bool)$this->_prepare();
|
||||
return $this->_prepare();
|
||||
}
|
||||
|
||||
|
||||
|
||||
+5
-1
@@ -154,7 +154,7 @@ class Connection extends Component
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function getName(): string
|
||||
public function getName(): string
|
||||
{
|
||||
return strtolower($this->driver) . '.' . $this->cds;
|
||||
}
|
||||
@@ -264,6 +264,7 @@ class Connection extends Component
|
||||
$pdo->rollback();
|
||||
}
|
||||
$this->release($pdo);
|
||||
Context::remove($this->cds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,6 +284,7 @@ class Connection extends Component
|
||||
$pdo->commit();
|
||||
}
|
||||
$this->release($pdo);
|
||||
Context::remove($this->cds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,6 +304,8 @@ class Connection extends Component
|
||||
$pdo->rollback();
|
||||
}
|
||||
$this->release($pdo);
|
||||
Context::remove($this->cds);
|
||||
$this->storey = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ class DatabasesProviders extends Providers
|
||||
return;
|
||||
}
|
||||
foreach ($databases as $key => $database) {
|
||||
$this->set($key, $this->_settings($database));
|
||||
$this->set($key, $this->_settings($key, $database));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,14 +64,15 @@ class DatabasesProviders extends Providers
|
||||
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @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];
|
||||
return [
|
||||
'id' => $database['id'],
|
||||
'id' => $key,
|
||||
'cds' => $database['cds'],
|
||||
'class' => Connection::class,
|
||||
'username' => $database['username'],
|
||||
|
||||
@@ -285,6 +285,7 @@ class Model extends Base\Model
|
||||
|
||||
$primaryKey = str_replace('\\', '_', $modelName) . '_' . $foreignKey . '_' . $value;
|
||||
if (!$relation->hasIdentification($primaryKey)) {
|
||||
/** @var $modelName ModelInterface */
|
||||
$relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value]));
|
||||
}
|
||||
return $primaryKey;
|
||||
@@ -346,6 +347,7 @@ class Model extends Base\Model
|
||||
|
||||
$primaryKey = str_replace('\\', '_', $modelName) . '_' . $foreignKey . '_' . implode('_', $value);
|
||||
if (!$relation->hasIdentification($primaryKey)) {
|
||||
/** @var $modelName ModelInterface */
|
||||
$relation->bindIdentification($primaryKey, $modelName::query()->whereIn($foreignKey, $value));
|
||||
}
|
||||
|
||||
|
||||
+54
-45
@@ -9,6 +9,7 @@ namespace Database;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Database\Traits\QueryTrait;
|
||||
|
||||
|
||||
/**
|
||||
@@ -52,7 +53,7 @@ class SqlBuilder extends Component
|
||||
*/
|
||||
public function getCondition(): string
|
||||
{
|
||||
return $this->where($this->query->getWhere());
|
||||
return $this->where($this->query->where);
|
||||
}
|
||||
|
||||
|
||||
@@ -74,13 +75,13 @@ class SqlBuilder extends Component
|
||||
*/
|
||||
public function update(array $attributes): bool|string
|
||||
{
|
||||
$conditions = $this->query->getParams();
|
||||
$this->query->setParams([]);
|
||||
$data = $this->__updateBuilder($this->makeParams($attributes));
|
||||
foreach ($conditions as $condition) {
|
||||
$this->query->pushParam($condition);
|
||||
$params = $this->query->params;
|
||||
$this->query->params = [];
|
||||
$array = $this->makeParams($attributes);
|
||||
foreach ($params as $name => $value) {
|
||||
$this->query->pushParam($value);
|
||||
}
|
||||
return $data;
|
||||
return $this->__updateBuilder($array);
|
||||
}
|
||||
|
||||
|
||||
@@ -110,7 +111,8 @@ class SqlBuilder extends Component
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@@ -122,7 +124,7 @@ class SqlBuilder extends Component
|
||||
*/
|
||||
public function insert(array $attributes, bool $isBatch = false): string
|
||||
{
|
||||
$update = 'INSERT INTO ' . $this->query->getFrom();
|
||||
$update = 'INSERT INTO ' . $this->getFromAlias();
|
||||
if ($isBatch === false) {
|
||||
$attributes = [$attributes];
|
||||
}
|
||||
@@ -144,7 +146,9 @@ class SqlBuilder extends Component
|
||||
*/
|
||||
public function delete(): string
|
||||
{
|
||||
return 'DELETE FROM ' . $this->query->getFrom() . $this->make();
|
||||
return 'DELETE FROM ' .
|
||||
$this->getFromAlias() . ' ' .
|
||||
$this->make();
|
||||
}
|
||||
|
||||
|
||||
@@ -152,7 +156,8 @@ class SqlBuilder extends Component
|
||||
* @param $attributes
|
||||
* @return array
|
||||
*/
|
||||
#[Pure] private function getFields(array $attributes): array
|
||||
#[Pure]
|
||||
private function getFields(array $attributes): array
|
||||
{
|
||||
return array_keys(current($attributes));
|
||||
}
|
||||
@@ -207,7 +212,7 @@ class SqlBuilder extends Component
|
||||
public function one(): string
|
||||
{
|
||||
$this->query->offset(0)->limit(1);
|
||||
return $this->makeSelect($this->query->getSelect()) . $this->make() . $this->makeLimit();
|
||||
return $this->all();
|
||||
}
|
||||
|
||||
|
||||
@@ -217,7 +222,7 @@ class SqlBuilder extends Component
|
||||
*/
|
||||
public function all(): string
|
||||
{
|
||||
return $this->makeSelect($this->query->getSelect()) . $this->make() . $this->makeLimit();
|
||||
return $this->makeSelect($this->query) . ' ' . $this->make() . ' ' . $this->makeLimit();
|
||||
}
|
||||
|
||||
|
||||
@@ -227,7 +232,7 @@ class SqlBuilder extends Component
|
||||
*/
|
||||
public function count(): string
|
||||
{
|
||||
return $this->makeSelect(['COUNT(*)']) . $this->make();
|
||||
return $this->makeSelect(['COUNT(*)']) . ' ' . $this->make();
|
||||
}
|
||||
|
||||
|
||||
@@ -237,7 +242,7 @@ class SqlBuilder extends Component
|
||||
*/
|
||||
public function exists(): string
|
||||
{
|
||||
return $this->makeSelect(['0']) . $this->make();
|
||||
return $this->makeSelect(['0']) . ' ' . $this->make();
|
||||
}
|
||||
|
||||
|
||||
@@ -250,7 +255,6 @@ class SqlBuilder extends Component
|
||||
{
|
||||
$driver = $this->getDriver();
|
||||
if (in_array($driver, ['pgsql', 'postgresql'])) {
|
||||
// PostgreSQL 使用 information_schema
|
||||
$tableName = trim($table, '"`');
|
||||
return "SELECT
|
||||
column_name as Field,
|
||||
@@ -263,7 +267,6 @@ class SqlBuilder extends Component
|
||||
WHERE table_name = '$tableName'
|
||||
ORDER BY ordinal_position";
|
||||
}
|
||||
// MySQL 使用 SHOW FULL FIELDS
|
||||
return 'SHOW FULL FIELDS FROM ' . $table;
|
||||
}
|
||||
|
||||
@@ -274,26 +277,23 @@ class SqlBuilder extends Component
|
||||
*/
|
||||
private function make(): string
|
||||
{
|
||||
$select = $this->makeCondition();
|
||||
$select .= $this->makeGroup();
|
||||
$select .= $this->makeOrder();
|
||||
return $select;
|
||||
return $this->makeCondition() . ' ' . $this->makeGroup() . ' ' . $this->makeOrder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $select
|
||||
* @param mixed $select 列名数组或 QueryTrait 对象
|
||||
* @return string
|
||||
*/
|
||||
private function makeSelect(array $select = ['*']): string
|
||||
private function makeSelect(mixed $select = ['*']): string
|
||||
{
|
||||
$select = "SELECT " . implode(',', $select) . " FROM " . $this->query->getFrom();
|
||||
if ($this->query->getAlias() != "") {
|
||||
$select .= " AS " . $this->query->getAlias();
|
||||
if (!is_array($select)) {
|
||||
$select = ['*'];
|
||||
}
|
||||
if (count($this->query->getJoin()) > 0) {
|
||||
$select .= ' ' . implode(' ', $this->query->getJoin());
|
||||
$sql = 'SELECT ' . implode(',', $select) . ' FROM ' . $this->getFromAlias();
|
||||
if ($this->query->join !== []) {
|
||||
$sql .= ' ' . implode(' ', $this->query->join);
|
||||
}
|
||||
return $select;
|
||||
return $sql;
|
||||
}
|
||||
|
||||
|
||||
@@ -302,8 +302,8 @@ class SqlBuilder extends Component
|
||||
*/
|
||||
private function makeGroup(): string
|
||||
{
|
||||
if ($this->query->getGroup() != "") {
|
||||
return ' GROUP BY ' . $this->query->getGroup();
|
||||
if ($this->query->group !== '' && $this->query->group !== '0') {
|
||||
return 'GROUP BY ' . $this->query->group;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
@@ -314,8 +314,8 @@ class SqlBuilder extends Component
|
||||
*/
|
||||
private function makeOrder(): string
|
||||
{
|
||||
if (count($this->query->getOrder()) > 0) {
|
||||
return ' ORDER BY ' . implode(',', $this->query->getOrder());
|
||||
if (count($this->query->order) > 0) {
|
||||
return 'ORDER BY ' . implode(',', $this->query->order);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
@@ -326,11 +326,11 @@ class SqlBuilder extends Component
|
||||
*/
|
||||
private function makeCondition(): string
|
||||
{
|
||||
$condition = $this->where($this->query->getWhere());
|
||||
$condition = $this->where($this->query->where);
|
||||
if (empty($condition)) {
|
||||
return '';
|
||||
}
|
||||
return ' WHERE ' . $condition;
|
||||
return 'WHERE ' . $condition;
|
||||
}
|
||||
|
||||
|
||||
@@ -340,14 +340,12 @@ class SqlBuilder extends Component
|
||||
*/
|
||||
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();
|
||||
if (in_array($driver, ['pgsql', 'postgresql'])) {
|
||||
// PostgreSQL 使用 LIMIT ... OFFSET ... 语法
|
||||
return ' LIMIT ' . $this->query->getLimit() . ' OFFSET ' . $this->query->getOffset();
|
||||
return 'LIMIT ' . $this->query->limit . ' OFFSET ' . $this->query->offset;
|
||||
}
|
||||
// MySQL 使用 LIMIT offset,limit 语法
|
||||
return ' LIMIT ' . $this->query->getOffset() . ',' . $this->query->getLimit();
|
||||
return 'LIMIT ' . $this->query->offset . ',' . $this->query->limit;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
@@ -373,7 +371,7 @@ class SqlBuilder extends Component
|
||||
*/
|
||||
public function truncate(): string
|
||||
{
|
||||
return sprintf('TRUNCATE %s', $this->query->getFrom());
|
||||
return sprintf('TRUNCATE %s', $this->getFromAlias());
|
||||
}
|
||||
|
||||
|
||||
@@ -434,7 +432,6 @@ class SqlBuilder extends Component
|
||||
private function getDriver(): string
|
||||
{
|
||||
try {
|
||||
// 尝试从 query 对象获取 connection
|
||||
if ($this->query instanceof ActiveQuery && $this->query->modelClass !== null) {
|
||||
if ($this->query->modelClass instanceof Model) {
|
||||
$connection = $this->query->modelClass->getConnection();
|
||||
@@ -443,7 +440,6 @@ class SqlBuilder extends Component
|
||||
}
|
||||
}
|
||||
}
|
||||
// 如果是 Db 查询,尝试获取默认 connection
|
||||
if (method_exists($this->query, 'getConnection')) {
|
||||
$connection = $this->query->getConnection();
|
||||
if ($connection instanceof Connection) {
|
||||
@@ -451,12 +447,25 @@ class SqlBuilder extends Component
|
||||
}
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
// 忽略错误,返回默认值
|
||||
$this->getLogger()->json_log($e);
|
||||
}
|
||||
// 默认返回 mysql
|
||||
return 'mysql';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取 FROM 子句(表名 + 别名)
|
||||
* 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+100
-228
@@ -27,26 +27,105 @@ use Kiri\Abstracts\Component;
|
||||
*/
|
||||
abstract class QueryTrait extends Component implements ActiveQueryInterface, ISqlBuilder
|
||||
{
|
||||
protected array $where = [];
|
||||
protected array $select = ['t1.*'];
|
||||
protected array $join = [];
|
||||
protected array $order = [];
|
||||
protected int $offset = -1;
|
||||
protected int $limit = -1;
|
||||
protected string $group = '';
|
||||
protected string $from = '';
|
||||
protected string $alias = '';
|
||||
protected array $filter = [];
|
||||
public array $where = [] {
|
||||
&get {
|
||||
return $this->where;
|
||||
}
|
||||
}
|
||||
public array $select = ['t1.*'] {
|
||||
&get {
|
||||
return $this->select;
|
||||
}
|
||||
}
|
||||
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 SqlBuilder $builder;
|
||||
protected array $params = [];
|
||||
protected array $_alias = [];
|
||||
public array $params = [] {
|
||||
&get {
|
||||
return $this->params;
|
||||
}
|
||||
}
|
||||
protected array $_alias = [] {
|
||||
&get {
|
||||
return $this->_alias;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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,26 +142,6 @@ 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 array $attributes
|
||||
@@ -93,7 +152,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
{
|
||||
if ($callback instanceof Query) {
|
||||
$this->where[] = 'NOT EXISTS(' . $callback->build() . ')';
|
||||
$this->mergeParams($callback->getParams());
|
||||
$this->mergeParams($callback->params);
|
||||
} else {
|
||||
$this->where[] = 'NOT EXISTS(' . $this->makeClosureFunction($callback) . ')';
|
||||
}
|
||||
@@ -114,7 +173,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
{
|
||||
if ($callback instanceof Query) {
|
||||
$this->where[] = 'EXISTS(' . $callback->build() . ')';
|
||||
$this->mergeParams($callback->getParams());
|
||||
$this->mergeParams($callback->params);
|
||||
} else {
|
||||
$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
|
||||
* @return void
|
||||
@@ -465,7 +336,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
{
|
||||
if ($tableName instanceof Query) {
|
||||
$this->from = '(' . $tableName->build() . ')';
|
||||
$this->mergeParams($tableName->getParams());
|
||||
$this->mergeParams($tableName->params);
|
||||
} else if ($tableName instanceof Closure) {
|
||||
$this->from = '(' . $this->makeClosureFunction($tableName) . ')';
|
||||
} else if (class_exists($tableName)) {
|
||||
@@ -825,12 +696,13 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
public function whereIn(string $columns, array|Closure|Query $value): static
|
||||
{
|
||||
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) . ')';
|
||||
} else if ($value instanceof Query) {
|
||||
$this->where[] = $columns . ' IN (' . $value->build() . ')';
|
||||
$this->mergeParams($value->getParams());
|
||||
$this->mergeParams($value->params);
|
||||
} else {
|
||||
$this->where[] = $columns . ' IN (' . $this->makeClosureFunction($value) . ')';
|
||||
}
|
||||
@@ -844,7 +716,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
*/
|
||||
public function queryInstance(): Query
|
||||
{
|
||||
return new Query();
|
||||
return new Query;
|
||||
}
|
||||
|
||||
|
||||
@@ -978,7 +850,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
} else {
|
||||
$generate->addArray($closure);
|
||||
}
|
||||
$this->mergeParams($generate->getParams());
|
||||
$this->mergeParams($generate->params);
|
||||
return $generate->build();
|
||||
}
|
||||
|
||||
|
||||
+22
-3
@@ -21,9 +21,27 @@ class When
|
||||
public ActiveQuery|ISqlBuilder $query;
|
||||
|
||||
|
||||
private array $_condition = [];
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $_condition = [] {
|
||||
&get {
|
||||
return $this->_condition;
|
||||
}
|
||||
}
|
||||
|
||||
private string $else = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $else = '' {
|
||||
get {
|
||||
return $this->else;
|
||||
}
|
||||
set(string $value) {
|
||||
$this->else = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -63,7 +81,8 @@ class When
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] public function end(): string
|
||||
#[Pure]
|
||||
public function end(): string
|
||||
{
|
||||
if (empty($this->_condition)) {
|
||||
return '';
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@
|
||||
],
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"php": ">=8.4",
|
||||
"php": ">=8.5",
|
||||
"ext-json": "*",
|
||||
"ext-pdo": "*",
|
||||
"game-worker/kiri-pool": "^v1.0"
|
||||
|
||||
Reference in New Issue
Block a user