Compare commits

...

18 Commits

Author SHA1 Message Date
as2252258 50f2db7a2d 改名 2021-11-09 16:40:32 +08:00
as2252258 a538c39837 改名 2021-11-09 14:37:45 +08:00
as2252258 8c98ce8c7f 改名 2021-11-09 13:58:08 +08:00
as2252258 f8718fa4de 改名 2021-11-09 13:55:32 +08:00
as2252258 ffae51bccf 改名 2021-11-09 11:17:03 +08:00
as2252258 714df3dda4 改名 2021-11-09 11:15:43 +08:00
as2252258 f3a7789184 改名 2021-11-09 11:14:51 +08:00
as2252258 e186ebd54c 改名 2021-11-09 10:59:29 +08:00
as2252258 4d7c9fdd17 改名 2021-11-09 10:58:57 +08:00
as2252258 2ea8e8276b 改名 2021-11-09 10:50:07 +08:00
as2252258 d2bdb4355f 改名 2021-11-09 10:49:12 +08:00
as2252258 f7e88e6c18 改名 2021-11-09 10:48:27 +08:00
as2252258 67b961ce6e 改名 2021-11-09 10:47:50 +08:00
as2252258 81d0e09c65 改名 2021-11-09 10:43:29 +08:00
as2252258 022f3936c4 改名 2021-11-09 10:35:57 +08:00
as2252258 cf468b8a24 改名 2021-11-09 10:33:55 +08:00
as2252258 7796b86826 改名 2021-11-05 15:23:36 +08:00
as2252258 13dc4e5299 改名 2021-11-05 15:14:34 +08:00
11 changed files with 55 additions and 37 deletions
+2 -2
View File
@@ -1,5 +1,5 @@
{
"name": "game-worker/db-connection",
"name": "game-worker/kiri-databases",
"description": "db",
"authors": [
{
@@ -12,7 +12,7 @@
"php": ">=8.0",
"ext-json": "*",
"ext-pdo": "*",
"game-worker/validator": "^v1.2",
"game-worker/kiri-validator": "^v1.2",
"game-worker/kiri-event": "^v1.0"
},
"autoload": {
-1
View File
@@ -237,7 +237,6 @@ class ActiveQuery extends Component implements ISqlBuilder
*/
public function count(): int
{
$this->select = ['COUNT(*)'];
$data = $this->execute($this->builder->count())->one();
if ($data && is_array($data)) {
return (int)array_shift($data);
+10 -9
View File
@@ -13,12 +13,10 @@ defined('SAVE_FAIL') or define('SAVE_FAIL', 3227);
defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.');
use Annotation\Inject;
use ArrayAccess;
use Closure;
use Database\ActiveQuery;
use Database\Connection;
use Database\HasCount;
use Database\HasMany;
use Database\HasOne;
use Database\ModelInterface;
@@ -98,12 +96,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
protected string $connection = 'db';
/**
* @var array
*/
protected array $rules = [];
/**
* @var array
*/
@@ -119,6 +111,15 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
}
/**
* @return array
*/
public function rules(): array
{
return [];
}
/**
* @param string $name
* @param mixed $value
@@ -611,7 +612,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
if (!is_null($data)) {
$this->_attributes = merge($this->_attributes, $data);
}
if (!$this->validator($this->rules) || !$this->beforeSave($this)) {
if (!$this->validator($this->rules()) || !$this->beforeSave($this)) {
return false;
}
[$change, $condition, $fields] = $this->separation();
+2 -3
View File
@@ -20,9 +20,8 @@ class HashCondition extends Condition
return '';
}
foreach ($this->value as $key => $value) {
if ($value === null) {
continue;
}
if (is_null($value)) continue;
$array[] = sprintf("%s = '%s'", $key, addslashes($value));
}
return implode(' AND ', $array);
+12 -1
View File
@@ -108,6 +108,17 @@ class Connection extends Component
}
/**
* @param $config
* @return $this
*/
public function configure($config): static
{
Kiri::configure($this, $config);
return $this;
}
/**
* @throws Exception
*/
@@ -141,7 +152,7 @@ class Connection extends Component
* @return mixed
* @throws ReflectionException
* @throws NotFindClassException
* @throws \Exception
* @throws Exception
*/
public function getSchema(): Schema
{
+7 -10
View File
@@ -6,13 +6,13 @@ namespace Database;
use Annotation\Inject;
use Exception;
use Server\Events\OnWorkerStart;
use Kiri\Abstracts\Config;
use Kiri\Abstracts\Providers;
use Kiri\Application;
use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Server\Events\OnWorkerStart;
/**
* Class DatabasesProviders
@@ -53,11 +53,9 @@ class DatabasesProviders extends Providers
*/
public function get($name): Connection
{
$application = Kiri::app();
if (!$application->has('databases.' . $name)) {
$application->set('databases.' . $name, $this->_settings($this->getConfig($name)));
}
return $application->get('databases.' . $name);
$config = $this->_settings($this->getConfig($name));
return Kiri::getDi()->get(Connection::class)->configure($config);
}
@@ -71,11 +69,10 @@ class DatabasesProviders extends Providers
if (empty($databases)) {
return;
}
$application = Kiri::app();
foreach ($databases as $name => $database) {
$connection = Kiri::getDi()->get(Connection::class);
foreach ($databases as $database) {
/** @var Connection $connection */
$application->set('databases.' . $name, $this->_settings($database));
$application->get('databases.' . $name)->fill();
$connection->configure($database)->fill();
}
}
+4 -4
View File
@@ -334,7 +334,7 @@ class Model extends Base\Model
*/
public function hasOne(string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery
{
if (($value = $this->getAttribute($localKey)) === null) {
if (($value = $this->{$localKey}) === null) {
throw new Exception("Need join table primary key.");
}
@@ -353,7 +353,7 @@ class Model extends Base\Model
*/
public function hasCount($modelName, $foreignKey, $localKey): ActiveQuery|HasCount
{
if (($value = $this->getAttribute($localKey)) === null) {
if (($value = $this->{$localKey}) === null) {
throw new Exception("Need join table primary key.");
}
@@ -372,7 +372,7 @@ class Model extends Base\Model
*/
public function hasMany($modelName, $foreignKey, $localKey): ActiveQuery|HasMany
{
if (($value = $this->getAttribute($localKey)) === null) {
if (($value = $this->{$localKey}) === null) {
throw new Exception("Need join table primary key.");
}
@@ -390,7 +390,7 @@ class Model extends Base\Model
*/
public function hasIn($modelName, $foreignKey, $localKey): ActiveQuery|HasMany
{
if (($value = $this->getAttribute($localKey)) === null) {
if (($value = $this->{$localKey}) === null) {
throw new Exception("Need join table primary key.");
}
+1 -1
View File
@@ -316,7 +316,7 @@ class Columns extends Component
/**
* @param $column
* @param array $column
* @param $table
* @return array
*/
+7 -5
View File
@@ -240,7 +240,7 @@ class SqlBuilder extends Component
if (empty($this->query->from) && !empty($this->query->modelClass)) {
$this->query->from($this->query->getTable());
}
return $this->_prefix();
return $this->_prefix(false, true);
}
@@ -256,14 +256,15 @@ class SqlBuilder extends Component
/**
* @param bool $hasOrder
* @param bool $isCount
* @return string
* @throws Exception
*/
private function _prefix(bool $hasOrder = false): string
private function _prefix(bool $hasOrder = false, bool $isCount = false): string
{
$select = '';
if (!empty($this->query->from)) {
$select = $this->_selectPrefix();
$select = $this->_selectPrefix($isCount);
}
$select = $this->_wherePrefix($select);
if (!empty($this->query->attributes) && is_array($this->query->attributes)) {
@@ -302,12 +303,13 @@ class SqlBuilder extends Component
/**
* @param bool $isCount
* @return string
* @throws Exception
*/
private function _selectPrefix(): string
private function _selectPrefix(bool $isCount): string
{
$select = $this->builderSelect($this->query->select) . ' FROM ' . $this->tableName();
$select = $this->builderSelect($this->query->select, $isCount) . ' FROM ' . $this->tableName();
if (!empty($this->query->alias)) {
$select .= $this->builderAlias($this->query->alias);
}
+9 -1
View File
@@ -61,10 +61,14 @@ trait Builder
/**
* @param null $select
* @param bool $isCount
* @return string
*/
#[Pure] private function builderSelect($select = NULL): string
#[Pure] private function builderSelect($select = NULL, bool $isCount = false): string
{
if ($isCount) {
return "SELECT COUNT(*)";
}
if (empty($select)) {
return "SELECT *";
}
@@ -129,6 +133,8 @@ trait Builder
if (empty($where)) return '';
if (is_string($where)) return $where;
foreach ($where as $key => $value) {
if (is_null($value)) continue;
$_value = $this->resolveCondition($key, $value, $_tmp);
if (empty($_value)) continue;
@@ -210,6 +216,8 @@ trait Builder
{
$_array = [];
foreach ($condition as $key => $value) {
if (is_null($value)) continue;
$value = is_numeric($value) ? $value : '\'' . $value . '\'';
if (!is_numeric($key)) {
$_array[] = sprintf('%s = %s', $key, $value);
+1
View File
@@ -923,6 +923,7 @@ trait QueryTrait
private function addArray(array $array): static
{
foreach ($array as $key => $value) {
if (is_null($value)) continue;
if (is_numeric($key)) {
[$column, $opera, $value] = $this->opera(...$value);