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
* @throws
*/
public function populate($data): ModelInterface|array
public function populate(array $data): ModelInterface|array
{
$model = $this->modelClass->populates($data);
@@ -187,12 +187,12 @@ class ActiveQuery extends QueryTrait implements ISqlBuilder
}
/**
* @param $filed
* @param string $filed
*
* @return mixed
* @throws
*/
public function value($filed): mixed
public function value(string $filed): mixed
{
return $this->first()[$filed] ?? NULL;
}
+4 -1
View File
@@ -6,7 +6,6 @@ namespace Database\Base;
use Database\ModelInterface;
use Exception;
/**
@@ -16,6 +15,10 @@ use Exception;
class CollectionIterator extends \ArrayIterator
{
/**
* @var ModelInterface|string
*/
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\DatabasesProviders;
use Database\ModelInterface;
use Database\Mysql\Columns;
use Database\Relation;
use Database\SqlBuilder;
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
* @return static
+6 -7
View File
@@ -191,12 +191,11 @@ class Collection extends AbstractCollection
/**
* @param $value
* @param $condition
* @param array|ModelInterface $value
* @param array $condition
* @return bool
* @throws
*/
private function filterCheck($value, $condition): bool
private function filterCheck(array|ModelInterface $value, array $condition): bool
{
$_value = $value;
if ($_value instanceof ModelInterface) {
@@ -211,11 +210,11 @@ class Collection extends AbstractCollection
/**
* @param $key
* @param $value
* @param string $key
* @param mixed $value
* @return mixed
*/
public function exists($key, $value): mixed
public function exists(string $key, mixed $value): mixed
{
foreach ($this as $item) {
if ($item->$key === $value) {
-1
View File
@@ -13,7 +13,6 @@ namespace Database;
use Exception;
use Kiri\Abstracts\Component;
use Kiri\Di\Container;
use Kiri\Error\StdoutLogger;
use PDO;
use Throwable;
+2 -17
View File
@@ -14,7 +14,6 @@ namespace Database;
use Database\Affair\BeginTransaction;
use Database\Affair\Commit;
use Database\Affair\Rollback;
use Database\Mysql\Schema;
use Exception;
use Kiri;
use Kiri\Server\Events\OnWorkerExit;
@@ -56,7 +55,6 @@ class Connection extends Component
public bool $enableCache = false;
public string $cacheDriver = 'redis';
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
* @throws
@@ -283,12 +268,12 @@ class Connection extends Component
/**
* @param null $sql
* @param string $sql
* @param array $attributes
* @return Command
* @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);
}
+6 -6
View File
@@ -41,33 +41,33 @@ class DatabasesProviders extends Providers
/**
* @param $name
* @param string $name
* @return Connection
* @throws Exception
*/
public function get($name): Connection
public function get(string $name): Connection
{
return $this->connections[$name];
}
/**
* @param $key
* @param string $key
* @param array $connection
* @return void
* @throws Exception
*/
protected function set($key, array $connection): void
protected function set(string $key, array $connection): void
{
$this->connections[$key] = Kiri::createObject($connection);
}
/**
* @param $database
* @param array $database
* @return array
*/
private function _settings($database): array
private function _settings(array $database): array
{
$clientPool = $database['pool'] ?? ['min' => 1, 'max' => 5, 'tick' => 60];
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
* @return array
*/
#[Pure] private function getFields($attributes): array
#[Pure] private function getFields(array $attributes): array
{
return array_keys(current($attributes));
}
@@ -240,10 +240,10 @@ class SqlBuilder extends Component
/**
* @param $table
* @param string $table
* @return string
*/
public function columns($table): string
public function columns(string $table): string
{
return 'SHOW FULL FIELDS FROM ' . $table;
}
@@ -366,11 +366,11 @@ class SqlBuilder extends Component
/**
* @param $field
* @param $condition
* @param string $field
* @param mixed $condition
* @return string
*/
private function resolveCondition($field, $condition): string
private function resolveCondition(string $field, mixed $condition): string
{
if (is_string($field)) {
$this->query->pushParam($condition);
@@ -387,7 +387,7 @@ class SqlBuilder extends Component
* @param $condition
* @return array
*/
private function _hashMap($condition): array
private function _hashMap(array $condition): array
{
$_array = [];
foreach ($condition as $key => $value) {
+7 -7
View File
@@ -47,13 +47,13 @@ abstract class HasBase implements \Database\Traits\Relation
{
}
/**
* @param $name
* @param $arguments
* @return static
* @throws
* @param string $name
* @param array $arguments
* @return $this|mixed
*/
public function __call($name, $arguments)
public function __call(string $name, array $arguments)
{
if ($name !== 'get') {
$relation = Kiri::getDi()->get(Relation::class);
@@ -66,10 +66,10 @@ abstract class HasBase implements \Database\Traits\Relation
/**
* @param $name
* @param string $name
* @return mixed
*/
public function __get($name): mixed
public function __get(string $name): mixed
{
if ($this->data === null) {
$this->data = $this->get();
+3 -3
View File
@@ -724,12 +724,12 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param $column
* @param $value
* @param string $column
* @param string $value
* @param string $opera
* @return string
*/
private function sprintf($column, $value, string $opera = '='): string
private function sprintf(string $column, string $value, string $opera = '='): string
{
$this->pushParam($value);
return $column . ' ' . $opera . ' ?';