改名
This commit is contained in:
@@ -152,9 +152,12 @@ class ActiveRecord extends BaseActiveRecord
|
|||||||
if (empty($condition)) {
|
if (empty($condition)) {
|
||||||
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
|
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
|
||||||
}
|
}
|
||||||
return static::getDb()->createCommand()
|
$execute = static::getDb()->createCommand()
|
||||||
->mathematics(self::getTable(), [$action => $columns], $condition)
|
->mathematics(self::getTable(), [$action => $columns], $condition);
|
||||||
->exec($this);
|
if ($execute instanceof Command) {
|
||||||
|
return $execute->exec($this);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+15
-9
@@ -10,11 +10,13 @@ declare(strict_types=1);
|
|||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
|
|
||||||
|
use ReflectionException;
|
||||||
use Snowflake\Abstracts\Component;
|
use Snowflake\Abstracts\Component;
|
||||||
use Exception;
|
use Exception;
|
||||||
use PDO;
|
use PDO;
|
||||||
use PDOStatement;
|
use PDOStatement;
|
||||||
use Snowflake\Abstracts\Config;
|
use Snowflake\Abstracts\Config;
|
||||||
|
use Snowflake\Exception\NotFindClassException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Command
|
* Class Command
|
||||||
@@ -88,8 +90,8 @@ class Command extends Component
|
|||||||
*/
|
*/
|
||||||
public function batchUpdate($tableName, $attributes, $condition): Command
|
public function batchUpdate($tableName, $attributes, $condition): Command
|
||||||
{
|
{
|
||||||
$change = $this->db->getSchema()->getChange();
|
$change = $this->db->getSchema();
|
||||||
[$sql, $param] = $change->batchUpdate($tableName, $attributes, $condition);
|
[$sql, $param] = $change->getChange()->batchUpdate($tableName, $change, $attributes, $condition);
|
||||||
return $this->setSql($sql)->bindValues($param);
|
return $this->setSql($sql)->bindValues($param);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,9 +104,8 @@ class Command extends Component
|
|||||||
*/
|
*/
|
||||||
public function batchInsert($tableName, $attributes): Command
|
public function batchInsert($tableName, $attributes): Command
|
||||||
{
|
{
|
||||||
$change = $this->db->getSchema()->getChange();
|
$change = $this->db->getSchema();
|
||||||
$attribute_key = array_keys(current($attributes));
|
[$sql, $param] = $change->getChange()->batchInsert($tableName, $change, $attributes);
|
||||||
[$sql, $param] = $change->batchInsert($tableName, $attribute_key, $attributes);
|
|
||||||
return $this->setSql($sql)->bindValues($param);
|
return $this->setSql($sql)->bindValues($param);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,8 +118,8 @@ class Command extends Component
|
|||||||
*/
|
*/
|
||||||
public function insert($tableName, $attributes, $param): Command
|
public function insert($tableName, $attributes, $param): Command
|
||||||
{
|
{
|
||||||
$change = $this->db->getSchema()->getChange();
|
$sql = $this->db->getSchema()
|
||||||
$sql = $change->insert($tableName, $attributes, $param);
|
->getChange()->insert($tableName, $attributes, $param);
|
||||||
return $this->setSql($sql)->bindValues($param);
|
return $this->setSql($sql)->bindValues($param);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,13 +136,18 @@ class Command extends Component
|
|||||||
* @param $tableName
|
* @param $tableName
|
||||||
* @param $param
|
* @param $param
|
||||||
* @param $condition
|
* @param $condition
|
||||||
* @return Command
|
* @return bool|Command
|
||||||
|
* @throws ReflectionException
|
||||||
|
* @throws NotFindClassException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function mathematics($tableName, $param, $condition): static
|
public function mathematics($tableName, $param, $condition): bool|static
|
||||||
{
|
{
|
||||||
$change = $this->db->getSchema()->getChange();
|
$change = $this->db->getSchema()->getChange();
|
||||||
$sql = $change->mathematics($tableName, $param, $condition);
|
$sql = $change->mathematics($tableName, $param, $condition);
|
||||||
|
if ($sql === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return $this->setSql($sql);
|
return $this->setSql($sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,103 +0,0 @@
|
|||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Database\Condition;
|
|
||||||
|
|
||||||
|
|
||||||
use Database\ActiveQuery;
|
|
||||||
use Database\Base\ConditionClassMap;
|
|
||||||
use Exception;
|
|
||||||
|
|
||||||
use Snowflake\Core\Str;
|
|
||||||
use Snowflake\Snowflake;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class ArrayCondition
|
|
||||||
* @package Database\Condition
|
|
||||||
*/
|
|
||||||
class ArrayCondition extends Condition
|
|
||||||
{
|
|
||||||
|
|
||||||
private array $math = ['like', 'in', 'or', 'eq', 'neq', 'gt', 'ngt', 'lt', 'nlt'];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function builder(): mixed
|
|
||||||
{
|
|
||||||
if ($this->value instanceof Condition) {
|
|
||||||
return $this->value->builder();
|
|
||||||
}
|
|
||||||
|
|
||||||
$conditions = [];
|
|
||||||
|
|
||||||
$classMap = ConditionClassMap::$conditionMap;
|
|
||||||
foreach ($this->value as $key => $value) {
|
|
||||||
if ($value === null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if ($value instanceof Condition) {
|
|
||||||
$value = $value->builder();
|
|
||||||
} else if (isset($value[0]) && isset($classMap[strtoupper($value[0])])) {
|
|
||||||
$value = $this->buildOperaCondition($value);
|
|
||||||
} else {
|
|
||||||
$value = $this->buildHashCondition($key, $value);
|
|
||||||
}
|
|
||||||
if (empty($value)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$conditions[] = Str::encode($value);
|
|
||||||
}
|
|
||||||
if (is_array($conditions)) {
|
|
||||||
$conditions = implode(' AND ', $conditions);
|
|
||||||
}
|
|
||||||
return $conditions;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $value
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function isMath($value): bool
|
|
||||||
{
|
|
||||||
return isset($value[0]) && in_array($value[0], $this->math);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $value
|
|
||||||
* @return mixed
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function buildOperaCondition(array $value): mixed
|
|
||||||
{
|
|
||||||
[$option['opera'], $option['column'], $option['value']] = $value;
|
|
||||||
$strPer = strtoupper($option['opera']);
|
|
||||||
if (isset($this->conditionMap[$strPer])) {
|
|
||||||
$class = ConditionClassMap::$conditionMap[$strPer];
|
|
||||||
if (!is_array($class)) {
|
|
||||||
$class = ['class' => $class];
|
|
||||||
}
|
|
||||||
$option = array_merge($option, $class);
|
|
||||||
} else if ($value instanceof ActiveQuery) {
|
|
||||||
$option['value'] = $value->getBuild()->getQuery($value);
|
|
||||||
$option['class'] = ChildCondition::class;
|
|
||||||
} else {
|
|
||||||
$option['class'] = DefaultCondition::class;
|
|
||||||
}
|
|
||||||
/** @var Condition $class */
|
|
||||||
$class = Snowflake::createObject($option);
|
|
||||||
return $conditions[] = $class->builder();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $key
|
|
||||||
* @param $value
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function buildHashCondition($key, $value): string
|
|
||||||
{
|
|
||||||
return $this->resolve($key, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -17,7 +17,7 @@ class BetweenCondition extends Condition
|
|||||||
*/
|
*/
|
||||||
public function builder(): string
|
public function builder(): string
|
||||||
{
|
{
|
||||||
return $this->column . ' BETWEEN ' . $this->value[0] . ' AND ' . $this->value[1];
|
return $this->column . ' BETWEEN ' . (int)$this->value[0] . ' AND ' . (int)$this->value[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
|
|
||||||
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class DefaultCondition
|
* Class DefaultCondition
|
||||||
* @package Database\Condition
|
* @package Database\Condition
|
||||||
@@ -14,9 +16,9 @@ class DefaultCondition extends Condition
|
|||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function builder(): string
|
#[Pure] public function builder(): string
|
||||||
{
|
{
|
||||||
return $this->resolve($this->column, $this->value, $this->opera);
|
return sprintf('%s %s %s', $this->column, $this->opera, addslashes($this->value));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -22,7 +23,7 @@ class HashCondition extends Condition
|
|||||||
if ($value === null) {
|
if ($value === null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$array[] = $this->resolve($key, $value);
|
$array[] = sprintf('%s=%s', $key, addslashes($value));
|
||||||
}
|
}
|
||||||
return implode(' AND ', $array);
|
return implode(' AND ', $array);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace Database\Condition;
|
|||||||
|
|
||||||
use Database\ActiveQuery;
|
use Database\ActiveQuery;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class InCondition
|
* Class InCondition
|
||||||
@@ -18,18 +19,12 @@ class InCondition extends Condition
|
|||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function builder(): string
|
#[Pure] public function builder(): string
|
||||||
{
|
{
|
||||||
if ($this->value instanceof ActiveQuery) {
|
if (is_array($this->value)) {
|
||||||
$this->value = $this->value->getBuild()->getQuery($this->value);
|
return sprintf('%s IN (\'%s\')', $this->column, implode('\',\'', $this->value));
|
||||||
} else {
|
|
||||||
$this->value = array_filter($this->format($this->value));
|
|
||||||
if (empty($this->value)) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
$this->value = implode(',', $this->value);
|
|
||||||
}
|
}
|
||||||
return '`' . $this->column . '` in(' . $this->value . ')';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace Database\Condition;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class JsonCondition
|
||||||
|
* @package Database\Condition
|
||||||
|
*/
|
||||||
|
class JsonCondition extends Condition
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
public function builder()
|
||||||
|
{
|
||||||
|
// TODO: Implement builder() method.
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -22,8 +22,7 @@ class LLikeCondition extends Condition
|
|||||||
if (!is_string($this->value)) {
|
if (!is_string($this->value)) {
|
||||||
$this->value = array_shift($this->value);
|
$this->value = array_shift($this->value);
|
||||||
}
|
}
|
||||||
$this->value = Str::encode($this->value);
|
return $this->column . ' LIKE \'%' . addslashes($this->value) . '\'';
|
||||||
return $this->column . ' LIKE \'%' . $this->value . '\'';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,8 +22,7 @@ class LikeCondition extends Condition
|
|||||||
if (!is_string($this->value)) {
|
if (!is_string($this->value)) {
|
||||||
$this->value = array_shift($this->value);
|
$this->value = array_shift($this->value);
|
||||||
}
|
}
|
||||||
$this->value = Str::encode($this->value);
|
return $this->column . ' LIKE \'%' . addslashes($this->value) . '%\'';
|
||||||
return $this->column . ' LIKE \'%' . $this->value . '%\'';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class NotBetweenCondition extends Condition
|
|||||||
*/
|
*/
|
||||||
public function builder(): string
|
public function builder(): string
|
||||||
{
|
{
|
||||||
return $this->column . ' NOT BETWEEN ' . $this->value[0] . ' AND ' . $this->value[1];
|
return $this->column . ' NOT BETWEEN ' . (int)$this->value[0] . ' AND ' . (int)$this->value[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class NotInCondition
|
* Class NotInCondition
|
||||||
* @package Database\Condition
|
* @package Database\Condition
|
||||||
@@ -12,17 +14,15 @@ class NotInCondition extends Condition
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
public function builder(): string
|
#[Pure] public function builder(): ?string
|
||||||
{
|
{
|
||||||
|
if (!is_array($this->value)) {
|
||||||
$format = array_filter($this->format($this->value));
|
return null;
|
||||||
if (empty($format)) {
|
|
||||||
return '';
|
|
||||||
}
|
}
|
||||||
|
$value = '\'' . implode('\',\'', $this->value) . '\'';
|
||||||
return '`' . $this->column . '` not in(' . implode(',', $format) . ')';
|
return '`' . $this->column . '` not in(' . $value . ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,8 +22,7 @@ class NotLikeCondition extends Condition
|
|||||||
if (!is_string($this->value)) {
|
if (!is_string($this->value)) {
|
||||||
$this->value = array_shift($this->value);
|
$this->value = array_shift($this->value);
|
||||||
}
|
}
|
||||||
$this->value = Str::encode($this->value);
|
return $this->column . ' NOT LIKE \'%' . addslashes($this->value) . '%\'';
|
||||||
return $this->column . ' NOT LIKE \'%' . $this->value . '%\'';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
|
|
||||||
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class OrCondition
|
* Class OrCondition
|
||||||
@@ -12,12 +13,15 @@ namespace Database\Condition;
|
|||||||
class OrCondition extends Condition
|
class OrCondition extends Condition
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public array $oldParams = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function builder(): string
|
#[Pure] public function builder(): string
|
||||||
{
|
{
|
||||||
return 'OR ' . $this->resolve($this->column, $this->value, $this->opera);
|
return sprintf('(%s) OR %s', implode(' AND ', $this->oldParams), addslashes($this->value));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,8 +22,7 @@ class RLikeCondition extends Condition
|
|||||||
if (!is_string($this->value)) {
|
if (!is_string($this->value)) {
|
||||||
$this->value = array_shift($this->value);
|
$this->value = array_shift($this->value);
|
||||||
}
|
}
|
||||||
$this->value = Str::encode($this->value);
|
return sprintf('%s LIKE \'%s\'', $this->column, addslashes($this->value));
|
||||||
return $this->column . ' LIKE \'' . $this->value . '%\'';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ class Connection extends Component
|
|||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
*/
|
*/
|
||||||
public function getSchema(): mixed
|
public function getSchema(): Schema
|
||||||
{
|
{
|
||||||
if ($this->_schema === null) {
|
if ($this->_schema === null) {
|
||||||
$this->_schema = Snowflake::createObject([
|
$this->_schema = Snowflake::createObject([
|
||||||
|
|||||||
@@ -39,9 +39,9 @@ class Schema extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Change|null
|
* @return Change
|
||||||
*/
|
*/
|
||||||
public function getChange(): ?Change
|
public function getChange(): Change
|
||||||
{
|
{
|
||||||
if ($this->_change === null) {
|
if ($this->_change === null) {
|
||||||
$this->_change = new Change();
|
$this->_change = new Change();
|
||||||
|
|||||||
+24
-72
@@ -4,10 +4,13 @@ declare(strict_types=1);
|
|||||||
namespace Database\Orm;
|
namespace Database\Orm;
|
||||||
|
|
||||||
|
|
||||||
|
use Database\Connection;
|
||||||
|
use Database\Mysql\Schema;
|
||||||
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Snowflake\Abstracts\BaseObject;
|
use Snowflake\Abstracts\BaseObject;
|
||||||
use Database\ActiveQuery;
|
use Database\ActiveQuery;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Change
|
* Class Change
|
||||||
@@ -51,20 +54,23 @@ class Change extends BaseObject
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $table
|
* @param string $table
|
||||||
|
* @param Schema $db
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @param $condition
|
* @param $condition
|
||||||
* @return array|string
|
* @return array|string
|
||||||
* @throws
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function batchUpdate(string $table, array $attributes, $condition): array|string
|
public function batchUpdate(string $table, Schema $db, array $attributes, $condition): array|string
|
||||||
{
|
{
|
||||||
$param = [];
|
$param = [];
|
||||||
$_attributes = [];
|
$_attributes = [];
|
||||||
|
|
||||||
|
$format = $db->getColumns()->table($table);
|
||||||
foreach ($attributes as $key => $val) {
|
foreach ($attributes as $key => $val) {
|
||||||
if ($val === null) {
|
if ($val === null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$_attributes[':' . $key] = $this->valueEncode($val, true);
|
$_attributes[':' . $key] = $format->fieldFormat($key, $val);
|
||||||
$param[] = $key . '=:' . $key;
|
$param[] = $key . '=:' . $key;
|
||||||
}
|
}
|
||||||
if (empty($param)) {
|
if (empty($param)) {
|
||||||
@@ -81,10 +87,10 @@ class Change extends BaseObject
|
|||||||
* @param $table
|
* @param $table
|
||||||
* @param $params
|
* @param $params
|
||||||
* @param $condition
|
* @param $condition
|
||||||
* @return string
|
* @return string|bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function mathematics($table, $params, $condition): string
|
public function mathematics($table, $params, $condition): string|bool
|
||||||
{
|
{
|
||||||
$_tmp = $newParam = [];
|
$_tmp = $newParam = [];
|
||||||
if (isset($params['incr']) && is_array($params['incr'])) {
|
if (isset($params['incr']) && is_array($params['incr'])) {
|
||||||
@@ -94,7 +100,7 @@ class Change extends BaseObject
|
|||||||
$_tmp = $this->assemble($params['decr'], ' - ', $_tmp);
|
$_tmp = $this->assemble($params['decr'], ' - ', $_tmp);
|
||||||
}
|
}
|
||||||
if (empty($_tmp)) {
|
if (empty($_tmp)) {
|
||||||
throw new Exception('Not has IncrBy or DecrBy values.');
|
return $this->addError('Not has IncrBy or DecrBy values.');
|
||||||
}
|
}
|
||||||
$_tmp = implode(',', $_tmp);
|
$_tmp = implode(',', $_tmp);
|
||||||
if (!empty($condition)) {
|
if (!empty($condition)) {
|
||||||
@@ -112,40 +118,12 @@ class Change extends BaseObject
|
|||||||
*/
|
*/
|
||||||
private function assemble($params, $op, array $_tmp): array
|
private function assemble($params, $op, array $_tmp): array
|
||||||
{
|
{
|
||||||
$message = 'Incr And Decr action. The value must a numeric.';
|
|
||||||
foreach ($params as $key => $val) {
|
foreach ($params as $key => $val) {
|
||||||
$_tmp[] = $key . '=' . $key . $op . $val;
|
$_tmp[] = sprintf('%s=%s%s%d', $key, $key, $op, $val);
|
||||||
if (!is_numeric($val)) {
|
|
||||||
throw new Exception($message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $_tmp;
|
return $_tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $table
|
|
||||||
* @param array $params
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function insertOrUpdateByDUPLICATE($table, array $params): string
|
|
||||||
{
|
|
||||||
$keys = implode(',', array_keys($params));
|
|
||||||
|
|
||||||
$onValues = [];
|
|
||||||
$values = array_values($params);
|
|
||||||
foreach ($values as $key => $val) {
|
|
||||||
$onValues[] = $this->valueEncode($val, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
$onUpdates = [];
|
|
||||||
foreach ($params as $key => $val) {
|
|
||||||
$onUpdates[] = $key . '=' . $this->valueEncode($val, true);
|
|
||||||
}
|
|
||||||
$newSql = $this->inserts($table, $keys, '(' . implode(',', $onValues) . ')');
|
|
||||||
|
|
||||||
return $newSql . ' ON DUPLICATE KEY UPDATE ' . implode(',', $onUpdates);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $table
|
* @param $table
|
||||||
@@ -171,24 +149,29 @@ class Change extends BaseObject
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $table
|
* @param $table
|
||||||
* @param $attributes
|
* @param Schema $db
|
||||||
* @param array|NULL $params
|
* @param array|NULL $params
|
||||||
* @return array
|
* @return array
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function batchInsert($table, $attributes, array $params = NULL): array
|
public function batchInsert($table, Schema $db, array $params = NULL): array
|
||||||
{
|
{
|
||||||
if (empty($params)) {
|
if (empty($params)) {
|
||||||
throw new Exception("save data param not find.");
|
throw new Exception("save data param not find.");
|
||||||
}
|
}
|
||||||
$insert = $insertData = [];
|
$insert = $insertData = [];
|
||||||
|
|
||||||
|
$format = $db->getColumns()->table($table);
|
||||||
|
|
||||||
|
$attributes = array_keys(current($params));
|
||||||
|
|
||||||
foreach ($params as $key => $val) {
|
foreach ($params as $key => $val) {
|
||||||
if (!is_array($val)) {
|
if (!is_array($val)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
array_push($insert, '(:' . implode($key . ',:', $attributes) . $key . ')');
|
array_push($insert, '(:' . implode($key . ',:', $attributes) . $key . ')');
|
||||||
foreach ($attributes as $myVal) {
|
foreach ($attributes as $myVal) {
|
||||||
$insertData[':' . $myVal . $key] = $this->valueEncode($val[$myVal], true);
|
$insertData[':' . $myVal . $key] = $format->fieldFormat($myVal, $val[$myVal]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (empty($insertData) || empty($insert)) {
|
if (empty($insertData) || empty($insert)) {
|
||||||
@@ -206,40 +189,9 @@ class Change extends BaseObject
|
|||||||
* @return string
|
* @return string
|
||||||
* 构建SQL语句
|
* 构建SQL语句
|
||||||
*/
|
*/
|
||||||
private function inserts($table, $fields, $data): string
|
#[Pure] private function inserts($table, $fields, $data): string
|
||||||
{
|
{
|
||||||
$query = [
|
return sprintf('INSERT IGNORE INTO' . ' %s (%s) VALUES %s', $table, $fields, $data);
|
||||||
'INSERT IGNORE INTO', '%s', '(%s)', 'VALUES %s'
|
|
||||||
];
|
|
||||||
$query = implode(' ', $query);
|
|
||||||
|
|
||||||
return sprintf($query, $table, $fields, $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $table
|
|
||||||
* @param $attributes
|
|
||||||
* @param $condition
|
|
||||||
* @return bool|string
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function updateAll($table, $attributes, $condition): bool|string
|
|
||||||
{
|
|
||||||
$param = [];
|
|
||||||
foreach ($attributes as $key => $val) {
|
|
||||||
if ($val === null || $val === '') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$param[] = $key . '=' . $this->valueEncode($val);
|
|
||||||
}
|
|
||||||
if (empty($param)) return true;
|
|
||||||
|
|
||||||
$param = implode(',', $param);
|
|
||||||
if (!empty($condition)) {
|
|
||||||
$param .= $this->builderWhere($condition);
|
|
||||||
}
|
|
||||||
return 'UPDATE ' . $table . ' SET ' . $param;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+87
-59
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||||||
namespace Database\Orm;
|
namespace Database\Orm;
|
||||||
|
|
||||||
|
|
||||||
|
use Database\Condition\OrCondition;
|
||||||
|
use JetBrains\PhpStorm\Pure;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Snowflake\Core\Json;
|
use Snowflake\Core\Json;
|
||||||
use Snowflake\Core\Str;
|
use Snowflake\Core\Str;
|
||||||
@@ -61,7 +63,7 @@ trait Condition
|
|||||||
* @param $join
|
* @param $join
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function builderJoin($join): string
|
#[Pure] private function builderJoin($join): string
|
||||||
{
|
{
|
||||||
if (!empty($join)) {
|
if (!empty($join)) {
|
||||||
return ' ' . implode(' ', $join);
|
return ' ' . implode(' ', $join);
|
||||||
@@ -76,77 +78,107 @@ trait Condition
|
|||||||
*/
|
*/
|
||||||
private function builderWhere($where): string
|
private function builderWhere($where): string
|
||||||
{
|
{
|
||||||
if (empty($where)) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
if (is_string($where)) {
|
|
||||||
return sprintf(' WHERE %s', $where);
|
|
||||||
}
|
|
||||||
$_tmp = [];
|
$_tmp = [];
|
||||||
$where = array_filter($where);
|
if (empty($where)) return '';
|
||||||
foreach ($where as $key => $value) {
|
foreach ($where as $value) {
|
||||||
if (is_array($value)) {
|
$_value = is_string($value) ? $value : $this->conditionMap($value);
|
||||||
$value = $this->arrayMap($value);
|
|
||||||
} else if (!is_numeric($key)) {
|
if (empty($_value)) continue;
|
||||||
$value = $key . '=' . $this->valueEncode($value);
|
|
||||||
}
|
|
||||||
if ($value === null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$_tmp[] = $value;
|
$_tmp[] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($_tmp)) {
|
if (!empty($_tmp)) {
|
||||||
return ' WHERE ' . implode(' AND ', $_tmp);
|
return sprintf(' WHERE %s', implode(' AND ', $_tmp));
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $value
|
* @param $condition
|
||||||
* @return mixed
|
* @return string
|
||||||
* @throws ReflectionException
|
* @throws Exception
|
||||||
* @throws NotFindClassException
|
|
||||||
*/
|
*/
|
||||||
private function arrayMap($value): mixed
|
private function conditionMap($condition): string
|
||||||
{
|
{
|
||||||
$classMap = ConditionClassMap::$conditionMap;
|
$array = [];
|
||||||
if (isset($value[0])) {
|
if (is_string($condition) || empty($condition)) {
|
||||||
if (!isset($classMap[strtoupper($value[0])])) {
|
return $condition;
|
||||||
return $value[0];
|
}
|
||||||
|
foreach ($condition as $key => $value) {
|
||||||
|
if (empty($value)) continue;
|
||||||
|
if (!is_numeric($key)) {
|
||||||
|
$array[] = sprintf('%s=%s', $key, $value);
|
||||||
|
} else if (is_array($value)) {
|
||||||
|
$array = $this->_arrayMap($value, $array);
|
||||||
|
} else {
|
||||||
|
$array[] = sprintf('%s', $value);
|
||||||
}
|
}
|
||||||
$value[0] = strtoupper($value[0]);
|
|
||||||
$result = $this->classMap($value);
|
|
||||||
} else {
|
|
||||||
/** @var HashCondition $condition */
|
|
||||||
$condition = Snowflake::createObject(HashCondition::class);
|
|
||||||
$condition->setValue($value);
|
|
||||||
$condition->setOpera('=');
|
|
||||||
$result = $condition->builder();
|
|
||||||
}
|
}
|
||||||
return $result;
|
return implode(' AND ', $array);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $value
|
* @param $condition
|
||||||
* @return mixed
|
* @param $array
|
||||||
|
* @return array
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
private function classMap($value): mixed
|
private function _arrayMap($condition, $array): mixed
|
||||||
{
|
{
|
||||||
[$option['opera'], $option['column'], $option['value']] = $value;
|
if (!isset($condition[0])) {
|
||||||
|
return $this->_arrayHash($array, $condition);
|
||||||
$class = ConditionClassMap::$conditionMap[strtoupper($option['opera'])];
|
|
||||||
if (!is_array($class)) {
|
|
||||||
$class = ['class' => $class];
|
|
||||||
}
|
}
|
||||||
$option = array_merge($option, $class);
|
$stroppier = strtoupper($condition[0]);
|
||||||
|
if (str_contains($stroppier, 'OR')) {
|
||||||
/** @var Condition $class */
|
if (!is_string($condition[2])) {
|
||||||
return Snowflake::createObject($option)->builder();
|
$condition[2] = $this->_hashMap($condition[2]);
|
||||||
|
}
|
||||||
|
return Snowflake::createObject(['class' => OrCondition::class, 'value' => $condition[2], 'column' => $condition[1], 'oldParams' => $array]);
|
||||||
|
}
|
||||||
|
if (isset(ConditionClassMap::$conditionMap[$stroppier])) {
|
||||||
|
$defaultConfig = ConditionClassMap::$conditionMap[$stroppier];
|
||||||
|
$create = array_merge($defaultConfig, ['column' => $condition[1], 'value' => $condition[2]]);
|
||||||
|
$array[] = Snowflake::createObject($create);
|
||||||
|
} else {
|
||||||
|
$array[] = Snowflake::createObject(['class' => HashCondition::class, 'value' => $condition]);
|
||||||
|
}
|
||||||
|
return $array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $array
|
||||||
|
* @param $condition
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private function _arrayHash($array, $condition): array
|
||||||
|
{
|
||||||
|
$array[] = implode(' AND ', $this->_hashMap($condition));
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $condition
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function _hashMap($condition): array
|
||||||
|
{
|
||||||
|
$_array = [];
|
||||||
|
foreach ($condition as $key => $value) {
|
||||||
|
if (!is_numeric($key)) {
|
||||||
|
$_array[] = sprintf('%s=%s', $key, $value);
|
||||||
|
} else {
|
||||||
|
$_array[] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $_array;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $group
|
* @param $group
|
||||||
* @return string
|
* @return string
|
||||||
@@ -163,7 +195,7 @@ trait Condition
|
|||||||
* @param $order
|
* @param $order
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function builderOrder($order): string
|
#[Pure] private function builderOrder($order): string
|
||||||
{
|
{
|
||||||
if (!empty($order)) {
|
if (!empty($order)) {
|
||||||
return ' ORDER BY ' . implode(',', $order);
|
return ' ORDER BY ' . implode(',', $order);
|
||||||
@@ -176,19 +208,15 @@ trait Condition
|
|||||||
* @param ActiveQuery $query
|
* @param ActiveQuery $query
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function builderLimit(ActiveQuery $query): string
|
#[Pure] private function builderLimit(ActiveQuery $query): string
|
||||||
{
|
{
|
||||||
$limit = $query->limit;
|
if (!is_numeric($query->limit) || $query->limit < 1) {
|
||||||
if (!is_numeric($limit) || $limit < 1) {
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
$offset = $query->offset;
|
if ($query->offset !== null) {
|
||||||
|
return ' LIMIT ' . $query->offset . ',' . $query->limit;
|
||||||
if ($offset === null) {
|
|
||||||
return ' LIMIT ' . $limit;
|
|
||||||
}
|
}
|
||||||
|
return ' LIMIT ' . $query->limit;
|
||||||
return ' LIMIT ' . $offset . ',' . $limit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
* Time: 17:49
|
* Time: 17:49
|
||||||
*/
|
*/
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
|
|
||||||
@@ -30,4 +31,16 @@ class Sql
|
|||||||
{
|
{
|
||||||
return (new Select())->getQuery($this);
|
return (new Select())->getQuery($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function getCondition(): string
|
||||||
|
{
|
||||||
|
return (new Select())->getWhere($this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ namespace Database\Traits;
|
|||||||
|
|
||||||
use Database\ActiveQuery;
|
use Database\ActiveQuery;
|
||||||
use Database\ActiveRecord;
|
use Database\ActiveRecord;
|
||||||
|
use Database\Condition\MathematicsCondition;
|
||||||
use Database\Orm\Select;
|
use Database\Orm\Select;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
@@ -421,42 +422,27 @@ trait QueryTrait
|
|||||||
/**
|
/**
|
||||||
* @param array $conditionArray
|
* @param array $conditionArray
|
||||||
*
|
*
|
||||||
* @return $this|array|ActiveQuery
|
* @return QueryTrait
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
public function or(array $conditionArray = []): static
|
public function or(array $conditionArray = []): static
|
||||||
{
|
{
|
||||||
$conditions = [];
|
$this->where = ['or', $conditionArray];
|
||||||
if (empty($conditionArray) || count($conditionArray) < 2) {
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
$select = new Select();
|
|
||||||
|
|
||||||
$conditions[] = $select->getWhere($this->where);
|
|
||||||
$conditions[] = $select->getWhere($conditionArray);
|
|
||||||
if (empty($conditions[count($conditions) - 1])) {
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
$this->where = ['(' . implode(' OR ', $conditions) . ')'];
|
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $columns
|
* @param string $columns
|
||||||
* @param string $oprea
|
* @param string|int|bool|null $value
|
||||||
* @param null $value
|
|
||||||
*
|
*
|
||||||
* @return array|ActiveQuery|mixed
|
* @param string $opera
|
||||||
* @throws Exception
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function and($columns, $value = NULL, $oprea = '='): static
|
public function and(string $columns, string|int|null|bool $value = NULL, $opera = '='): static
|
||||||
{
|
{
|
||||||
if (!is_numeric($value) && !is_bool($value)) {
|
if (!is_numeric($value) && !is_bool($value)) {
|
||||||
$value = '\'' . $value . '\'';
|
$value = '\'' . $value . '\'';
|
||||||
}
|
}
|
||||||
$this->where[] = $columns . $oprea . $value;
|
$this->where[] = $columns . $opera . $value;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -487,7 +473,7 @@ trait QueryTrait
|
|||||||
$columns = 'CONCAT(' . implode(',^,', $columns) . ')';
|
$columns = 'CONCAT(' . implode(',^,', $columns) . ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->where[] = ['LIKE', $columns, $value];
|
$this->where[] = $columns . ' LIKE \'%' . addslashes($value) . '%\'';
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -508,7 +494,7 @@ trait QueryTrait
|
|||||||
$columns = 'CONCAT(' . implode(',^,', $columns) . ')';
|
$columns = 'CONCAT(' . implode(',^,', $columns) . ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->where[] = ['LLike', $columns, rtrim($value, '%')];
|
$this->where[] = $columns . ' LLike \'%' . addslashes($value) . '\'';
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -529,7 +515,7 @@ trait QueryTrait
|
|||||||
$columns = 'CONCAT(' . implode(',^,', $columns) . ')';
|
$columns = 'CONCAT(' . implode(',^,', $columns) . ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->where[] = ['RLike', $columns, ltrim($value, '%')];
|
$this->where[] = $columns . ' RLike \'' . addslashes($value) . '%\'';
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -551,7 +537,7 @@ trait QueryTrait
|
|||||||
$columns = 'CONCAT(' . implode(',^,', $columns) . ')';
|
$columns = 'CONCAT(' . implode(',^,', $columns) . ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->where[] = ['NOT LIKE', $columns, ltrim($value, '%')];
|
$this->where[] = $columns . ' NOT LIKE \'%' . addslashes($value) . '%\'';
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -561,6 +547,7 @@ trait QueryTrait
|
|||||||
* @param int $value
|
* @param int $value
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
|
* @see MathematicsCondition
|
||||||
*/
|
*/
|
||||||
public function eq(string $column, int $value): static
|
public function eq(string $column, int $value): static
|
||||||
{
|
{
|
||||||
@@ -575,6 +562,7 @@ trait QueryTrait
|
|||||||
* @param int $value
|
* @param int $value
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
|
* @see MathematicsCondition
|
||||||
*/
|
*/
|
||||||
public function neq(string $column, int $value): static
|
public function neq(string $column, int $value): static
|
||||||
{
|
{
|
||||||
@@ -589,6 +577,7 @@ trait QueryTrait
|
|||||||
* @param int $value
|
* @param int $value
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
|
* @see MathematicsCondition
|
||||||
*/
|
*/
|
||||||
public function gt(string $column, int $value): static
|
public function gt(string $column, int $value): static
|
||||||
{
|
{
|
||||||
@@ -602,6 +591,7 @@ trait QueryTrait
|
|||||||
* @param int $value
|
* @param int $value
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
|
* @see MathematicsCondition
|
||||||
*/
|
*/
|
||||||
public function egt(string $column, int $value): static
|
public function egt(string $column, int $value): static
|
||||||
{
|
{
|
||||||
@@ -616,6 +606,7 @@ trait QueryTrait
|
|||||||
* @param int $value
|
* @param int $value
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
|
* @see MathematicsCondition
|
||||||
*/
|
*/
|
||||||
public function lt(string $column, int $value): static
|
public function lt(string $column, int $value): static
|
||||||
{
|
{
|
||||||
@@ -629,6 +620,7 @@ trait QueryTrait
|
|||||||
* @param int $value
|
* @param int $value
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
|
* @see MathematicsCondition
|
||||||
*/
|
*/
|
||||||
public function elt(string $column, int $value): static
|
public function elt(string $column, int $value): static
|
||||||
{
|
{
|
||||||
@@ -645,15 +637,8 @@ trait QueryTrait
|
|||||||
*/
|
*/
|
||||||
public function in($columns, $value): static
|
public function in($columns, $value): static
|
||||||
{
|
{
|
||||||
if ($value instanceof \Closure) {
|
if (empty($value) || !is_array($value)) {
|
||||||
$value = $this->makeNewQuery($value);
|
|
||||||
} else if (empty($value) || !is_array($value)) {
|
|
||||||
$value = [-1];
|
$value = [-1];
|
||||||
} else {
|
|
||||||
$value = array_filter($value, function ($value) {
|
|
||||||
return $value !== null;
|
|
||||||
});
|
|
||||||
$value = array_unique($value);
|
|
||||||
}
|
}
|
||||||
$this->where[] = ['IN', $columns, $value];
|
$this->where[] = ['IN', $columns, $value];
|
||||||
return $this;
|
return $this;
|
||||||
@@ -684,42 +669,43 @@ trait QueryTrait
|
|||||||
*/
|
*/
|
||||||
public function notIn($columns, $value): static
|
public function notIn($columns, $value): static
|
||||||
{
|
{
|
||||||
|
if (empty($value) || !is_array($value)) {
|
||||||
|
$value = [-1];
|
||||||
|
}
|
||||||
$this->where[] = ['NOT IN', $columns, $value];
|
$this->where[] = ['NOT IN', $columns, $value];
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $column
|
||||||
|
* @param int $start
|
||||||
|
* @param int $end
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function between(string $column, int $start, int $end): static
|
||||||
|
{
|
||||||
|
if (empty($column) || empty($start) || empty($end)) {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->where[] = $column . ' BETWEEN' . $start . ' AND ' . $end;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param string $start
|
* @param int $start
|
||||||
* @param string $end
|
* @param int $end
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
public function between(string $column, string $start, string $end): static
|
public function notBetween(string $column, int $start, int $end): static
|
||||||
{
|
|
||||||
if (empty($column) || empty($start) || empty($end)) {
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
$this->where[] = ['BETWEEN', $column, [$start, $end]];
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $column
|
|
||||||
* @param string $start
|
|
||||||
* @param string $end
|
|
||||||
* @return $this
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function notBetween(string $column, string $start, string $end): static
|
|
||||||
{
|
{
|
||||||
if (empty($column) || empty($start) || empty($end)) {
|
if (empty($column) || empty($start) || empty($end)) {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->where[] = ['NOT BETWEEN', $column, [$start, $end]];
|
$this->where[] = $column . 'NOT BETWEEN' . $start . ' AND ' . $end;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -744,14 +730,7 @@ trait QueryTrait
|
|||||||
*/
|
*/
|
||||||
public function where(callable|array|string $conditions): static
|
public function where(callable|array|string $conditions): static
|
||||||
{
|
{
|
||||||
if ($conditions instanceof \Closure) {
|
$this->where[] = $conditions;
|
||||||
call_user_func($conditions, $this);
|
|
||||||
} else {
|
|
||||||
if (is_string($conditions)) {
|
|
||||||
$conditions = [$conditions];
|
|
||||||
}
|
|
||||||
$this->where[] = $conditions;
|
|
||||||
}
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user