qqq
This commit is contained in:
@@ -5,7 +5,6 @@ declare(strict_types=1);
|
|||||||
namespace Database\Base;
|
namespace Database\Base;
|
||||||
|
|
||||||
|
|
||||||
use Database\ActiveQuery;
|
|
||||||
use Database\ModelInterface;
|
use Database\ModelInterface;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
|
|||||||
@@ -1,75 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Database\Base;
|
|
||||||
|
|
||||||
use Database\ModelInterface;
|
|
||||||
|
|
||||||
class Getter
|
|
||||||
{
|
|
||||||
|
|
||||||
private array $getter = [];
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @param string $className
|
|
||||||
* @param string $method
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function write(string $name, string $className, string $method): void
|
|
||||||
{
|
|
||||||
if (!isset($this->getter[$className])) {
|
|
||||||
$this->getter[$className] = [];
|
|
||||||
}
|
|
||||||
$this->getter[$className][$name] = $method;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $className
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getAll(string $className): array
|
|
||||||
{
|
|
||||||
return $this->getter[$className] ?? [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $className
|
|
||||||
* @param string $name
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function has(string $className, string $name): bool
|
|
||||||
{
|
|
||||||
return isset($this->getter[$className]) && isset($this->getter[$className][$name]);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param ModelInterface $class
|
|
||||||
* @param string $key
|
|
||||||
* @param mixed $value
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function override(ModelInterface $class, string $key, mixed $value): mixed
|
|
||||||
{
|
|
||||||
$method = $this->getter[$class::class][$key] ?? null;
|
|
||||||
if ($method !== null) {
|
|
||||||
return $class->{$method}($value);
|
|
||||||
}
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $className
|
|
||||||
* @param string $name
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function get(string $className, string $name): ?string
|
|
||||||
{
|
|
||||||
if (!$this->has($className,$name)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return $this->getter[$className][$name];
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
+1
-1
@@ -536,7 +536,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
|
|||||||
}
|
}
|
||||||
$validate = $this->resolve($rule);
|
$validate = $this->resolve($rule);
|
||||||
if (!$validate->validation()) {
|
if (!$validate->validation()) {
|
||||||
return \Kiri::getLogger()->addError($validate->getError(), 'mysql');
|
return \Kiri::getLogger()->failure($validate->getError(), 'mysql');
|
||||||
} else {
|
} else {
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,80 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Database\Base;
|
|
||||||
|
|
||||||
use Database\ModelInterface;
|
|
||||||
|
|
||||||
class Setter
|
|
||||||
{
|
|
||||||
|
|
||||||
private array $setter = [];
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @param string $className
|
|
||||||
* @param string $method
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function write(string $name, string $className, string $method): void
|
|
||||||
{
|
|
||||||
if (!isset($this->setter[$className])) {
|
|
||||||
$this->setter[$className] = [];
|
|
||||||
}
|
|
||||||
$this->setter[$className][$name] = $method;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $className
|
|
||||||
* @param string $name
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function has(string $className, string $name): bool
|
|
||||||
{
|
|
||||||
return isset($this->setter[$className]) && isset($this->setter[$className][$name]);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $className
|
|
||||||
* @return array|null
|
|
||||||
*/
|
|
||||||
public function getAll(string $className): ?array
|
|
||||||
{
|
|
||||||
return $this->setter[$className] ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param ModelInterface $class
|
|
||||||
* @param string $key
|
|
||||||
* @param mixed $value
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function override(ModelInterface $class, string $key, mixed $value): mixed
|
|
||||||
{
|
|
||||||
$method = $this->setter[$class::class][$key] ?? null;
|
|
||||||
if ($method !== null) {
|
|
||||||
return $class->{$method}($value);
|
|
||||||
}
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $className
|
|
||||||
* @param string $name
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function get(string $className, string $name): ?string
|
|
||||||
{
|
|
||||||
if (!$this->has($className, $name)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return $this->setter[$className][$name];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
+9
-12
@@ -59,16 +59,14 @@ class Collection extends AbstractCollection
|
|||||||
public function update(array $attributes): bool
|
public function update(array $attributes): bool
|
||||||
{
|
{
|
||||||
$lists = [];
|
$lists = [];
|
||||||
$primary = $this->getModel()->getPrimary();
|
$model = $this->getModel();
|
||||||
$items = $this->getItems();
|
if (!$this->isEmpty()) {
|
||||||
if ($this->isEmpty() || !isset($items[0][$primary])) {
|
foreach ($this->_item as $item) {
|
||||||
return false;
|
$lists[] = $item[$model->getPrimary()];
|
||||||
}
|
}
|
||||||
foreach ($items as $item) {
|
return $model::query()->whereIn($model->getPrimary(), $lists)->update($attributes);
|
||||||
$lists[] = $item[$primary];
|
}
|
||||||
}
|
return false;
|
||||||
return $this->getModel()::query()->whereIn($primary, $lists)
|
|
||||||
->update($attributes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -185,8 +183,7 @@ class Collection extends AbstractCollection
|
|||||||
{
|
{
|
||||||
$model = $this->getModel();
|
$model = $this->getModel();
|
||||||
if ($model->hasPrimary()) {
|
if ($model->hasPrimary()) {
|
||||||
$key = $model->getPrimary();
|
return $model::query()->whereIn($model->getPrimary(), $this->column($model->getPrimary()))->delete();
|
||||||
return $model::query()->whereIn($key, $this->column($key))->delete();
|
|
||||||
}
|
}
|
||||||
throw new Exception('Must set primary key. if you wante delete');
|
throw new Exception('Must set primary key. if you wante delete');
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-27
@@ -15,6 +15,7 @@ use Kiri\Abstracts\Component;
|
|||||||
use Kiri\Di\Container;
|
use Kiri\Di\Container;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use PDO;
|
use PDO;
|
||||||
|
use PDOStatement;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
@@ -77,12 +78,7 @@ class Command extends Component
|
|||||||
public function all(): bool|array
|
public function all(): bool|array
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$client = $this->connection->getConnection();
|
return $this->prepare()->fetchAll(PDO::FETCH_ASSOC);
|
||||||
if (($prepare = $client->prepare($this->sql)) === false) {
|
|
||||||
throw new Exception($client->errorInfo()[1]);
|
|
||||||
}
|
|
||||||
$prepare->execute($this->params);
|
|
||||||
return $prepare->fetchAll(PDO::FETCH_ASSOC);
|
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
if ($this->canReconnect($throwable->getMessage())) {
|
if ($this->canReconnect($throwable->getMessage())) {
|
||||||
return $this->all();
|
return $this->all();
|
||||||
@@ -100,12 +96,7 @@ class Command extends Component
|
|||||||
public function one(): null|bool|array
|
public function one(): null|bool|array
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$client = $this->connection->getConnection();
|
return $this->prepare()->fetch(PDO::FETCH_ASSOC);
|
||||||
if (($prepare = $client->prepare($this->sql)) === false) {
|
|
||||||
throw new Exception($client->errorInfo()[1]);
|
|
||||||
}
|
|
||||||
$prepare->execute($this->params);
|
|
||||||
return $prepare->fetch(PDO::FETCH_ASSOC);
|
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
if ($this->canReconnect($throwable->getMessage())) {
|
if ($this->canReconnect($throwable->getMessage())) {
|
||||||
return $this->one();
|
return $this->one();
|
||||||
@@ -123,12 +114,7 @@ class Command extends Component
|
|||||||
public function fetchColumn(): null|bool|array
|
public function fetchColumn(): null|bool|array
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$client = $this->connection->getConnection();
|
return $this->prepare()->fetchColumn(PDO::FETCH_ASSOC);
|
||||||
if (($prepare = $client->prepare($this->sql)) === false) {
|
|
||||||
throw new Exception($client->errorInfo()[1]);
|
|
||||||
}
|
|
||||||
$prepare->execute($this->params);
|
|
||||||
return $prepare->fetchColumn(PDO::FETCH_ASSOC);
|
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
if ($this->canReconnect($throwable->getMessage())) {
|
if ($this->canReconnect($throwable->getMessage())) {
|
||||||
return $this->fetchColumn();
|
return $this->fetchColumn();
|
||||||
@@ -146,12 +132,7 @@ class Command extends Component
|
|||||||
public function rowCount(): int|bool
|
public function rowCount(): int|bool
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$client = $this->connection->getConnection();
|
return $this->prepare()->rowCount();
|
||||||
if (($prepare = $client->prepare($this->sql)) === false) {
|
|
||||||
throw new Exception($client->errorInfo()[1]);
|
|
||||||
}
|
|
||||||
$prepare->execute($this->params);
|
|
||||||
return $prepare->rowCount();
|
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
if ($this->canReconnect($throwable->getMessage())) {
|
if ($this->canReconnect($throwable->getMessage())) {
|
||||||
return $this->rowCount();
|
return $this->rowCount();
|
||||||
@@ -163,6 +144,21 @@ class Command extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return PDOStatement
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
protected function prepare(): PDOStatement
|
||||||
|
{
|
||||||
|
$client = $this->connection->getConnection();
|
||||||
|
if (($prepare = $client->prepare($this->sql)) === false) {
|
||||||
|
throw new Exception($client->errorInfo()[1]);
|
||||||
|
}
|
||||||
|
$prepare->execute($this->params);
|
||||||
|
return $prepare;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int|bool
|
* @return int|bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
@@ -173,7 +169,6 @@ class Command extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool|int
|
* @return bool|int
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
@@ -230,8 +225,7 @@ class Command extends Component
|
|||||||
*/
|
*/
|
||||||
private function error(Throwable $throwable): bool
|
private function error(Throwable $throwable): bool
|
||||||
{
|
{
|
||||||
$message = $this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE) . PHP_EOL . jTraceEx($throwable);
|
return trigger_print_error($this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE) . PHP_EOL . jTraceEx($throwable), 'mysql');
|
||||||
return addError($message, 'mysql');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ use Kiri;
|
|||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
|
use ReflectionException;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -56,7 +57,9 @@ class Db implements ISqlBuilder
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
* @throws Exception
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public static function beginTransaction(): void
|
public static function beginTransaction(): void
|
||||||
{
|
{
|
||||||
@@ -78,8 +81,7 @@ class Db implements ISqlBuilder
|
|||||||
try {
|
try {
|
||||||
$result = call_user_func($closure, ...$params);
|
$result = call_user_func($closure, ...$params);
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
error($throwable);
|
$result = trigger_print_error($throwable->getMessage(), 'mysql');
|
||||||
$result = addError($throwable->getMessage(), 'mysql');
|
|
||||||
} finally {
|
} finally {
|
||||||
if ($result === false) {
|
if ($result === false) {
|
||||||
static::rollback();
|
static::rollback();
|
||||||
@@ -93,6 +95,9 @@ class Db implements ISqlBuilder
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public static function commit(): void
|
public static function commit(): void
|
||||||
{
|
{
|
||||||
@@ -102,6 +107,9 @@ class Db implements ISqlBuilder
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public static function rollback(): void
|
public static function rollback(): void
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -10,15 +10,10 @@ declare(strict_types=1);
|
|||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
|
|
||||||
use Database\Base\Getter;
|
|
||||||
use Database\Traits\HasBase;
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri;
|
use Kiri;
|
||||||
use Kiri\Exception\NotFindClassException;
|
|
||||||
use Kiri\Error\StdoutLoggerInterface;
|
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
use ReflectionException;
|
|
||||||
|
|
||||||
defined('SAVE_FAIL') or define('SAVE_FAIL', 3227);
|
defined('SAVE_FAIL') or define('SAVE_FAIL', 3227);
|
||||||
defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.');
|
defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.');
|
||||||
@@ -185,7 +180,7 @@ class Model extends Base\Model
|
|||||||
public static function inserts(array $data): bool
|
public static function inserts(array $data): bool
|
||||||
{
|
{
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
return addError('Insert data empty.', 'mysql');
|
return trigger_print_error('Insert data empty.', 'mysql');
|
||||||
}
|
}
|
||||||
return static::query()->insert($data);
|
return static::query()->insert($data);
|
||||||
}
|
}
|
||||||
@@ -196,15 +191,15 @@ class Model extends Base\Model
|
|||||||
*/
|
*/
|
||||||
public function delete(): bool
|
public function delete(): bool
|
||||||
{
|
{
|
||||||
if (!$this->beforeDelete()) {
|
if ($this->beforeDelete()) {
|
||||||
return false;
|
if ($this->hasPrimary()) {
|
||||||
|
$result = static::deleteByCondition("id = :id", [":id" => $this->getPrimaryValue()]);
|
||||||
|
} else {
|
||||||
|
$result = static::deleteByCondition($this->_attributes);
|
||||||
|
}
|
||||||
|
return $this->afterDelete($result);
|
||||||
}
|
}
|
||||||
if ($this->hasPrimary()) {
|
return false;
|
||||||
$result = static::deleteByCondition("id = :id", [":id" => $this->getPrimaryValue()]);
|
|
||||||
} else {
|
|
||||||
$result = static::deleteByCondition($this->_attributes);
|
|
||||||
}
|
|
||||||
return $this->afterDelete($result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -217,8 +212,7 @@ class Model extends Base\Model
|
|||||||
*/
|
*/
|
||||||
public static function updateAll(mixed $condition, array $attributes = []): bool
|
public static function updateAll(mixed $condition, array $attributes = []): bool
|
||||||
{
|
{
|
||||||
$condition = static::query()->where($condition);
|
return static::query()->where($condition)->update($attributes);
|
||||||
return $condition->update($attributes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+353
-350
@@ -25,382 +25,385 @@ use Kiri\Core\Json;
|
|||||||
class Columns extends Component
|
class Columns extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
* field types
|
* field types
|
||||||
*/
|
*/
|
||||||
private array $columns = [];
|
private array $columns = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Connection
|
* @var string
|
||||||
* Mysql client
|
* tableName
|
||||||
*/
|
*/
|
||||||
public Connection $db;
|
public string $table = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var array
|
||||||
* tableName
|
* field primary key
|
||||||
*/
|
*/
|
||||||
public string $table = '';
|
private array $_primary = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
* field primary key
|
* by mysql field auto_increment
|
||||||
*/
|
*/
|
||||||
private array $_primary = [];
|
private array $_auto_increment = [];
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
* by mysql field auto_increment
|
|
||||||
*/
|
|
||||||
private array $_auto_increment = [];
|
|
||||||
|
|
||||||
|
|
||||||
private array $_fields = [];
|
private array $_fields = [];
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $table
|
|
||||||
* @return $this
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
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 mixed
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function fieldFormat($key, $val): mixed
|
|
||||||
{
|
|
||||||
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 Connection $db
|
||||||
* @param $value
|
*/
|
||||||
* @return mixed
|
public function __construct(public Connection $db)
|
||||||
* @throws Exception
|
{
|
||||||
*/
|
parent::__construct();
|
||||||
public function _decode(string $name, $value): mixed
|
}
|
||||||
{
|
|
||||||
return $this->decode($value, $this->get_fields($name));
|
/**
|
||||||
}
|
* @param string $table
|
||||||
|
* @return $this
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
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 Exception
|
||||||
|
*/
|
||||||
|
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 $val
|
* @param string $name
|
||||||
* @param null $format
|
* @param $value
|
||||||
* @return float|bool|int|string
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function encode($val, $format = null): float|bool|int|string
|
public function _decode(string $name, $value): mixed
|
||||||
{
|
{
|
||||||
if (empty($format)) {
|
return $this->decode($value, $this->get_fields($name));
|
||||||
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
|
* @param $val
|
||||||
* @throws
|
* @param null $format
|
||||||
*/
|
* @return float|bool|int|string
|
||||||
public function format(): array
|
* @throws Exception
|
||||||
{
|
*/
|
||||||
return $this->columns('Default', 'Field');
|
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 mixed
|
* @return array
|
||||||
* @throws Exception
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function getFields(): array
|
public function format(): array
|
||||||
{
|
{
|
||||||
if (empty($this->_fields)) {
|
return $this->columns('Default', 'Field');
|
||||||
$this->structure($this->table);
|
}
|
||||||
}
|
|
||||||
return $this->_fields[$this->table];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @return mixed
|
||||||
* @return bool
|
* @throws Exception
|
||||||
* @throws Exception
|
*/
|
||||||
*/
|
public function getFields(): array
|
||||||
public function hasField(string $name): bool
|
{
|
||||||
{
|
if (empty($this->_fields)) {
|
||||||
return array_key_exists($name, $this->getFields());
|
$this->structure($this->table);
|
||||||
}
|
}
|
||||||
|
return $this->_fields[$this->table];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int|string|null
|
* @param string $name
|
||||||
* @throws Exception
|
* @return bool
|
||||||
*/
|
* @throws Exception
|
||||||
public function getAutoIncrement(): int|string|null
|
*/
|
||||||
{
|
public function hasField(string $name): bool
|
||||||
return $this->_auto_increment[$this->table] ?? null;
|
{
|
||||||
}
|
return array_key_exists($name, $this->getFields());
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @return array|null|string
|
|
||||||
*
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
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 Exception
|
|
||||||
*/
|
|
||||||
#[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 Exception
|
|
||||||
*/
|
|
||||||
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 Exception
|
|
||||||
*/
|
|
||||||
private function getColumns(): array|static
|
|
||||||
{
|
|
||||||
return $this->structure($this->getTable());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $table
|
* @return int|string|null
|
||||||
* @return array|Columns
|
* @throws Exception
|
||||||
* @throws Exception
|
*/
|
||||||
*/
|
public function getAutoIncrement(): int|string|null
|
||||||
private function structure($table): array|static
|
{
|
||||||
{
|
return $this->_auto_increment[$this->table] ?? null;
|
||||||
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 array|null|string
|
||||||
}
|
*
|
||||||
return $this->columns[$table] = $this->resolve($column, $table);
|
* @throws Exception
|
||||||
}
|
*/
|
||||||
return $this->columns[$table];
|
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 Exception
|
||||||
|
*/
|
||||||
|
#[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 Exception
|
||||||
|
*/
|
||||||
|
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 Exception
|
||||||
|
*/
|
||||||
|
private function getColumns(): array|static
|
||||||
|
{
|
||||||
|
return $this->structure($this->getTable());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $column
|
* @param $table
|
||||||
* @param $table
|
* @return array|Columns
|
||||||
* @return array
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function resolve(array $column, $table): array
|
private function structure($table): array|static
|
||||||
{
|
{
|
||||||
foreach ($column as $key => $item) {
|
if (!isset($this->columns[$table]) || empty($this->columns[$table])) {
|
||||||
$this->addPrimary($item, $table);
|
$column = $this->db->createCommand(SqlBuilder::builder(null)->columns($table))->all();
|
||||||
$column[$key]['Type'] = $this->clean($item['Type']);
|
if (empty($column)) {
|
||||||
}
|
throw new Exception("The table " . $table . " not exists.");
|
||||||
|
}
|
||||||
$this->_fields[$table] = array_column($column, 'Default', 'Field');
|
return $this->columns[$table] = $this->resolve($column, $table);
|
||||||
|
}
|
||||||
return $column;
|
return $this->columns[$table];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $item
|
|
||||||
* @param $table
|
|
||||||
*/
|
|
||||||
private function addPrimary($item, $table)
|
|
||||||
{
|
|
||||||
if (!isset($this->_primary[$table])) {
|
|
||||||
$this->_primary[$table] = [];
|
|
||||||
}
|
|
||||||
if ($item['Key'] === 'PRI') {
|
|
||||||
$this->_primary[$table][] = $item['Field'];
|
|
||||||
}
|
|
||||||
$this->addIncrement($item, $table);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $item
|
* @param array $column
|
||||||
* @param $table
|
* @param $table
|
||||||
*/
|
* @return array
|
||||||
private function addIncrement($item, $table)
|
*/
|
||||||
{
|
private function resolve(array $column, $table): array
|
||||||
if ($item['Extra'] !== 'auto_increment') {
|
{
|
||||||
return;
|
foreach ($column as $key => $item) {
|
||||||
}
|
$this->addPrimary($item, $table);
|
||||||
$this->_auto_increment[$table] = $item['Field'];
|
$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)
|
||||||
|
{
|
||||||
|
if (!isset($this->_primary[$table])) {
|
||||||
|
$this->_primary[$table] = [];
|
||||||
|
}
|
||||||
|
if ($item['Key'] === 'PRI') {
|
||||||
|
$this->_primary[$table][] = $item['Field'];
|
||||||
|
}
|
||||||
|
$this->addIncrement($item, $table);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $type
|
* @param $item
|
||||||
* @return string
|
* @param $table
|
||||||
*/
|
*/
|
||||||
public function clean($type): string
|
private function addIncrement($item, $table)
|
||||||
{
|
{
|
||||||
if (!str_contains($type, ')')) {
|
if ($item['Extra'] !== 'auto_increment') {
|
||||||
return $type;
|
return;
|
||||||
}
|
}
|
||||||
$replace = preg_replace('/\(\d+(,\d+)?\)(\s+\w+)*/', '', $type);
|
$this->_auto_increment[$table] = $item['Field'];
|
||||||
if (str_contains($replace, ' ')) {
|
}
|
||||||
$replace = explode(' ', $replace)[1];
|
|
||||||
}
|
|
||||||
return $replace;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param null $field
|
|
||||||
* @return array|string|null
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
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
|
* @param $type
|
||||||
* @throws Exception
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getAllField(): array
|
public function clean($type): string
|
||||||
{
|
{
|
||||||
return $this->columns('Type', 'Field');
|
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 Exception
|
||||||
|
*/
|
||||||
|
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 Exception
|
||||||
|
*/
|
||||||
|
public function getAllField(): array
|
||||||
|
{
|
||||||
|
return $this->columns('Type', 'Field');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -28,7 +28,7 @@ class Schema extends Component
|
|||||||
public function getColumns(): ?Columns
|
public function getColumns(): ?Columns
|
||||||
{
|
{
|
||||||
if ($this->_column === null) {
|
if ($this->_column === null) {
|
||||||
$this->_column = new Columns(['db' => $this->db]);
|
$this->_column = new Columns($this->db);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_column;
|
return $this->_column;
|
||||||
|
|||||||
+162
-158
@@ -15,193 +15,197 @@ use Kiri\Abstracts\Component;
|
|||||||
class Pagination extends Component
|
class Pagination extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
/** @var ActiveQuery */
|
/** @var ActiveQuery */
|
||||||
private ActiveQuery $activeQuery;
|
private ActiveQuery $activeQuery;
|
||||||
|
|
||||||
/** @var int 从第几个开始查 */
|
/** @var int 从第几个开始查 */
|
||||||
private int $_offset = 0;
|
private int $_offset = 0;
|
||||||
|
|
||||||
/** @var int 每页数量 */
|
/** @var int 每页数量 */
|
||||||
private int $_limit = 100;
|
private int $_limit = 100;
|
||||||
|
|
||||||
/** @var int 最大查询数量 */
|
/** @var int 最大查询数量 */
|
||||||
private int $_max = 0;
|
private int $_max = 0;
|
||||||
|
|
||||||
/** @var int 当前已查询数量 */
|
/** @var int 当前已查询数量 */
|
||||||
private int $_length = 0;
|
private int $_length = 0;
|
||||||
|
|
||||||
/** @var Closure */
|
/** @var Closure */
|
||||||
private Closure $_callback;
|
private Closure $_callback;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PaginationIteration constructor.
|
* PaginationIteration constructor.
|
||||||
* @param ActiveQuery $activeQuery
|
* @param ActiveQuery $activeQuery
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct(ActiveQuery $activeQuery, array $config = [])
|
public function __construct(ActiveQuery $activeQuery, array $config = [])
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->activeQuery = $activeQuery;
|
$this->activeQuery = $activeQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function clean()
|
/**
|
||||||
{
|
* @return void
|
||||||
unset($this->activeQuery, $this->_callback, $this->_group);
|
*/
|
||||||
$this->_offset = 0;
|
public function clean(): void
|
||||||
$this->_limit = 100;
|
{
|
||||||
$this->_max = 0;
|
unset($this->activeQuery, $this->_callback, $this->_group);
|
||||||
$this->_length = 0;;
|
$this->_offset = 0;
|
||||||
}
|
$this->_limit = 100;
|
||||||
|
$this->_max = 0;
|
||||||
|
$this->_length = 0;;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* recover class by clone
|
* recover class by clone
|
||||||
*/
|
*/
|
||||||
public function __clone()
|
public function __clone()
|
||||||
{
|
{
|
||||||
$this->clean();
|
$this->clean();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|Closure $callback
|
* @param array|Closure $callback
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function setCallback(array|Closure $callback)
|
public function setCallback(array|Closure $callback)
|
||||||
{
|
{
|
||||||
if (!is_callable($callback, true)) {
|
if (!is_callable($callback, true)) {
|
||||||
throw new Exception('非法回调函数~');
|
throw new Exception('非法回调函数~');
|
||||||
}
|
}
|
||||||
$this->_callback = $callback;
|
$this->_callback = $callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $number
|
* @param int $number
|
||||||
* @return Pagination
|
* @return Pagination
|
||||||
*/
|
*/
|
||||||
public function setOffset(int $number): static
|
public function setOffset(int $number): static
|
||||||
{
|
{
|
||||||
if ($number < 0) {
|
if ($number < 0) {
|
||||||
$number = 0;
|
$number = 0;
|
||||||
}
|
}
|
||||||
$this->_offset = $number;
|
$this->_offset = $number;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $number
|
* @param int $number
|
||||||
* @return Pagination
|
* @return Pagination
|
||||||
*/
|
*/
|
||||||
public function setLimit(int $number): static
|
public function setLimit(int $number): static
|
||||||
{
|
{
|
||||||
if ($number < 1) {
|
if ($number < 1) {
|
||||||
$number = 100;
|
$number = 100;
|
||||||
} else if ($number > 5000) {
|
} else if ($number > 5000) {
|
||||||
$number = 5000;
|
$number = 5000;
|
||||||
}
|
}
|
||||||
$this->_limit = $number;
|
$this->_limit = $number;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $number
|
* @param int $number
|
||||||
* @return Pagination
|
* @return Pagination
|
||||||
*/
|
*/
|
||||||
public function setMax(int $number): static
|
public function setMax(int $number): static
|
||||||
{
|
{
|
||||||
if ($number < 0) {
|
if ($number < 0) {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
$this->_max = $number;
|
$this->_max = $number;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $param
|
* @param array $param
|
||||||
* @return void
|
* @return void
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function plunk(array $param = []): void
|
public function plunk(array $param = []): void
|
||||||
{
|
{
|
||||||
$this->loop($param);
|
$this->loop($param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 轮训
|
* 轮训
|
||||||
* @param $param
|
* @param $param
|
||||||
* @return array
|
* @return array
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function loop($param): array
|
public function loop($param): array
|
||||||
{
|
{
|
||||||
if ($this->_max > 0 && $this->_length >= $this->_max) {
|
if ($this->_max > 0 && $this->_length >= $this->_max) {
|
||||||
return $this->output();
|
return $this->output();
|
||||||
}
|
}
|
||||||
[$length, $data] = $this->get();
|
[$length, $data] = $this->get();
|
||||||
try {
|
try {
|
||||||
call_user_func($this->_callback, $data, $param);
|
call_user_func($this->_callback, $data, $param);
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
error($exception);
|
error($exception);
|
||||||
} finally {
|
} finally {
|
||||||
$data = null;
|
$data = null;
|
||||||
}
|
}
|
||||||
if ($length < $this->_limit) {
|
if ($length < $this->_limit) {
|
||||||
return $this->output();
|
return $this->output();
|
||||||
}
|
}
|
||||||
return $this->loop($param);
|
return $this->loop($param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function output(): array
|
public function output(): array
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $data
|
* @param $data
|
||||||
* @param $param
|
* @param $param
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function executed($data, $param): void
|
private function executed($data, $param): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
call_user_func($this->_callback, $data, $param);
|
call_user_func($this->_callback, $data, $param);
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
error($exception);
|
error($exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|Collection
|
* @return array|Collection
|
||||||
*/
|
* @throws Exception
|
||||||
private function get(): Collection|array
|
*/
|
||||||
{
|
private function get(): Collection|array
|
||||||
if ($this->_max > 0 && $this->_length + $this->_limit > $this->_max) {
|
{
|
||||||
$this->_limit = $this->_length + $this->_limit - $this->_max;
|
if ($this->_max > 0 && $this->_length + $this->_limit > $this->_max) {
|
||||||
}
|
$this->_limit = $this->_length + $this->_limit - $this->_max;
|
||||||
$data = $this->activeQuery->offset($this->_offset)->limit($this->_limit)->get();
|
}
|
||||||
$this->_offset += $this->_limit;
|
$data = $this->activeQuery->offset($this->_offset)->limit($this->_limit)->get();
|
||||||
|
$this->_offset += $this->_limit;
|
||||||
|
|
||||||
if (is_array($data)) {
|
if (is_array($data)) {
|
||||||
$size = count($data);
|
$size = count($data);
|
||||||
} else {
|
} else {
|
||||||
$size = $data->size();
|
$size = $data->size();
|
||||||
}
|
}
|
||||||
$this->_length += $size;
|
$this->_length += $size;
|
||||||
return [$size, $data];
|
return [$size, $data];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-2
@@ -105,9 +105,8 @@ class SqlBuilder extends Component
|
|||||||
private function __updateBuilder(array $string): string|bool
|
private function __updateBuilder(array $string): string|bool
|
||||||
{
|
{
|
||||||
if (empty($string)) {
|
if (empty($string)) {
|
||||||
return \Kiri::getLogger()->addError('None data update.');
|
return \Kiri::getLogger()->failure('None data update.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'UPDATE ' . $this->query->from . ' SET ' . implode(',', $string) . $this->_prefix();
|
return 'UPDATE ' . $this->query->from . ' SET ' . implode(',', $string) . $this->_prefix();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-12
@@ -112,23 +112,13 @@ trait Builder
|
|||||||
/**
|
/**
|
||||||
* @param array $where
|
* @param array $where
|
||||||
* @return string
|
* @return string
|
||||||
* @throws NotFindClassException
|
*/
|
||||||
* @throws ReflectionException
|
|
||||||
*/
|
|
||||||
private function where(array $where): string
|
private function where(array $where): string
|
||||||
{
|
{
|
||||||
if (count($where) < 1) {
|
if (count($where) < 1) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
$_tmp = [];
|
return implode(' AND ', $where);
|
||||||
foreach ($where as $key => $value) {
|
|
||||||
$_tmp[] = $this->resolveCondition($key, $value, $_tmp);
|
|
||||||
}
|
|
||||||
if (count($_tmp) > 0) {
|
|
||||||
return implode(' AND ', $_tmp);
|
|
||||||
} else {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+761
-872
File diff suppressed because it is too large
Load Diff
+2
-2
@@ -54,8 +54,8 @@ class When
|
|||||||
/**
|
/**
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
*/
|
*/
|
||||||
public function else(string $alias)
|
public function else(string $alias): void
|
||||||
{
|
{
|
||||||
$this->else = $alias;
|
$this->else = $alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user