Creates a condition based on column-value pairs.

@param array $condition the condition specification.
@param array $params    the binding parameters to be populated

@return string the generated SQL expression
@throws Exception
492938
public function buildHashCondition($condition, &$params)
    {
        $parts = [];

        foreach ($condition as $column => $value) {
            if (ArrayHelper::isTraversable($value) || $value instanceof Query) {
                // IN condition
                $parts[] = $this->buildInCondition('IN', [$column, $value], $params);
            }
            else {
                if (strpos($column, '(') === false) {
                    $column = $this->db->quoteColumnName($column);
                }

                if ($value === null) {
                    $parts[] = "$column IS NULL";
                }
                elseif ($value instanceof Expression) {
                    $parts[] = "$column=" . $value->expression;

                    foreach ($value->params as $n => $v) {
                        $params[$n] = $v;
                    }
                }
                else {
                    $phName = self::PARAM_PREFIX . count($params);
                    $parts[] = "$column=$phName";
                    $params[$phName] = $value;
                }
            }
        }

        return count($parts) === 1 ? $parts[0] : '(' . implode(') AND (', $parts) . ')';
    }
648852
public static function countByAttributes($attributes, $condition = null, $params = array())
    {
        if (!is_a($condition, '\\mpf\\datasources\\sql\\ModelCondition')) {
            $condition = ModelCondition::getFrom($condition, get_called_class());
        }
        /* @var $condition ModelCondition */
        foreach ($attributes as $column => $value) {
            $condition->compareColumn($column, $value);
        }
        return static::count($condition, $params);
    }
1063111
public static function render(SQL\Condition $condition)
    {
        $content = $condition->getContent();
        $parameters = $condition->getParameters();

        if ($parameters and is_string($content)) {
            $content = self::expandParameterArrays($content, $parameters);
        }

        return Compiler::expression(array(
            Compiler::name($condition->getColumn()),
            $content
        ));
    }
555950
public function delete($condition=null, $params=null) {
		return $this->driver->delete($condition, $params);
	}
1122035
private function build_conditions( $where, $columns, $glue = 'AND' ) {
		list ( $_where, $_where_format ) = $this->filter( $where, $columns );
		$conditions = $values = [];
		$index      = 0;
		foreach ( $_where as $field => $value ) {
			$field  = trim( $field );
			$format = $_where_format[ $index ++ ];
			if ( is_null( $value ) ) {
				$conditions[] = "$field IS NULL";
				continue;
			}

			if ( in_array( strtoupper( $field ), [
				'EXISTS',
				'NOT EXISTS',
			] ) ) {
				! is_array( $value ) and $value = [ $value ];
				foreach ( $value as $sub_query ) {
					$conditions[] = "$field ($sub_query)";
				}
				continue;
			}

			$op = '=';
			if ( is_array( $value ) ) {
				if ( count( $value ) > 1 ) {
					$op  = trim( $value[0] );
					$val = $value[1];
					if ( in_array( strtoupper( $op ), [
						'OR',
						'AND',
					] ) ) {
						array_shift( $value );
						$_conditions = [];
						foreach ( $value as $v ) {
							if ( ! is_array( $v ) ) {
								$_conditions[] = "1=0";
								continue;
							}
							list( $c, $v ) = $this->build_conditions( $v, $columns );
							$values        = array_merge( $values, $v );
							$_conditions[] = "({$c})";
						}
						$conditions[] = implode( " {$op} ", $_conditions );

						continue;
					}
					if ( is_array( $val ) ) {
						if ( empty( $val ) ) {
							$conditions[] = "1=0";
						} else {
							foreach ( $val as $v ) {
								$values[] = $v;
							}
							$conditions[] = "$field $op (" . str_repeat( $format . ',', count( $val ) - 1 ) . $format . ')';
						}
						continue;
					}
					if ( count( $value ) > 2 ) {
						$val          = $this->get_field_name( $val, $columns );
						$conditions[] = "$field $op $val";
						continue;
					}
				} else {
					$value        = reset( $value );
					$conditions[] = "$field ($value)";
					continue;
				}
			} else {
				$val = $value;
			}

			$conditions[] = "$field $op $format";
			$values[]     = $val;
		}
		$conditions = implode( " {$glue} ", $conditions );

		return [ $conditions, $values ];
	}
329693
public function where(array $condition = null): Select
    {
        if (!\is_array($condition)) {
            throw new InvalidArgumentException('$condition has to be an array');
        }
        $this->where = (new Condition($this->namesClass->getAliases()))->parse($condition);

        return $this;
    }
465685
public static function generateKey($condition)
    {
        if(is_string($condition))
        {
            return $condition;
        }
        else
        {
            $rule = ModelManager::getKeyRule(static::class);
            $result = $rule->rule;
            foreach($rule->paramNames as $paramName)
            {
                if(!isset($condition[$paramName]))
                {
                    throw new \Exception(sprintf('GenerateKey param %s does not exists', $paramName));
                }
                $result = str_replace('{' . $paramName . '}', $condition[$paramName], $result);
            }
            return $result;
        }
    }
124783
public static function deleteAllByAttributes($attributes, $condition = null, $params = array())
    {
        if (!is_a($condition, '\\mpf\\datasources\\sql\\ModelCondition')) {
            $condition = ModelCondition::getFrom($condition, get_called_class());
        }
        /* @var $condition ModelCondition */
        foreach ($attributes as $k => $v) {
            $condition->compareColumn($k, $v);
        }
        return static::deleteAll($condition, $params);
    }
911251
private function cleanCondition($condition)
    {
        if (is_array($condition)) {
            $condition = array_filter($condition);
            if (empty($condition)) {
                $condition = null;
            }
        }

        return $condition;
    }
491010
public function add($condition, array $params = [])
    {
        $this->conditions[] = $condition;
        $this->bind($params);
        return $this;
    }
969875
protected function processCondition($conditions, $params, $types)
    {
        // Regard numeric and null as primary key value
        if (is_numeric($conditions) || empty($conditions)) {
            $conditions = array($this->primaryKey => $conditions);
        }

        if (is_array($conditions)) {
            $where = array();
            $params = array();
            foreach ($conditions as $field => $condition) {
                if (is_array($condition)) {
                    $where[] = $field . ' IN (' . implode(', ', array_pad(array(), count($condition), '?')) . ')';
                    $params = array_merge($params, $condition);
                } else {
                    $where[] = $field . " = ?";
                    $params[] = $condition;
                }
            }
            $conditions = implode(' AND ', $where);
        }

        if ($params !== false) {
            if (is_array($params)) {
                $this->params = array_merge($this->params, $params);
                $this->paramTypes = array_merge($this->paramTypes, $types);
            } else {
                $this->params[] = $params;
                if ($types) {
                    $this->paramTypes[] = $types;
                }
            }
        }

        return $conditions;
    }
44494
protected function parseCondition($condition)
    {
        $tableAlias = $this->getAlias();
        $parts = [];

        if ($condition instanceof QueryBuilderAwareInterface) {
            $condition->setQueryBuilder($this);
        }

        if ($condition instanceof Expression) {
            $parts[] = $this->getAdapter()->quoteSql($condition->toSQL());
        } elseif ($condition instanceof Q) {
            $condition->setLookupBuilder($this->getLookupBuilder());
            $condition->setAdapter($this->getAdapter());
            $condition->setTableAlias($tableAlias);
            $parts[] = $condition->toSQL();
        } elseif ($condition instanceof ToSqlInterface) {
            $parts[] = $condition->toSQL();
        } elseif (is_array($condition)) {
            foreach ($condition as $key => $value) {
                if ($value instanceof Q) {
                    $parts[] = $this->parseCondition($value);
                } else {
                    list($lookup, $column, $lookupValue) = $this->lookupBuilder->parseLookup($this, $key, $value);
                    $column = $this->getLookupBuilder()->fetchColumnName($column);
                    if (false === empty($tableAlias) && false === strpos($column, '.')) {
                        $column = $tableAlias.'.'.$column;
                    }
                    $parts[] = $this->lookupBuilder->runLookup($this->getAdapter(), $lookup, $column, $lookupValue);
                }
            }
        } elseif (is_string($condition)) {
            $parts[] = $condition;
        } elseif ($condition instanceof Expression) {
            $parts[] = $condition->toSQL();
        }

        if (1 === count($parts)) {
            return $parts[0];
        }

        return '('.implode(') AND (', $parts).')';
    }
612789
public function addCondition(Condition $condition): Condition
    {
        $query = $this->connection->createQueryBuilder()
            ->insert('ngbm_rule_condition')
            ->values(
                [
                    'id' => ':id',
                    'status' => ':status',
                    'rule_id' => ':rule_id',
                    'type' => ':type',
                    'value' => ':value',
                ]
            )
            ->setValue(
                'id',
                $condition->id !== null ?
                    (int) $condition->id :
                    $this->connectionHelper->getAutoIncrementValue('ngbm_rule_condition')
            )
            ->setParameter('status', $condition->status, Type::INTEGER)
            ->setParameter('rule_id', $condition->ruleId, Type::INTEGER)
            ->setParameter('type', $condition->type, Type::STRING)
            ->setParameter('value', json_encode($condition->value), Type::STRING);

        $query->execute();

        $condition->id = $condition->id ?? (int) $this->connectionHelper->lastInsertId('ngbm_rule_condition');

        return $condition;
    }
1204786
public function findOneBy(array $conditions)
    {
        $s = $this->selector()->one();
        foreach ($conditions as $column => $value) {
            $s->where($column, '=', $value);
        }

        return $s->execute();
    }
631590
public function fetchs($condition = NULL, $params = NULL, $fields=null, $order='',$fetchMode=PDO::FETCH_ASSOC) {
		 $this->driver->field($fields)->where($condition, $params);
         $order and $this->orderby($order);
         return  $this->fetchAll($fetchMode);
	}
152153
public function addInSelectCondition($column, $query, $params = array(), $link = 'AND') {
        $this->conditionColumns[] = $column;
        return $this->addCondition($this->_column($column) . ' IN  (' . $query . ')', $params, $link);
    }
906651
public static function count($condition, $params = array())
    {
        if (!is_a($condition, '\\mpf\\datasources\\sql\\ModelCondition')) {
            $condition = ModelCondition::getFrom($condition, get_called_class());
        }
        /* @var $condition ModelCondition */
        $condition->fields = 'COUNT(*) as number';
        $condition->setParams($params);
        $number = static::getDb()->queryAssoc($condition->__toString(true), $condition->getParams());
        return $number[0]['number'];
    }