Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2109ed7667 | |||
| a45d71d760 | |||
| e71adc7cf3 | |||
| ef3e874c0c | |||
| 614b601afa |
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Database\Note;
|
namespace Database\Annotation;
|
||||||
|
|
||||||
|
|
||||||
use Attribute;
|
use Attribute;
|
||||||
@@ -11,9 +11,9 @@ use Exception;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Get
|
* Class Get
|
||||||
* @package Note\Model
|
* @package Annotation\Model
|
||||||
*/
|
*/
|
||||||
#[Attribute(Attribute::TARGET_METHOD)] class Get extends \Note\Attribute
|
#[Attribute(Attribute::TARGET_METHOD)] class Get extends \Annotation\Attribute
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
@@ -1,17 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Database\Note;
|
namespace Database\Annotation;
|
||||||
|
|
||||||
|
|
||||||
use Note\Attribute;
|
use Kiri\Annotation\Attribute;
|
||||||
use Database\Base\Relate;
|
use Database\Base\Relate;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Relation
|
* Class Relation
|
||||||
* @package Note\Model
|
* @package Annotation\Model
|
||||||
*/
|
*/
|
||||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Relation extends Attribute
|
#[\Attribute(\Attribute::TARGET_METHOD)] class Relation extends Attribute
|
||||||
{
|
{
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Database\Note;
|
namespace Database\Annotation;
|
||||||
|
|
||||||
|
|
||||||
use Note\Attribute;
|
use Kiri\Annotation\Attribute;
|
||||||
use Database\Base\Setter;
|
use Database\Base\Setter;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
+1090
File diff suppressed because it is too large
Load Diff
@@ -39,9 +39,6 @@ class Command extends Component
|
|||||||
/** @var string */
|
/** @var string */
|
||||||
public string $dbname = '';
|
public string $dbname = '';
|
||||||
|
|
||||||
/** @var PDOStatement|null */
|
|
||||||
private ?PDOStatement $prepare = null;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|bool|int|string|PDOStatement|null
|
* @return array|bool|int|string|PDOStatement|null
|
||||||
@@ -53,12 +50,10 @@ class Command extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param bool $isInsert
|
|
||||||
* @param mixed $hasAutoIncrement
|
|
||||||
* @return int|bool|array|string|null
|
* @return int|bool|array|string|null
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function save(bool $isInsert = TRUE, mixed $hasAutoIncrement = null): int|bool|array|string|null
|
public function save(): int|bool|array|string|null
|
||||||
{
|
{
|
||||||
return $this->execute(static::EXECUTE);
|
return $this->execute(static::EXECUTE);
|
||||||
}
|
}
|
||||||
@@ -110,11 +105,11 @@ class Command extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $type
|
* @param string $type
|
||||||
* @return int|bool|array|string|null
|
* @return int|bool|array|string|null
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function execute($type): int|bool|array|string|null
|
private function execute(string $type): int|bool|array|string|null
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$time = microtime(true);
|
$time = microtime(true);
|
||||||
@@ -136,11 +131,11 @@ class Command extends Component
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $type
|
* @param string $type
|
||||||
* @return array|int|bool|null
|
* @return array|int|bool|null
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function search($type): array|int|bool|null
|
private function search(string $type): array|int|bool|null
|
||||||
{
|
{
|
||||||
$pdo = $this->db->getConnect($this->sql);
|
$pdo = $this->db->getConnect($this->sql);
|
||||||
if ($type === static::FETCH_COLUMN) {
|
if ($type === static::FETCH_COLUMN) {
|
||||||
@@ -180,9 +175,6 @@ class Command extends Component
|
|||||||
*/
|
*/
|
||||||
public function bindValues(array $data = []): static
|
public function bindValues(array $data = []): static
|
||||||
{
|
{
|
||||||
if (!is_array($this->params)) {
|
|
||||||
$this->params = [];
|
|
||||||
}
|
|
||||||
if (!empty($data)) {
|
if (!empty($data)) {
|
||||||
$this->params = array_merge($this->params, $data);
|
$this->params = array_merge($this->params, $data);
|
||||||
}
|
}
|
||||||
@@ -22,7 +22,7 @@ use Kiri\Abstracts\Config;
|
|||||||
use Kiri\Events\EventProvider;
|
use Kiri\Events\EventProvider;
|
||||||
use Kiri\Exception\NotFindClassException;
|
use Kiri\Exception\NotFindClassException;
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
use Note\Inject;
|
use Kiri\Annotation\Inject;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Server\Events\OnWorkerExit;
|
use Server\Events\OnWorkerExit;
|
||||||
use Server\Events\OnWorkerStop;
|
use Server\Events\OnWorkerStop;
|
||||||
@@ -68,11 +68,7 @@ class Connection extends Component
|
|||||||
public array $attributes = [];
|
public array $attributes = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
private ?Schema $_schema = null;
|
||||||
* @var Schema
|
|
||||||
*/
|
|
||||||
#[Inject(Schema::class)]
|
|
||||||
public Schema $_schema;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -86,12 +82,6 @@ class Connection extends Component
|
|||||||
$this->eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
|
$this->eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
|
||||||
$this->eventProvider->on(Rollback::class, [$this, 'rollback'], 0);
|
$this->eventProvider->on(Rollback::class, [$this, 'rollback'], 0);
|
||||||
$this->eventProvider->on(Commit::class, [$this, 'commit'], 0);
|
$this->eventProvider->on(Commit::class, [$this, 'commit'], 0);
|
||||||
|
|
||||||
if (Db::transactionsActive()) {
|
|
||||||
$this->beginTransaction();
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->_schema->db = $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -106,17 +96,6 @@ class Connection extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $config
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function configure($config): static
|
|
||||||
{
|
|
||||||
Kiri::configure($this, $config);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@@ -8,7 +8,6 @@ use Exception;
|
|||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
use Kiri\Abstracts\Providers;
|
use Kiri\Abstracts\Providers;
|
||||||
use Kiri\Application;
|
use Kiri\Application;
|
||||||
use Kiri\Events\EventProvider;
|
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
use Server\Events\OnWorkerStart;
|
use Server\Events\OnWorkerStart;
|
||||||
@@ -20,8 +19,6 @@ use Server\Events\OnWorkerStart;
|
|||||||
class DatabasesProviders extends Providers
|
class DatabasesProviders extends Providers
|
||||||
{
|
{
|
||||||
|
|
||||||
private array $_pooLength = ['min' => 0, 'max' => 1];
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Application $application
|
* @param Application $application
|
||||||
@@ -29,10 +26,6 @@ class DatabasesProviders extends Providers
|
|||||||
*/
|
*/
|
||||||
public function onImport(Application $application)
|
public function onImport(Application $application)
|
||||||
{
|
{
|
||||||
$application->set('db', $this);
|
|
||||||
|
|
||||||
$this->_pooLength = Config::get('databases.pool', ['min' => 0, 'max' => 1]);
|
|
||||||
|
|
||||||
$this->eventProvider->on(OnWorkerStart::class, [$this, 'createPool']);
|
$this->eventProvider->on(OnWorkerStart::class, [$this, 'createPool']);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,14 +33,11 @@ class DatabasesProviders extends Providers
|
|||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @return Connection
|
* @return Connection
|
||||||
* @throws ConfigException
|
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function get($name): Connection
|
public function get($name): Connection
|
||||||
{
|
{
|
||||||
$config = $this->_settings($this->getConfig($name));
|
return Kiri::app()->get($name);
|
||||||
|
|
||||||
return Kiri::getDi()->get(Connection::class)->configure($config);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -61,10 +51,15 @@ class DatabasesProviders extends Providers
|
|||||||
if (empty($databases)) {
|
if (empty($databases)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$connection = Kiri::getDi()->get(Connection::class);
|
|
||||||
foreach ($databases as $database) {
|
$app = Kiri::app();
|
||||||
/** @var Connection $connection */
|
foreach ($databases as $key => $database) {
|
||||||
$connection->configure($database)->fill();
|
$database = $this->_settings($database);
|
||||||
|
|
||||||
|
$connection = Kiri::getDi()->create(Connection::class, $database);
|
||||||
|
$connection->fill();
|
||||||
|
|
||||||
|
$app->set($key, $connection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,15 +88,4 @@ class DatabasesProviders extends Providers
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $name
|
|
||||||
* @return mixed
|
|
||||||
* @throws ConfigException
|
|
||||||
*/
|
|
||||||
public function getConfig($name): mixed
|
|
||||||
{
|
|
||||||
return Config::get('databases.connections.' . $name, null, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
+14
-12
@@ -17,6 +17,7 @@ use Kiri\Exception\NotFindClassException;
|
|||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
use Kiri\ToArray;
|
use Kiri\ToArray;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
|
use Swoole\Coroutine;
|
||||||
|
|
||||||
defined('SAVE_FAIL') or define('SAVE_FAIL', 3227);
|
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.');
|
defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.');
|
||||||
@@ -213,16 +214,18 @@ class Model extends Base\Model
|
|||||||
*/
|
*/
|
||||||
public function delete(): bool
|
public function delete(): bool
|
||||||
{
|
{
|
||||||
$conditions = $this->_oldAttributes;
|
|
||||||
if (empty($conditions)) {
|
|
||||||
return $this->addError("Delete condition do not empty.", 'mysql');
|
|
||||||
}
|
|
||||||
$primary = $this->getPrimary();
|
$primary = $this->getPrimary();
|
||||||
|
if (empty($primary) || !$this->hasPrimaryValue()) {
|
||||||
if (!empty($primary)) {
|
return $this->addError("Only primary key operations are supported.", 'mysql');
|
||||||
$conditions = [$primary => $this->getAttribute($primary)];
|
|
||||||
}
|
}
|
||||||
return static::deleteByCondition($conditions);
|
if (!$this->beforeDelete()) {
|
||||||
|
$result = static::deleteByCondition([$primary => $this->getPrimaryValue()]);
|
||||||
|
Coroutine::create(function () use ($result) {
|
||||||
|
$this->afterDelete($result);
|
||||||
|
});
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -401,12 +404,11 @@ class Model extends Base\Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @param bool $result
|
||||||
* @throws Exception
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function afterDelete(): bool
|
public function afterDelete(bool $result): void
|
||||||
{
|
{
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -195,7 +195,7 @@ trait QueryTrait
|
|||||||
* @param string $column
|
* @param string $column
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function whereNotEmpty(string $column): static
|
public function whereAnnotationmpty(string $column): static
|
||||||
{
|
{
|
||||||
$this->where[] = $column . ' <> \'\'';
|
$this->where[] = $column . ' <> \'\'';
|
||||||
return $this;
|
return $this;
|
||||||
+3
-3
@@ -12,12 +12,12 @@
|
|||||||
"php": ">=8.0",
|
"php": ">=8.0",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"ext-pdo": "*",
|
"ext-pdo": "*",
|
||||||
"game-worker/kiri-validator": "^v1.2",
|
"game-worker/kiri-validator": "~v2.0",
|
||||||
"game-worker/kiri-event": "^v1.0"
|
"game-worker/kiri-event": "~v2.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Database\\": "src/"
|
"Database\\": "./"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
|||||||
-1091
File diff suppressed because it is too large
Load Diff
@@ -1,26 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Database;
|
|
||||||
|
|
||||||
|
|
||||||
use Database\Model;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
class TestModel extends Model
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
protected string $connection = '';
|
|
||||||
|
|
||||||
|
|
||||||
protected string $table = '';
|
|
||||||
|
|
||||||
|
|
||||||
public ?string $primary = '';
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
TestModel::query()->get();
|
|
||||||
Reference in New Issue
Block a user