Compare commits

..

36 Commits

Author SHA1 Message Date
as2252258 dce38ad64e eee 2026-07-07 11:28:53 +08:00
as2252258 fe9d054be0 eee 2026-07-07 02:08:19 +08:00
as2252258 abec2ffbff eee 2026-07-07 02:06:57 +08:00
as2252258 bcd75c84ac eee 2026-07-06 21:26:40 +08:00
as2252258 06de50699a eee 2026-07-06 21:25:47 +08:00
as2252258 efb3077bec eee 2026-07-06 21:21:08 +08:00
as2252258 008e6ababa eee 2026-07-06 21:20:58 +08:00
as2252258 99f458f81e eee 2026-07-06 21:18:48 +08:00
as2252258 3e72503b6a eee 2026-07-06 21:15:25 +08:00
as2252258 26395d7d29 eee 2026-07-06 21:13:07 +08:00
as2252258 98c4a9cf0f eee 2026-07-06 21:10:56 +08:00
as2252258 395d31e045 eee 2026-07-06 21:09:37 +08:00
as2252258 efea04878f eee 2026-07-06 21:08:18 +08:00
as2252258 703ca7bb76 eee 2026-07-06 21:06:09 +08:00
as2252258 ade6e48f0b eee 2026-07-06 21:04:28 +08:00
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
6 changed files with 496 additions and 479 deletions
+1
View File
@@ -487,6 +487,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
return TRUE; return TRUE;
} }
$generate = SqlBuilder::builder($query)->update($change); $generate = SqlBuilder::builder($query)->update($change);
var_dump($generate, $query->params);
if ($generate === FALSE) { if ($generate === FALSE) {
return FALSE; return FALSE;
} }
+4 -4
View File
@@ -148,8 +148,7 @@ class Command extends Component
} }
} }
$errorMsg = 'Max retry exceeded for SQL: ' . $this->sql; return $this->getLogger()->logCategory('Max retry exceeded for SQL: ' . $this->sql, 'mysql');
return $this->getLogger()->logCategory($errorMsg, 'mysql');
} }
@@ -180,6 +179,8 @@ class Command extends Component
if (($prepare = $client->prepare($this->sql)) === false) { if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]); throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
} }
if ($prepare->execute($this->params) === false) { if ($prepare->execute($this->params) === false) {
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]); throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
} }
@@ -213,8 +214,7 @@ class Command extends Component
} }
} }
$errorMsg = 'Max retry exceeded for SQL: ' . $this->sql; return $this->getLogger()->logCategory('Max retry exceeded for SQL: ' . $this->sql, 'mysql');
return $this->getLogger()->logCategory($errorMsg, 'mysql');
} }
+4 -4
View File
@@ -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;
} }
@@ -372,9 +372,9 @@ class Connection extends Component
]; ];
// MySQL 特定的选项 // MySQL 特定的选项
if ($driver === 'mysql') { // if ($driver === 'mysql') {
$options[\PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $this->charset; // $options[\PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $this->charset;
} // }
$pdo = new PDO($this->database, $this->cds, $this->username, $this->password, $options, $driver); $pdo = new PDO($this->database, $this->cds, $this->username, $this->password, $options, $driver);
foreach ($this->attributes as $key => $attribute) { foreach ($this->attributes as $key => $attribute) {
+4 -3
View File
@@ -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));
} }
} }
@@ -64,14 +64,15 @@ class DatabasesProviders extends Providers
/** /**
* @param string $key
* @param array $database * @param array $database
* @return array * @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'],
+23 -20
View File
@@ -75,13 +75,13 @@ class SqlBuilder extends Component
*/ */
public function update(array $attributes): bool|string public function update(array $attributes): bool|string
{ {
$conditions = $this->query; $params = $this->query->params;
$this->query = []; $this->query->params = [];
$data = $this->__updateBuilder($this->makeParams($attributes)); $array = $this->makeParams($attributes);
foreach ($conditions as $condition) { foreach ($params as $name => $value) {
$this->query->pushParam($condition); $this->query->pushParam($value);
} }
return $data; return $this->__updateBuilder($array);
} }
@@ -111,7 +111,8 @@ 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->getFromAlias() . ' SET ' . implode(',', $string) . $this->make();
return 'UPDATE ' . $this->getFromAlias() . ' ' . 'SET' . ' ' . implode(', ', $string) . ' ' . $this->make();
} }
@@ -123,7 +124,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->getFromAlias(); $update = 'INSERT INTO ' . $this->getFromAlias(true);
if ($isBatch === false) { if ($isBatch === false) {
$attributes = [$attributes]; $attributes = [$attributes];
} }
@@ -145,7 +146,7 @@ class SqlBuilder extends Component
*/ */
public function delete(): string public function delete(): string
{ {
return 'DELETE FROM ' . $this->getFromAlias() . $this->make(); return 'DELETE FROM ' . $this->getFromAlias() . ' ' . $this->make();
} }
@@ -153,7 +154,8 @@ class SqlBuilder extends Component
* @param $attributes * @param $attributes
* @return array * @return array
*/ */
#[Pure] private function getFields(array $attributes): array #[Pure]
private function getFields(array $attributes): array
{ {
return array_keys(current($attributes)); return array_keys(current($attributes));
} }
@@ -208,7 +210,7 @@ class SqlBuilder extends Component
public function one(): string public function one(): string
{ {
$this->query->offset(0)->limit(1); $this->query->offset(0)->limit(1);
return $this->makeSelect($this->query) . $this->make() . $this->makeLimit(); return $this->all();
} }
@@ -218,7 +220,7 @@ class SqlBuilder extends Component
*/ */
public function all(): string public function all(): string
{ {
return $this->makeSelect($this->query) . $this->make() . $this->makeLimit(); return $this->makeSelect($this->query->select) . ' ' . $this->make() . ' ' . $this->makeLimit();
} }
@@ -228,7 +230,7 @@ class SqlBuilder extends Component
*/ */
public function count(): string public function count(): string
{ {
return $this->makeSelect(['COUNT(*)']) . $this->make(); return $this->makeSelect(['COUNT(*)']) . ' ' . $this->make();
} }
@@ -238,7 +240,7 @@ class SqlBuilder extends Component
*/ */
public function exists(): string public function exists(): string
{ {
return $this->makeSelect(['0']) . $this->make(); return $this->makeSelect(['0']) . ' ' . $this->make();
} }
@@ -273,10 +275,7 @@ class SqlBuilder extends Component
*/ */
private function make(): string private function make(): string
{ {
$select = $this->makeCondition(); return $this->makeCondition() . ' ' . $this->makeGroup() . ' ' . $this->makeOrder();
$select .= $this->makeGroup();
$select .= $this->makeOrder();
return $select;
} }
/** /**
@@ -288,7 +287,7 @@ class SqlBuilder extends Component
if (!is_array($select)) { if (!is_array($select)) {
$select = ['*']; $select = ['*'];
} }
$sql = "SELECT " . implode(',', $select) . " FROM " . $this->getFromAlias(); $sql = 'SELECT ' . implode(',', $select) . ' FROM ' . $this->getFromAlias();
if ($this->query->join !== []) { if ($this->query->join !== []) {
$sql .= ' ' . implode(' ', $this->query->join); $sql .= ' ' . implode(' ', $this->query->join);
} }
@@ -457,9 +456,13 @@ class SqlBuilder extends Component
* QueryTrait 对象有 $from 和 $alias 属性 * QueryTrait 对象有 $from 和 $alias 属性
* @return string * @return string
*/ */
private function getFromAlias(): string private function getFromAlias(bool $insert = false): string
{ {
$from = $this->query->from; $from = $this->query->from;
if ($insert) {
return $from;
}
if ($this->query->alias !== '' && $this->query->alias !== '0') { if ($this->query->alias !== '' && $this->query->alias !== '0') {
$from .= ' AS ' . $this->query->alias; $from .= ' AS ' . $this->query->alias;
} }
+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. * Comply constructor.
* @throws * @throws