Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dce38ad64e | |||
| fe9d054be0 | |||
| abec2ffbff | |||
| bcd75c84ac | |||
| 06de50699a | |||
| efb3077bec | |||
| 008e6ababa | |||
| 99f458f81e | |||
| 3e72503b6a | |||
| 26395d7d29 | |||
| 98c4a9cf0f | |||
| 395d31e045 | |||
| efea04878f | |||
| 703ca7bb76 | |||
| ade6e48f0b | |||
| 39905b77e3 | |||
| fce82ec56c | |||
| 2970c3bc6b | |||
| a35d0bb921 | |||
| fce2b8bcdb | |||
| ce92f310a5 | |||
| 28fa17ecfb | |||
| 9fc910f10f | |||
| 0a95e722d6 | |||
| 9f2611972f | |||
| b7e59d1c96 | |||
| 9b0ee54608 | |||
| 763a4baa57 | |||
| fe639bbd1f | |||
| e40de4ee4a | |||
| ffc2af69da | |||
| 218a38da48 | |||
| 5acb2f4600 | |||
| d6a07446bb | |||
| 6c936c3896 | |||
| 14448d824a |
@@ -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
@@ -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
@@ -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) {
|
||||||
|
|||||||
@@ -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
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user