Compare commits

...

9 Commits

Author SHA1 Message Date
as2252258 227aad2910 Revert "改名"
This reverts commit fdf58326
2022-01-10 11:39:56 +08:00
as2252258 5d77face53 1 2022-01-09 14:46:34 +08:00
as2252258 2109ed7667 e 2022-01-09 03:49:51 +08:00
as2252258 a45d71d760 1 2022-01-09 03:46:41 +08:00
as2252258 e71adc7cf3 1 2022-01-09 02:44:07 +08:00
as2252258 ef3e874c0c Revert "改名"
This reverts commit fdf58326
2022-01-08 18:49:06 +08:00
as2252258 614b601afa Revert "改名"
This reverts commit fdf58326
2022-01-07 14:38:36 +08:00
as2252258 cf2f26ec21 Revert "改名"
This reverts commit fdf58326
2022-01-04 17:27:37 +08:00
as2252258 2a52172af6 Revert "改名"
This reverts commit fdf58326
2022-01-04 16:04:22 +08:00
58 changed files with 7281 additions and 7366 deletions
+3 -3
View File
@@ -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
{ {
+2 -2
View File
@@ -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;
@@ -101,7 +101,7 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
*/ */
public function getIterator(): Traversable|CollectionIterator|ArrayIterator public function getIterator(): Traversable|CollectionIterator|ArrayIterator
{ {
return new CollectionIterator($this->model, $this->query, $this->_item); return new CollectionIterator($this->model, $this->_item);
} }
@@ -20,31 +20,17 @@ class CollectionIterator extends \ArrayIterator
private ModelInterface|string $model; private ModelInterface|string $model;
/** @var ActiveQuery */
private ActiveQuery $query;
private ?ModelInterface $_clone = null;
public function clean()
{
unset($this->query);
}
/** /**
* CollectionIterator constructor. * CollectionIterator constructor.
* @param $model * @param $model
* @param $query
* @param array $array * @param array $array
* @param int $flags * @param int $flags
* @throws Exception * @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->model = $model;
$this->query = $query;
parent::__construct($array, $flags); parent::__construct($array, $flags);
} }
+1090
View File
File diff suppressed because it is too large Load Diff
+5 -13
View File
@@ -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);
} }
+9 -31
View File
@@ -22,10 +22,10 @@ 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 Kiri\Server\Events\OnWorkerExit;
use Server\Events\OnWorkerStop; use Kiri\Server\Events\OnWorkerStop;
/** /**
* Class Connection * Class Connection
@@ -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;
/** /**
@@ -81,18 +77,11 @@ class Connection extends Component
*/ */
public function init() public function init()
{ {
$eventProvider = Kiri::getDi()->get(EventProvider::class); $this->eventProvider->on(OnWorkerStop::class, [$this, 'clear_connection'], 0);
$eventProvider->on(OnWorkerStop::class, [$this, 'clear_connection'], 0); $this->eventProvider->on(OnWorkerExit::class, [$this, 'clear_connection'], 0);
$eventProvider->on(OnWorkerExit::class, [$this, 'clear_connection'], 0); $this->eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
$eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0); $this->eventProvider->on(Rollback::class, [$this, 'rollback'], 0);
$eventProvider->on(Rollback::class, [$this, 'rollback'], 0); $this->eventProvider->on(Commit::class, [$this, 'commit'], 0);
$eventProvider->on(Commit::class, [$this, 'commit'], 0);
if (Db::transactionsActive()) {
$this->beginTransaction();
}
$this->_schema->db = $this;
} }
@@ -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 * @throws Exception
*/ */
@@ -8,10 +8,9 @@ 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 Kiri\Server\Events\OnWorkerStart;
/** /**
* Class DatabasesProviders * Class DatabasesProviders
@@ -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);
}
} }
View File
View File
View File
View File
+14 -12
View File
@@ -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;
} }
/** /**
View File
View File
View File
@@ -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
View File
@@ -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
View File
File diff suppressed because it is too large Load Diff
-26
View File
@@ -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();