This commit is contained in:
2021-02-26 11:03:57 +08:00
parent 73466e415e
commit c746907884
3 changed files with 9 additions and 91 deletions
+5 -90
View File
@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Database\Condition;
use Snowflake\Abstracts\BaseObject;
use Snowflake\Core\Str;
@@ -48,95 +47,11 @@ abstract class Condition extends BaseObject
*/
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 = '='): string
{
if ($value === NULL) {
return '';
}
$value = Str::encode((string)$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 (str_contains($explode, ' ')) {
$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|null $param
* @return array|null
*/
protected function format(?array $param): ?array
{
if (!is_array($param)) {
return null;
}
$_tmp = [];
foreach ($param as $value) {
if ($value === null) {
continue;
}
$value = Str::encode((string)$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 = '='): string
{
if (is_numeric($value)) {
if ($value != (int)$value) {
return $column . ' ' . $oprea . ' \'' . $value . '\'';
}
return $column . ' ' . $oprea . ' ' . $value;
} else {
$encode = '\'' . Str::encode((string)$value) . '\'';
return $column . ' ' . $oprea . ' ' . $encode;
$this->value = is_numeric($value) ? $value : '\'' . $value . '\'';
if (is_array($this->value)) {
$this->value = array_map(function ($value) {
return is_numeric($value) ? $value : '\''.$value.'\'';
}, $this->value);
}
}
+1 -1
View File
@@ -22,7 +22,7 @@ class InCondition extends Condition
#[Pure] public function builder(): string
{
if (is_array($this->value)) {
return sprintf('%s IN (\'%s\')', $this->column, implode('\',\'', $this->value));
return sprintf('%s IN (%s)', $this->column, implode(',', $this->value));
}
return '';
}
+3
View File
@@ -170,6 +170,8 @@ trait Builder
{
if (empty($value)) return $array;
if (!is_numeric($key)) {
$value = is_numeric($value) ? $value : '\'' . $value . '\'';
$array[] = sprintf("%s='%s'", $key, $value);
} else if (is_array($value)) {
$array = $this->_arrayMap($value, $array);
@@ -219,6 +221,7 @@ trait Builder
{
$_array = [];
foreach ($condition as $key => $value) {
$value = is_numeric($value) ? $value : '\'' . $value . '\'';
if (!is_numeric($key)) {
$_array[] = sprintf('%s=%s', $key, $value);
} else {