This commit is contained in:
2020-08-31 12:38:32 +08:00
parent 683a39dded
commit 65c443ec87
126 changed files with 7369 additions and 228 deletions
+102
View File
@@ -0,0 +1,102 @@
<?php
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 $math = ['like', 'in', 'or', 'eq', 'neq', 'gt', 'ngt', 'lt', 'nlt'];
/**
* @return mixed
* @throws Exception
*/
public function builder()
{
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)
{
return isset($value[0]) && in_array($value[0], $this->math);
}
/**
* @param array $value
* @return mixed
* @throws Exception
*/
public function buildOperaCondition($value)
{
[$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->adaptation();
$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)
{
return $this->resolve($key, $value);
}
}
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace Database\Condition;
/**
* Class BetweenCondition
* @package Database\Condition
*/
class BetweenCondition extends Condition
{
/**
* @return string
*/
public function builder()
{
return $this->column . ' BETWEEN ' . $this->value[0] . ' AND ' . $this->value[1];
}
}
+22
View File
@@ -0,0 +1,22 @@
<?php
namespace Database\Condition;
/**
* Class ChildCondition
* @package Database\Condition
*/
class ChildCondition extends Condition
{
/**
* @return string
*/
public function builder()
{
return $this->column . ' ' . $this->opera . ' (' . $this->value . ')';
}
}
+142
View File
@@ -0,0 +1,142 @@
<?php
namespace Database\Condition;
use Snowflake\Abstracts\BaseObject;
use Snowflake\Core\Str;
/**
* Class Condition
* @package Database\Condition
*/
abstract class Condition extends BaseObject
{
protected $column = '';
protected $opera = '=';
/** @var array|mixed */
protected $value;
const INT_TYPE = ['bit', 'bool', 'tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'float', 'double', 'decimal', 'timestamp'];
protected $attributes = [];
abstract public function builder();
/**
* @param string $column
*/
public function setColumn(string $column): void
{
$this->column = $column;
}
/**
* @param string $opera
*/
public function setOpera(string $opera): void
{
$this->opera = $opera;
}
/**
* @param $value
*/
public function setValue($value): void
{
$this->value = $value;
}
/**
* @param $column
* @param $value
* @param $oprea
*
* @return string
*
* $query = new Build();
* $query->where('id', '2');
* $query->where(['id' => 3]);
* $query->where('id', '<', 4);
* $query->orWhere('id', '=', 5);
* $query->orWhere('id', '=', 6);
* $query->ANDWhere('id', '=', 7);
* $sql = '(((id=2 AND id=3 AND id<4) OR id=5) OR id=6) AND i(d=7)';
*/
protected function resolve($column, $value = null, $oprea = '=')
{
if ($value === NULL) {
return '';
}
$value = Str::encode($value);
if (trim($oprea) == 'like') {
return $column . ' ' . $oprea . ' \'%' . $value . '%\'';
}
$columns = $this->column[$column] ?? '';
if (empty($columns)) {
return $this->typeBuilder($column, $value, $oprea);
}
$explode = explode('(', $columns);
$explode = array_shift($explode);
if (strpos($explode, ' ') !== false) {
$explode = explode(' ', $explode)[0];
}
if (!in_array(trim($explode), static::INT_TYPE)) {
$str = $column . ' ' . $oprea . ' \'' . $value . '\'';
} else {
$str = $column . ' ' . $oprea . ' ' . $value;
}
return $str;
}
/**
* @param array $param
* @return array
*/
protected function format($param)
{
if (!is_array($param)) {
return null;
}
$_tmp = [];
foreach ($param as $value) {
if ($value === null) {
continue;
}
$value = Str::encode($value);
if (is_numeric($value)) {
$_tmp[] = Str::encode($value);
} else {
$_tmp[] = '\'' . Str::encode($value) . '\'';
}
}
return $_tmp;
}
/**
* @param $column
* @param null $value
* @param string $oprea
* @return string
*/
public function typeBuilder($column, $value = null, $oprea = '=')
{
if (is_numeric($value)) {
if ($value != (int)$value) {
return $column . ' ' . $oprea . ' \'' . $value . '\'';
}
return $column . ' ' . $oprea . ' ' . $value;
} else {
$encode = '\'' . Str::encode($value) . '\'';
return $column . ' ' . $oprea . ' ' . $encode;
}
}
}
+22
View File
@@ -0,0 +1,22 @@
<?php
namespace Database\Condition;
/**
* Class DefaultCondition
* @package Database\Condition
*/
class DefaultCondition extends Condition
{
/**
* @return string
*/
public function builder()
{
return $this->resolve($this->column, $this->value, $this->opera);
}
}
+30
View File
@@ -0,0 +1,30 @@
<?php
namespace Database\Condition;
/**
* Class HashCondition
* @package Yoc\db\condition
*/
class HashCondition extends Condition
{
/**
* @return string
*/
public function builder()
{
$array = [];
if (empty($this->value)) {
return '';
}
foreach ($this->value as $key => $value) {
if ($value === null) {
continue;
}
$array[] = $this->resolve($key, $value);
}
return implode(' AND ', $array);
}
}
+34
View File
@@ -0,0 +1,34 @@
<?php
namespace Database\Condition;
use Database\ActiveQuery;
/**
* Class InCondition
* @package Database\Condition
*/
class InCondition extends Condition
{
/**
* @return string
* @throws \Exception
*/
public function builder()
{
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);
}
return '`' . $this->column . '` in(' . $this->value . ')';
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace Database\Condition;
use Snowflake\Core\Str;
/**
* Class LLikeCondition
* @package Database\Condition
*/
class LLikeCondition extends Condition
{
public $pos = '';
/**
* @return string
*/
public function builder()
{
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
}
$this->value = Str::encode($this->value);
return $this->column . ' LIKE \'%' . $this->value . '\'';
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace Database\Condition;
use Snowflake\Core\Str;
/**
* Class LikeCondition
* @package Database\Condition
*/
class LikeCondition extends Condition
{
public $pos = '';
/**
* @return string
*/
public function builder()
{
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
}
$this->value = Str::encode($this->value);
return $this->column . ' LIKE \'%' . $this->value . '%\'';
}
}
@@ -0,0 +1,73 @@
<?php
namespace Database\Condition;
/**
* Class MathematicsCondition
* @package Database\Condition
*/
class MathematicsCondition extends Condition
{
public $type = '';
/**
* @return mixed
*/
public function builder()
{
$this->value = (float)$this->value;
return $this->{strtolower($this->type)}();
}
/**
* @return string
*/
public function eq()
{
return $this->column . ' = ' . $this->value;
}
/**
* @return string
*/
public function neq()
{
return $this->column . ' <> ' . $this->value;
}
/**
* @return string
*/
public function gt()
{
return $this->column . ' > ' . $this->value;
}
/**
* @return string
*/
public function egt()
{
return $this->column . ' >= ' . $this->value;
}
/**
* @return string
*/
public function lt()
{
return $this->column . ' < ' . $this->value;
}
/**
* @return string
*/
public function elt()
{
return $this->column . ' <= ' . $this->value;
}
}
@@ -0,0 +1,22 @@
<?php
namespace Database\Condition;
/**
* Class NotBetweenCondition
* @package Database\Condition
*/
class NotBetweenCondition extends Condition
{
/**
* @return string
*/
public function builder()
{
return $this->column . ' NOT BETWEEN ' . $this->value[0] . ' AND ' . $this->value[1];
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace Database\Condition;
/**
* Class NotInCondition
* @package Database\Condition
*/
class NotInCondition extends Condition
{
/**
* @return string
*/
public function builder()
{
$format = array_filter($this->format($this->value));
if (empty($format)) {
return '';
}
return '`' . $this->column . '` not in(' . implode(',', $format) . ')';
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace Database\Condition;
use Snowflake\Core\Str;
/**
* Class NotLikeCondition
* @package Database\Condition
*/
class NotLikeCondition extends Condition
{
public $pos = '';
/**
* @return string
*/
public function builder()
{
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
}
$this->value = Str::encode($this->value);
return $this->column . ' NOT LIKE \'%' . $this->value . '%\'';
}
}
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace Database\Condition;
/**
* Class OrCondition
* @package Database\Condition
*/
class OrCondition extends Condition
{
/**
* @return string
*/
public function builder()
{
return 'OR ' . $this->resolve($this->column, $this->value, $this->opera);
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace Database\Condition;
use Snowflake\Core\Str;
/**
* Class RLikeCondition
* @package Database\Condition
*/
class RLikeCondition extends Condition
{
public $pos = '';
/**
* @return string
*/
public function builder()
{
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
}
$this->value = Str::encode($this->value);
return $this->column . ' LIKE \'' . $this->value . '%\'';
}
}