This commit is contained in:
2026-06-24 22:00:50 +08:00
parent 404b511c5f
commit 06074c38c1
+34 -29
View File
@@ -9,6 +9,7 @@ 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;
/** /**
@@ -52,7 +53,7 @@ class SqlBuilder extends Component
*/ */
public function getCondition(): string public function getCondition(): string
{ {
return $this->where($this->query); return $this->where($this->query->where);
} }
@@ -110,7 +111,7 @@ class SqlBuilder extends Component
if (empty($string)) { if (empty($string)) {
return Kiri::getLogger()->logCategory('None data update.'); return Kiri::getLogger()->logCategory('None data update.');
} }
return 'UPDATE ' . $this->query . ' SET ' . implode(',', $string) . $this->make(); return 'UPDATE ' . $this->getFromAlias() . ' SET ' . implode(',', $string) . $this->make();
} }
@@ -122,7 +123,7 @@ class SqlBuilder extends Component
*/ */
public function insert(array $attributes, bool $isBatch = false): string public function insert(array $attributes, bool $isBatch = false): string
{ {
$update = 'INSERT INTO ' . $this->query; $update = 'INSERT INTO ' . $this->getFromAlias();
if ($isBatch === false) { if ($isBatch === false) {
$attributes = [$attributes]; $attributes = [$attributes];
} }
@@ -144,7 +145,7 @@ class SqlBuilder extends Component
*/ */
public function delete(): string public function delete(): string
{ {
return 'DELETE FROM ' . $this->query . $this->make(); return 'DELETE FROM ' . $this->getFromAlias() . $this->make();
} }
@@ -250,7 +251,6 @@ class SqlBuilder extends Component
{ {
$driver = $this->getDriver(); $driver = $this->getDriver();
if (in_array($driver, ['pgsql', 'postgresql'])) { if (in_array($driver, ['pgsql', 'postgresql'])) {
// PostgreSQL 使用 information_schema
$tableName = trim($table, '"`'); $tableName = trim($table, '"`');
return "SELECT return "SELECT
column_name as Field, column_name as Field,
@@ -263,7 +263,6 @@ class SqlBuilder extends Component
WHERE table_name = '$tableName' WHERE table_name = '$tableName'
ORDER BY ordinal_position"; ORDER BY ordinal_position";
} }
// MySQL 使用 SHOW FULL FIELDS
return 'SHOW FULL FIELDS FROM ' . $table; return 'SHOW FULL FIELDS FROM ' . $table;
} }
@@ -281,7 +280,7 @@ class SqlBuilder extends Component
} }
/** /**
* @param array $select * @param mixed $select 列名数组或 QueryTrait 对象
* @return string * @return string
*/ */
private function makeSelect(mixed $select = ['*']): string private function makeSelect(mixed $select = ['*']): string
@@ -289,14 +288,11 @@ class SqlBuilder extends Component
if (!is_array($select)) { if (!is_array($select)) {
$select = ['*']; $select = ['*'];
} }
$select = "SELECT " . implode(',', $select) . " FROM " . $this->query; $sql = "SELECT " . implode(',', $select) . " FROM " . $this->getFromAlias();
if ($this->query != "") { if ($this->query->join !== []) {
$select .= " AS " . $this->query; $sql .= ' ' . implode(' ', $this->query->join);
} }
if (count($this->query) > 0) { return $sql;
$select .= ' ' . implode(' ', $this->query);
}
return $select;
} }
@@ -305,8 +301,8 @@ class SqlBuilder extends Component
*/ */
private function makeGroup(): string private function makeGroup(): string
{ {
if ($this->query != "") { if ($this->query->group !== '' && $this->query->group !== '0') {
return ' GROUP BY ' . $this->query; return ' GROUP BY ' . $this->query->group;
} }
return ''; return '';
} }
@@ -317,8 +313,8 @@ class SqlBuilder extends Component
*/ */
private function makeOrder(): string private function makeOrder(): string
{ {
if (count($this->query) > 0) { if (count($this->query->order) > 0) {
return ' ORDER BY ' . implode(',', $this->query); return ' ORDER BY ' . implode(',', $this->query->order);
} }
return ''; return '';
} }
@@ -329,7 +325,7 @@ class SqlBuilder extends Component
*/ */
private function makeCondition(): string private function makeCondition(): string
{ {
$condition = $this->where($this->query); $condition = $this->where($this->query->where);
if (empty($condition)) { if (empty($condition)) {
return ''; return '';
} }
@@ -343,14 +339,12 @@ class SqlBuilder extends Component
*/ */
private function makeLimit(): string private function makeLimit(): string
{ {
if ($this->query >= 0 && $this->query >= 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 . ' OFFSET ' . $this->query;
} }
// MySQL 使用 LIMIT offset,limit 语法 return ' LIMIT ' . $this->query->offset . ',' . $this->query->limit;
return ' LIMIT ' . $this->query . ',' . $this->query;
} }
return ''; return '';
} }
@@ -376,7 +370,7 @@ class SqlBuilder extends Component
*/ */
public function truncate(): string public function truncate(): string
{ {
return sprintf('TRUNCATE %s', $this->query); return sprintf('TRUNCATE %s', $this->getFromAlias());
} }
@@ -437,7 +431,6 @@ class SqlBuilder extends Component
private function getDriver(): string private function getDriver(): string
{ {
try { try {
// 尝试从 query 对象获取 connection
if ($this->query instanceof ActiveQuery && $this->query->modelClass !== null) { if ($this->query instanceof ActiveQuery && $this->query->modelClass !== null) {
if ($this->query->modelClass instanceof Model) { if ($this->query->modelClass instanceof Model) {
$connection = $this->query->modelClass->getConnection(); $connection = $this->query->modelClass->getConnection();
@@ -446,7 +439,6 @@ class SqlBuilder extends Component
} }
} }
} }
// 如果是 Db 查询,尝试获取默认 connection
if (method_exists($this->query, 'getConnection')) { if (method_exists($this->query, 'getConnection')) {
$connection = $this->query->getConnection(); $connection = $this->query->getConnection();
if ($connection instanceof Connection) { if ($connection instanceof Connection) {
@@ -454,12 +446,25 @@ class SqlBuilder extends Component
} }
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
// 忽略错误,返回默认值
$this->getLogger()->json_log($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;
}
} }