This commit is contained in:
2021-02-24 18:34:57 +08:00
parent 22148402b6
commit 9024c7c3ab
21 changed files with 245 additions and 349 deletions
-103
View File
@@ -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);
}
}
+1 -1
View File
@@ -17,7 +17,7 @@ class BetweenCondition extends Condition
*/
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 -2
View File
@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Database\Condition;
use JetBrains\PhpStorm\Pure;
/**
* Class DefaultCondition
* @package Database\Condition
@@ -14,9 +16,9 @@ class DefaultCondition extends Condition
/**
* @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));
}
}
+2 -1
View File
@@ -1,5 +1,6 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
/**
@@ -22,7 +23,7 @@ class HashCondition extends Condition
if ($value === null) {
continue;
}
$array[] = $this->resolve($key, $value);
$array[] = sprintf('%s=%s', $key, addslashes($value));
}
return implode(' AND ', $array);
}
+5 -10
View File
@@ -5,6 +5,7 @@ namespace Database\Condition;
use Database\ActiveQuery;
use Exception;
use JetBrains\PhpStorm\Pure;
/**
* Class InCondition
@@ -18,18 +19,12 @@ class InCondition extends Condition
* @return string
* @throws Exception
*/
public function builder(): string
#[Pure] public function builder(): string
{
if ($this->value instanceof ActiveQuery) {
$this->value = $this->value->getBuild()->getQuery($this->value);
} else {
$this->value = array_filter($this->format($this->value));
if (empty($this->value)) {
return '';
}
$this->value = implode(',', $this->value);
if (is_array($this->value)) {
return sprintf('%s IN (\'%s\')', $this->column, implode('\',\'', $this->value));
}
return '`' . $this->column . '` in(' . $this->value . ')';
return '';
}
}
+20
View File
@@ -0,0 +1,20 @@
<?php
namespace Database\Condition;
/**
* Class JsonCondition
* @package Database\Condition
*/
class JsonCondition extends Condition
{
public function builder()
{
// TODO: Implement builder() method.
}
}
+1 -2
View File
@@ -22,8 +22,7 @@ class LLikeCondition extends Condition
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
}
$this->value = Str::encode($this->value);
return $this->column . ' LIKE \'%' . $this->value . '\'';
return $this->column . ' LIKE \'%' . addslashes($this->value) . '\'';
}
}
+1 -2
View File
@@ -22,8 +22,7 @@ class LikeCondition extends Condition
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
}
$this->value = Str::encode($this->value);
return $this->column . ' LIKE \'%' . $this->value . '%\'';
return $this->column . ' LIKE \'%' . addslashes($this->value) . '%\'';
}
}
+1 -1
View File
@@ -16,7 +16,7 @@ class NotBetweenCondition extends Condition
*/
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];
}
}
+8 -8
View File
@@ -3,6 +3,8 @@ declare(strict_types=1);
namespace Database\Condition;
use JetBrains\PhpStorm\Pure;
/**
* Class NotInCondition
* @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
{
$format = array_filter($this->format($this->value));
if (empty($format)) {
return '';
if (!is_array($this->value)) {
return null;
}
return '`' . $this->column . '` not in(' . implode(',', $format) . ')';
$value = '\'' . implode('\',\'', $this->value) . '\'';
return '`' . $this->column . '` not in(' . $value . ')';
}
}
+1 -2
View File
@@ -22,8 +22,7 @@ class NotLikeCondition extends Condition
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
}
$this->value = Str::encode($this->value);
return $this->column . ' NOT LIKE \'%' . $this->value . '%\'';
return $this->column . ' NOT LIKE \'%' . addslashes($this->value) . '%\'';
}
}
+6 -2
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Database\Condition;
use JetBrains\PhpStorm\Pure;
/**
* Class OrCondition
@@ -12,12 +13,15 @@ namespace Database\Condition;
class OrCondition extends Condition
{
public array $oldParams = [];
/**
* @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));
}
}
+1 -2
View File
@@ -22,8 +22,7 @@ class RLikeCondition extends Condition
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
}
$this->value = Str::encode($this->value);
return $this->column . ' LIKE \'' . $this->value . '%\'';
return sprintf('%s LIKE \'%s\'', $this->column, addslashes($this->value));
}
}