This commit is contained in:
2020-12-17 14:09:14 +08:00
parent 672a719dbd
commit 36c1d0502a
151 changed files with 1937 additions and 2848 deletions
+28 -35
View File
@@ -6,6 +6,7 @@
* Time: 14:42
*/
declare(strict_types=1);
namespace Database;
use Snowflake\Abstracts\Component;
@@ -86,7 +87,7 @@ class ActiveQuery extends Component
* @param $value
* @return $this
*/
public function addParam($key, $value)
public function addParam($key, $value): static
{
$this->attributes[$key] = $value;
return $this;
@@ -96,7 +97,7 @@ class ActiveQuery extends Component
* @param array $values
* @return $this
*/
public function addParams(array $values)
public function addParams(array $values): static
{
foreach ($values as $key => $val) {
$this->addParam($key, $val);
@@ -108,7 +109,7 @@ class ActiveQuery extends Component
* @param $name
* @return $this
*/
public function with($name)
public function with($name): static
{
if (empty($name)) {
return $this;
@@ -126,17 +127,17 @@ class ActiveQuery extends Component
* @param bool $isArray
* @return $this
*/
public function asArray($isArray = TRUE)
public function asArray($isArray = TRUE): static
{
$this->asArray = $isArray;
return $this;
}
/**
* @return ActiveRecord
* @throws
* @return ActiveRecord|array|null
* @throws Exception
*/
public function first()
public function first(): ActiveRecord|array|null
{
$data = $this->modelClass::getDb()
->createCommand($this->oneLimit()->queryBuilder())
@@ -156,7 +157,7 @@ class ActiveQuery extends Component
/**
* @return array|Collection
*/
public function get()
public function get(): Collection|array
{
return $this->all();
}
@@ -165,7 +166,7 @@ class ActiveQuery extends Component
/**
* @throws Exception
*/
public function flush()
public function flush(): array|bool|int|string|null
{
$sql = $this->getChange()->truncate($this->getTable());
return $this->command($sql)->flush();
@@ -178,7 +179,7 @@ class ActiveQuery extends Component
* @return Pagination
* @throws Exception
*/
public function page(int $size, callable $callback)
public function page(int $size, callable $callback): Pagination
{
$pagination = new Pagination($this);
$pagination->setOffset(0);
@@ -194,7 +195,7 @@ class ActiveQuery extends Component
* @return array|null
* @throws Exception
*/
public function column(string $field, $setKey = '')
public function column(string $field, $setKey = ''): ?array
{
return $this->all()->column($field, $setKey);
}
@@ -203,7 +204,7 @@ class ActiveQuery extends Component
* @return array|Collection
* @throws
*/
public function all()
public function all(): Collection|array
{
$collect = new Collection($this, $this->modelClass::getDb()
->createCommand($this->queryBuilder())
@@ -220,7 +221,7 @@ class ActiveQuery extends Component
* @return ActiveRecord
* @throws Exception
*/
public function populate(ActiveRecord $model, $data)
public function populate(ActiveRecord $model, $data): ActiveRecord
{
return $this->getWith($model::populate($data));
}
@@ -230,7 +231,7 @@ class ActiveQuery extends Component
* @param $model
* @return mixed
*/
public function getWith($model)
public function getWith($model): mixed
{
if (empty($this->with) || !is_array($this->with)) {
return $model;
@@ -249,7 +250,7 @@ class ActiveQuery extends Component
* @return int
* @throws Exception
*/
public function count()
public function count(): int
{
$data = $this->modelClass::getDb()
->createCommand($this->getBuild()->count($this))
@@ -266,7 +267,7 @@ class ActiveQuery extends Component
* @return array|Command|bool|int|string
* @throws Exception
*/
public function batchUpdate(array $data)
public function batchUpdate(array $data): Command|array|bool|int|string
{
return $this->getDb()->createCommand()
->batchUpdate($this->getTable(), $data, $this->getCondition())
@@ -278,7 +279,7 @@ class ActiveQuery extends Component
* @return bool
* @throws Exception
*/
public function batchInsert(array $data)
public function batchInsert(array $data): bool
{
return $this->getDb()->createCommand()
->batchInsert($this->getTable(), $data)
@@ -301,7 +302,7 @@ class ActiveQuery extends Component
* @return bool
* @throws Exception
*/
public function exists()
public function exists(): bool
{
$column = $this->modelClass::getDb()
->createCommand($this->queryBuilder())
@@ -309,31 +310,23 @@ class ActiveQuery extends Component
return $column !== false;
}
/**
* @return bool
* @throws Exception
*/
public function deleteAll()
public function delete(): bool
{
return $this->modelClass::getDb()
->createCommand($this->queryBuilder())
->delete();
}
/**
* @return bool
* @throws Exception
*/
public function delete()
{
return $this->deleteAll();
}
/**
* @return string
* @throws Exception
*/
public function getCondition()
public function getCondition(): string
{
return $this->getBuild()->getWhere($this->where);
}
@@ -342,7 +335,7 @@ class ActiveQuery extends Component
* @return string
* @throws Exception
*/
public function queryBuilder()
public function queryBuilder(): string
{
return $this->getBuild()->getQuery($this);
}
@@ -353,7 +346,7 @@ class ActiveQuery extends Component
* @return Command
* @throws Exception
*/
private function command($sql, $attr = [])
private function command($sql, $attr = []): Command
{
if (!empty($attr) && is_array($attr)) {
$attr = array_merge($this->attributes, $attr);
@@ -368,7 +361,7 @@ class ActiveQuery extends Component
* @return Select
* @throws Exception
*/
public function getBuild()
public function getBuild(): Select
{
return $this->getDb()->getSchema()->getQueryBuilder();
}
@@ -377,7 +370,7 @@ class ActiveQuery extends Component
* @return orm\Change
* @throws Exception
*/
public function getChange()
public function getChange(): orm\Change
{
return $this->getDb()->getSchema()->getChange();
}
@@ -386,7 +379,7 @@ class ActiveQuery extends Component
* @return Connection
* @throws Exception
*/
public function getDb()
public function getDb(): Connection
{
return $this->modelClass::getDb();
}
@@ -395,7 +388,7 @@ class ActiveQuery extends Component
* @return mixed
* @throws Exception
*/
public function getPrimary()
public function getPrimary(): mixed
{
return $this->modelClass->getPrimary();
}
+36 -34
View File
@@ -14,6 +14,7 @@ use Exception;
use Database\Base\BaseActiveRecord;
use Snowflake\Core\ArrayAccess;
use Snowflake\Error\Logger;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake;
defined('SAVE_FAIL') or define('SAVE_FAIL', 3227);
@@ -37,7 +38,7 @@ class ActiveRecord extends BaseActiveRecord
/**
* @return array
*/
public function rules()
public function rules(): array
{
return [];
}
@@ -48,7 +49,7 @@ class ActiveRecord extends BaseActiveRecord
* @return ActiveRecord|false
* @throws Exception
*/
public function increment(string $column, int $value)
public function increment(string $column, int $value): bool|ActiveRecord
{
if (!$this->mathematics(self::INCR, [$column => $value])) {
return false;
@@ -64,7 +65,7 @@ class ActiveRecord extends BaseActiveRecord
* @return ActiveRecord|false
* @throws Exception
*/
public function decrement(string $column, int $value)
public function decrement(string $column, int $value): bool|ActiveRecord
{
if (!$this->mathematics(self::DECR, [$column => $value])) {
return false;
@@ -79,7 +80,7 @@ class ActiveRecord extends BaseActiveRecord
* @return ActiveRecord|false
* @throws Exception
*/
public function increments(array $columns)
public function increments(array $columns): bool|static
{
if (!$this->mathematics(self::INCR, $columns)) {
return false;
@@ -96,7 +97,7 @@ class ActiveRecord extends BaseActiveRecord
* @return ActiveRecord|false
* @throws Exception
*/
public function decrements(array $columns)
public function decrements(array $columns): bool|static
{
if (!$this->mathematics(self::DECR, $columns)) {
return false;
@@ -108,12 +109,13 @@ class ActiveRecord extends BaseActiveRecord
}
/**
* @param $attributes
* @param $condition
* @return bool|ActiveRecord|mixed
* @param array $condition
* @param array $attributes
* @return mixed
* @throws ComponentException
* @throws Exception
*/
public static function findOrCreate(array $condition, array $attributes = [])
public static function findOrCreate(array $condition, array $attributes = []): mixed
{
$select = static::find()->where($condition)->first();
if (!empty($select)) {
@@ -138,7 +140,7 @@ class ActiveRecord extends BaseActiveRecord
* @return array|bool|int|string|null
* @throws Exception
*/
private function mathematics($action, $columns, $condition = [])
private function mathematics($action, $columns, $condition = []): int|bool|array|string|null
{
if (empty($condition)) {
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
@@ -155,7 +157,7 @@ class ActiveRecord extends BaseActiveRecord
* @return bool
* @throws Exception
*/
public static function incrAll($column, $value, $condition = [])
public static function incrAll($column, $value, $condition = []): bool
{
return static::getDb()->createCommand()
->mathematics(self::getTable(), [self::INCR => [$column, $value]], $condition)
@@ -170,7 +172,7 @@ class ActiveRecord extends BaseActiveRecord
* @return bool
* @throws Exception
*/
public static function decrAll($column, $value, $condition = [])
public static function decrAll($column, $value, $condition = []): bool
{
return static::getDb()->createCommand()
->mathematics(self::getTable(), [self::DECR => [$column, $value]], $condition)
@@ -183,7 +185,7 @@ class ActiveRecord extends BaseActiveRecord
* @return bool
* @throws
*/
public function update(array $attributes)
public function update(array $attributes): bool
{
return $this->save($attributes);
}
@@ -194,7 +196,7 @@ class ActiveRecord extends BaseActiveRecord
* @return bool|static
* @throws Exception
*/
public static function insertOrUpdate(array $params, array $condition)
public static function insertOrUpdate(array $params, array $condition): bool|static
{
$first = static::findOrCreate($condition, $params);
$first->attributes = $params;
@@ -223,7 +225,7 @@ class ActiveRecord extends BaseActiveRecord
* @return bool
* @throws Exception
*/
public function delete()
public function delete(): bool
{
$conditions = $this->_oldAttributes;
if (empty($conditions)) {
@@ -253,7 +255,7 @@ class ActiveRecord extends BaseActiveRecord
* @return bool
* @throws Exception
*/
public static function batchUpdate($condition, $attributes = [])
public static function batchUpdate($condition, $attributes = []): bool
{
$condition = static::find()->where($condition);
return $condition->batchUpdate($attributes);
@@ -263,10 +265,10 @@ class ActiveRecord extends BaseActiveRecord
* @param $condition
* @param array $attributes
*
* @return array|mixed|null|Collection
* @return array|Collection
* @throws Exception
*/
public static function findAll($condition, $attributes = [])
public static function findAll($condition, $attributes = []): array|Collection
{
$query = static::find()->where($condition);
if (!empty($attributes)) {
@@ -277,10 +279,10 @@ class ActiveRecord extends BaseActiveRecord
/**
* @param $data
* @return array|mixed
* @return mixed
* @throws Exception
*/
private function resolveObject($data)
private function resolveObject($data): mixed
{
if (is_numeric($data) || !is_string($data)) {
return $data;
@@ -303,10 +305,10 @@ class ActiveRecord extends BaseActiveRecord
/**
* @return array|mixed
* @return array
* @throws Exception
*/
public function toArray()
public function toArray(): array
{
$data = [];
foreach ($this->_attributes as $key => $val) {
@@ -319,7 +321,7 @@ class ActiveRecord extends BaseActiveRecord
* @return array
* @throws Exception
*/
private function runRelate()
private function runRelate(): array
{
$relates = [];
if (empty($this->_relate)) {
@@ -333,13 +335,13 @@ class ActiveRecord extends BaseActiveRecord
/**
* @param $modelName
* @param string $modelName
* @param $foreignKey
* @param $localKey
* @return mixed|ActiveQuery
* @return HasOne
* @throws Exception
*/
public function hasOne(string $modelName, $foreignKey, $localKey)
public function hasOne(string $modelName, $foreignKey, $localKey): HasOne
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
@@ -357,10 +359,10 @@ class ActiveRecord extends BaseActiveRecord
* @param $modelName
* @param $foreignKey
* @param $localKey
* @return mixed|ActiveQuery
* @return HasCount
* @throws Exception
*/
public function hasCount($modelName, $foreignKey, $localKey)
public function hasCount($modelName, $foreignKey, $localKey): HasCount
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
@@ -378,10 +380,10 @@ class ActiveRecord extends BaseActiveRecord
* @param $modelName
* @param $foreignKey
* @param $localKey
* @return mixed|ActiveQuery
* @return HasMany
* @throws Exception
*/
public function hasMany($modelName, $foreignKey, $localKey)
public function hasMany($modelName, $foreignKey, $localKey): HasMany
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
@@ -398,10 +400,10 @@ class ActiveRecord extends BaseActiveRecord
* @param $modelName
* @param $foreignKey
* @param $localKey
* @return mixed|ActiveQuery
* @return HasMany
* @throws Exception
*/
public function hasIn($modelName, $foreignKey, $localKey)
public function hasIn($modelName, $foreignKey, $localKey): HasMany
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
@@ -418,7 +420,7 @@ class ActiveRecord extends BaseActiveRecord
* @return bool
* @throws Exception
*/
public function afterDelete()
public function afterDelete(): bool
{
if (!$this->hasPrimary()) {
return TRUE;
@@ -434,7 +436,7 @@ class ActiveRecord extends BaseActiveRecord
* @return bool
* @throws Exception
*/
public function beforeDelete()
public function beforeDelete(): bool
{
if (!$this->hasPrimary()) {
return TRUE;
+4 -2
View File
@@ -12,6 +12,8 @@ namespace Database\Base;
use ArrayIterator;
use Database\ActiveQuery;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Abstracts\Component;
use Database\ActiveRecord;
use Snowflake\Exception\NotFindClassException;
@@ -53,7 +55,7 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
/**
* @return int
*/
public function getLength(): int
#[Pure] public function getLength(): int
{
return count($this->_item);
}
@@ -86,7 +88,7 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
/**
* @return Traversable|CollectionIterator|ArrayIterator
* @throws \ReflectionException
* @throws ReflectionException
* @throws NotFindClassException
*/
public function getIterator(): Traversable|CollectionIterator|ArrayIterator
+18 -15
View File
@@ -11,6 +11,7 @@ namespace Database\Base;
use HttpServer\Http\Context;
use ReflectionException;
use Snowflake\Abstracts\Component;
use Snowflake\Core\JSON;
use Database\ActiveQuery;
@@ -23,6 +24,7 @@ use Database\Relation;
use Snowflake\Error\Logger;
use Exception;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException;
use validator\Validator;
use Database\IOrm;
use Snowflake\Snowflake;
@@ -181,7 +183,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return null|string
* @throws Exception
*/
public function getPrimaryValue()
public function getPrimaryValue(): ?string
{
if (!$this->hasPrimary()) {
return null;
@@ -190,14 +192,14 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
}
/**
* @param $condition
* @param $param
* @param $db
* @return $this
* @throws
*/
public static function findOne($condition, $db = NULL)
public static function findOne($param, $db = NULL): static
{
if (is_numeric($condition)) {
if (is_numeric($param)) {
$primary = static::getColumns()->getPrimaryKeys();
if (empty($primary)) {
throw new Exception('Primary key cannot be empty.');
@@ -205,9 +207,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
if (is_array($primary)) {
$primary = current($primary);
}
$condition = [$primary => $condition];
$param = [$primary => $param];
}
return static::find()->where($condition)->first();
return static::find()->where($param)->first();
}
/**
@@ -234,10 +236,11 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
}
/**
* @return mixed|ActiveQuery
* @throws
* @return ActiveQuery
* @throws ReflectionException
* @throws NotFindClassException
*/
public static function find()
public static function find():ActiveQuery
{
return Snowflake::createObject(ActiveQuery::class, [get_called_class()]);
}
@@ -250,19 +253,19 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return bool
* @throws Exception
*/
public static function deleteAll($condition = NULL, $attributes = [], $if_condition_is_null = false)
public static function delete($condition = NULL, $attributes = [], $if_condition_is_null = false): bool
{
if (empty($condition)) {
if (!$if_condition_is_null) {
return false;
}
return static::find()->deleteAll();
return static::find()->delete();
}
$model = static::find()->ifNotWhere($if_condition_is_null)->where($condition);
if (!empty($attributes)) {
$model->bindParams($attributes);
}
return $model->deleteAll();
return $model->delete();
}
@@ -659,10 +662,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
/**
* @param $name
* @return mixed|null
* @return mixed
* @throws Exception
*/
public function __get($name)
public function __get($name): mixed
{
$method = 'get' . ucfirst($name);
if (method_exists($this, $method . 'Attribute')) {
@@ -780,7 +783,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return mixed
* @throws Exception
*/
public static function setDatabaseConnect($bsName)
public static function setDatabaseConnect($bsName): Connection
{
return Snowflake::app()->db->get($bsName);
}
+18 -17
View File
@@ -6,6 +6,7 @@
* Time: 13:38
*/
declare(strict_types=1);
namespace Database;
use Database\Base\AbstractCollection;
@@ -35,7 +36,7 @@ class Collection extends AbstractCollection
* @return array|null
* @throws Exception
*/
public function values($field)
public function values($field): ?array
{
if (empty($field) || !is_string($field)) {
return NULL;
@@ -53,7 +54,7 @@ class Collection extends AbstractCollection
* @param string $field
* @return array|null
*/
public function keyBy(string $field)
public function keyBy(string $field): ?array
{
$array = $this->toArray();
$column = array_flip(array_column($array, $field));
@@ -67,7 +68,7 @@ class Collection extends AbstractCollection
/**
* @return $this
*/
public function orderRand()
public function orderRand(): static
{
shuffle($this->_item);
return $this;
@@ -79,7 +80,7 @@ class Collection extends AbstractCollection
*
* @return array
*/
public function slice($start = 0, $length = 20)
#[Pure] public function slice($start = 0, $length = 20): array
{
if (empty($this->_item) || !is_array($this->_item)) {
return [];
@@ -97,7 +98,7 @@ class Collection extends AbstractCollection
*
* @return array|null
*/
public function column($field, $setKey = '')
public function column($field, $setKey = ''): ?array
{
$data = $this->toArray();
if (empty($data)) {
@@ -115,7 +116,7 @@ class Collection extends AbstractCollection
*
* @return float|int|null
*/
public function sum($field)
public function sum($field): float|int|null
{
$array = $this->column($field);
if (empty($array)) {
@@ -125,9 +126,9 @@ class Collection extends AbstractCollection
}
/**
* @return ActiveRecord|mixed
* @return ActiveRecord|array
*/
public function current()
#[Pure] public function current(): ActiveRecord|array
{
return current($this->_item);
}
@@ -135,7 +136,7 @@ class Collection extends AbstractCollection
/**
* @return int
*/
public function size()
#[Pure] public function size(): int
{
return (int)count($this->_item);
}
@@ -144,7 +145,7 @@ class Collection extends AbstractCollection
* @return array
* @throws
*/
public function toArray()
public function toArray(): array
{
$array = [];
foreach ($this as $value) {
@@ -161,7 +162,7 @@ class Collection extends AbstractCollection
* @throws Exception
* 批量删除
*/
public function delete()
public function delete(): bool
{
$model = $this->model;
if (!$model->hasPrimary()) {
@@ -177,7 +178,7 @@ class Collection extends AbstractCollection
}
return $this->model::find()
->in($model->getPrimary(), $ids)
->deleteAll();
->delete();
}
/**
@@ -185,7 +186,7 @@ class Collection extends AbstractCollection
* @return Collection
* @throws
*/
public function filter(array $condition)
public function filter(array $condition): Collection|static
{
$_filters = [];
if (empty($condition)) {
@@ -207,7 +208,7 @@ class Collection extends AbstractCollection
* @return bool
* @throws Exception
*/
private function filterCheck($value, $condition)
private function filterCheck($value, $condition): bool
{
$_value = $value;
if ($_value instanceof ActiveRecord) {
@@ -224,9 +225,9 @@ class Collection extends AbstractCollection
/**
* @param $key
* @param $value
* @return ActiveRecord|mixed|null
* @return mixed
*/
public function exists($key, $value)
public function exists($key, $value): mixed
{
foreach ($this as $item) {
if ($item->$key === $value) {
@@ -239,7 +240,7 @@ class Collection extends AbstractCollection
/**
* @return bool
*/
public function isEmpty()
#[Pure] public function isEmpty(): bool
{
return $this->size() < 1;
}
+38 -38
View File
@@ -43,10 +43,10 @@ class Command extends Component
/**
* @return bool|PDOStatement
* @throws
* @return array|bool|int|string|PDOStatement|null
* @throws Exception
*/
public function incrOrDecr()
public function incrOrDecr(): array|bool|int|string|PDOStatement|null
{
return $this->execute(static::EXECUTE);
}
@@ -54,10 +54,10 @@ class Command extends Component
/**
* @param bool $isInsert
* @param bool $hasAutoIncrement
* @return bool|string
* @throws
* @return int|bool|array|string|null
* @throws Exception
*/
public function save($isInsert = TRUE, $hasAutoIncrement = true)
public function save($isInsert = TRUE, $hasAutoIncrement = true): int|bool|array|string|null
{
return $this->execute(static::EXECUTE, $isInsert, $hasAutoIncrement);
}
@@ -70,7 +70,7 @@ class Command extends Component
* @return Command
* @throws Exception
*/
public function update($model, $attributes, $condition, $param)
public function update($model, $attributes, $condition, $param): Command
{
$change = $this->db->getSchema()->getChange();
$sql = $change->update($model, $attributes, $condition, $param);
@@ -85,7 +85,7 @@ class Command extends Component
* @return Command
* @throws Exception
*/
public function batchUpdate($tableName, $attributes, $condition)
public function batchUpdate($tableName, $attributes, $condition): Command
{
$change = $this->db->getSchema()->getChange();
[$sql, $param] = $change->batchUpdate($tableName, $attributes, $condition);
@@ -99,7 +99,7 @@ class Command extends Component
* @return Command
* @throws Exception
*/
public function batchInsert($tableName, $attributes)
public function batchInsert($tableName, $attributes): Command
{
$change = $this->db->getSchema()->getChange();
$attribute_key = array_keys(current($attributes));
@@ -114,7 +114,7 @@ class Command extends Component
* @return Command
* @throws Exception
*/
public function insert($tableName, $attributes, $param)
public function insert($tableName, $attributes, $param): Command
{
$change = $this->db->getSchema()->getChange();
$sql = $change->insert($tableName, $attributes, $param);
@@ -122,10 +122,10 @@ class Command extends Component
}
/**
* @return bool|int
* @return int|bool|array|string|null
* @throws Exception
*/
public function all()
public function all(): int|bool|array|string|null
{
return $this->execute(static::FETCH_ALL);
}
@@ -137,7 +137,7 @@ class Command extends Component
* @return Command
* @throws Exception
*/
public function mathematics($tableName, $param, $condition)
public function mathematics($tableName, $param, $condition): static
{
$change = $this->db->getSchema()->getChange();
$sql = $change->mathematics($tableName, $param, $condition);
@@ -145,49 +145,49 @@ class Command extends Component
}
/**
* @return array|mixed
* @return array|bool|int|string|null
* @throws Exception
*/
public function one()
public function one(): null|array|bool|int|string
{
return $this->execute(static::FETCH);
}
/**
* @return bool|int
* @return int|bool|array|string|null
* @throws Exception
*/
public function fetchColumn()
public function fetchColumn(): int|bool|array|string|null
{
return $this->execute(static::FETCH_COLUMN);
}
/**
* @return bool|int
* @return int|bool|array|string|null
* @throws Exception
*/
public function rowCount()
public function rowCount(): int|bool|array|string|null
{
return $this->execute(static::ROW_COUNT);
}
/**
* @return bool|int
* @return int|bool|array|string|null
* @throws Exception
*/
public function flush()
public function flush(): int|bool|array|string|null
{
return $this->execute(static::EXECUTE);
}
/**
* @param $type
* @param $isInsert
* @param $hasAutoIncrement
* @return bool|int
* @param null $isInsert
* @param bool $hasAutoIncrement
* @return int|bool|array|string|null
* @throws Exception
*/
private function execute($type, $isInsert = null, $hasAutoIncrement = true)
private function execute($type, $isInsert = null, $hasAutoIncrement = true): int|bool|array|string|null
{
try {
$time = microtime(true);
@@ -211,10 +211,10 @@ class Command extends Component
/**
* @param $type
* @return array|int|mixed
* @return mixed
* @throws Exception
*/
private function search($type)
private function search($type): mixed
{
$connect = $this->db->getConnect($this->sql);
if (!($connect instanceof PDO)) {
@@ -239,7 +239,7 @@ class Command extends Component
* @return bool|string
* @throws Exception
*/
private function insert_or_change($isInsert, $hasAutoIncrement)
private function insert_or_change($isInsert, $hasAutoIncrement): bool|string
{
if (!($connection = $this->initPDOStatement())) {
return false;
@@ -262,7 +262,7 @@ class Command extends Component
* 重新构建
* @throws
*/
private function initPDOStatement()
private function initPDOStatement(): PDO|bool|null
{
if (empty($this->sql)) {
return null;
@@ -281,7 +281,7 @@ class Command extends Component
* @param $modelName
* @return $this
*/
public function setModelName($modelName)
public function setModelName($modelName): static
{
$this->_modelName = $modelName;
return $this;
@@ -290,25 +290,25 @@ class Command extends Component
/**
* @return string
*/
public function getModelName()
public function getModelName(): string
{
return $this->_modelName;
}
/**
* @return bool|int
* @return int|bool|array|string|null
* @throws Exception
*/
public function delete()
public function delete(): int|bool|array|string|null
{
return $this->execute(static::EXECUTE);
}
/**
* @return bool|int
* @return int|bool|array|string|null
* @throws Exception
*/
public function exec()
public function exec(): int|bool|array|string|null
{
return $this->execute(static::EXECUTE);
}
@@ -317,7 +317,7 @@ class Command extends Component
* @param array|null $data
* @return $this
*/
public function bindValues(array $data = NULL)
public function bindValues(array $data = NULL): static
{
if (!is_array($this->params)) {
$this->params = [];
@@ -333,7 +333,7 @@ class Command extends Component
* @return $this
* @throws Exception
*/
public function setSql($sql)
public function setSql($sql): static
{
$this->sql = $sql;
return $this;
@@ -342,7 +342,7 @@ class Command extends Component
/**
* @return string
*/
public function getError()
public function getError(): string
{
return $this->prepare->errorInfo()[2] ?? 'Db 驱动错误.';
}
+6 -5
View File
@@ -7,6 +7,7 @@ namespace Database\Condition;
use Database\ActiveQuery;
use Database\Base\ConditionClassMap;
use Exception;
use JetBrains\PhpStorm\Pure;
use Snowflake\Core\Str;
use Snowflake\Snowflake;
@@ -23,7 +24,7 @@ class ArrayCondition extends Condition
* @return mixed
* @throws Exception
*/
public function builder()
public function builder(): mixed
{
if ($this->value instanceof Condition) {
return $this->value->builder();
@@ -58,7 +59,7 @@ class ArrayCondition extends Condition
* @param $value
* @return bool
*/
private function isMath($value)
#[Pure] private function isMath($value): bool
{
return isset($value[0]) && in_array($value[0], $this->math);
}
@@ -68,7 +69,7 @@ class ArrayCondition extends Condition
* @return mixed
* @throws Exception
*/
public function buildOperaCondition($value)
public function buildOperaCondition(array $value): mixed
{
[$option['opera'], $option['column'], $option['value']] = $value;
$strPer = strtoupper($option['opera']);
@@ -79,7 +80,7 @@ class ArrayCondition extends Condition
}
$option = array_merge($option, $class);
} else if ($value instanceof ActiveQuery) {
$option['value'] = $value->adaptation();
$option['value'] = $value->getBuild()->getQuery($value);
$option['class'] = ChildCondition::class;
} else {
$option['class'] = DefaultCondition::class;
@@ -94,7 +95,7 @@ class ArrayCondition extends Condition
* @param $value
* @return string
*/
public function buildHashCondition($key, $value)
public function buildHashCondition($key, $value): string
{
return $this->resolve($key, $value);
}
+1 -1
View File
@@ -15,7 +15,7 @@ class BetweenCondition extends Condition
/**
* @return string
*/
public function builder()
public function builder(): string
{
return $this->column . ' BETWEEN ' . $this->value[0] . ' AND ' . $this->value[1];
}
+1 -1
View File
@@ -14,7 +14,7 @@ class ChildCondition extends Condition
/**
* @return string
*/
public function builder()
public function builder(): string
{
return $this->column . ' ' . $this->opera . ' (' . $this->value . ')';
}
+6 -5
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Database\Condition;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\BaseObject;
use Snowflake\Core\Str;
@@ -66,7 +67,7 @@ abstract class Condition extends BaseObject
* $query->ANDWhere('id', '=', 7);
* $sql = '(((id=2 AND id=3 AND id<4) OR id=5) OR id=6) AND i(d=7)';
*/
protected function resolve($column, $value = null, $oprea = '=')
protected function resolve($column, $value = null, $oprea = '='): string
{
if ($value === NULL) {
return '';
@@ -83,7 +84,7 @@ abstract class Condition extends BaseObject
$explode = explode('(', $columns);
$explode = array_shift($explode);
if (strpos($explode, ' ') !== false) {
if (str_contains($explode, ' ')) {
$explode = explode(' ', $explode)[0];
}
if (!in_array(trim($explode), static::INT_TYPE)) {
@@ -97,9 +98,9 @@ abstract class Condition extends BaseObject
/**
* @param array|null $param
* @return array
* @return array|null
*/
protected function format(?array $param)
protected function format(?array $param): ?array
{
if (!is_array($param)) {
return null;
@@ -126,7 +127,7 @@ abstract class Condition extends BaseObject
* @param string $oprea
* @return string
*/
public function typeBuilder($column, $value = null, $oprea = '=')
#[Pure] public function typeBuilder($column, $value = null, $oprea = '='): string
{
if (is_numeric($value)) {
if ($value != (int)$value) {
+1 -1
View File
@@ -14,7 +14,7 @@ class DefaultCondition extends Condition
/**
* @return string
*/
public function builder()
public function builder(): string
{
return $this->resolve($this->column, $this->value, $this->opera);
}
+1 -1
View File
@@ -12,7 +12,7 @@ class HashCondition extends Condition
/**
* @return string
*/
public function builder()
public function builder(): string
{
$array = [];
if (empty($this->value)) {
+3 -2
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Database\Condition;
use Database\ActiveQuery;
use Exception;
/**
* Class InCondition
@@ -15,9 +16,9 @@ class InCondition extends Condition
/**
* @return string
* @throws \Exception
* @throws Exception
*/
public function builder()
public function builder(): string
{
if ($this->value instanceof ActiveQuery) {
$this->value = $this->value->getBuild()->getQuery($this->value);
+1 -1
View File
@@ -17,7 +17,7 @@ class LLikeCondition extends Condition
/**
* @return string
*/
public function builder()
public function builder(): string
{
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
+1 -1
View File
@@ -17,7 +17,7 @@ class LikeCondition extends Condition
/**
* @return string
*/
public function builder()
public function builder(): string
{
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
+7 -7
View File
@@ -15,7 +15,7 @@ class MathematicsCondition extends Condition
/**
* @return mixed
*/
public function builder()
public function builder(): mixed
{
return $this->{strtolower($this->type)}((float)$this->value);
}
@@ -24,7 +24,7 @@ class MathematicsCondition extends Condition
* @param $value
* @return string
*/
public function eq($value)
public function eq($value): string
{
return $this->column . ' = ' . $value;
}
@@ -33,7 +33,7 @@ class MathematicsCondition extends Condition
* @param $value
* @return string
*/
public function neq($value)
public function neq($value): string
{
return $this->column . ' <> ' . $value;
}
@@ -42,7 +42,7 @@ class MathematicsCondition extends Condition
* @param $value
* @return string
*/
public function gt($value)
public function gt($value): string
{
return $this->column . ' > ' . $value;
}
@@ -51,7 +51,7 @@ class MathematicsCondition extends Condition
* @param $value
* @return string
*/
public function egt($value)
public function egt($value): string
{
return $this->column . ' >= ' . $value;
}
@@ -61,7 +61,7 @@ class MathematicsCondition extends Condition
* @param $value
* @return string
*/
public function lt($value)
public function lt($value): string
{
return $this->column . ' < ' . $value;
}
@@ -70,7 +70,7 @@ class MathematicsCondition extends Condition
* @param $value
* @return string
*/
public function elt($value)
public function elt($value): string
{
return $this->column . ' <= ' . $value;
}
+1 -1
View File
@@ -14,7 +14,7 @@ class NotBetweenCondition extends Condition
/**
* @return string
*/
public function builder()
public function builder(): string
{
return $this->column . ' NOT BETWEEN ' . $this->value[0] . ' AND ' . $this->value[1];
}
+1 -1
View File
@@ -14,7 +14,7 @@ class NotInCondition extends Condition
/**
* @return string
*/
public function builder()
public function builder(): string
{
$format = array_filter($this->format($this->value));
+1 -1
View File
@@ -17,7 +17,7 @@ class NotLikeCondition extends Condition
/**
* @return string
*/
public function builder()
public function builder(): string
{
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
+1 -1
View File
@@ -15,7 +15,7 @@ class OrCondition extends Condition
/**
* @return string
*/
public function builder()
public function builder(): string
{
return 'OR ' . $this->resolve($this->column, $this->value, $this->opera);
}
+1 -1
View File
@@ -17,7 +17,7 @@ class RLikeCondition extends Condition
/**
* @return string
*/
public function builder()
public function builder(): string
{
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
+21 -17
View File
@@ -11,6 +11,8 @@ declare(strict_types=1);
namespace Database;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Abstracts\Component;
use Database\Mysql\Schema;
use Database\Orm\Select;
@@ -18,6 +20,7 @@ use Exception;
use PDO;
use Snowflake\Event;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
@@ -92,7 +95,7 @@ class Connection extends Component
* @return PDO
* @throws Exception
*/
public function getConnect($sql = NULL)
public function getConnect($sql = NULL): PDO
{
$connections = Snowflake::app()->connections;
$connections->initConnections($this->cds, true, $this->maxNumber);
@@ -119,7 +122,7 @@ class Connection extends Component
* @return PDO
* @throws Exception
*/
private function getPdo($sql)
private function getPdo($sql): PDO
{
if ($this->isWrite($sql)) {
$connect = $this->masterInstance();
@@ -130,10 +133,11 @@ class Connection extends Component
}
/**
* @return mixed|object|Schema
* @throws Exception
* @return mixed
* @throws ReflectionException
* @throws NotFindClassException
*/
public function getSchema()
public function getSchema(): mixed
{
if ($this->_schema === null) {
$this->_schema = Snowflake::createObject([
@@ -148,7 +152,7 @@ class Connection extends Component
* @param $sql
* @return bool
*/
public function isWrite($sql)
#[Pure] public function isWrite($sql): bool
{
if (empty($sql)) return false;
@@ -158,10 +162,10 @@ class Connection extends Component
}
/**
* @return mixed|null
* @return mixed
* @throws ComponentException
*/
public function getCacheDriver()
public function getCacheDriver(): mixed
{
if (!$this->enableCache) {
return null;
@@ -173,7 +177,7 @@ class Connection extends Component
* @return PDO
* @throws Exception
*/
public function masterInstance()
public function masterInstance(): PDO
{
$config = [
'cds' => $this->cds,
@@ -188,7 +192,7 @@ class Connection extends Component
* @return PDO
* @throws Exception
*/
public function slaveInstance()
public function slaveInstance(): PDO
{
if (empty($this->slaveConfig)) {
return $this->masterInstance();
@@ -203,7 +207,7 @@ class Connection extends Component
* @return $this
* @throws Exception
*/
public function beginTransaction()
public function beginTransaction(): static
{
$connections = Snowflake::app()->connections;
$connections->beginTransaction($this->cds);
@@ -214,7 +218,7 @@ class Connection extends Component
* @return $this|bool
* @throws Exception
*/
public function inTransaction()
public function inTransaction(): bool|static
{
$connections = Snowflake::app()->connections;
return $connections->inTransaction($this->cds);
@@ -227,7 +231,7 @@ class Connection extends Component
public function rollback()
{
$connections = Snowflake::app()->connections;
return $connections->rollback($this->cds);
$connections->rollback($this->cds);
}
/**
@@ -237,7 +241,7 @@ class Connection extends Component
public function commit()
{
$connections = Snowflake::app()->connections;
return $connections->commit($this->cds);
$connections->commit($this->cds);
}
/**
@@ -245,7 +249,7 @@ class Connection extends Component
* @return PDO
* @throws Exception
*/
public function refresh($sql)
public function refresh($sql): PDO
{
if ($this->isWrite($sql)) {
$instance = $this->masterInstance();
@@ -261,7 +265,7 @@ class Connection extends Component
* @return Command
* @throws
*/
public function createCommand($sql = null, $attributes = [])
public function createCommand($sql = null, $attributes = []): Command
{
$command = new Command(['db' => $this, 'sql' => $sql]);
return $command->bindValues($attributes);
@@ -271,7 +275,7 @@ class Connection extends Component
* @return Select
* @throws Exception
*/
public function getBuild()
public function getBuild(): Select
{
return $this->getSchema()->getQueryBuilder();
}
+3 -3
View File
@@ -35,7 +35,7 @@ class DatabasesProviders extends Providers
* @throws ConfigException
* @throws Exception
*/
public function get($name)
public function get($name): Connection
{
$application = Snowflake::app();
if ($application->has('databases.' . $name)) {
@@ -87,10 +87,10 @@ class DatabasesProviders extends Providers
/**
* @param $name
* @return array|mixed|null
* @return mixed
* @throws ConfigException
*/
public function getConfig($name)
public function getConfig($name): mixed
{
return Config::get('databases.' . $name, true);
}
+22 -22
View File
@@ -25,7 +25,7 @@ class Db
/**
* @return bool
*/
public static function transactionsActive()
public static function transactionsActive(): bool
{
return static::$isActive;
}
@@ -65,7 +65,7 @@ class Db
*
* @return static
*/
public static function table($table)
public static function table($table): Db|static
{
$db = new Db();
$db->from($table);
@@ -77,7 +77,7 @@ class Db
* @param string $alias
* @return string
*/
public static function any_value(string $column, string $alias = '')
public static function any_value(string $column, string $alias = ''): string
{
if (empty($alias)) {
$alias = $column . '_any_value';
@@ -90,7 +90,7 @@ class Db
* @return mixed
* @throws Exception
*/
public function get(Connection $db = NULL)
public function get(Connection $db = NULL): mixed
{
if (empty($db)) {
$db = Snowflake::app()->database;
@@ -104,17 +104,17 @@ class Db
* @param $column
* @return string
*/
public static function raw($column)
public static function raw($column): string
{
return '`' . $column . '`';
}
/**
* @param Connection|null $db
* @return array|mixed
* @return mixed
* @throws Exception
*/
public function find(Connection $db = NULL)
public function find(Connection $db = NULL): mixed
{
if (empty($db)) {
$db = Snowflake::app()->database;
@@ -129,7 +129,7 @@ class Db
* @return bool|int
* @throws Exception
*/
public function count(Connection $db = NULL)
public function count(Connection $db = NULL): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->database;
@@ -144,7 +144,7 @@ class Db
* @return bool|int
* @throws Exception
*/
public function exists(Connection $db = NULL)
public function exists(Connection $db = NULL): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->database;
@@ -161,7 +161,7 @@ class Db
* @return array|bool|int|string|null
* @throws Exception
*/
public static function findAllBySql(string $sql, array $attributes = [], Connection $db = NULL)
public static function findAllBySql(string $sql, array $attributes = [], Connection $db = NULL): int|bool|array|string|null
{
return $db->createCommand($sql, $attributes)->all();
}
@@ -173,7 +173,7 @@ class Db
* @return array|mixed
* @throws Exception
*/
public static function findBySql(string $sql, array $attributes = [], Connection $db = NULL)
public static function findBySql(string $sql, array $attributes = [], Connection $db = NULL): mixed
{
return $db->createCommand($sql, $attributes)->one();
}
@@ -183,7 +183,7 @@ class Db
* @return array|null
* @throws Exception
*/
public function values(string $field)
public function values(string $field): ?array
{
$data = $this->get();
if (empty($data) || empty($field)) {
@@ -198,10 +198,10 @@ class Db
/**
* @param $field
* @return array|mixed|null
* @return mixed
* @throws Exception
*/
public function value($field)
public function value($field): mixed
{
$data = $this->find();
if (!empty($field) && isset($data[$field])) {
@@ -215,7 +215,7 @@ class Db
* @return bool|int
* @throws Exception
*/
public function delete($db = null)
public function delete($db = null): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->database;
@@ -232,7 +232,7 @@ class Db
* @return bool|int
* @throws Exception
*/
public static function drop($table, $db = null)
public static function drop($table, $db = null): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->database;
@@ -246,7 +246,7 @@ class Db
* @return bool|int
* @throws Exception
*/
public static function truncate($table, $db = null)
public static function truncate($table, $db = null): bool|int
{
if (empty($db)) {
@@ -259,10 +259,10 @@ class Db
/**
* @param $table
* @param Connection|NULL $db
* @return array|mixed|null
* @return mixed
* @throws Exception
*/
public static function showCreateSql($table, Connection $db = NULL)
public static function showCreateSql($table, Connection $db = NULL): mixed
{
if (empty($db)) {
@@ -283,7 +283,7 @@ class Db
* @return bool|int|null
* @throws Exception
*/
public static function desc($table, Connection $db = NULL)
public static function desc($table, Connection $db = NULL): bool|int|null
{
if (empty($db)) {
$db = Snowflake::app()->database;
@@ -300,10 +300,10 @@ class Db
/**
* @param string $table
* @param Connection|NULL $db
* @return array|mixed|null
* @return mixed
* @throws Exception
*/
public static function show(string $table, Connection $db = NULL)
public static function show(string $table, Connection $db = NULL): mixed
{
if (empty($table)) {
return null;
+3 -6
View File
@@ -21,15 +21,13 @@ abstract class HasBase
{
/** @var ActiveRecord|Collection */
protected $data;
protected Collection|ActiveRecord $data;
/**
* @var IOrm
*/
protected IOrm $model;
/** @var */
protected $primaryId;
/** @var array */
protected array $value = [];
@@ -46,7 +44,7 @@ abstract class HasBase
* @param Relation $relation
* @throws Exception
*/
public function __construct(IOrm $model, $primaryId, $value, Relation $relation)
public function __construct(mixed $model, $primaryId, $value, Relation $relation)
{
if (!class_exists($model)) {
throw new Exception('Model must implement ' . ActiveRecord::class);
@@ -64,7 +62,6 @@ abstract class HasBase
$this->_relation = $relation->bindIdentification($model, $_model);
$this->model = $model;
$this->primaryId = $primaryId;
$this->value = $value;
}
@@ -74,7 +71,7 @@ abstract class HasBase
* @param $name
* @return mixed
*/
public function __get($name)
public function __get($name): mixed
{
if (empty($this->value)) {
return null;
+2 -2
View File
@@ -18,7 +18,7 @@ class HasCount extends HasBase
* @return $this
* @throws Exception
*/
public function __call($name, $arguments)
public function __call($name, $arguments): static
{
$this->_relation->getQuery($this->model::className())->$name(...$arguments);
return $this;
@@ -28,7 +28,7 @@ class HasCount extends HasBase
* @return array|null|ActiveRecord
* @throws Exception
*/
public function get()
public function get(): array|ActiveRecord|null
{
return $this->_relation->count($this->model::className(), $this->value);
}
+3 -3
View File
@@ -22,10 +22,10 @@ class HasMany extends HasBase
/**
* @param $name
* @param $arguments
* @return mixed
* @return static
* @throws Exception
*/
public function __call($name, $arguments)
public function __call($name, $arguments): static
{
$this->_relation->getQuery($this->model::className())->$name(...$arguments);
return $this;
@@ -35,7 +35,7 @@ class HasMany extends HasBase
* @return array|null|ActiveRecord
* @throws Exception
*/
public function get()
public function get(): array|ActiveRecord|null
{
return $this->_relation->get($this->model::className(), $this->value);
}
+2 -2
View File
@@ -24,7 +24,7 @@ class HasOne extends HasBase
* @return $this
* @throws Exception
*/
public function __call($name, $arguments)
public function __call($name, $arguments): static
{
$this->_relation->getQuery($this->model::className())->$name(...$arguments);
return $this;
@@ -34,7 +34,7 @@ class HasOne extends HasBase
* @return array|null|ActiveRecord
* @throws Exception
*/
public function get()
public function get(): array|ActiveRecord|null
{
return $this->_relation->first($this->model::className(), $this->value);
}
+4 -4
View File
@@ -21,27 +21,27 @@ interface IOrm
* @param null $db
* @return ActiveRecord
*/
public static function findOne($param, $db = NULL);
public static function findOne($param, $db = NULL): static;
/**
* @return string
*/
public static function className();
public static function className(): string;
/**
* @return ActiveQuery
* return a sql queryBuilder
*/
public static function find();
public static function find(): ActiveQuery;
/**
* @param $dbName
* @return Connection
*/
public static function setDatabaseConnect($dbName);
public static function setDatabaseConnect($dbName): Connection;
// public static function deleteAll($condition, $attributes);
+37 -37
View File
@@ -11,6 +11,7 @@ declare(strict_types=1);
namespace Database\Mysql;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\Component;
use Database\Connection;
use Exception;
@@ -58,7 +59,7 @@ class Columns extends Component
* @return $this
* @throws Exception
*/
public function table(string $table)
public function table(string $table): static
{
$this->structure($this->table = $table);
return $this;
@@ -67,7 +68,7 @@ class Columns extends Component
/**
* @return string
*/
public function getTable()
public function getTable(): string
{
return $this->table;
}
@@ -75,10 +76,10 @@ class Columns extends Component
/**
* @param $key
* @param $val
* @return float|int|mixed|string
* @return mixed
* @throws Exception
*/
public function fieldFormat($key, $val)
public function fieldFormat($key, $val): mixed
{
return $this->encode($val, $this->get_fields($key));
}
@@ -88,7 +89,7 @@ class Columns extends Component
* @return array
* @throws
*/
public function populate($data)
public function populate($data): array
{
$column = $this->get_fields();
foreach ($data as $key => $val) {
@@ -102,11 +103,10 @@ class Columns extends Component
/**
* @param $val
* @param $format
* @return float|int|mixed|string
* @throws
* @param null $format
* @return mixed
*/
public function decode($val, $format = null)
public function decode($val, $format = null): mixed
{
if (empty($format)) {
return $val;
@@ -125,12 +125,12 @@ class Columns extends Component
/**
* @param $name
* @param string $name
* @param $value
* @return float|int|mixed|string
* @return mixed
* @throws Exception
*/
public function _decode(string $name, $value)
public function _decode(string $name, $value): mixed
{
return $this->decode($value, $this->get_fields($name));
}
@@ -138,11 +138,11 @@ class Columns extends Component
/**
* @param $val
* @param $format
* @return float|int|mixed|string
* @throws
* @param null $format
* @return mixed
* @throws Exception
*/
public function encode($val, $format = null)
public function encode($val, $format = null): mixed
{
if (empty($format)) {
return $val;
@@ -163,7 +163,7 @@ class Columns extends Component
* @param $format
* @return bool
*/
public function isInt($format)
#[Pure] public function isInt($format): bool
{
return in_array($format, ['int', 'bigint', 'tinyint', 'smallint', 'mediumint']);
}
@@ -172,7 +172,7 @@ class Columns extends Component
* @param $format
* @return bool
*/
public function isFloat($format)
#[Pure] public function isFloat($format): bool
{
return in_array($format, ['float', 'double', 'decimal']);
}
@@ -181,7 +181,7 @@ class Columns extends Component
* @param $format
* @return bool
*/
public function isJson($format)
#[Pure] public function isJson($format): bool
{
return in_array($format, ['json']);
}
@@ -190,7 +190,7 @@ class Columns extends Component
* @param $format
* @return bool
*/
public function isString($format)
#[Pure] public function isString($format): bool
{
return in_array($format, ['varchar', 'char', 'text', 'longtext', 'tinytext', 'mediumtext']);
}
@@ -200,7 +200,7 @@ class Columns extends Component
* @return array
* @throws
*/
public function format()
public function format(): array
{
return $this->columns('Default', 'Field');
}
@@ -209,7 +209,7 @@ class Columns extends Component
* @return int|string|null
* @throws Exception
*/
public function getAutoIncrement()
public function getAutoIncrement(): int|string|null
{
return $this->_auto_increment[$this->table] ?? null;
}
@@ -219,7 +219,7 @@ class Columns extends Component
*
* @throws Exception
*/
public function getPrimaryKeys()
public function getPrimaryKeys(): array|string|null
{
if (isset($this->_auto_increment[$this->table])) {
return $this->_auto_increment[$this->table];
@@ -232,7 +232,7 @@ class Columns extends Component
*
* @throws Exception
*/
public function getFirstPrimary()
#[Pure] public function getFirstPrimary(): array|string|null
{
if (isset($this->_auto_increment[$this->table])) {
return $this->_auto_increment[$this->table];
@@ -249,7 +249,7 @@ class Columns extends Component
* @return array
* @throws Exception
*/
private function columns($name, $index = null)
private function columns($name, $index = null): array
{
if (empty($index)) {
return array_column($this->getColumns(), $name);
@@ -259,10 +259,10 @@ class Columns extends Component
}
/**
* @return array|bool|int|mixed|string
* @return array|static
* @throws Exception
*/
private function getColumns()
private function getColumns(): array|static
{
return $this->structure($this->getTable());
}
@@ -270,10 +270,10 @@ class Columns extends Component
/**
* @param $table
* @return $this
* @return array|Columns
* @throws Exception
*/
private function structure($table)
private function structure($table): array|static
{
if (!isset($this->columns[$table]) || empty($this->columns[$table])) {
$sql = $this->db->getBuild()->getColumn($table);
@@ -291,9 +291,9 @@ class Columns extends Component
/**
* @param $column
* @param $table
* @return mixed
* @return array
*/
private function resolve(array $column, $table)
private function resolve(array $column, $table): array
{
foreach ($column as $key => $item) {
$this->addPrimary($item, $table);
@@ -335,24 +335,24 @@ class Columns extends Component
* @param $type
* @return string
*/
private function clean($type)
private function clean($type): string
{
if (strpos($type, ')') === false) {
if (!str_contains($type, ')')) {
return $type;
}
$replace = preg_replace('/\(\d+(,\d+)?\)(\s+\w+)*/', '', $type);
if (strpos(' ', $replace) !== FALSE) {
if (str_contains(' ', $replace)) {
$replace = explode(' ', $replace)[1];
}
return $replace;
}
/**
* @param $field
* @return array|string
* @param null $field
* @return array|string|null
* @throws Exception
*/
public function get_fields($field = null)
public function get_fields($field = null): array|string|null
{
$fields = $this->columns('Type', 'Field');
if (empty($field)) {
+6 -6
View File
@@ -28,9 +28,9 @@ class Schema extends Component
private ?Change $_change = null;
/**
* @return Select
* @return Select|null
*/
public function getQueryBuilder()
public function getQueryBuilder(): ?Select
{
if ($this->_builder === null) {
$this->_builder = new Select();
@@ -39,9 +39,9 @@ class Schema extends Component
}
/**
* @return Change
* @return Change|null
*/
public function getChange()
public function getChange(): ?Change
{
if ($this->_change === null) {
$this->_change = new Change();
@@ -51,9 +51,9 @@ class Schema extends Component
/**
* @return Columns
* @return Columns|null
*/
public function getColumns()
public function getColumns(): ?Columns
{
if ($this->_column === null) {
$this->_column = new Columns(['db' => $this->db]);
+13 -12
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Database\Orm;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\BaseObject;
use Database\ActiveQuery;
use Exception;
@@ -26,7 +27,7 @@ class Change extends BaseObject
* @return string
* @throws Exception
*/
public function update(string $model, $attributes, $condition, &$params)
public function update(string $model, $attributes, $condition, &$params): string
{
if (empty($params)) {
throw new Exception("Not has update values.");
@@ -55,7 +56,7 @@ class Change extends BaseObject
* @return array|string
* @throws
*/
public function batchUpdate(string $table, array $attributes, $condition)
public function batchUpdate(string $table, array $attributes, $condition): array|string
{
$param = [];
$_attributes = [];
@@ -83,7 +84,7 @@ class Change extends BaseObject
* @return string
* @throws Exception
*/
public function mathematics($table, $params, $condition)
public function mathematics($table, $params, $condition): string
{
$_tmp = $newParam = [];
if (isset($params['incr']) && is_array($params['incr'])) {
@@ -109,7 +110,7 @@ class Change extends BaseObject
* @return array
* @throws Exception
*/
private function assemble($params, $op, array $_tmp)
private function assemble($params, $op, array $_tmp): array
{
$message = 'Incr And Decr action. The value must a numeric.';
foreach ($params as $key => $val) {
@@ -127,7 +128,7 @@ class Change extends BaseObject
* @param array $params
* @return string
*/
public function insertOrUpdateByDUPLICATE($table, array $params)
public function insertOrUpdateByDUPLICATE($table, array $params): string
{
$keys = implode(',', array_keys($params));
@@ -153,14 +154,14 @@ class Change extends BaseObject
* @return string
* @throws Exception
*/
public function insert($table, $attributes, array $params = NULL)
public function insert($table, $attributes, array $params = NULL): string
{
$sql = $this->inserts($table, implode(',', $attributes), '(:' . implode(',:', $attributes) . ')');
if (empty($params)) {
throw new Exception("save data param not find.");
}
foreach ($params as $key => $val) {
if (strpos($sql, ':' . $key) === FALSE) {
if (!str_contains($sql, ':' . $key)) {
throw new Exception("save $key data param not find.");
}
}
@@ -175,7 +176,7 @@ class Change extends BaseObject
* @return array
* @throws Exception
*/
public function batchInsert($table, $attributes, array $params = NULL)
public function batchInsert($table, $attributes, array $params = NULL): array
{
if (empty($params)) {
throw new Exception("save data param not find.");
@@ -205,7 +206,7 @@ class Change extends BaseObject
* @return string
* 构建SQL语句
*/
private function inserts($table, $fields, $data)
#[Pure] private function inserts($table, $fields, $data): string
{
$query = [
'INSERT IGNORE INTO', '%s', '(%s)', 'VALUES %s'
@@ -223,7 +224,7 @@ class Change extends BaseObject
* @return bool|string
* @throws Exception
*/
public function updateAll($table, $attributes, $condition)
public function updateAll($table, $attributes, $condition): bool|string
{
$param = [];
foreach ($attributes as $key => $val) {
@@ -247,7 +248,7 @@ class Change extends BaseObject
* @return string
* @throws Exception
*/
public function delete(ActiveQuery $query)
public function delete(ActiveQuery $query): string
{
if (empty($query->from)) {
$query->from = $query->getTable();
@@ -266,7 +267,7 @@ class Change extends BaseObject
* @param string $tableName
* @return string
*/
public function truncate(string $tableName)
public function truncate(string $tableName): string
{
return 'TRUNCATE ' . $tableName;
}
+20 -15
View File
@@ -3,8 +3,11 @@ declare(strict_types=1);
namespace Database\Orm;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Core\JSON;
use Snowflake\Core\Str;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use Database\ActiveQuery;
use Database\Base\ConditionClassMap;
@@ -26,7 +29,7 @@ trait Condition
* @return string
* @throws Exception
*/
public function getWhere($query)
public function getWhere($query): string
{
return $this->builderWhere($query);
}
@@ -36,7 +39,7 @@ trait Condition
* @param $alias
* @return string
*/
private function builderAlias($alias)
private function builderAlias($alias): string
{
return " AS " . $alias;
}
@@ -46,7 +49,7 @@ trait Condition
* @return string
* @throws Exception
*/
private function builderFrom($table)
private function builderFrom($table): string
{
if ($table instanceof ActiveQuery) {
$table = '(' . $table->getBuild()->getQuery($table) . ')';
@@ -58,7 +61,7 @@ trait Condition
* @param $join
* @return string
*/
private function builderJoin($join)
#[Pure] private function builderJoin($join): string
{
if (!empty($join)) {
return ' ' . implode(' ', $join);
@@ -71,7 +74,7 @@ trait Condition
* @return string
* @throws Exception
*/
private function builderWhere($where)
private function builderWhere($where): string
{
if (empty($where)) {
return '';
@@ -101,10 +104,11 @@ trait Condition
/**
* @param $value
* @return mixed|object|string|null
* @throws Exception
* @return mixed
* @throws ReflectionException
* @throws NotFindClassException
*/
private function arrayMap($value)
private function arrayMap($value): mixed
{
$classMap = ConditionClassMap::$conditionMap;
if (isset($value[0])) {
@@ -125,10 +129,11 @@ trait Condition
/**
* @param $value
* @return mixed|object
* @throws Exception
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
*/
private function classMap($value)
private function classMap($value): mixed
{
[$option['opera'], $option['column'], $option['value']] = $value;
@@ -146,7 +151,7 @@ trait Condition
* @param $group
* @return string
*/
private function builderGroup($group)
private function builderGroup($group): string
{
if (empty($group)) {
return '';
@@ -158,7 +163,7 @@ trait Condition
* @param $order
* @return string
*/
private function builderOrder($order)
private function builderOrder($order): string
{
if (!empty($order)) {
return ' ORDER BY ' . implode(',', $order);
@@ -171,7 +176,7 @@ trait Condition
* @param ActiveQuery $query
* @return string
*/
private function builderLimit(ActiveQuery $query)
private function builderLimit(ActiveQuery $query): string
{
$limit = $query->limit;
if (!is_numeric($limit) || $limit < 1) {
@@ -192,7 +197,7 @@ trait Condition
* @param bool $isSearch
* @return int|string
*/
public function valueEncode($value, $isSearch = false)
public function valueEncode($value, $isSearch = false): int|string
{
if ($isSearch) {
return $value;
+7 -6
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Database\Orm;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\BaseObject;
use Database\ActiveQuery;
use Database\Sql;
@@ -24,17 +25,17 @@ class Select extends BaseObject
* @return string
* @throws Exception
*/
public function getQuery($query)
public function getQuery(ActiveQuery|Sql|Db $query): string
{
return $this->generate($query, false);
}
/**
* @param ActiveQuery|Db|mixed $query
* @param ActiveQuery|Db|Sql $query
* @return string
* @throws Exception
*/
public function count($query)
public function count(ActiveQuery|Db|Sql $query): string
{
return $this->generate($query, true);
}
@@ -45,7 +46,7 @@ class Select extends BaseObject
* @return string
* @throws Exception
*/
private function generate($query, $isCount = false)
private function generate(ActiveQuery|Db|Sql $query, $isCount = false): string
{
if (empty($query->from)) {
$query->from = $query->getTable();
@@ -78,7 +79,7 @@ class Select extends BaseObject
* @param bool $isCount
* @return string
*/
private function builderSelect($select = NULL, $isCount = false)
#[Pure] private function builderSelect($select = NULL, $isCount = false): string
{
if ($isCount === true) {
return 'SELECT COUNT(*)';
@@ -97,7 +98,7 @@ class Select extends BaseObject
* @param $table
* @return string
*/
public function getColumn($table)
public function getColumn($table): string
{
return 'SHOW FULL FIELDS FROM ' . $table;
}
+10 -10
View File
@@ -52,10 +52,10 @@ class Pagination extends Component
/**
* @param Closure|array $callback
* @param array|Closure $callback
* @throws Exception
*/
public function setCallback($callback)
public function setCallback(array|Closure $callback)
{
if (!is_callable($callback, true)) {
throw new Exception('非法回调函数~');
@@ -68,7 +68,7 @@ class Pagination extends Component
* @param int $number
* @return Pagination
*/
public function setOffset(int $number)
public function setOffset(int $number): static
{
if ($number < 0) {
$number = 0;
@@ -82,7 +82,7 @@ class Pagination extends Component
* @param int $number
* @return Pagination
*/
public function setLimit(int $number)
public function setLimit(int $number): static
{
if ($number < 1) {
$number = 100;
@@ -96,9 +96,9 @@ class Pagination extends Component
/**
* @param int $number
* @return Pagination|void
* @return Pagination
*/
public function setMax(int $number)
public function setMax(int $number): static
{
if ($number < 0) {
return $this;
@@ -125,7 +125,7 @@ class Pagination extends Component
* @param $param
* @return array
*/
public function loop($param)
public function loop($param): array
{
if ($this->_max > 0 && $this->_length >= $this->_max) {
return $this->output();
@@ -145,7 +145,7 @@ class Pagination extends Component
/**
* @return array
*/
public function output()
public function output(): array
{
return [];
}
@@ -172,7 +172,7 @@ class Pagination extends Component
* 解释器
* @return mixed
*/
private function executed($callback, $data, $param)
private function executed($callback, $data, $param): mixed
{
$this->_group->add(1);
return Coroutine::create(function ($callback, $data, $param): void {
@@ -192,7 +192,7 @@ class Pagination extends Component
/**
* @return array|Collection
*/
private function get()
private function get(): Collection|array
{
if ($this->_length + $this->_limit > $this->_max) {
$this->_limit = $this->_length + $this->_limit - $this->_max;
+13 -14
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Database;
use Exception;
use Snowflake\Abstracts\Component;
/**
@@ -23,7 +24,7 @@ class Relation extends Component
* @param ActiveQuery $query
* @return $this
*/
public function bindIdentification(string $identification, ActiveQuery $query)
public function bindIdentification(string $identification, ActiveQuery $query): static
{
$this->_query[$identification] = $query;
return $this;
@@ -33,19 +34,18 @@ class Relation extends Component
* @param $name
* @return ActiveQuery|null
*/
public function getQuery(string $name)
public function getQuery(string $name): ?ActiveQuery
{
return $this->_query[$name] ?? null;
}
/**
* @param $identification
* @param string $identification
* @param $localValue
* @return ActiveRecord|mixed
* @throws
* @return mixed
*/
public function first(string $identification, $localValue)
public function first(string $identification, $localValue): mixed
{
$_identification = $identification . '_first_' . $localValue;
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
@@ -62,12 +62,12 @@ class Relation extends Component
/**
* @param $identification
* @param string $identification
* @param $localValue
* @return ActiveRecord|mixed
* @throws
* @return mixed
* @throws Exception
*/
public function count(string $identification, $localValue)
public function count(string $identification, $localValue): mixed
{
$_identification = $identification . '_count_' . $localValue;
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
@@ -84,12 +84,11 @@ class Relation extends Component
/**
* @param $identification
* @param string $identification
* @param $localValue
* @return array|Collection|mixed|null
* @throws
* @return mixed
*/
public function get(string $identification, $localValue)
public function get(string $identification, $localValue): mixed
{
if (is_array($localValue)) {
$_identification = $identification . '_get_' . implode('_', $localValue);
+3 -2
View File
@@ -11,6 +11,7 @@ namespace Database;
use Database\Orm\Select;
use Database\Traits\QueryTrait;
use Exception;
/**
* Class Sql
@@ -23,9 +24,9 @@ class Sql
/**
* @return string
* @throws \Exception
* @throws Exception
*/
public function getSql()
public function getSql(): string
{
return (new Select())->getQuery($this);
}
File diff suppressed because it is too large Load Diff