Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5d77face53 | |||
| 2109ed7667 | |||
| a45d71d760 | |||
| e71adc7cf3 | |||
| ef3e874c0c | |||
| 614b601afa | |||
| cf2f26ec21 | |||
| 2a52172af6 | |||
| 5bf8a7feb1 | |||
| 52d26c4481 | |||
| dd369d348c |
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Database\Note;
|
||||
namespace Database\Annotation;
|
||||
|
||||
|
||||
use Attribute;
|
||||
@@ -11,9 +11,9 @@ use Exception;
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
|
||||
namespace Database\Note;
|
||||
namespace Database\Annotation;
|
||||
|
||||
|
||||
use Note\Attribute;
|
||||
use Kiri\Annotation\Attribute;
|
||||
use Database\Base\Relate;
|
||||
use Exception;
|
||||
|
||||
|
||||
/**
|
||||
* Class Relation
|
||||
* @package Note\Model
|
||||
* @package Annotation\Model
|
||||
*/
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Relation extends Attribute
|
||||
{
|
||||
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Database\Note;
|
||||
namespace Database\Annotation;
|
||||
|
||||
|
||||
use Note\Attribute;
|
||||
use Kiri\Annotation\Attribute;
|
||||
use Database\Base\Setter;
|
||||
use Exception;
|
||||
|
||||
@@ -17,6 +17,7 @@ use Kiri\ToArray;
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Abstracts\Component;
|
||||
use ReturnTypeWillChange;
|
||||
use Traversable;
|
||||
|
||||
/**
|
||||
@@ -91,7 +92,7 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
|
||||
*/
|
||||
public function addItem($item)
|
||||
{
|
||||
array_push($this->_item, $item);
|
||||
$this->_item[] = $item;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,7 +101,7 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
|
||||
*/
|
||||
public function getIterator(): Traversable|CollectionIterator|ArrayIterator
|
||||
{
|
||||
return new CollectionIterator($this->model, $this->query, $this->_item);
|
||||
return new CollectionIterator($this->model, $this->_item);
|
||||
}
|
||||
|
||||
|
||||
@@ -143,7 +144,7 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
|
||||
* @param mixed $offset
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function offsetSet(mixed $offset, mixed $value)
|
||||
#[ReturnTypeWillChange] public function offsetSet(mixed $offset, mixed $value)
|
||||
{
|
||||
$this->_item[$offset] = $value;
|
||||
}
|
||||
@@ -152,7 +153,7 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
|
||||
/**
|
||||
* @param mixed $offset
|
||||
*/
|
||||
public function offsetUnset(mixed $offset)
|
||||
#[ReturnTypeWillChange] public function offsetUnset(mixed $offset)
|
||||
{
|
||||
if ($this->offsetExists($offset)) {
|
||||
unset($this->_item[$offset]);
|
||||
@@ -20,31 +20,17 @@ class CollectionIterator extends \ArrayIterator
|
||||
private ModelInterface|string $model;
|
||||
|
||||
|
||||
/** @var ActiveQuery */
|
||||
private ActiveQuery $query;
|
||||
|
||||
|
||||
private ?ModelInterface $_clone = null;
|
||||
|
||||
|
||||
public function clean()
|
||||
{
|
||||
unset($this->query);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* CollectionIterator constructor.
|
||||
* @param $model
|
||||
* @param $query
|
||||
* @param array $array
|
||||
* @param int $flags
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct($model, $query, array $array = [], int $flags = 0)
|
||||
public function __construct($model, array $array = [], int $flags = 0)
|
||||
{
|
||||
$this->model = $model;
|
||||
$this->query = $query;
|
||||
parent::__construct($array, $flags);
|
||||
}
|
||||
|
||||
+1090
File diff suppressed because it is too large
Load Diff
@@ -39,9 +39,6 @@ class Command extends Component
|
||||
/** @var string */
|
||||
public string $dbname = '';
|
||||
|
||||
/** @var PDOStatement|null */
|
||||
private ?PDOStatement $prepare = 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
|
||||
* @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);
|
||||
}
|
||||
@@ -110,11 +105,11 @@ class Command extends Component
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $type
|
||||
* @param string $type
|
||||
* @return int|bool|array|string|null
|
||||
* @throws Exception
|
||||
*/
|
||||
private function execute($type): int|bool|array|string|null
|
||||
private function execute(string $type): int|bool|array|string|null
|
||||
{
|
||||
try {
|
||||
$time = microtime(true);
|
||||
@@ -136,11 +131,11 @@ class Command extends Component
|
||||
|
||||
|
||||
/**
|
||||
* @param $type
|
||||
* @param string $type
|
||||
* @return array|int|bool|null
|
||||
* @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);
|
||||
if ($type === static::FETCH_COLUMN) {
|
||||
@@ -180,9 +175,6 @@ class Command extends Component
|
||||
*/
|
||||
public function bindValues(array $data = []): static
|
||||
{
|
||||
if (!is_array($this->params)) {
|
||||
$this->params = [];
|
||||
}
|
||||
if (!empty($data)) {
|
||||
$this->params = array_merge($this->params, $data);
|
||||
}
|
||||
@@ -22,7 +22,7 @@ use Kiri\Abstracts\Config;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\Kiri;
|
||||
use Note\Inject;
|
||||
use Kiri\Annotation\Inject;
|
||||
use ReflectionException;
|
||||
use Server\Events\OnWorkerExit;
|
||||
use Server\Events\OnWorkerStop;
|
||||
@@ -68,11 +68,7 @@ class Connection extends Component
|
||||
public array $attributes = [];
|
||||
|
||||
|
||||
/**
|
||||
* @var Schema
|
||||
*/
|
||||
#[Inject(Schema::class)]
|
||||
public Schema $_schema;
|
||||
private ?Schema $_schema = null;
|
||||
|
||||
|
||||
/**
|
||||
@@ -81,18 +77,11 @@ class Connection extends Component
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$eventProvider = Kiri::getDi()->get(EventProvider::class);
|
||||
$eventProvider->on(OnWorkerStop::class, [$this, 'clear_connection'], 0);
|
||||
$eventProvider->on(OnWorkerExit::class, [$this, 'clear_connection'], 0);
|
||||
$eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
|
||||
$eventProvider->on(Rollback::class, [$this, 'rollback'], 0);
|
||||
$eventProvider->on(Commit::class, [$this, 'commit'], 0);
|
||||
|
||||
if (Db::transactionsActive()) {
|
||||
$this->beginTransaction();
|
||||
}
|
||||
|
||||
$this->_schema->db = $this;
|
||||
$this->eventProvider->on(OnWorkerStop::class, [$this, 'clear_connection'], 0);
|
||||
$this->eventProvider->on(OnWorkerExit::class, [$this, 'clear_connection'], 0);
|
||||
$this->eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
|
||||
$this->eventProvider->on(Rollback::class, [$this, 'rollback'], 0);
|
||||
$this->eventProvider->on(Commit::class, [$this, 'commit'], 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -107,17 +96,6 @@ class Connection extends Component
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $config
|
||||
* @return $this
|
||||
*/
|
||||
public function configure($config): static
|
||||
{
|
||||
Kiri::configure($this, $config);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
@@ -8,7 +8,6 @@ use Exception;
|
||||
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;
|
||||
@@ -20,8 +19,6 @@ use Server\Events\OnWorkerStart;
|
||||
class DatabasesProviders extends Providers
|
||||
{
|
||||
|
||||
private array $_pooLength = ['min' => 0, 'max' => 1];
|
||||
|
||||
|
||||
/**
|
||||
* @param Application $application
|
||||
@@ -29,10 +26,6 @@ class DatabasesProviders extends Providers
|
||||
*/
|
||||
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']);
|
||||
}
|
||||
|
||||
@@ -40,14 +33,11 @@ class DatabasesProviders extends Providers
|
||||
/**
|
||||
* @param $name
|
||||
* @return Connection
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get($name): Connection
|
||||
{
|
||||
$config = $this->_settings($this->getConfig($name));
|
||||
|
||||
return Kiri::getDi()->get(Connection::class)->configure($config);
|
||||
return Kiri::app()->get($name);
|
||||
}
|
||||
|
||||
|
||||
@@ -61,10 +51,15 @@ class DatabasesProviders extends Providers
|
||||
if (empty($databases)) {
|
||||
return;
|
||||
}
|
||||
$connection = Kiri::getDi()->get(Connection::class);
|
||||
foreach ($databases as $database) {
|
||||
/** @var Connection $connection */
|
||||
$connection->configure($database)->fill();
|
||||
|
||||
$app = Kiri::app();
|
||||
foreach ($databases as $key => $database) {
|
||||
$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\ToArray;
|
||||
use ReflectionException;
|
||||
use Swoole\Coroutine;
|
||||
|
||||
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.');
|
||||
@@ -213,16 +214,18 @@ class Model extends Base\Model
|
||||
*/
|
||||
public function delete(): bool
|
||||
{
|
||||
$conditions = $this->_oldAttributes;
|
||||
if (empty($conditions)) {
|
||||
return $this->addError("Delete condition do not empty.", 'mysql');
|
||||
}
|
||||
$primary = $this->getPrimary();
|
||||
|
||||
if (!empty($primary)) {
|
||||
$conditions = [$primary => $this->getAttribute($primary)];
|
||||
if (empty($primary) || !$this->hasPrimaryValue()) {
|
||||
return $this->addError("Only primary key operations are supported.", 'mysql');
|
||||
}
|
||||
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
|
||||
* @throws Exception
|
||||
* @param bool $result
|
||||
* @return void
|
||||
*/
|
||||
public function afterDelete(): bool
|
||||
public function afterDelete(bool $result): void
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -195,7 +195,7 @@ trait QueryTrait
|
||||
* @param string $column
|
||||
* @return $this
|
||||
*/
|
||||
public function whereNotEmpty(string $column): static
|
||||
public function whereAnnotationmpty(string $column): static
|
||||
{
|
||||
$this->where[] = $column . ' <> \'\'';
|
||||
return $this;
|
||||
+3
-3
@@ -12,12 +12,12 @@
|
||||
"php": ">=8.0",
|
||||
"ext-json": "*",
|
||||
"ext-pdo": "*",
|
||||
"game-worker/kiri-validator": "^v1.2",
|
||||
"game-worker/kiri-event": "^v1.0"
|
||||
"game-worker/kiri-validator": "~v2.0",
|
||||
"game-worker/kiri-event": "~v2.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Database\\": "src/"
|
||||
"Database\\": "./"
|
||||
}
|
||||
},
|
||||
"require-dev": {
|
||||
|
||||
-1090
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