This commit is contained in:
2023-12-18 18:11:58 +08:00
parent 4bcd4a8a44
commit 9b249e538b
13 changed files with 39 additions and 559 deletions
+4 -4
View File
@@ -135,11 +135,11 @@ class ActiveQuery extends QueryTrait implements ISqlBuilder
/** /**
* @param $data * @param array $data
* @return ModelInterface|array * @return ModelInterface|array
* @throws * @throws
*/ */
public function populate($data): ModelInterface|array public function populate(array $data): ModelInterface|array
{ {
$model = $this->modelClass->populates($data); $model = $this->modelClass->populates($data);
@@ -187,12 +187,12 @@ class ActiveQuery extends QueryTrait implements ISqlBuilder
} }
/** /**
* @param $filed * @param string $filed
* *
* @return mixed * @return mixed
* @throws * @throws
*/ */
public function value($filed): mixed public function value(string $filed): mixed
{ {
return $this->first()[$filed] ?? NULL; return $this->first()[$filed] ?? NULL;
} }
+4 -1
View File
@@ -6,7 +6,6 @@ namespace Database\Base;
use Database\ModelInterface; use Database\ModelInterface;
use Exception;
/** /**
@@ -16,6 +15,10 @@ use Exception;
class CollectionIterator extends \ArrayIterator class CollectionIterator extends \ArrayIterator
{ {
/**
* @var ModelInterface|string
*/
private ModelInterface|string $model; private ModelInterface|string $model;
-72
View File
@@ -1,72 +0,0 @@
<?php
declare(strict_types=1);
namespace Database\Base;
use Database\Condition\BetweenCondition;
use Database\Condition\InCondition;
use Database\Condition\LikeCondition;
use Database\Condition\LLikeCondition;
use Database\Condition\MathematicsCondition;
use Database\Condition\NotBetweenCondition;
use Database\Condition\NotInCondition;
use Database\Condition\NotLikeCondition;
use Database\Condition\RLikeCondition;
/**
* Class ConditionClassMap
* @package Database\Base
*/
class ConditionClassMap
{
public static array $conditionMap = [
'IN' => [
'class' => InCondition::class
],
'NOT IN' => [
'class' => NotInCondition::class
],
'LIKE' => [
'class' => LikeCondition::class
],
'NOT LIKE' => [
'class' => NotLikeCondition::class
],
'LLike' => [
'class' => LLikeCondition::class
],
'RLike' => [
'class' => RLikeCondition::class
],
'EQ' => [
'class' => MathematicsCondition::class,
'type' => 'EQ'
],
'NEQ' => [
'class' => MathematicsCondition::class,
'type' => 'NEQ'
],
'GT' => [
'class' => MathematicsCondition::class,
'type' => 'GT'
],
'EGT' => [
'class' => MathematicsCondition::class,
'type' => 'EGT'
],
'LT' => [
'class' => MathematicsCondition::class,
'type' => 'LT'
],
'ELT' => [
'class' => MathematicsCondition::class,
'type' => 'ELT'
],
'BETWEEN' => BetweenCondition::class,
'NOT BETWEEN' => NotBetweenCondition::class,
];
}
-11
View File
@@ -19,7 +19,6 @@ use Database\Collection;
use Database\Connection; use Database\Connection;
use Database\DatabasesProviders; use Database\DatabasesProviders;
use Database\ModelInterface; use Database\ModelInterface;
use Database\Mysql\Columns;
use Database\Relation; use Database\Relation;
use Database\SqlBuilder; use Database\SqlBuilder;
use Exception; use Exception;
@@ -793,16 +792,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
} }
/**
* @return Columns
* @throws
*/
public function getColumns(): Columns
{
return $this->getConnection()->getSchema()->getColumns()->table($this->getTable());
}
/** /**
* @param array $data * @param array $data
* @return static * @return static
+6 -7
View File
@@ -191,12 +191,11 @@ class Collection extends AbstractCollection
/** /**
* @param $value * @param array|ModelInterface $value
* @param $condition * @param array $condition
* @return bool * @return bool
* @throws
*/ */
private function filterCheck($value, $condition): bool private function filterCheck(array|ModelInterface $value, array $condition): bool
{ {
$_value = $value; $_value = $value;
if ($_value instanceof ModelInterface) { if ($_value instanceof ModelInterface) {
@@ -211,11 +210,11 @@ class Collection extends AbstractCollection
/** /**
* @param $key * @param string $key
* @param $value * @param mixed $value
* @return mixed * @return mixed
*/ */
public function exists($key, $value): mixed public function exists(string $key, mixed $value): mixed
{ {
foreach ($this as $item) { foreach ($this as $item) {
if ($item->$key === $value) { if ($item->$key === $value) {
-1
View File
@@ -13,7 +13,6 @@ namespace Database;
use Exception; use Exception;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Di\Container; use Kiri\Di\Container;
use Kiri\Error\StdoutLogger;
use PDO; use PDO;
use Throwable; use Throwable;
+2 -17
View File
@@ -14,7 +14,6 @@ namespace Database;
use Database\Affair\BeginTransaction; use Database\Affair\BeginTransaction;
use Database\Affair\Commit; use Database\Affair\Commit;
use Database\Affair\Rollback; use Database\Affair\Rollback;
use Database\Mysql\Schema;
use Exception; use Exception;
use Kiri; use Kiri;
use Kiri\Server\Events\OnWorkerExit; use Kiri\Server\Events\OnWorkerExit;
@@ -56,7 +55,6 @@ class Connection extends Component
public bool $enableCache = false; public bool $enableCache = false;
public string $cacheDriver = 'redis'; public string $cacheDriver = 'redis';
public array $attributes = []; public array $attributes = [];
private ?Schema $_schema = null;
/** /**
@@ -142,19 +140,6 @@ class Connection extends Component
} }
/**
* @return mixed
* @throws
*/
public function getSchema(): Schema
{
if ($this->_schema === null) {
$this->_schema = Kiri::createObject(['class' => Schema::class, 'db' => $this]);
}
return $this->_schema;
}
/** /**
* @return PDO * @return PDO
* @throws * @throws
@@ -283,12 +268,12 @@ class Connection extends Component
/** /**
* @param null $sql * @param string $sql
* @param array $attributes * @param array $attributes
* @return Command * @return Command
* @throws * @throws
*/ */
public function createCommand($sql = null, array $attributes = []): Command public function createCommand(string $sql, array $attributes = []): Command
{ {
return (new Command(['connection' => $this, 'sql' => $sql]))->bindValues($attributes); return (new Command(['connection' => $this, 'sql' => $sql]))->bindValues($attributes);
} }
+6 -6
View File
@@ -41,33 +41,33 @@ class DatabasesProviders extends Providers
/** /**
* @param $name * @param string $name
* @return Connection * @return Connection
* @throws Exception * @throws Exception
*/ */
public function get($name): Connection public function get(string $name): Connection
{ {
return $this->connections[$name]; return $this->connections[$name];
} }
/** /**
* @param $key * @param string $key
* @param array $connection * @param array $connection
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
protected function set($key, array $connection): void protected function set(string $key, array $connection): void
{ {
$this->connections[$key] = Kiri::createObject($connection); $this->connections[$key] = Kiri::createObject($connection);
} }
/** /**
* @param $database * @param array $database
* @return array * @return array
*/ */
private function _settings($database): array private function _settings(array $database): array
{ {
$clientPool = $database['pool'] ?? ['min' => 1, 'max' => 5, 'tick' => 60]; $clientPool = $database['pool'] ?? ['min' => 1, 'max' => 5, 'tick' => 60];
return [ return [
-388
View File
@@ -1,388 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2019-03-18
* Time: 17:22
*/
declare(strict_types=1);
namespace Database\Mysql;
use Database\Connection;
use Database\SqlBuilder;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component;
use Kiri\Core\Json;
/**
* Class Columns
* @package Database\Mysql
*/
class Columns extends Component
{
private array $columns = [];
public string $table = '';
private array $_primary = [];
private array $_auto_increment = [];
private array $_fields = [];
/**
* @param Connection $db
*/
public function __construct(public Connection $db)
{
parent::__construct();
}
/**
* @param string $table
* @return $this
* @throws
*/
public function table(string $table): static
{
$this->structure($this->table = $table);
return $this;
}
/**
* @return string
*/
public function getTable(): string
{
return $this->table;
}
/**
* @param $key
* @param $val
* @return string|int|bool|float
* @throws
*/
public function fieldFormat($key, $val): string|int|bool|float
{
return $this->encode($val, $this->get_fields($key));
}
/**
* @param $data
* @return array
* @throws
*/
public function populate($data): array
{
$column = $this->get_fields();
foreach ($data as $key => $val) {
if (!isset($column[$key])) {
continue;
}
$data[$key] = $this->decode($val, $column[$key]);
}
return $data;
}
/**
* @param $val
* @param null $format
* @return mixed
*/
public function decode($val, $format = null): mixed
{
if (empty($format) || $val === null) {
return $val;
}
$format = strtolower($format);
if ($this->isInt($format)) {
return (int)$val;
} else if ($this->isJson($format)) {
return Json::decode($val, true);
} else if ($this->isFloat($format)) {
return (float)$val;
} else {
return stripslashes((string)$val);
}
}
/**
* @param string $name
* @param $value
* @return mixed
* @throws
*/
public function _decode(string $name, $value): mixed
{
return $this->decode($value, $this->get_fields($name));
}
/**
* @param $val
* @param null $format
* @return float|bool|int|string
* @throws
*/
public function encode($val, $format = null): float|bool|int|string
{
if (empty($format)) {
return $val;
}
$format = strtolower($format);
if ($this->isInt($format)) {
return (int)$val;
} else if ($this->isJson($format)) {
return Json::encode($val);
} else if ($this->isFloat($format)) {
return (float)$val;
} else {
return addslashes($val);
}
}
/**
* @param $format
* @return bool
*/
#[Pure] public function isInt($format): bool
{
return in_array($format, ['int', 'bigint', 'tinyint', 'smallint', 'mediumint']);
}
/**
* @param $format
* @return bool
*/
#[Pure] public function isFloat($format): bool
{
return in_array($format, ['float', 'double', 'decimal']);
}
/**
* @param $format
* @return bool
*/
#[Pure] public function isJson($format): bool
{
return $format == 'json';
}
/**
* @param $format
* @return bool
*/
#[Pure] public function isString($format): bool
{
return in_array($format, ['varchar', 'char', 'text', 'longtext', 'tinytext', 'mediumtext']);
}
/**
* @return array
* @throws
*/
public function format(): array
{
return $this->columns('Default', 'Field');
}
/**
* @return mixed
* @throws
*/
public function getFields(): array
{
if (empty($this->_fields)) {
$this->structure($this->table);
}
return $this->_fields[$this->table];
}
/**
* @param string $name
* @return bool
* @throws
*/
public function hasField(string $name): bool
{
return array_key_exists($name, $this->getFields());
}
/**
* @return int|string|null
* @throws
*/
public function getAutoIncrement(): int|string|null
{
return $this->_auto_increment[$this->table] ?? null;
}
/**
* @return array|null|string
*
* @throws
*/
public function getPrimaryKeys(): array|string|null
{
if (isset($this->_auto_increment[$this->table])) {
return $this->_auto_increment[$this->table];
}
return $this->_primary[$this->table] ?? null;
}
/**
* @return array|null|string
*
* @throws
*/
#[Pure] public function getFirstPrimary(): array|string|null
{
if (isset($this->_auto_increment[$this->table])) {
return $this->_auto_increment[$this->table];
}
if (isset($this->_primary[$this->table])) {
return current($this->_primary[$this->table]);
}
return null;
}
/**
* @param $name
* @param null $index
* @return array
* @throws
*/
private function columns($name, $index = null): array
{
if (empty($index)) {
return array_column($this->getColumns(), $name);
} else {
return array_column($this->getColumns(), $name, $index);
}
}
/**
* @return array|static
* @throws
*/
private function getColumns(): array|static
{
return $this->structure($this->getTable());
}
/**
* @param $table
* @return array|Columns
* @throws
*/
private function structure($table): array|static
{
if (!isset($this->columns[$table]) || empty($this->columns[$table])) {
$column = $this->db->createCommand(SqlBuilder::builder(null)->columns($table))->all();
if (empty($column)) {
throw new \Exception("The table " . $table . " not exists.");
}
return $this->columns[$table] = $this->resolve($column, $table);
}
return $this->columns[$table];
}
/**
* @param array $column
* @param $table
* @return array
*/
private function resolve(array $column, $table): array
{
foreach ($column as $key => $item) {
$this->addPrimary($item, $table);
$column[$key]['Type'] = $this->clean($item['Type']);
}
$this->_fields[$table] = array_column($column, 'Default', 'Field');
return $column;
}
/**
* @param $item
* @param $table
*/
private function addPrimary($item, $table): void
{
if (!isset($this->_primary[$table])) {
$this->_primary[$table] = [];
}
if ($item['Key'] === 'PRI') {
$this->_primary[$table][] = $item['Field'];
}
$this->addIncrement($item, $table);
}
/**
* @param $item
* @param $table
*/
private function addIncrement($item, $table): void
{
if ($item['Extra'] !== 'auto_increment') {
return;
}
$this->_auto_increment[$table] = $item['Field'];
}
/**
* @param $type
* @return string
*/
public function clean($type): string
{
if (!str_contains($type, ')')) {
return $type;
}
$replace = preg_replace('/\(\d+(,\d+)?\)(\s+\w+)*/', '', $type);
if (str_contains($replace, ' ')) {
$replace = explode(' ', $replace)[1];
}
return $replace;
}
/**
* @param null $field
* @return array|string|null
* @throws
*/
public function get_fields($field = null): array|string|null
{
$fields = $this->getAllField();
if (empty($field)) {
return $fields;
}
if (isset($fields[$field])) {
return strtolower($fields[$field]);
}
return null;
}
/**
* @return array
* @throws
*/
public function getAllField(): array
{
return $this->columns('Type', 'Field');
}
}
-35
View File
@@ -1,35 +0,0 @@
<?php
declare(strict_types=1);
namespace Database\Mysql;
use Kiri\Abstracts\Component;
use Database\Connection;
/**
* Class Schema
* @package Database\Mysql
*/
class Schema extends Component
{
/** @var ?Connection */
public ?Connection $db = null;
/** @var ?Columns $_column*/
private ?Columns $_column = null;
/**
* @return Columns|null
* @throws
*/
public function getColumns(): ?Columns
{
if ($this->_column === null) {
$this->_column = new Columns($this->db);
}
return $this->_column;
}
}
+7 -7
View File
@@ -151,7 +151,7 @@ class SqlBuilder extends Component
* @param $attributes * @param $attributes
* @return array * @return array
*/ */
#[Pure] private function getFields($attributes): array #[Pure] private function getFields(array $attributes): array
{ {
return array_keys(current($attributes)); return array_keys(current($attributes));
} }
@@ -240,10 +240,10 @@ class SqlBuilder extends Component
/** /**
* @param $table * @param string $table
* @return string * @return string
*/ */
public function columns($table): string public function columns(string $table): string
{ {
return 'SHOW FULL FIELDS FROM ' . $table; return 'SHOW FULL FIELDS FROM ' . $table;
} }
@@ -366,11 +366,11 @@ class SqlBuilder extends Component
/** /**
* @param $field * @param string $field
* @param $condition * @param mixed $condition
* @return string * @return string
*/ */
private function resolveCondition($field, $condition): string private function resolveCondition(string $field, mixed $condition): string
{ {
if (is_string($field)) { if (is_string($field)) {
$this->query->pushParam($condition); $this->query->pushParam($condition);
@@ -387,7 +387,7 @@ class SqlBuilder extends Component
* @param $condition * @param $condition
* @return array * @return array
*/ */
private function _hashMap($condition): array private function _hashMap(array $condition): array
{ {
$_array = []; $_array = [];
foreach ($condition as $key => $value) { foreach ($condition as $key => $value) {
+7 -7
View File
@@ -47,13 +47,13 @@ abstract class HasBase implements \Database\Traits\Relation
{ {
} }
/** /**
* @param $name * @param string $name
* @param $arguments * @param array $arguments
* @return static * @return $this|mixed
* @throws
*/ */
public function __call($name, $arguments) public function __call(string $name, array $arguments)
{ {
if ($name !== 'get') { if ($name !== 'get') {
$relation = Kiri::getDi()->get(Relation::class); $relation = Kiri::getDi()->get(Relation::class);
@@ -66,10 +66,10 @@ abstract class HasBase implements \Database\Traits\Relation
/** /**
* @param $name * @param string $name
* @return mixed * @return mixed
*/ */
public function __get($name): mixed public function __get(string $name): mixed
{ {
if ($this->data === null) { if ($this->data === null) {
$this->data = $this->get(); $this->data = $this->get();
+3 -3
View File
@@ -724,12 +724,12 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/** /**
* @param $column * @param string $column
* @param $value * @param string $value
* @param string $opera * @param string $opera
* @return string * @return string
*/ */
private function sprintf($column, $value, string $opera = '='): string private function sprintf(string $column, string $value, string $opera = '='): string
{ {
$this->pushParam($value); $this->pushParam($value);
return $column . ' ' . $opera . ' ?'; return $column . ' ' . $opera . ' ?';