diff --git a/ActiveQuery.php b/ActiveQuery.php index 7d2594b..d31456e 100644 --- a/ActiveQuery.php +++ b/ActiveQuery.php @@ -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; } diff --git a/Base/CollectionIterator.php b/Base/CollectionIterator.php index 3d48324..1cc2cfa 100644 --- a/Base/CollectionIterator.php +++ b/Base/CollectionIterator.php @@ -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; diff --git a/Base/ConditionClassMap.php b/Base/ConditionClassMap.php deleted file mode 100644 index f852168..0000000 --- a/Base/ConditionClassMap.php +++ /dev/null @@ -1,72 +0,0 @@ - [ - '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, - ]; - -} diff --git a/Base/Model.php b/Base/Model.php index 94e96bc..9d82bcb 100644 --- a/Base/Model.php +++ b/Base/Model.php @@ -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 diff --git a/Collection.php b/Collection.php index 48ee4ca..ab2c8da 100644 --- a/Collection.php +++ b/Collection.php @@ -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) { diff --git a/Command.php b/Command.php index 15ff8bc..e012c27 100644 --- a/Command.php +++ b/Command.php @@ -13,7 +13,6 @@ namespace Database; use Exception; use Kiri\Abstracts\Component; use Kiri\Di\Container; -use Kiri\Error\StdoutLogger; use PDO; use Throwable; diff --git a/Connection.php b/Connection.php index 2539f48..ffeb762 100644 --- a/Connection.php +++ b/Connection.php @@ -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); } diff --git a/DatabasesProviders.php b/DatabasesProviders.php index 6f10aa8..b518dba 100644 --- a/DatabasesProviders.php +++ b/DatabasesProviders.php @@ -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 [ diff --git a/Mysql/Columns.php b/Mysql/Columns.php deleted file mode 100644 index 98343af..0000000 --- a/Mysql/Columns.php +++ /dev/null @@ -1,388 +0,0 @@ -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'); - } - -} diff --git a/Mysql/Schema.php b/Mysql/Schema.php deleted file mode 100644 index 1e19837..0000000 --- a/Mysql/Schema.php +++ /dev/null @@ -1,35 +0,0 @@ -_column === null) { - $this->_column = new Columns($this->db); - } - - return $this->_column; - } -} diff --git a/SqlBuilder.php b/SqlBuilder.php index ad45c51..f25811c 100644 --- a/SqlBuilder.php +++ b/SqlBuilder.php @@ -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) { diff --git a/Traits/HasBase.php b/Traits/HasBase.php index 1c262ed..22dd975 100644 --- a/Traits/HasBase.php +++ b/Traits/HasBase.php @@ -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(); diff --git a/Traits/QueryTrait.php b/Traits/QueryTrait.php index 2682a9c..9f3a6c8 100644 --- a/Traits/QueryTrait.php +++ b/Traits/QueryTrait.php @@ -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 . ' ?';