Compare commits

...

24 Commits

Author SHA1 Message Date
as2252258 39905b77e3 eee 2026-07-03 14:35:45 +08:00
as2252258 fce82ec56c eee 2026-06-30 23:27:42 +08:00
as2252258 2970c3bc6b eee 2026-06-30 23:16:18 +08:00
as2252258 a35d0bb921 eee 2026-06-30 23:15:59 +08:00
as2252258 fce2b8bcdb eee 2026-06-30 23:15:06 +08:00
as2252258 ce92f310a5 eee 2026-06-30 23:14:22 +08:00
as2252258 28fa17ecfb eee 2026-06-30 23:12:17 +08:00
as2252258 9fc910f10f eee 2026-06-30 23:11:55 +08:00
as2252258 0a95e722d6 eee 2026-06-30 23:09:52 +08:00
as2252258 9f2611972f eee 2026-06-30 23:08:13 +08:00
as2252258 b7e59d1c96 eee 2026-06-30 23:06:55 +08:00
as2252258 9b0ee54608 eee 2026-06-30 23:03:51 +08:00
as2252258 763a4baa57 eee 2026-06-30 23:01:40 +08:00
as2252258 fe639bbd1f eee 2026-06-30 23:01:11 +08:00
as2252258 e40de4ee4a eee 2026-06-30 23:00:25 +08:00
as2252258 ffc2af69da eee 2026-06-30 22:54:04 +08:00
as2252258 218a38da48 eee 2026-06-30 22:46:19 +08:00
as2252258 5acb2f4600 eee 2026-06-30 22:43:20 +08:00
as2252258 d6a07446bb eee 2026-06-30 22:35:02 +08:00
as2252258 6c936c3896 eee 2026-06-30 22:31:47 +08:00
as2252258 14448d824a eee 2026-06-30 22:07:04 +08:00
as2252258 06074c38c1 eee 2026-06-24 22:00:50 +08:00
as2252258 404b511c5f eee 2026-06-24 21:53:42 +08:00
as2252258 bf4f8538ed eee 2026-06-24 20:11:10 +08:00
5 changed files with 782 additions and 730 deletions
+84 -58
View File
@@ -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,40 +104,51 @@ class Command extends Component
/**
* @param string $method
* 执行查询类 SQL 操作,内置断连重试机制
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
* @param string $method PDO 结果获取方法名
* @return mixed
* @throws
*/
protected function search(string $method): mixed
{
$client = $this->connection->getConnection();
try {
$startTime = microtime(true);
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
$retryCount = 0;
while ($retryCount <= self::MAX_RETRIES) {
$client = $this->connection->getConnection();
try {
$startTime = microtime(true);
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
}
$prepare->execute($this->params);
$result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC);
$prepare->closeCursor();
$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) && $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');
}
$prepare->execute($this->params);
$result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC);
$prepare->closeCursor();
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
return $result;
} catch (Throwable $throwable) {
$this->getLogger()->json_log($throwable);
if ($this->isRefresh($throwable)) {
return $this->search($method);
}
$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');
}
@@ -149,44 +163,56 @@ class Command extends Component
/**
* 执行写入类 SQL 操作,内置断连重试机制
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
* @return int|bool
* @throws
*/
private function _prepare(): int|bool
{
$client = $this->connection->getConnection();
try {
$startTime = microtime(true);
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
$retryCount = 0;
while ($retryCount <= self::MAX_RETRIES) {
$client = $this->connection->getConnection();
try {
$startTime = microtime(true);
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
}
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) && $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');
}
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')) {
return $prepare->rowCount();
}
return $result == 0 ? $prepare->rowCount() : (int)$result;
} catch (Throwable $throwable) {
$this->getLogger()->json_log($throwable);
if ($this->isRefresh($throwable)) {
return $this->_prepare();
}
$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');
}
+5 -1
View File
@@ -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;
}
+8 -7
View File
@@ -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));
}
}
@@ -63,15 +63,16 @@ class DatabasesProviders extends Providers
}
/**
* @param array $database
* @return array
*/
private function _settings(array $database): array
/**
* @param string $key
* @param array $database
* @return 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'],
+378 -369
View File
@@ -9,6 +9,7 @@ namespace Database;
use JetBrains\PhpStorm\Pure;
use Kiri;
use Kiri\Abstracts\Component;
use Database\Traits\QueryTrait;
/**
@@ -18,241 +19,244 @@ use Kiri\Abstracts\Component;
class SqlBuilder extends Component
{
/**
* @var ActiveQuery|Query|ISqlBuilder|null
*/
public ActiveQuery|Query|ISqlBuilder|null $query;
/**
* @var ActiveQuery|Query|ISqlBuilder|null
*/
public ActiveQuery|Query|ISqlBuilder|null $query;
/**
* @param ActiveQuery|Query|ISqlBuilder|null $config
*/
public function __construct(ActiveQuery|Query|null|ISqlBuilder $config)
{
parent::__construct();
/**
* @param ActiveQuery|Query|ISqlBuilder|null $config
*/
public function __construct(ActiveQuery|Query|null|ISqlBuilder $config)
{
parent::__construct();
$this->query = $config;
}
$this->query = $config;
}
/**
* @param ISqlBuilder|null $query
* @return $this
* @throws
*/
public static function builder(ISqlBuilder|null $query): static
{
return new static($query);
}
/**
* @param ISqlBuilder|null $query
* @return $this
* @throws
*/
public static function builder(ISqlBuilder|null $query): static
{
return new static($query);
}
/**
* @return string
* @throws
*/
public function getCondition(): string
{
return $this->where($this->query);
}
/**
* @return string
* @throws
*/
public function getCondition(): string
{
return $this->where($this->query->where);
}
/**
* @param array $compiler
* @return string
* @throws
*/
public function hashCompiler(array $compiler): string
{
return $this->where($compiler);
}
/**
* @param array $compiler
* @return string
* @throws
*/
public function hashCompiler(array $compiler): string
{
return $this->where($compiler);
}
/**
* @param array $attributes
* @return bool|array
* @throws
*/
public function update(array $attributes): bool|string
{
$conditions = $this->query;
$this->query = [];
$data = $this->__updateBuilder($this->makeParams($attributes));
foreach ($conditions as $condition) {
$this->query->pushParam($condition);
}
return $data;
}
/**
* @param array $attributes
* @return bool|array
* @throws
*/
public function update(array $attributes): bool|string
{
$params = $this->query->params;
$this->query->params = [];
$array = $this->makeParams($attributes);
foreach ($params as $name => $value) {
$this->query->pushParam($value);
}
return $this->__updateBuilder($array);
}
/**
* @param array $attributes
* @param string $opera
* @return bool|array
* @throws
*/
public function mathematics(array $attributes, string $opera = '+'): bool|string
{
$string = [];
foreach ($attributes as $attribute => $value) {
$string[] = $attribute . '=' . $attribute . $opera . $value;
}
return $this->__updateBuilder($string);
}
/**
* @param array $attributes
* @param string $opera
* @return bool|array
* @throws
*/
public function mathematics(array $attributes, string $opera = '+'): bool|string
{
$string = [];
foreach ($attributes as $attribute => $value) {
$string[] = $attribute . '=' . $attribute . $opera . $value;
}
return $this->__updateBuilder($string);
}
/**
* @param array $string
* @return string|bool
* @throws
*/
private function __updateBuilder(array $string): string|bool
{
if (empty($string)) {
return Kiri::getLogger()->logCategory('None data update.');
}
return 'UPDATE ' . $this->query . ' SET ' . implode(',', $string) . $this->make();
}
/**
* @param array $string
* @return string|bool
* @throws
*/
private function __updateBuilder(array $string): string|bool
{
if (empty($string)) {
return Kiri::getLogger()->logCategory('None data update.');
}
return 'UPDATE ' . $this->getFromAlias() . ' ' . 'SET' . ' ' . implode(', ', $string) . ' ' . $this->make();
}
/**
* @param array $attributes
* @param false $isBatch
* @return array
* @throws
*/
public function insert(array $attributes, bool $isBatch = false): string
{
$update = 'INSERT INTO ' . $this->query;
if ($isBatch === false) {
$attributes = [$attributes];
}
$update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES ';
/**
* @param array $attributes
* @param false $isBatch
* @return array
* @throws
*/
public function insert(array $attributes, bool $isBatch = false): string
{
$update = 'INSERT INTO ' . $this->getFromAlias();
if ($isBatch === false) {
$attributes = [$attributes];
}
$update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES ';
$keys = [];
foreach ($attributes as $attribute) {
$_keys = $this->makeParams($attribute, true);
$keys = [];
foreach ($attributes as $attribute) {
$_keys = $this->makeParams($attribute, true);
$keys[] = implode(',', $_keys);
}
return $update . '(' . implode('),(', $keys) . ')';
}
$keys[] = implode(',', $_keys);
}
return $update . '(' . implode('),(', $keys) . ')';
}
/**
* @return string
* @throws
*/
public function delete(): string
{
return 'DELETE FROM ' . $this->query . $this->make();
}
/**
* @return string
* @throws
*/
public function delete(): string
{
return 'DELETE FROM ' .
$this->getFromAlias() . ' ' .
$this->make();
}
/**
* @param $attributes
* @return array
*/
#[Pure] private function getFields(array $attributes): array
{
return array_keys(current($attributes));
}
/**
* @param $attributes
* @return array
*/
#[Pure]
private function getFields(array $attributes): array
{
return array_keys(current($attributes));
}
/**
* @param array $attributes
* @param bool $isInsert
* @return array[]
* a=:b,
*/
private function makeParams(array $attributes, bool $isInsert = false): array
{
$keys = [];
foreach ($attributes as $key => $value) {
if ($isInsert === true) {
$keys[] = '?';
$this->query->pushParam($value);
} else {
$keys = $this->resolveParams($key, $value, $keys);
}
}
return $keys;
}
/**
* @param array $attributes
* @param bool $isInsert
* @return array[]
* a=:b,
*/
private function makeParams(array $attributes, bool $isInsert = false): array
{
$keys = [];
foreach ($attributes as $key => $value) {
if ($isInsert === true) {
$keys[] = '?';
$this->query->pushParam($value);
} else {
$keys = $this->resolveParams($key, $value, $keys);
}
}
return $keys;
}
/**
* @param string $key
* @param mixed $value
* @param array $keys
* @return array
*/
private function resolveParams(string $key, mixed $value, array $keys): array
{
if (is_null($value)) {
return $keys;
}
if (preg_match('/^[+|-]\s\d+$/', (string)$value)) {
$keys[] = $key . '=' . $key . ' ' . $value;
} else {
$this->query->pushParam($value);
$keys[] = $key . '= ?';
}
return $keys;
}
/**
* @param string $key
* @param mixed $value
* @param array $keys
* @return array
*/
private function resolveParams(string $key, mixed $value, array $keys): array
{
if (is_null($value)) {
return $keys;
}
if (preg_match('/^[+|-]\s\d+$/', (string)$value)) {
$keys[] = $key . '=' . $key . ' ' . $value;
} else {
$this->query->pushParam($value);
$keys[] = $key . '= ?';
}
return $keys;
}
/**
* @return string
* @throws
*/
public function one(): string
{
$this->query->offset(0)->limit(1);
return $this->makeSelect($this->query) . $this->make() . $this->makeLimit();
}
/**
* @return string
* @throws
*/
public function one(): string
{
$this->query->offset(0)->limit(1);
return $this->all();
}
/**
* @return string
* @throws
*/
public function all(): string
{
return $this->makeSelect($this->query) . $this->make() . $this->makeLimit();
}
/**
* @return string
* @throws
*/
public function all(): string
{
return $this->makeSelect($this->query) . ' ' . $this->make() . ' ' . $this->makeLimit();
}
/**
* @return string
* @throws
*/
public function count(): string
{
return $this->makeSelect(['COUNT(*)']) . $this->make();
}
/**
* @return string
* @throws
*/
public function count(): string
{
return $this->makeSelect(['COUNT(*)']) . ' ' . $this->make();
}
/**
* @return string
* @throws
*/
public function exists(): string
{
return $this->makeSelect(['0']) . $this->make();
}
/**
* @return string
* @throws
*/
public function exists(): string
{
return $this->makeSelect(['0']) . ' ' . $this->make();
}
/**
* @param string $table
* @return string
* @throws
*/
public function columns(string $table): string
{
$driver = $this->getDriver();
if (in_array($driver, ['pgsql', 'postgresql'])) {
// PostgreSQL 使用 information_schema
$tableName = trim($table, '"`');
return "SELECT
/**
* @param string $table
* @return string
* @throws
*/
public function columns(string $table): string
{
$driver = $this->getDriver();
if (in_array($driver, ['pgsql', 'postgresql'])) {
$tableName = trim($table, '"`');
return "SELECT
column_name as Field,
data_type as Type,
is_nullable as Null,
@@ -262,201 +266,206 @@ class SqlBuilder extends Component
FROM information_schema.columns
WHERE table_name = '$tableName'
ORDER BY ordinal_position";
}
// MySQL 使用 SHOW FULL FIELDS
return 'SHOW FULL FIELDS FROM ' . $table;
}
}
return 'SHOW FULL FIELDS FROM ' . $table;
}
/**
* @return string
* @throws
*/
private function make(): string
{
$select = $this->makeCondition();
$select .= $this->makeGroup();
$select .= $this->makeOrder();
return $select;
}
/**
* @return string
* @throws
*/
private function make(): string
{
return $this->makeCondition() . ' ' . $this->makeGroup() . ' ' . $this->makeOrder();
}
/**
* @param array $select
* @return string
*/
private function makeSelect(array $select = ['*']): string
{
$select = "SELECT " . implode(',', $select) . " FROM " . $this->query;
if ($this->query != "") {
$select .= " AS " . $this->query;
}
if (count($this->query) > 0) {
$select .= ' ' . implode(' ', $this->query);
}
return $select;
}
/**
* @param mixed $select 列名数组或 QueryTrait 对象
* @return string
*/
private function makeSelect(mixed $select = ['*']): string
{
if (!is_array($select)) {
$select = ['*'];
}
$sql = 'SELECT ' . implode(',', $select) . ' FROM ' . $this->getFromAlias();
if ($this->query->join !== []) {
$sql .= ' ' . implode(' ', $this->query->join);
}
return $sql;
}
/**
* @return string
*/
private function makeGroup(): string
{
if ($this->query != "") {
return ' GROUP BY ' . $this->query;
}
return '';
}
/**
* @return string
*/
private function makeGroup(): string
{
if ($this->query->group !== '' && $this->query->group !== '0') {
return 'GROUP BY ' . $this->query->group;
}
return '';
}
/**
* @return string
*/
private function makeOrder(): string
{
if (count($this->query) > 0) {
return ' ORDER BY ' . implode(',', $this->query);
}
return '';
}
/**
* @return string
*/
private function makeOrder(): string
{
if (count($this->query->order) > 0) {
return 'ORDER BY ' . implode(',', $this->query->order);
}
return '';
}
/**
* @return string
*/
private function makeCondition(): string
{
$condition = $this->where($this->query);
if (empty($condition)) {
return '';
}
return ' WHERE ' . $condition;
}
/**
* @return string
*/
private function makeCondition(): string
{
$condition = $this->where($this->query->where);
if (empty($condition)) {
return '';
}
return 'WHERE ' . $condition;
}
/**
* @return string
* @throws
*/
private function makeLimit(): string
{
if ($this->query >= 0 && $this->query >= 1) {
$driver = $this->getDriver();
if (in_array($driver, ['pgsql', 'postgresql'])) {
// PostgreSQL 使用 LIMIT ... OFFSET ... 语法
return ' LIMIT ' . $this->query . ' OFFSET ' . $this->query;
}
// MySQL 使用 LIMIT offset,limit 语法
return ' LIMIT ' . $this->query . ',' . $this->query;
}
return '';
}
/**
* @return string
* @throws
*/
private function makeLimit(): string
{
if ($this->query->offset >= 0 && $this->query->limit >= 1) {
$driver = $this->getDriver();
if (in_array($driver, ['pgsql', 'postgresql'])) {
return 'LIMIT ' . $this->query->limit . ' OFFSET ' . $this->query->offset;
}
return 'LIMIT ' . $this->query->offset . ',' . $this->query->limit;
}
return '';
}
/**
* @param false $isCount
* @return string
* @throws
*/
public function get(bool $isCount = false): string
{
if ($isCount === false) {
return $this->all();
}
return $this->count();
}
/**
* @param false $isCount
* @return string
* @throws
*/
public function get(bool $isCount = false): string
{
if ($isCount === false) {
return $this->all();
}
return $this->count();
}
/**
* @return string
* @throws
*/
public function truncate(): string
{
return sprintf('TRUNCATE %s', $this->query);
}
/**
* @return string
* @throws
*/
public function truncate(): string
{
return sprintf('TRUNCATE %s', $this->getFromAlias());
}
/**
* @param array $where
* @return string
*/
private function where(array $where): string
{
if (count($where) < 1) {
return '';
}
$_tmp = [];
foreach ($where as $key => $value) {
$_tmp[] = $this->resolveCondition($key, $value);
}
return implode(' AND ', $_tmp);
}
/**
* @param array $where
* @return string
*/
private function where(array $where): string
{
if (count($where) < 1) {
return '';
}
$_tmp = [];
foreach ($where as $key => $value) {
$_tmp[] = $this->resolveCondition($key, $value);
}
return implode(' AND ', $_tmp);
}
/**
* @param string $field
* @param mixed $condition
* @return string
*/
private function resolveCondition(string|int $field, mixed $condition): string
{
if (is_string($field)) {
$this->query->pushParam($condition);
return $field . ' = ?';
} else if (is_string($condition)) {
return $condition;
} else {
return implode(' AND ', $this->_hashMap($condition));
}
}
/**
* @param string $field
* @param mixed $condition
* @return string
*/
private function resolveCondition(string|int $field, mixed $condition): string
{
if (is_string($field)) {
$this->query->pushParam($condition);
return $field . ' = ?';
} else if (is_string($condition)) {
return $condition;
} else {
return implode(' AND ', $this->_hashMap($condition));
}
}
/**
* @param $condition
* @return array
*/
private function _hashMap(array $condition): array
{
$_array = [];
foreach ($condition as $key => $value) {
$this->query->pushParam($value);
$_array[] = $key . '= ?';
}
return $_array;
}
/**
* @param $condition
* @return array
*/
private function _hashMap(array $condition): array
{
$_array = [];
foreach ($condition as $key => $value) {
$this->query->pushParam($value);
$_array[] = $key . '= ?';
}
return $_array;
}
/**
* 获取数据库驱动类型
* @return string
* @throws
*/
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();
if ($connection instanceof Connection) {
return strtolower($connection->driver ?? 'mysql');
}
}
}
// 如果是 Db 查询,尝试获取默认 connection
if (method_exists($this->query, 'getConnection')) {
$connection = $this->query->getConnection();
if ($connection instanceof Connection) {
return strtolower($connection->driver ?? 'mysql');
}
}
} catch (\Throwable $e) {
// 忽略错误,返回默认值
/**
* 获取数据库驱动类型
* @return string
* @throws
*/
private function getDriver(): string
{
try {
if ($this->query instanceof ActiveQuery && $this->query->modelClass !== null) {
if ($this->query->modelClass instanceof Model) {
$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) {
return strtolower($connection->driver ?? 'mysql');
}
}
} catch (\Throwable $e) {
$this->getLogger()->json_log($e);
}
// 默认返回 mysql
return '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;
}
}
+12
View File
@@ -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.
* @throws