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];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
+8
-11
@@ -59,17 +59,15 @@ 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) {
|
||||||
|
$lists[] = $item[$model->getPrimary()];
|
||||||
|
}
|
||||||
|
return $model::query()->whereIn($model->getPrimary(), $lists)->update($attributes);
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
foreach ($items as $item) {
|
|
||||||
$lists[] = $item[$primary];
|
|
||||||
}
|
|
||||||
return $this->getModel()::query()->whereIn($primary, $lists)
|
|
||||||
->update($attributes);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
@@ -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,9 +191,7 @@ class Model extends Base\Model
|
|||||||
*/
|
*/
|
||||||
public function delete(): bool
|
public function delete(): bool
|
||||||
{
|
{
|
||||||
if (!$this->beforeDelete()) {
|
if ($this->beforeDelete()) {
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if ($this->hasPrimary()) {
|
if ($this->hasPrimary()) {
|
||||||
$result = static::deleteByCondition("id = :id", [":id" => $this->getPrimaryValue()]);
|
$result = static::deleteByCondition("id = :id", [":id" => $this->getPrimaryValue()]);
|
||||||
} else {
|
} else {
|
||||||
@@ -206,6 +199,8 @@ class Model extends Base\Model
|
|||||||
}
|
}
|
||||||
return $this->afterDelete($result);
|
return $this->afterDelete($result);
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+11
-8
@@ -31,12 +31,6 @@ class Columns extends Component
|
|||||||
*/
|
*/
|
||||||
private array $columns = [];
|
private array $columns = [];
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Connection
|
|
||||||
* Mysql client
|
|
||||||
*/
|
|
||||||
public Connection $db;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
* tableName
|
* tableName
|
||||||
@@ -58,6 +52,15 @@ class Columns extends Component
|
|||||||
|
|
||||||
private array $_fields = [];
|
private array $_fields = [];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Connection $db
|
||||||
|
*/
|
||||||
|
public function __construct(public Connection $db)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @return $this
|
* @return $this
|
||||||
@@ -80,10 +83,10 @@ class Columns extends Component
|
|||||||
/**
|
/**
|
||||||
* @param $key
|
* @param $key
|
||||||
* @param $val
|
* @param $val
|
||||||
* @return mixed
|
* @return string|int|bool|float
|
||||||
* @throws Exception
|
* @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));
|
return $this->encode($val, $this->get_fields($key));
|
||||||
}
|
}
|
||||||
|
|||||||
+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;
|
||||||
|
|||||||
+5
-1
@@ -47,7 +47,10 @@ class Pagination extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function clean()
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function clean(): void
|
||||||
{
|
{
|
||||||
unset($this->activeQuery, $this->_callback, $this->_group);
|
unset($this->activeQuery, $this->_callback, $this->_group);
|
||||||
$this->_offset = 0;
|
$this->_offset = 0;
|
||||||
@@ -186,6 +189,7 @@ class Pagination extends Component
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|Collection
|
* @return array|Collection
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function get(): Collection|array
|
private function get(): Collection|array
|
||||||
{
|
{
|
||||||
|
|||||||
+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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-11
@@ -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 '';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+13
-124
@@ -12,14 +12,11 @@ namespace Database\Traits;
|
|||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Database\ActiveQuery;
|
use Database\ActiveQuery;
|
||||||
use Database\Condition\MathematicsCondition;
|
|
||||||
use Database\ModelInterface;
|
use Database\ModelInterface;
|
||||||
use Database\Query;
|
use Database\Query;
|
||||||
use Database\SqlBuilder;
|
use Database\SqlBuilder;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Exception\NotFindClassException;
|
|
||||||
use Kiri;
|
use Kiri;
|
||||||
use ReflectionException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Trait QueryTrait
|
* Trait QueryTrait
|
||||||
@@ -110,17 +107,6 @@ trait QueryTrait
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param bool $bool
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function ifNotWhere(bool $bool): static
|
|
||||||
{
|
|
||||||
$this->ifNotWhere = $bool;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws
|
* @throws
|
||||||
@@ -478,21 +464,6 @@ trait QueryTrait
|
|||||||
return $this;
|
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
|
* @param string $column
|
||||||
@@ -543,86 +514,6 @@ trait QueryTrait
|
|||||||
return $this;
|
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
|
* @param string $columns
|
||||||
@@ -750,27 +641,25 @@ trait QueryTrait
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|string $column
|
* @param array $column
|
||||||
* @param string $opera
|
|
||||||
* @param null $value
|
|
||||||
* @return $this
|
* @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);
|
return $this->addArray($column);
|
||||||
}
|
}
|
||||||
if (is_string($column)) {
|
|
||||||
$this->where[] = $column;
|
|
||||||
} else {
|
/**
|
||||||
[$column, $opera, $value] = $this->opera(...func_get_args());
|
* @param string $column
|
||||||
if ($value === null) {
|
* @param string $opera
|
||||||
return $this;
|
* @param mixed $value
|
||||||
}
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function whereMath(string $column, string $opera, mixed $value): static
|
||||||
|
{
|
||||||
$this->bindParam(':' . $column, $value);
|
$this->bindParam(':' . $column, $value);
|
||||||
$this->where[] = $column . ' ' . $opera . ':' . $column;
|
$this->where[] = $column . ' ' . $opera . ':' . $column;
|
||||||
}
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -797,7 +686,7 @@ trait QueryTrait
|
|||||||
if ($closure instanceof Closure) {
|
if ($closure instanceof Closure) {
|
||||||
call_user_func($closure, $generate);
|
call_user_func($closure, $generate);
|
||||||
} else {
|
} else {
|
||||||
$generate->where($closure);
|
$generate->addArray($closure);
|
||||||
}
|
}
|
||||||
return $generate->getSql();
|
return $generate->getSql();
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -54,7 +54,7 @@ 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