Compare commits

...

10 Commits

Author SHA1 Message Date
as2252258 516c7612a5 改名 2021-07-08 18:02:21 +08:00
as2252258 3bb3d5a1d7 改名 2021-07-08 17:36:28 +08:00
as2252258 fb48a0b508 改名 2021-07-07 18:44:29 +08:00
as2252258 a3964faa72 改名 2021-07-07 18:39:03 +08:00
as2252258 0fea7ac770 改名 2021-07-07 18:37:43 +08:00
as2252258 7eccee5d16 改名 2021-07-07 18:36:20 +08:00
as2252258 565943b76f 改名 2021-07-07 17:51:02 +08:00
as2252258 adb95af124 改名 2021-07-07 14:11:17 +08:00
as2252258 b554b53cfd 改名 2021-07-07 14:07:27 +08:00
as2252258 0e0caaf8a9 改名 2021-07-07 14:04:05 +08:00
13 changed files with 403 additions and 339 deletions
+29 -9
View File
@@ -11,7 +11,6 @@ namespace Database;
use Database\Traits\QueryTrait; use Database\Traits\QueryTrait;
use Exception; use Exception;
use HttpServer\Http\Context;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -93,6 +92,27 @@ class Db implements ISqlBuilder
return 'ANY_VALUE(' . $column . ') as ' . $alias; return 'ANY_VALUE(' . $column . ') as ' . $alias;
} }
/**
* @param string $column
* @return string
*/
public static function increment(string $column): string
{
return '+ ' . $column;
}
/**
* @param string $column
* @return string
*/
public static function decrement(string $column): string
{
return '- ' . $column;
}
/** /**
* @param Connection|null $db * @param Connection|null $db
* @return mixed * @return mixed
@@ -167,19 +187,19 @@ class Db implements ISqlBuilder
*/ */
public static function findAllBySql(string $sql, array $attributes = [], Connection $db = NULL): int|bool|array|string|null public static function findAllBySql(string $sql, array $attributes = [], Connection $db = NULL): int|bool|array|string|null
{ {
return $db->createCommand($sql, $attributes)->all(); return $db->createCommand($sql, $db?->database, $attributes)->all();
} }
/** /**
* @param string $sql * @param string $sql
* @param array $attributes * @param array $attributes
* @param Connection|NULL $db * @param Connection|NULL $db
* @return array|mixed * @return string|array|bool|int|null
* @throws Exception * @throws Exception
*/ */
public static function findBySql(string $sql, array $attributes = [], Connection $db = NULL): mixed public static function findBySql(string $sql, array $attributes = [], Connection $db = NULL): string|array|bool|int|null
{ {
return $db->createCommand($sql, $attributes)->one(); return $db->createCommand($sql, $db?->database, $attributes)->one();
} }
/** /**
@@ -241,7 +261,7 @@ class Db implements ISqlBuilder
if (empty($db)) { if (empty($db)) {
$db = Snowflake::app()->get('db'); $db = Snowflake::app()->get('db');
} }
return $db->createCommand('DROP TABLE ' . $table)->delete(); return $db->createCommand('DROP TABLE `' . $db->database . '`.' . $table)->delete();
} }
/** /**
@@ -257,7 +277,7 @@ class Db implements ISqlBuilder
$db = Snowflake::app()->get('db'); $db = Snowflake::app()->get('db');
} }
return $db->createCommand('TRUNCATE ' . $table)->exec(); return $db->createCommand('TRUNCATE `' . $db->database . '`.' . $table)->exec();
} }
/** /**
@@ -278,7 +298,7 @@ class Db implements ISqlBuilder
return null; return null;
} }
return $db->createCommand('SHOW CREATE TABLE ' . $table)->one(); return $db->createCommand('SHOW CREATE TABLE `' . $db->database . '`.' . $table)->one();
} }
/** /**
@@ -297,7 +317,7 @@ class Db implements ISqlBuilder
return null; return null;
} }
return $db->createCommand('SHOW FULL FIELDS FROM ' . $table)->all(); return $db->createCommand('SHOW FULL FIELDS FROM `' . $db->database . '`.' . $table)->all();
} }
+16 -17
View File
@@ -52,19 +52,8 @@ class SqlBuilder extends Component
public function update(array $attributes): bool|array public function update(array $attributes): bool|array
{ {
[$string, $array] = $this->builderParams($attributes); [$string, $array] = $this->builderParams($attributes);
if (empty($string) || empty($array)) {
return $this->addError('None data update.');
}
$condition = $this->conditionToString(); return $this->__updateBuilder($string, $array);
if (!empty($condition)) {
$condition = ' WHERE ' . $condition;
}
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition;
$update .= $this->builderLimit($this->query);
return [$update, $array];
} }
@@ -76,12 +65,22 @@ class SqlBuilder extends Component
*/ */
public function mathematics(array $attributes, string $opera = '+'): bool|array public function mathematics(array $attributes, string $opera = '+'): bool|array
{ {
$string = $array = []; $string = [];
foreach ($attributes as $attribute => $value) { foreach ($attributes as $attribute => $value) {
$string[] = $attribute . '=' . $attribute . $opera . $value; $string[] = $attribute . '=' . $attribute . $opera . $value;
} }
return $this->__updateBuilder($string, []);
}
/**
* @param array $string
* @param array $params
* @return array|bool
* @throws Exception
*/
private function __updateBuilder(array $string, array $params): array|bool
{
if (empty($string)) { if (empty($string)) {
return $this->addError('None data update.'); return $this->addError('None data update.');
} }
@@ -92,9 +91,9 @@ class SqlBuilder extends Component
} }
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition; $update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition;
$update .= $this->builderLimit($this->query); $update .= $this->builderLimit($this->query, false);
return [$update, []]; return [$update, $params];
} }
@@ -185,7 +184,7 @@ class SqlBuilder extends Component
str_starts_with($value, '+ ') || str_starts_with($value, '+ ') ||
str_starts_with($value, '- ') str_starts_with($value, '- ')
) { ) {
$keys[] = $key . '=' . $key . $value; $keys[] = $key . '=' . $key . ' ' . $value;
} else { } else {
$params[$key . $order] = $value; $params[$key . $order] = $value;
$keys[] = $key . '=:' . $key . $order; $keys[] = $key . '=:' . $key . $order;
+3 -2
View File
@@ -104,14 +104,15 @@ trait Builder
/** /**
* @param ActiveQuery|Query $query * @param ActiveQuery|Query $query
* @param bool $hasLimit
* @return string * @return string
*/ */
#[Pure] private function builderLimit(ActiveQuery|Query $query): string #[Pure] private function builderLimit(ActiveQuery|Query $query, bool $hasLimit = true): string
{ {
if (!is_numeric($query->limit) || $query->limit < 1) { if (!is_numeric($query->limit) || $query->limit < 1) {
return ""; return "";
} }
if ($query->offset !== null) { if ($query->offset !== null && $hasLimit) {
return ' LIMIT ' . $query->offset . ',' . $query->limit; return ' LIMIT ' . $query->offset . ',' . $query->limit;
} }
return ' LIMIT ' . $query->limit; return ' LIMIT ' . $query->limit;
+3 -3
View File
@@ -12,9 +12,6 @@ namespace Gii;
use Database\Connection; use Database\Connection;
use Database\Db; use Database\Db;
use Exception; use Exception;
use JetBrains\PhpStorm\ArrayShape;
use Snowflake\Abstracts\Config;
use Snowflake\Abstracts\Input; use Snowflake\Abstracts\Input;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
@@ -42,6 +39,9 @@ class Gii
public string $controllerNamespace = 'App\\Http\\Controllers\\'; public string $controllerNamespace = 'App\\Http\\Controllers\\';
public static array $createSqls = [];
public array $keyword = [ public array $keyword = [
'ADD', 'ALL', 'ALTER', 'AND', 'AS', 'ASC', 'ASENSITIVE', 'BEFORE', 'BETWEEN', 'BIGINT', 'BINARY', 'BLOB', 'BOTH', 'BY', 'CALL', 'CASCADE', 'CASE', 'CHANGE', 'CHAR', 'CHARACTER', 'CHECK', 'COLLATE', 'COLUMN', 'CONDITION', 'CONNECTION', 'CONSTRAINT', 'CONTINUE', 'CONVERT', 'CREATE', 'CROSS', 'CURRENT_DATE', 'CURRENT_TIME', 'CURRENT_TIMESTAMP', 'CURRENT_USER', 'CURSOR', 'DATABASE', 'DATABASES', 'DAY_HOUR', 'DAY_MICROSECOND', 'DAY_MINUTE', 'DAY_SECOND', 'DEC', 'DECIMAL', 'DECLARE', 'DEFAULT', 'DELAYED', 'DELETE', 'DESC', 'DESCRIBE', 'DETERMINISTIC', 'DISTINCT', 'DISTINCTROW', 'DIV', 'DOUBLE', 'DROP', 'DUAL', 'EACH', 'ELSE', 'ELSEIF', 'ENCLOSED', 'ESCAPED', 'EXISTS', 'EXIT', 'EXPLAIN', 'FALSE', 'FETCH', 'FLOAT', 'FLOAT4', 'FLOAT8', 'FOR', 'FORCE', 'FOREIGN', 'FROM', 'FULLTEXT', 'GOTO', 'GRANT', 'GROUP', 'HAVING', 'HIGH_PRIORITY', 'HOUR_MICROSECOND', 'HOUR_MINUTE', 'HOUR_SECOND', 'IF', 'IGNORE', 'IN', 'INDEX', 'INFILE', 'INNER', 'INOUT', 'INSENSITIVE', 'INSERT', 'INT', 'INT1', 'INT2', 'INT3', 'INT4', 'INT8', 'INTEGER', 'INTERVAL', 'INTO', 'IS', 'ITERATE', 'JOIN', 'KEY', 'KEYS', 'KILL', 'LABEL', 'LEADING', 'LEAVE', 'LEFT', 'LIKE', 'LIMIT', 'LINEAR', 'LINES', 'LOAD', 'LOCALTIME', 'LOCALTIMESTAMP', 'LOCK', 'LONG', 'LONGBLOB', 'LONGTEXT', 'LOOP', 'LOW_PRIORITY', 'MATCH', 'MEDIUMBLOB', 'MEDIUMINT', 'MEDIUMTEXT', 'MIDDLEINT', 'MINUTE_MICROSECOND', 'MINUTE_SECOND', 'MOD', 'MODIFIES', 'NATURAL', 'NOT', 'NO_WRITE_TO_BINLOG', 'NULL', 'NUMERIC', 'ON', 'OPTIMIZE', 'OPTION', 'OPTIONALLY', 'OR', 'ORDER', 'OUT', 'OUTER', 'OUTFILE', 'PRECISION', 'PRIMARY', 'PROCEDURE', 'PURGE', 'RAID0', 'RANGE', 'READ', 'READS', 'REAL', 'REFERENCES', 'REGEXP', 'RELEASE', 'RENAME', 'REPEAT', 'REPLACE', 'REQUIRE', 'RESTRICT', 'RETURN', 'REVOKE', 'RIGHT', 'RLIKE', 'SCHEMA', 'SCHEMAS', 'SECOND_MICROSECOND', 'SELECT', 'SENSITIVE', 'SEPARATOR', 'SET', 'SHOW', 'SMALLINT', 'SPATIAL', 'SPECIFIC', 'SQL', 'SQLEXCEPTION', 'SQLSTATE', 'SQLWARNING', 'SQL_BIG_RESULT', 'SQL_CALC_FOUND_ROWS', 'SQL_SMALL_RESULT', 'SSL', 'STARTING', 'STRAIGHT_JOIN', 'TABLE', 'TERMINATED', 'THEN', 'TINYBLOB', 'TINYINT', 'TINYTEXT', 'TO', 'TRAILING', 'TRIGGER', 'TRUE', 'UNDO', 'UNION', 'UNIQUE', 'UNLOCK', 'UNSIGNED', 'UPDATE', 'USAGE', 'USE', 'USING', 'UTC_DATE', 'UTC_TIME', 'UTC_TIMESTAMP', 'VALUES', 'VARBINARY', 'VARCHAR', 'VARCHARACTER', 'VARYING', 'WHEN', 'WHERE', 'WHILE', 'WITH', 'WRITE', 'X509', 'XOR', 'YEAR_MONTH', 'ZEROFILL' 'ADD', 'ALL', 'ALTER', 'AND', 'AS', 'ASC', 'ASENSITIVE', 'BEFORE', 'BETWEEN', 'BIGINT', 'BINARY', 'BLOB', 'BOTH', 'BY', 'CALL', 'CASCADE', 'CASE', 'CHANGE', 'CHAR', 'CHARACTER', 'CHECK', 'COLLATE', 'COLUMN', 'CONDITION', 'CONNECTION', 'CONSTRAINT', 'CONTINUE', 'CONVERT', 'CREATE', 'CROSS', 'CURRENT_DATE', 'CURRENT_TIME', 'CURRENT_TIMESTAMP', 'CURRENT_USER', 'CURSOR', 'DATABASE', 'DATABASES', 'DAY_HOUR', 'DAY_MICROSECOND', 'DAY_MINUTE', 'DAY_SECOND', 'DEC', 'DECIMAL', 'DECLARE', 'DEFAULT', 'DELAYED', 'DELETE', 'DESC', 'DESCRIBE', 'DETERMINISTIC', 'DISTINCT', 'DISTINCTROW', 'DIV', 'DOUBLE', 'DROP', 'DUAL', 'EACH', 'ELSE', 'ELSEIF', 'ENCLOSED', 'ESCAPED', 'EXISTS', 'EXIT', 'EXPLAIN', 'FALSE', 'FETCH', 'FLOAT', 'FLOAT4', 'FLOAT8', 'FOR', 'FORCE', 'FOREIGN', 'FROM', 'FULLTEXT', 'GOTO', 'GRANT', 'GROUP', 'HAVING', 'HIGH_PRIORITY', 'HOUR_MICROSECOND', 'HOUR_MINUTE', 'HOUR_SECOND', 'IF', 'IGNORE', 'IN', 'INDEX', 'INFILE', 'INNER', 'INOUT', 'INSENSITIVE', 'INSERT', 'INT', 'INT1', 'INT2', 'INT3', 'INT4', 'INT8', 'INTEGER', 'INTERVAL', 'INTO', 'IS', 'ITERATE', 'JOIN', 'KEY', 'KEYS', 'KILL', 'LABEL', 'LEADING', 'LEAVE', 'LEFT', 'LIKE', 'LIMIT', 'LINEAR', 'LINES', 'LOAD', 'LOCALTIME', 'LOCALTIMESTAMP', 'LOCK', 'LONG', 'LONGBLOB', 'LONGTEXT', 'LOOP', 'LOW_PRIORITY', 'MATCH', 'MEDIUMBLOB', 'MEDIUMINT', 'MEDIUMTEXT', 'MIDDLEINT', 'MINUTE_MICROSECOND', 'MINUTE_SECOND', 'MOD', 'MODIFIES', 'NATURAL', 'NOT', 'NO_WRITE_TO_BINLOG', 'NULL', 'NUMERIC', 'ON', 'OPTIMIZE', 'OPTION', 'OPTIONALLY', 'OR', 'ORDER', 'OUT', 'OUTER', 'OUTFILE', 'PRECISION', 'PRIMARY', 'PROCEDURE', 'PURGE', 'RAID0', 'RANGE', 'READ', 'READS', 'REAL', 'REFERENCES', 'REGEXP', 'RELEASE', 'RENAME', 'REPEAT', 'REPLACE', 'REQUIRE', 'RESTRICT', 'RETURN', 'REVOKE', 'RIGHT', 'RLIKE', 'SCHEMA', 'SCHEMAS', 'SECOND_MICROSECOND', 'SELECT', 'SENSITIVE', 'SEPARATOR', 'SET', 'SHOW', 'SMALLINT', 'SPATIAL', 'SPECIFIC', 'SQL', 'SQLEXCEPTION', 'SQLSTATE', 'SQLWARNING', 'SQL_BIG_RESULT', 'SQL_CALC_FOUND_ROWS', 'SQL_SMALL_RESULT', 'SSL', 'STARTING', 'STRAIGHT_JOIN', 'TABLE', 'TERMINATED', 'THEN', 'TINYBLOB', 'TINYINT', 'TINYTEXT', 'TO', 'TRAILING', 'TRIGGER', 'TRUE', 'UNDO', 'UNION', 'UNIQUE', 'UNLOCK', 'UNSIGNED', 'UPDATE', 'USAGE', 'USE', 'USING', 'UTC_DATE', 'UTC_TIME', 'UTC_TIMESTAMP', 'VALUES', 'VARBINARY', 'VARCHAR', 'VARCHARACTER', 'VARYING', 'WHEN', 'WHERE', 'WHILE', 'WITH', 'WRITE', 'X509', 'XOR', 'YEAR_MONTH', 'ZEROFILL'
]; ];
+20 -12
View File
@@ -22,12 +22,12 @@ class GiiModel extends GiiBase
public ?array $fields; public ?array $fields;
/** /**
* ModelFile constructor. * GiiModel constructor.
* @param $classFileName * @param string $classFileName
* @param $tableName * @param string $tableName
* @param $visible * @param array $visible
* @param $res * @param array $res
* @param $fields * @param array $fields
*/ */
public function __construct(string $classFileName, string $tableName, array $visible, array $res, array $fields) public function __construct(string $classFileName, string $tableName, array $visible, array $res, array $fields)
{ {
@@ -308,16 +308,20 @@ use Database\ActiveRecord;
if (empty($data)) return ''; if (empty($data)) return '';
$string = []; $string = [];
foreach ($data as $key => $_val) { foreach ($data as $key => $_val) {
if (is_string($key) && str_contains($key, ',')) { if (in_array($_val[0][1], $this->type['float'])) {
$key = '[' . $key . ']'; $e_x = explode(',', $key);
$key = '\'round\' => ' . $e_x[1] . ', \'maxLength\' => ' . ((int)$e_x[0] + 1);
} else if (is_string($key) && str_contains($key, ',')) {
$key = '\'between\' => [' . $key . ']';
} else {
$key = '\'maxLength\' => ' . $key;
} }
if (count($_val) == 1) { if (count($_val) == 1) {
[$typeRule, $type, $rule, $field] = current($_val);
$_tmp = ' $_tmp = '
[\'' . $field . '\', \'' . ($type == 'enum' ? 'enum' : 'maxLength') . '\' => ' . $key . ']'; [\'' . $_val[0][3] . '\', ' . ($_val[0][1] == 'enum' ? '\'enum\' => ' . $key : $key) . ']';
} else { } else {
$_tmp = ' $_tmp = '
[[\'' . implode('\', \'', array_column($_val, 3)) . '\'], \'maxLength\' => ' . $key . ']'; [[\'' . implode('\', \'', array_column($_val, 3)) . '\'], ' . $key . ']';
} }
$string[] = $_tmp; $string[] = $_tmp;
} }
@@ -412,6 +416,10 @@ use Database\ActiveRecord;
*/ */
private function setCreateSql($table): string private function setCreateSql($table): string
{ {
if (isset(Gii::$createSqls[$table])) {
return Gii::$createSqls[$table];
}
$text = Db::showCreateSql($table, $this->db)['Create Table'] ?? ''; $text = Db::showCreateSql($table, $this->db)['Create Table'] ?? '';
$_tmp = []; $_tmp = [];
@@ -419,7 +427,7 @@ use Database\ActiveRecord;
$_tmp[] = '// ' . $val; $_tmp[] = '// ' . $val;
} }
return implode(PHP_EOL, $_tmp); return Gii::$createSqls[$table] = implode(PHP_EOL, $_tmp);
} }
+1 -1
View File
@@ -22,7 +22,7 @@ abstract class HttpService extends Component
* @param string $category * @param string $category
* @throws Exception * @throws Exception
*/ */
protected function write($message, $category = 'app') protected function write($message, string $category = 'app')
{ {
$logger = Snowflake::app()->getLogger(); $logger = Snowflake::app()->getLogger();
$logger->write($message, $category); $logger->write($message, $category);
+2 -2
View File
@@ -150,12 +150,12 @@ class Response extends HttpService
} }
/** /**
* @param string|null $context * @param mixed $context
* @param int $statusCode * @param int $statusCode
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function send(?string $context = '', int $statusCode = 200): mixed public function send(mixed $context = '', int $statusCode = 200): mixed
{ {
$sendData = $this->parseData($context); $sendData = $this->parseData($context);
+3 -1
View File
@@ -35,7 +35,6 @@ use function Co\run;
* @package Snowflake * @package Snowflake
* *
* @property-read Config $config * @property-read Config $config
* @property-read WchatProviders $wchat
*/ */
class Application extends BaseApplication class Application extends BaseApplication
{ {
@@ -62,6 +61,9 @@ class Application extends BaseApplication
} }
/** /**
* @param Closure|array $closure * @param Closure|array $closure
* @return $this * @return $this
+1 -1
View File
@@ -42,7 +42,7 @@ abstract class Process extends \Swoole\Process implements SProcess
fire(Event::SERVER_WORKER_START); fire(Event::SERVER_WORKER_START);
if (Snowflake::getPlatform()->isLinux()) { if (Snowflake::getPlatform()->isLinux()) {
swoole_set_process_name($this->getProcessName()); name($this->pid, $this->getProcessName());
} }
if (method_exists($this, 'before')) { if (method_exists($this, 'before')) {
$this->before($process); $this->before($process);
+1 -1
View File
@@ -46,7 +46,7 @@ abstract class BaseValidator
* BaseValidator constructor. * BaseValidator constructor.
* @param array $config * @param array $config
*/ */
public function __construct($config = []) public function __construct(array $config = [])
{ {
$this->regConfig($config); $this->regConfig($config);
} }
+5 -8
View File
@@ -37,14 +37,11 @@ class LengthValidator extends BaseValidator
if (is_null($value)) { if (is_null($value)) {
return $this->addError('The param :attribute is null'); return $this->addError('The param :attribute is null');
} }
switch (strtolower($this->method)) { return match (strtolower($this->method)) {
case self::MAX_LENGTH: self::MAX_LENGTH => $this->maxLength($value),
return $this->maxLength($value); self::MIN_LENGTH => $this->minLength($value),
case self::MIN_LENGTH: default => $this->defaultLength($value),
return $this->minLength($value); };
default:
return $this->defaultLength($value);
}
} }
/** /**
+34
View File
@@ -0,0 +1,34 @@
<?php
namespace validator;
use Exception;
/**
* Class RoundValidator
* @package validator
*/
class RoundValidator extends BaseValidator
{
public ?int $value = null;
/**
* @return bool
* @throws Exception
*/
public function trigger(): bool
{
$value = $this->model->getAttribute($this->field);
if ($value == null || round($value, $this->value) != $value) {
return $this->addError('The param :attribute length error');
}
return true;
}
}
+3
View File
@@ -102,6 +102,9 @@ class Validator extends BaseValidator
'class' => 'validator\LengthValidator', 'class' => 'validator\LengthValidator',
'method' => 'default', 'method' => 'default',
], ],
'round' => [
'class' => 'validator\RoundValidator',
],
]; ];
/** /**