diff --git a/Database/ActiveRecord.php b/Database/ActiveRecord.php index 9c3faeee..ac3f3f68 100644 --- a/Database/ActiveRecord.php +++ b/Database/ActiveRecord.php @@ -152,9 +152,12 @@ class ActiveRecord extends BaseActiveRecord if (empty($condition)) { $condition = [$this->getPrimary() => $this->getPrimaryValue()]; } - return static::getDb()->createCommand() - ->mathematics(self::getTable(), [$action => $columns], $condition) - ->exec($this); + $execute = static::getDb()->createCommand() + ->mathematics(self::getTable(), [$action => $columns], $condition); + if ($execute instanceof Command) { + return $execute->exec($this); + } + return false; } diff --git a/Database/Command.php b/Database/Command.php index b72eb5a3..303e2769 100644 --- a/Database/Command.php +++ b/Database/Command.php @@ -10,11 +10,13 @@ declare(strict_types=1); namespace Database; +use ReflectionException; use Snowflake\Abstracts\Component; use Exception; use PDO; use PDOStatement; use Snowflake\Abstracts\Config; +use Snowflake\Exception\NotFindClassException; /** * Class Command @@ -88,8 +90,8 @@ class Command extends Component */ public function batchUpdate($tableName, $attributes, $condition): Command { - $change = $this->db->getSchema()->getChange(); - [$sql, $param] = $change->batchUpdate($tableName, $attributes, $condition); + $change = $this->db->getSchema(); + [$sql, $param] = $change->getChange()->batchUpdate($tableName, $change, $attributes, $condition); return $this->setSql($sql)->bindValues($param); } @@ -102,9 +104,8 @@ class Command extends Component */ public function batchInsert($tableName, $attributes): Command { - $change = $this->db->getSchema()->getChange(); - $attribute_key = array_keys(current($attributes)); - [$sql, $param] = $change->batchInsert($tableName, $attribute_key, $attributes); + $change = $this->db->getSchema(); + [$sql, $param] = $change->getChange()->batchInsert($tableName, $change, $attributes); return $this->setSql($sql)->bindValues($param); } @@ -117,8 +118,8 @@ class Command extends Component */ public function insert($tableName, $attributes, $param): Command { - $change = $this->db->getSchema()->getChange(); - $sql = $change->insert($tableName, $attributes, $param); + $sql = $this->db->getSchema() + ->getChange()->insert($tableName, $attributes, $param); return $this->setSql($sql)->bindValues($param); } @@ -135,13 +136,18 @@ class Command extends Component * @param $tableName * @param $param * @param $condition - * @return Command + * @return bool|Command + * @throws ReflectionException + * @throws NotFindClassException * @throws Exception */ - public function mathematics($tableName, $param, $condition): static + public function mathematics($tableName, $param, $condition): bool|static { $change = $this->db->getSchema()->getChange(); $sql = $change->mathematics($tableName, $param, $condition); + if ($sql === false) { + return false; + } return $this->setSql($sql); } diff --git a/Database/Condition/ArrayCondition.php b/Database/Condition/ArrayCondition.php deleted file mode 100644 index 035d9f1c..00000000 --- a/Database/Condition/ArrayCondition.php +++ /dev/null @@ -1,103 +0,0 @@ -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); - } - -} diff --git a/Database/Condition/BetweenCondition.php b/Database/Condition/BetweenCondition.php index db9120fd..017715c6 100644 --- a/Database/Condition/BetweenCondition.php +++ b/Database/Condition/BetweenCondition.php @@ -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]; } } diff --git a/Database/Condition/DefaultCondition.php b/Database/Condition/DefaultCondition.php index 12e2559e..79971e7f 100644 --- a/Database/Condition/DefaultCondition.php +++ b/Database/Condition/DefaultCondition.php @@ -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)); } } diff --git a/Database/Condition/HashCondition.php b/Database/Condition/HashCondition.php index c4bacea7..d6fd96dd 100644 --- a/Database/Condition/HashCondition.php +++ b/Database/Condition/HashCondition.php @@ -1,5 +1,6 @@ resolve($key, $value); + $array[] = sprintf('%s=%s', $key, addslashes($value)); } return implode(' AND ', $array); } diff --git a/Database/Condition/InCondition.php b/Database/Condition/InCondition.php index a1a2c6ef..378030b7 100644 --- a/Database/Condition/InCondition.php +++ b/Database/Condition/InCondition.php @@ -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 ''; } } diff --git a/Database/Condition/JsonCondition.php b/Database/Condition/JsonCondition.php new file mode 100644 index 00000000..48c8166f --- /dev/null +++ b/Database/Condition/JsonCondition.php @@ -0,0 +1,20 @@ +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) . '\''; } } diff --git a/Database/Condition/LikeCondition.php b/Database/Condition/LikeCondition.php index 9e64bacf..379b7fa6 100644 --- a/Database/Condition/LikeCondition.php +++ b/Database/Condition/LikeCondition.php @@ -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) . '%\''; } } diff --git a/Database/Condition/NotBetweenCondition.php b/Database/Condition/NotBetweenCondition.php index 4b3b9152..872083b7 100644 --- a/Database/Condition/NotBetweenCondition.php +++ b/Database/Condition/NotBetweenCondition.php @@ -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]; } } diff --git a/Database/Condition/NotInCondition.php b/Database/Condition/NotInCondition.php index acffcab4..f5e0165c 100644 --- a/Database/Condition/NotInCondition.php +++ b/Database/Condition/NotInCondition.php @@ -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 . ')'; } } diff --git a/Database/Condition/NotLikeCondition.php b/Database/Condition/NotLikeCondition.php index 20cfc8b4..585726af 100644 --- a/Database/Condition/NotLikeCondition.php +++ b/Database/Condition/NotLikeCondition.php @@ -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) . '%\''; } } diff --git a/Database/Condition/OrCondition.php b/Database/Condition/OrCondition.php index 24c1014e..06fee97f 100644 --- a/Database/Condition/OrCondition.php +++ b/Database/Condition/OrCondition.php @@ -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)); } } diff --git a/Database/Condition/RLikeCondition.php b/Database/Condition/RLikeCondition.php index d6f920fd..2bc1d3a8 100644 --- a/Database/Condition/RLikeCondition.php +++ b/Database/Condition/RLikeCondition.php @@ -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)); } } diff --git a/Database/Connection.php b/Database/Connection.php index c13c828a..65e691e5 100644 --- a/Database/Connection.php +++ b/Database/Connection.php @@ -138,7 +138,7 @@ class Connection extends Component * @throws ReflectionException * @throws NotFindClassException */ - public function getSchema(): mixed + public function getSchema(): Schema { if ($this->_schema === null) { $this->_schema = Snowflake::createObject([ diff --git a/Database/Mysql/Schema.php b/Database/Mysql/Schema.php index 03056af5..6f3f1a51 100644 --- a/Database/Mysql/Schema.php +++ b/Database/Mysql/Schema.php @@ -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) { $this->_change = new Change(); diff --git a/Database/Orm/Change.php b/Database/Orm/Change.php index f42d44ab..44dc25d1 100644 --- a/Database/Orm/Change.php +++ b/Database/Orm/Change.php @@ -4,10 +4,13 @@ declare(strict_types=1); namespace Database\Orm; - +use Database\Connection; +use Database\Mysql\Schema; +use JetBrains\PhpStorm\Pure; use Snowflake\Abstracts\BaseObject; use Database\ActiveQuery; use Exception; +use Snowflake\Snowflake; /** * Class Change @@ -51,20 +54,23 @@ class Change extends BaseObject /** * @param string $table + * @param Schema $db * @param array $attributes * @param $condition * @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 = []; $_attributes = []; + + $format = $db->getColumns()->table($table); foreach ($attributes as $key => $val) { if ($val === null) { continue; } - $_attributes[':' . $key] = $this->valueEncode($val, true); + $_attributes[':' . $key] = $format->fieldFormat($key, $val); $param[] = $key . '=:' . $key; } if (empty($param)) { @@ -81,10 +87,10 @@ class Change extends BaseObject * @param $table * @param $params * @param $condition - * @return string + * @return string|bool * @throws Exception */ - public function mathematics($table, $params, $condition): string + public function mathematics($table, $params, $condition): string|bool { $_tmp = $newParam = []; if (isset($params['incr']) && is_array($params['incr'])) { @@ -94,7 +100,7 @@ class Change extends BaseObject $_tmp = $this->assemble($params['decr'], ' - ', $_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); if (!empty($condition)) { @@ -112,40 +118,12 @@ class Change extends BaseObject */ private function assemble($params, $op, array $_tmp): array { - $message = 'Incr And Decr action. The value must a numeric.'; foreach ($params as $key => $val) { - $_tmp[] = $key . '=' . $key . $op . $val; - if (!is_numeric($val)) { - throw new Exception($message); - } + $_tmp[] = sprintf('%s=%s%s%d', $key, $key, $op, $val); } - 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 @@ -171,24 +149,29 @@ class Change extends BaseObject /** * @param $table - * @param $attributes + * @param Schema $db * @param array|NULL $params * @return array * @throws Exception */ - public function batchInsert($table, $attributes, array $params = NULL): array + public function batchInsert($table, Schema $db, array $params = NULL): array { if (empty($params)) { throw new Exception("save data param not find."); } $insert = $insertData = []; + + $format = $db->getColumns()->table($table); + + $attributes = array_keys(current($params)); + foreach ($params as $key => $val) { if (!is_array($val)) { continue; } array_push($insert, '(:' . implode($key . ',:', $attributes) . $key . ')'); 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)) { @@ -206,40 +189,9 @@ class Change extends BaseObject * @return string * 构建SQL语句 */ - private function inserts($table, $fields, $data): string + #[Pure] private function inserts($table, $fields, $data): string { - $query = [ - '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; + return sprintf('INSERT IGNORE INTO' . ' %s (%s) VALUES %s', $table, $fields, $data); } diff --git a/Database/Orm/Condition.php b/Database/Orm/Condition.php index 04bd258a..b4a426c9 100644 --- a/Database/Orm/Condition.php +++ b/Database/Orm/Condition.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Database\Orm; +use Database\Condition\OrCondition; +use JetBrains\PhpStorm\Pure; use ReflectionException; use Snowflake\Core\Json; use Snowflake\Core\Str; @@ -61,7 +63,7 @@ trait Condition * @param $join * @return string */ - private function builderJoin($join): string + #[Pure] private function builderJoin($join): string { if (!empty($join)) { return ' ' . implode(' ', $join); @@ -76,77 +78,107 @@ trait Condition */ private function builderWhere($where): string { - if (empty($where)) { - return ''; - } - if (is_string($where)) { - return sprintf(' WHERE %s', $where); - } $_tmp = []; - $where = array_filter($where); - foreach ($where as $key => $value) { - if (is_array($value)) { - $value = $this->arrayMap($value); - } else if (!is_numeric($key)) { - $value = $key . '=' . $this->valueEncode($value); - } - if ($value === null) { - continue; - } + if (empty($where)) return ''; + foreach ($where as $value) { + $_value = is_string($value) ? $value : $this->conditionMap($value); + + if (empty($_value)) continue; + $_tmp[] = $value; } - if (!empty($_tmp)) { - return ' WHERE ' . implode(' AND ', $_tmp); + return sprintf(' WHERE %s', implode(' AND ', $_tmp)); } return ''; } + /** - * @param $value - * @return mixed - * @throws ReflectionException - * @throws NotFindClassException + * @param $condition + * @return string + * @throws Exception */ - private function arrayMap($value): mixed + private function conditionMap($condition): string { - $classMap = ConditionClassMap::$conditionMap; - if (isset($value[0])) { - if (!isset($classMap[strtoupper($value[0])])) { - return $value[0]; + $array = []; + if (is_string($condition) || empty($condition)) { + return $condition; + } + 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 - * @return mixed + * @param $condition + * @param $array + * @return array * @throws NotFindClassException * @throws ReflectionException */ - private function classMap($value): mixed + private function _arrayMap($condition, $array): mixed { - [$option['opera'], $option['column'], $option['value']] = $value; - - $class = ConditionClassMap::$conditionMap[strtoupper($option['opera'])]; - if (!is_array($class)) { - $class = ['class' => $class]; + if (!isset($condition[0])) { + return $this->_arrayHash($array, $condition); } - $option = array_merge($option, $class); - - /** @var Condition $class */ - return Snowflake::createObject($option)->builder(); + $stroppier = strtoupper($condition[0]); + if (str_contains($stroppier, 'OR')) { + if (!is_string($condition[2])) { + $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 * @return string @@ -163,7 +195,7 @@ trait Condition * @param $order * @return string */ - private function builderOrder($order): string + #[Pure] private function builderOrder($order): string { if (!empty($order)) { return ' ORDER BY ' . implode(',', $order); @@ -176,19 +208,15 @@ trait Condition * @param ActiveQuery $query * @return string */ - private function builderLimit(ActiveQuery $query): string + #[Pure] private function builderLimit(ActiveQuery $query): string { - $limit = $query->limit; - if (!is_numeric($limit) || $limit < 1) { + if (!is_numeric($query->limit) || $query->limit < 1) { return ""; } - $offset = $query->offset; - - if ($offset === null) { - return ' LIMIT ' . $limit; + if ($query->offset !== null) { + return ' LIMIT ' . $query->offset . ',' . $query->limit; } - - return ' LIMIT ' . $offset . ',' . $limit; + return ' LIMIT ' . $query->limit; } diff --git a/Database/Sql.php b/Database/Sql.php index 0b923808..e91acb16 100644 --- a/Database/Sql.php +++ b/Database/Sql.php @@ -6,6 +6,7 @@ * Time: 17:49 */ declare(strict_types=1); + namespace Database; @@ -19,9 +20,9 @@ use Exception; */ class Sql { - + use QueryTrait; - + /** * @return string * @throws Exception @@ -30,4 +31,16 @@ class Sql { return (new Select())->getQuery($this); } + + + /** + * @return string + * @throws Exception + */ + public function getCondition(): string + { + return (new Select())->getWhere($this); + } + + } diff --git a/Database/Traits/QueryTrait.php b/Database/Traits/QueryTrait.php index 7554c5ca..c8e42c57 100644 --- a/Database/Traits/QueryTrait.php +++ b/Database/Traits/QueryTrait.php @@ -12,6 +12,7 @@ namespace Database\Traits; use Database\ActiveQuery; use Database\ActiveRecord; +use Database\Condition\MathematicsCondition; use Database\Orm\Select; use Exception; @@ -421,42 +422,27 @@ trait QueryTrait /** * @param array $conditionArray * - * @return $this|array|ActiveQuery - * @throws Exception + * @return QueryTrait */ public function or(array $conditionArray = []): static { - $conditions = []; - 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) . ')']; - + $this->where = ['or', $conditionArray]; return $this; } /** - * @param $columns - * @param string $oprea - * @param null $value + * @param string $columns + * @param string|int|bool|null $value * - * @return array|ActiveQuery|mixed - * @throws Exception + * @param string $opera + * @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)) { $value = '\'' . $value . '\''; } - $this->where[] = $columns . $oprea . $value; + $this->where[] = $columns . $opera . $value; return $this; } @@ -487,7 +473,7 @@ trait QueryTrait $columns = 'CONCAT(' . implode(',^,', $columns) . ')'; } - $this->where[] = ['LIKE', $columns, $value]; + $this->where[] = $columns . ' LIKE \'%' . addslashes($value) . '%\''; return $this; } @@ -508,7 +494,7 @@ trait QueryTrait $columns = 'CONCAT(' . implode(',^,', $columns) . ')'; } - $this->where[] = ['LLike', $columns, rtrim($value, '%')]; + $this->where[] = $columns . ' LLike \'%' . addslashes($value) . '\''; return $this; } @@ -529,7 +515,7 @@ trait QueryTrait $columns = 'CONCAT(' . implode(',^,', $columns) . ')'; } - $this->where[] = ['RLike', $columns, ltrim($value, '%')]; + $this->where[] = $columns . ' RLike \'' . addslashes($value) . '%\''; return $this; } @@ -551,7 +537,7 @@ trait QueryTrait $columns = 'CONCAT(' . implode(',^,', $columns) . ')'; } - $this->where[] = ['NOT LIKE', $columns, ltrim($value, '%')]; + $this->where[] = $columns . ' NOT LIKE \'%' . addslashes($value) . '%\''; return $this; } @@ -561,6 +547,7 @@ trait QueryTrait * @param int $value * @return $this * @throws Exception + * @see MathematicsCondition */ public function eq(string $column, int $value): static { @@ -575,6 +562,7 @@ trait QueryTrait * @param int $value * @return $this * @throws Exception + * @see MathematicsCondition */ public function neq(string $column, int $value): static { @@ -589,6 +577,7 @@ trait QueryTrait * @param int $value * @return $this * @throws Exception + * @see MathematicsCondition */ public function gt(string $column, int $value): static { @@ -602,6 +591,7 @@ trait QueryTrait * @param int $value * @return $this * @throws Exception + * @see MathematicsCondition */ public function egt(string $column, int $value): static { @@ -616,6 +606,7 @@ trait QueryTrait * @param int $value * @return $this * @throws Exception + * @see MathematicsCondition */ public function lt(string $column, int $value): static { @@ -629,6 +620,7 @@ trait QueryTrait * @param int $value * @return $this * @throws Exception + * @see MathematicsCondition */ public function elt(string $column, int $value): static { @@ -645,15 +637,8 @@ trait QueryTrait */ public function in($columns, $value): static { - if ($value instanceof \Closure) { - $value = $this->makeNewQuery($value); - } else if (empty($value) || !is_array($value)) { + if (empty($value) || !is_array($value)) { $value = [-1]; - } else { - $value = array_filter($value, function ($value) { - return $value !== null; - }); - $value = array_unique($value); } $this->where[] = ['IN', $columns, $value]; return $this; @@ -684,42 +669,43 @@ trait QueryTrait */ public function notIn($columns, $value): static { + if (empty($value) || !is_array($value)) { + $value = [-1]; + } $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; } /** * @param string $column - * @param string $start - * @param string $end + * @param int $start + * @param int $end * @return $this - * @throws Exception */ - public function between(string $column, string $start, string $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 + public function notBetween(string $column, int $start, int $end): static { if (empty($column) || empty($start) || empty($end)) { return $this; } - $this->where[] = ['NOT BETWEEN', $column, [$start, $end]]; + $this->where[] = $column . 'NOT BETWEEN' . $start . ' AND ' . $end; return $this; } @@ -744,14 +730,7 @@ trait QueryTrait */ public function where(callable|array|string $conditions): static { - if ($conditions instanceof \Closure) { - call_user_func($conditions, $this); - } else { - if (is_string($conditions)) { - $conditions = [$conditions]; - } - $this->where[] = $conditions; - } + $this->where[] = $conditions; return $this; }