This commit is contained in:
2023-07-31 23:08:59 +08:00
parent 45d0712cb2
commit 00a2125efc
15 changed files with 1334 additions and 1612 deletions
-1
View File
@@ -5,7 +5,6 @@ declare(strict_types=1);
namespace Database\Base;
use Database\ActiveQuery;
use Database\ModelInterface;
use Exception;
-75
View File
@@ -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
View File
@@ -536,7 +536,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
}
$validate = $this->resolve($rule);
if (!$validate->validation()) {
return \Kiri::getLogger()->addError($validate->getError(), 'mysql');
return \Kiri::getLogger()->failure($validate->getError(), 'mysql');
} else {
return TRUE;
}
-80
View File
@@ -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];
}
}
+8 -11
View File
@@ -59,17 +59,15 @@ class Collection extends AbstractCollection
public function update(array $attributes): bool
{
$lists = [];
$primary = $this->getModel()->getPrimary();
$items = $this->getItems();
if ($this->isEmpty() || !isset($items[0][$primary])) {
$model = $this->getModel();
if (!$this->isEmpty()) {
foreach ($this->_item as $item) {
$lists[] = $item[$model->getPrimary()];
}
return $model::query()->whereIn($model->getPrimary(), $lists)->update($attributes);
}
return false;
}
foreach ($items as $item) {
$lists[] = $item[$primary];
}
return $this->getModel()::query()->whereIn($primary, $lists)
->update($attributes);
}
/**
* @param string $field
@@ -185,8 +183,7 @@ class Collection extends AbstractCollection
{
$model = $this->getModel();
if ($model->hasPrimary()) {
$key = $model->getPrimary();
return $model::query()->whereIn($key, $this->column($key))->delete();
return $model::query()->whereIn($model->getPrimary(), $this->column($model->getPrimary()))->delete();
}
throw new Exception('Must set primary key. if you wante delete');
}
+21 -27
View File
@@ -15,6 +15,7 @@ use Kiri\Abstracts\Component;
use Kiri\Di\Container;
use Kiri\Exception\ConfigException;
use PDO;
use PDOStatement;
use ReflectionException;
use Throwable;
@@ -77,12 +78,7 @@ class Command extends Component
public function all(): bool|array
{
try {
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]);
}
$prepare->execute($this->params);
return $prepare->fetchAll(PDO::FETCH_ASSOC);
return $this->prepare()->fetchAll(PDO::FETCH_ASSOC);
} catch (Throwable $throwable) {
if ($this->canReconnect($throwable->getMessage())) {
return $this->all();
@@ -100,12 +96,7 @@ class Command extends Component
public function one(): null|bool|array
{
try {
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]);
}
$prepare->execute($this->params);
return $prepare->fetch(PDO::FETCH_ASSOC);
return $this->prepare()->fetch(PDO::FETCH_ASSOC);
} catch (Throwable $throwable) {
if ($this->canReconnect($throwable->getMessage())) {
return $this->one();
@@ -123,12 +114,7 @@ class Command extends Component
public function fetchColumn(): null|bool|array
{
try {
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]);
}
$prepare->execute($this->params);
return $prepare->fetchColumn(PDO::FETCH_ASSOC);
return $this->prepare()->fetchColumn(PDO::FETCH_ASSOC);
} catch (Throwable $throwable) {
if ($this->canReconnect($throwable->getMessage())) {
return $this->fetchColumn();
@@ -146,12 +132,7 @@ class Command extends Component
public function rowCount(): int|bool
{
try {
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]);
}
$prepare->execute($this->params);
return $prepare->rowCount();
return $this->prepare()->rowCount();
} catch (Throwable $throwable) {
if ($this->canReconnect($throwable->getMessage())) {
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
* @throws Exception
@@ -173,7 +169,6 @@ class Command extends Component
}
/**
* @return bool|int
* @throws ConfigException
@@ -230,8 +225,7 @@ class Command extends Component
*/
private function error(Throwable $throwable): bool
{
$message = $this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE) . PHP_EOL . jTraceEx($throwable);
return addError($message, 'mysql');
return trigger_print_error($this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE) . PHP_EOL . jTraceEx($throwable), 'mysql');
}
+11 -3
View File
@@ -19,6 +19,7 @@ use Kiri;
use Kiri\Exception\ConfigException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use Throwable;
/**
@@ -56,7 +57,9 @@ class Db implements ISqlBuilder
/**
* @return void
* @throws Exception
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public static function beginTransaction(): void
{
@@ -78,8 +81,7 @@ class Db implements ISqlBuilder
try {
$result = call_user_func($closure, ...$params);
} catch (Throwable $throwable) {
error($throwable);
$result = addError($throwable->getMessage(), 'mysql');
$result = trigger_print_error($throwable->getMessage(), 'mysql');
} finally {
if ($result === false) {
static::rollback();
@@ -93,6 +95,9 @@ class Db implements ISqlBuilder
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public static function commit(): void
{
@@ -102,6 +107,9 @@ class Db implements ISqlBuilder
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public static function rollback(): void
{
+5 -11
View File
@@ -10,15 +10,10 @@ declare(strict_types=1);
namespace Database;
use Database\Base\Getter;
use Database\Traits\HasBase;
use Exception;
use Kiri;
use Kiri\Exception\NotFindClassException;
use Kiri\Error\StdoutLoggerInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
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.');
@@ -185,7 +180,7 @@ class Model extends Base\Model
public static function inserts(array $data): bool
{
if (empty($data)) {
return addError('Insert data empty.', 'mysql');
return trigger_print_error('Insert data empty.', 'mysql');
}
return static::query()->insert($data);
}
@@ -196,9 +191,7 @@ class Model extends Base\Model
*/
public function delete(): bool
{
if (!$this->beforeDelete()) {
return false;
}
if ($this->beforeDelete()) {
if ($this->hasPrimary()) {
$result = static::deleteByCondition("id = :id", [":id" => $this->getPrimaryValue()]);
} else {
@@ -206,6 +199,8 @@ class Model extends Base\Model
}
return $this->afterDelete($result);
}
return false;
}
/**
@@ -217,8 +212,7 @@ class Model extends Base\Model
*/
public static function updateAll(mixed $condition, array $attributes = []): bool
{
$condition = static::query()->where($condition);
return $condition->update($attributes);
return static::query()->where($condition)->update($attributes);
}
+11 -8
View File
@@ -31,12 +31,6 @@ class Columns extends Component
*/
private array $columns = [];
/**
* @var Connection
* Mysql client
*/
public Connection $db;
/**
* @var string
* tableName
@@ -58,6 +52,15 @@ class Columns extends Component
private array $_fields = [];
/**
* @param Connection $db
*/
public function __construct(public Connection $db)
{
parent::__construct();
}
/**
* @param string $table
* @return $this
@@ -80,10 +83,10 @@ class Columns extends Component
/**
* @param $key
* @param $val
* @return mixed
* @return string|int|bool|float
* @throws Exception
*/
public function fieldFormat($key, $val): mixed
public function fieldFormat($key, $val): string|int|bool|float
{
return $this->encode($val, $this->get_fields($key));
}
+1 -1
View File
@@ -28,7 +28,7 @@ class Schema extends Component
public function getColumns(): ?Columns
{
if ($this->_column === null) {
$this->_column = new Columns(['db' => $this->db]);
$this->_column = new Columns($this->db);
}
return $this->_column;
+5 -1
View File
@@ -47,7 +47,10 @@ class Pagination extends Component
}
public function clean()
/**
* @return void
*/
public function clean(): void
{
unset($this->activeQuery, $this->_callback, $this->_group);
$this->_offset = 0;
@@ -186,6 +189,7 @@ class Pagination extends Component
/**
* @return array|Collection
* @throws Exception
*/
private function get(): Collection|array
{
+1 -2
View File
@@ -105,9 +105,8 @@ class SqlBuilder extends Component
private function __updateBuilder(array $string): string|bool
{
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();
}
+1 -11
View File
@@ -112,23 +112,13 @@ trait Builder
/**
* @param array $where
* @return string
* @throws NotFindClassException
* @throws ReflectionException
*/
private function where(array $where): string
{
if (count($where) < 1) {
return '';
}
$_tmp = [];
foreach ($where as $key => $value) {
$_tmp[] = $this->resolveCondition($key, $value, $_tmp);
}
if (count($_tmp) > 0) {
return implode(' AND ', $_tmp);
} else {
return '';
}
return implode(' AND ', $where);
}
+13 -124
View File
@@ -12,14 +12,11 @@ namespace Database\Traits;
use Closure;
use Database\ActiveQuery;
use Database\Condition\MathematicsCondition;
use Database\ModelInterface;
use Database\Query;
use Database\SqlBuilder;
use Exception;
use Kiri\Exception\NotFindClassException;
use Kiri;
use ReflectionException;
/**
* Trait QueryTrait
@@ -110,17 +107,6 @@ trait QueryTrait
return $this;
}
/**
* @param bool $bool
* @return $this
*/
public function ifNotWhere(bool $bool): static
{
$this->ifNotWhere = $bool;
return $this;
}
/**
* @return string
* @throws
@@ -478,21 +464,6 @@ trait QueryTrait
return $this;
}
/**
* @param string $columns
* @param string|int|bool|null $value
*
* @param string $opera
* @return QueryTrait
*/
public function whereAnd(string $columns, string $opera = '=', string|int|null|bool $value = NULL): static
{
[$columns, $opera, $value] = $this->opera(...func_get_args());
$this->where[] = $this->sprintf($columns, $value, $opera);
return $this;
}
/**
* @param string $column
@@ -543,86 +514,6 @@ trait QueryTrait
return $this;
}
/**
* @param string $column
* @param int $value
* @return $this
* @see MathematicsCondition
*/
public function whereEq(string $column, int $value): static
{
$this->bindParam(':eq' . $column, $value);
$this->where[] = $column . ' = :eq' . $column;
return $this;
}
/**
* @param string $column
* @param int $value
* @return $this
* @see MathematicsCondition
*/
public function whereNeq(string $column, int $value): static
{
$this->bindParam(':neq' . $column, $value);
$this->where[] = $column . ' <> :neq' . $column;
return $this;
}
/**
* @param string $column
* @param int $value
* @return $this
* @see MathematicsCondition
*/
public function whereGt(string $column, int $value): static
{
$this->bindParam(':gt' . $column, $value);
$this->where[] = $column . ' > :gt' . $column;
return $this;
}
/**
* @param string $column
* @param int $value
* @return $this
* @see MathematicsCondition
*/
public function whereEgt(string $column, int $value): static
{
$this->bindParam(':egt' . $column, $value);
$this->where[] = $column . ' >= :egt' . $column;
return $this;
}
/**
* @param string $column
* @param int $value
* @return $this
* @see MathematicsCondition
*/
public function whereLt(string $column, int $value): static
{
$this->bindParam(':lt' . $column, $value);
$this->where[] = $column . ' < :lt' . $column;
return $this;
}
/**
* @param string $column
* @param int $value
* @return $this
* @see MathematicsCondition
*/
public function whereElt(string $column, int $value): static
{
$this->bindParam(':elt' . $column, $value);
$this->where[] = $column . ' <= :elt' . $column;
return $this;
}
/**
* @param string $columns
@@ -750,27 +641,25 @@ trait QueryTrait
}
/**
* @param array|string $column
* @param string $opera
* @param null $value
* @param array $column
* @return $this
* @throws
*/
public function where(array|string $column, string $opera = '=', $value = null): static
public function where(array $column): static
{
if (is_array($column)) {
return $this->addArray($column);
}
if (is_string($column)) {
$this->where[] = $column;
} else {
[$column, $opera, $value] = $this->opera(...func_get_args());
if ($value === null) {
return $this;
}
/**
* @param string $column
* @param string $opera
* @param mixed $value
* @return $this
*/
public function whereMath(string $column, string $opera, mixed $value): static
{
$this->bindParam(':' . $column, $value);
$this->where[] = $column . ' ' . $opera . ':' . $column;
}
return $this;
}
@@ -797,7 +686,7 @@ trait QueryTrait
if ($closure instanceof Closure) {
call_user_func($closure, $generate);
} else {
$generate->where($closure);
$generate->addArray($closure);
}
return $generate->getSql();
}
+1 -1
View File
@@ -54,7 +54,7 @@ class When
/**
* @param string $alias
*/
public function else(string $alias)
public function else(string $alias): void
{
$this->else = $alias;
}