diff --git a/Database/Condition/Condition.php b/Database/Condition/Condition.php index d2a44033..94895475 100644 --- a/Database/Condition/Condition.php +++ b/Database/Condition/Condition.php @@ -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); } } diff --git a/Database/Condition/InCondition.php b/Database/Condition/InCondition.php index 378030b7..e68d1b74 100644 --- a/Database/Condition/InCondition.php +++ b/Database/Condition/InCondition.php @@ -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 ''; } diff --git a/Database/Traits/Builder.php b/Database/Traits/Builder.php index 3dc8558e..fc012c09 100644 --- a/Database/Traits/Builder.php +++ b/Database/Traits/Builder.php @@ -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 {