Search in sources :

Example 1 with Value

use of com.revolsys.record.query.Value in project com.revolsys.open by revolsys.

the class QueryWhereConditionField method toQueryValue.

@SuppressWarnings("unchecked")
private <V extends QueryValue> V toQueryValue(final ValueNode expression) {
    if (expression instanceof BetweenOperatorNode) {
        final BetweenOperatorNode betweenExpression = (BetweenOperatorNode) expression;
        final ValueNode leftValueNode = betweenExpression.getLeftOperand();
        final ValueNodeList rightOperandList = betweenExpression.getRightOperandList();
        final ValueNode betweenExpressionStart = rightOperandList.get(0);
        final ValueNode betweenExpressionEnd = rightOperandList.get(1);
        if (!(leftValueNode instanceof ColumnReference)) {
            setInvalidMessage("Between operator must use a column name not: " + leftValueNode);
            return null;
        }
        if (!(betweenExpressionStart instanceof NumericConstantNode)) {
            setInvalidMessage("Between min value must be a number not: " + betweenExpressionStart);
            return null;
        }
        if (!(betweenExpressionEnd instanceof NumericConstantNode)) {
            setInvalidMessage("Between max value must be a number not: " + betweenExpressionEnd);
            return null;
        }
        final Column column = toQueryValue(leftValueNode);
        final Value min = toQueryValue(betweenExpressionStart);
        final Value max = toQueryValue(betweenExpressionEnd);
        final FieldDefinition fieldDefinition = this.recordDefinition.getField(column.getName());
        min.convert(fieldDefinition);
        max.convert(fieldDefinition);
        return (V) new Between(column, min, max);
    } else if (expression instanceof BinaryLogicalOperatorNode) {
        final BinaryLogicalOperatorNode binaryOperatorNode = (BinaryLogicalOperatorNode) expression;
        final String operator = binaryOperatorNode.getOperator().toUpperCase();
        final ValueNode leftValueNode = binaryOperatorNode.getLeftOperand();
        final ValueNode rightValueNode = binaryOperatorNode.getRightOperand();
        final QueryValue leftValue = toQueryValue(leftValueNode);
        if (leftValue instanceof Condition) {
            final Condition leftCondition = (Condition) leftValue;
            final QueryValue rightValue = toQueryValue(rightValueNode);
            if (rightValue instanceof Condition) {
                final Condition rightCondition = (Condition) rightValue;
                if ("AND".equals(operator)) {
                    return (V) new And(leftCondition, rightCondition);
                } else if ("OR".equals(operator)) {
                    return (V) new Or(leftCondition, rightCondition);
                } else {
                    setInvalidMessage("Binary logical operator " + operator + " not supported.");
                    return null;
                }
            } else {
                setInvalidMessage("Right side of " + operator + " must be a condition (e.g. column_name = 'value') not: " + rightValue);
                return null;
            }
        } else {
            setInvalidMessage("Left side of " + operator + " must be a condition (e.g. column_name = 'value') not: " + leftValue);
            return null;
        }
    } else if (expression instanceof BinaryOperatorNode) {
        final BinaryOperatorNode binaryOperatorNode = (BinaryOperatorNode) expression;
        final String operator = binaryOperatorNode.getOperator();
        final ValueNode leftValueNode = binaryOperatorNode.getLeftOperand();
        final ValueNode rightValueNode = binaryOperatorNode.getRightOperand();
        if (QueryValue.SUPPORTED_BINARY_OPERATORS.contains(operator.toUpperCase())) {
            final QueryValue leftCondition = toQueryValue(leftValueNode);
            QueryValue rightCondition = toQueryValue(rightValueNode);
            if (leftCondition instanceof Column) {
                if (rightCondition instanceof Value) {
                    final Object value = ((Value) rightCondition).getValue();
                    if (value == null) {
                        setInvalidMessage("Values can't be null for " + operator + " use IS NULL or IS NOT NULL instead.");
                    } else {
                        final Column column = (Column) leftCondition;
                        final String name = column.getName();
                        final FieldDefinition fieldDefinition = this.recordDefinition.getField(name);
                        final CodeTable codeTable = this.recordDefinition.getCodeTableByFieldName(name);
                        if (codeTable == null || fieldDefinition == this.recordDefinition.getIdField()) {
                            try {
                                final Object convertedValue = fieldDefinition.toFieldValueException(value);
                                if (convertedValue == null) {
                                    setInvalidMessage("Values can't be null for " + operator + " use IS NULL or IS NOT NULL instead.");
                                    return null;
                                } else {
                                    rightCondition = new Value(fieldDefinition, convertedValue);
                                }
                            } catch (final Throwable t) {
                                setInvalidMessage(name + "='" + value + "' is not a valid " + fieldDefinition.getDataType().getValidationName());
                            }
                        } else {
                            Object id;
                            if (value instanceof String) {
                                final String string = (String) value;
                                final String[] values = string.split(":");
                                id = codeTable.getIdentifier((Object[]) values);
                            } else {
                                id = codeTable.getIdentifier(value);
                            }
                            if (id == null) {
                                setInvalidMessage(name + "='" + value + "' could not be found in the code table " + codeTable.getName());
                            } else {
                                rightCondition = new Value(fieldDefinition, id);
                            }
                        }
                    }
                }
            }
            if (expression instanceof BinaryArithmeticOperatorNode) {
                final QueryValue arithmaticCondition = Q.arithmatic(leftCondition, operator, rightCondition);
                return (V) arithmaticCondition;
            } else {
                final Condition binaryCondition = Q.binary(leftCondition, operator, rightCondition);
                return (V) binaryCondition;
            }
        } else {
            setInvalidMessage("Unsupported binary operator " + operator);
        }
    } else if (expression instanceof ColumnReference) {
        final ColumnReference column = (ColumnReference) expression;
        String columnName = column.getColumnName();
        columnName = columnName.replaceAll("\"", "");
        final FieldDefinition fieldDefinition = this.recordDefinition.getField(columnName);
        if (fieldDefinition == null) {
            setInvalidMessage("Invalid field name " + columnName);
        } else {
            return (V) new Column(fieldDefinition);
        }
    } else if (expression instanceof LikeEscapeOperatorNode) {
        final LikeEscapeOperatorNode likeEscapeOperatorNode = (LikeEscapeOperatorNode) expression;
        final ValueNode leftValueNode = likeEscapeOperatorNode.getReceiver();
        final ValueNode rightValueNode = likeEscapeOperatorNode.getLeftOperand();
        final QueryValue leftCondition = toQueryValue(leftValueNode);
        final QueryValue rightCondition = toQueryValue(rightValueNode);
        return (V) new ILike(leftCondition, rightCondition);
    } else if (expression instanceof NotNode) {
        final NotNode notNode = (NotNode) expression;
        final ValueNode operand = notNode.getOperand();
        final Condition condition = toQueryValue(operand);
        return (V) new Not(condition);
    } else if (expression instanceof InListOperatorNode) {
        final InListOperatorNode inListOperatorNode = (InListOperatorNode) expression;
        final ValueNode leftOperand = inListOperatorNode.getLeftOperand();
        final QueryValue leftCondition = toQueryValue(leftOperand);
        final List<QueryValue> conditions = new ArrayList<>();
        final RowConstructorNode itemsList = inListOperatorNode.getRightOperandList();
        for (final ValueNode itemValueNode : itemsList.getNodeList()) {
            final QueryValue itemCondition = toQueryValue(itemValueNode);
            conditions.add(itemCondition);
        }
        return (V) new In(leftCondition, new CollectionValue(conditions));
    } else if (expression instanceof IsNullNode) {
        final IsNullNode isNullNode = (IsNullNode) expression;
        final ValueNode operand = isNullNode.getOperand();
        final QueryValue value = toQueryValue(operand);
        if (isNullNode.getNodeType() == NodeTypes.IS_NOT_NULL_NODE) {
            return (V) new IsNotNull(value);
        } else {
            return (V) new IsNull(value);
        }
    // } else if (expression instanceof Parenthesis) {
    // final Parenthesis parenthesis = (Parenthesis)expression;
    // final ValueNode parenthesisValueNode = parenthesis.getExpression();
    // final Condition condition = toCondition(parenthesisExpression);
    // final ParenthesisCondition parenthesisCondition = new
    // ParenthesisCondition(
    // condition);
    // if (parenthesis.isNot()) {
    // return (V)Q.not(parenthesisCondition);
    // } else {
    // return (V)parenthesisCondition;
    // }
    } else if (expression instanceof RowConstructorNode) {
        final RowConstructorNode rowConstructorNode = (RowConstructorNode) expression;
        final ValueNodeList values = rowConstructorNode.getNodeList();
        final ValueNode valueNode = values.get(0);
        return (V) toQueryValue(valueNode);
    } else if (expression instanceof UserTypeConstantNode) {
        final UserTypeConstantNode constant = (UserTypeConstantNode) expression;
        final Object objectValue = constant.getObjectValue();
        return (V) new Value(objectValue);
    } else if (expression instanceof ConstantNode) {
        final ConstantNode constant = (ConstantNode) expression;
        final Object value = constant.getValue();
        return (V) new Value(value);
    } else if (expression instanceof SimpleStringOperatorNode) {
        final SimpleStringOperatorNode operatorNode = (SimpleStringOperatorNode) expression;
        final String functionName = operatorNode.getMethodName().toUpperCase();
        final ValueNode operand = operatorNode.getOperand();
        final QueryValue condition = toQueryValue(operand);
        return (V) new Function(functionName, condition);
    } else if (expression instanceof CastNode) {
        final CastNode castNode = (CastNode) expression;
        final String typeName = castNode.getType().getSQLstring();
        final ValueNode operand = castNode.getCastOperand();
        final QueryValue condition = toQueryValue(operand);
        return (V) new Cast(condition, typeName);
    } else if (expression == null) {
        return null;
    } else {
        setInvalidMessage("Unsupported expression" + expression.getClass() + " " + expression);
    }
    return null;
}
Also used : Cast(com.revolsys.record.query.Cast) NotNode(com.akiban.sql.parser.NotNode) Or(com.revolsys.record.query.Or) In(com.revolsys.record.query.In) FieldDefinition(com.revolsys.record.schema.FieldDefinition) ArrayList(java.util.ArrayList) BinaryArithmeticOperatorNode(com.akiban.sql.parser.BinaryArithmeticOperatorNode) UserTypeConstantNode(com.akiban.sql.parser.UserTypeConstantNode) Function(com.revolsys.record.query.functions.Function) CollectionValue(com.revolsys.record.query.CollectionValue) IsNotNull(com.revolsys.record.query.IsNotNull) NumericConstantNode(com.akiban.sql.parser.NumericConstantNode) ConstantNode(com.akiban.sql.parser.ConstantNode) UserTypeConstantNode(com.akiban.sql.parser.UserTypeConstantNode) Column(com.revolsys.record.query.Column) ValueNodeList(com.akiban.sql.parser.ValueNodeList) NumericConstantNode(com.akiban.sql.parser.NumericConstantNode) SimpleStringOperatorNode(com.akiban.sql.parser.SimpleStringOperatorNode) InListOperatorNode(com.akiban.sql.parser.InListOperatorNode) BinaryOperatorNode(com.akiban.sql.parser.BinaryOperatorNode) BetweenOperatorNode(com.akiban.sql.parser.BetweenOperatorNode) Condition(com.revolsys.record.query.Condition) CodeTable(com.revolsys.record.code.CodeTable) BinaryLogicalOperatorNode(com.akiban.sql.parser.BinaryLogicalOperatorNode) Between(com.revolsys.record.query.Between) ILike(com.revolsys.record.query.ILike) QueryValue(com.revolsys.record.query.QueryValue) CastNode(com.akiban.sql.parser.CastNode) Not(com.revolsys.record.query.Not) IsNullNode(com.akiban.sql.parser.IsNullNode) And(com.revolsys.record.query.And) ValueNode(com.akiban.sql.parser.ValueNode) QueryValue(com.revolsys.record.query.QueryValue) CollectionValue(com.revolsys.record.query.CollectionValue) Value(com.revolsys.record.query.Value) IsNull(com.revolsys.record.query.IsNull) RowConstructorNode(com.akiban.sql.parser.RowConstructorNode) ColumnReference(com.akiban.sql.parser.ColumnReference) LikeEscapeOperatorNode(com.akiban.sql.parser.LikeEscapeOperatorNode)

Example 2 with Value

use of com.revolsys.record.query.Value in project com.revolsys.open by revolsys.

the class FieldFilterPanel method setFilter.

public void setFilter(final Condition filter) {
    if (!this.fieldNames.isEmpty()) {
        Invoke.later(() -> {
            try (BaseCloseable settingFilter = this.settingFilter.closeable(true)) {
                setSearchFilter(filter);
                boolean simple = false;
                if (Property.isEmpty(filter)) {
                    final Field searchField = (Field) this.searchField;
                    if (searchField != null) {
                        searchField.setFieldValue(null);
                    }
                    setSearchOperator("=");
                    simple = true;
                } else if (filter instanceof Not) {
                    final Not not = (Not) filter;
                    final QueryValue condition = not.getValue();
                    if (condition instanceof IsNull) {
                        final IsNull isNull = (IsNull) condition;
                        final IsNotNull isNotNull = new IsNotNull(isNull.getValue());
                        setFilter(isNotNull);
                        return;
                    }
                } else if (filter instanceof ILike) {
                    final ILike equal = (ILike) filter;
                    final QueryValue leftCondition = equal.getLeft();
                    final QueryValue rightCondition = equal.getRight();
                    if (leftCondition instanceof Column && rightCondition instanceof Value) {
                        final Column column = (Column) leftCondition;
                        final String searchFieldName = column.getName();
                        setSearchFieldName(searchFieldName);
                        if (setSearchOperator("Like")) {
                            final Value value = (Value) rightCondition;
                            final Object searchValue = value.getValue();
                            String searchText = DataTypes.toString(searchValue);
                            if (Property.hasValue(searchText)) {
                                setSearchField(this.searchTextField);
                                searchText = searchText.replaceAll("%", "");
                                final String previousSearchText = this.searchTextField.getText();
                                if (!DataType.equal(searchText, previousSearchText)) {
                                    this.searchTextField.setFieldValue(searchText);
                                }
                                simple = true;
                            } else {
                                setSearchFilter(null);
                            }
                        }
                    }
                } else if (filter instanceof BinaryCondition) {
                    final BinaryCondition condition = (BinaryCondition) filter;
                    final QueryValue leftCondition = condition.getLeft();
                    final QueryValue rightCondition = condition.getRight();
                    if (leftCondition instanceof Column && rightCondition instanceof Value) {
                        final Column column = (Column) leftCondition;
                        final String searchFieldName = column.getName();
                        setSearchFieldName(searchFieldName);
                        final String searchOperator = condition.getOperator();
                        if (setSearchOperator(searchOperator)) {
                            final Value value = (Value) rightCondition;
                            final Object searchValue = value.getValue();
                            final String searchText = DataTypes.toString(searchValue);
                            final Field searchField = (Field) this.searchField;
                            final Object oldValue = searchField.getFieldValue();
                            if (!searchText.equalsIgnoreCase(DataTypes.toString(oldValue))) {
                                searchField.setFieldValue(searchText);
                            }
                            simple = true;
                        }
                    }
                } else if (filter instanceof RightUnaryCondition) {
                    final RightUnaryCondition condition = (RightUnaryCondition) filter;
                    final String operator = condition.getOperator();
                    if (filter instanceof IsNull || filter instanceof IsNotNull) {
                        final QueryValue leftValue = condition.getValue();
                        if (leftValue instanceof Column) {
                            final Column column = (Column) leftValue;
                            final String searchFieldName = column.getName();
                            setSearchFieldName(searchFieldName);
                            if (setSearchOperator(operator)) {
                                simple = true;
                            }
                        }
                    }
                }
                if (simple) {
                    this.whereLabel.setVisible(false);
                    if (this.nameField != null) {
                        this.nameField.setVisible(true);
                    }
                    if (this.operatorField != null) {
                        this.operatorField.setVisible(true);
                    }
                    this.searchFieldPanel.setVisible(true);
                } else {
                    String filterText = filter.toString();
                    if (filterText.length() > 40) {
                        filterText = filterText.substring(0, 40) + "...";
                    }
                    this.whereLabel.setText(filterText);
                    this.whereLabel.setToolTipText(filterText);
                    this.whereLabel.setVisible(true);
                    if (this.nameField != null) {
                        this.nameField.setVisible(false);
                    }
                    this.operatorField.setVisible(false);
                    this.searchFieldPanel.setVisible(false);
                }
            }
        });
    }
}
Also used : BaseCloseable(com.revolsys.io.BaseCloseable) ILike(com.revolsys.record.query.ILike) QueryValue(com.revolsys.record.query.QueryValue) JXSearchField(org.jdesktop.swingx.JXSearchField) DateField(com.revolsys.swing.field.DateField) AbstractRecordQueryField(com.revolsys.swing.field.AbstractRecordQueryField) Field(com.revolsys.swing.field.Field) TextField(com.revolsys.swing.field.TextField) QueryWhereConditionField(com.revolsys.swing.field.QueryWhereConditionField) RightUnaryCondition(com.revolsys.record.query.RightUnaryCondition) Not(com.revolsys.record.query.Not) IsNotNull(com.revolsys.record.query.IsNotNull) Column(com.revolsys.record.query.Column) BinaryCondition(com.revolsys.record.query.BinaryCondition) QueryValue(com.revolsys.record.query.QueryValue) GlobalBooleanValue(com.revolsys.value.GlobalBooleanValue) Value(com.revolsys.record.query.Value) IsNull(com.revolsys.record.query.IsNull)

Example 3 with Value

use of com.revolsys.record.query.Value in project com.revolsys.open by revolsys.

the class FileGdbRecordStore method appendQueryValue.

@Override
public void appendQueryValue(final Query query, final StringBuilder buffer, final QueryValue condition) {
    if (condition instanceof Like || condition instanceof ILike) {
        final BinaryCondition like = (BinaryCondition) condition;
        final QueryValue left = like.getLeft();
        final QueryValue right = like.getRight();
        buffer.append("UPPER(CAST(");
        appendQueryValue(query, buffer, left);
        buffer.append(" AS VARCHAR(4000))) LIKE ");
        if (right instanceof Value) {
            final Value valueCondition = (Value) right;
            final Object value = valueCondition.getValue();
            buffer.append("'");
            if (value != null) {
                final String string = DataTypes.toString(value);
                buffer.append(string.toUpperCase().replaceAll("'", "''"));
            }
            buffer.append("'");
        } else {
            appendQueryValue(query, buffer, right);
        }
    } else if (condition instanceof LeftUnaryCondition) {
        final LeftUnaryCondition unaryCondition = (LeftUnaryCondition) condition;
        final String operator = unaryCondition.getOperator();
        final QueryValue right = unaryCondition.getValue();
        buffer.append(operator);
        buffer.append(" ");
        appendQueryValue(query, buffer, right);
    } else if (condition instanceof RightUnaryCondition) {
        final RightUnaryCondition unaryCondition = (RightUnaryCondition) condition;
        final QueryValue left = unaryCondition.getValue();
        final String operator = unaryCondition.getOperator();
        appendQueryValue(query, buffer, left);
        buffer.append(" ");
        buffer.append(operator);
    } else if (condition instanceof BinaryCondition) {
        final BinaryCondition binaryCondition = (BinaryCondition) condition;
        final QueryValue left = binaryCondition.getLeft();
        final String operator = binaryCondition.getOperator();
        final QueryValue right = binaryCondition.getRight();
        appendQueryValue(query, buffer, left);
        buffer.append(" ");
        buffer.append(operator);
        buffer.append(" ");
        appendQueryValue(query, buffer, right);
    } else if (condition instanceof AbstractMultiCondition) {
        final AbstractMultiCondition multipleCondition = (AbstractMultiCondition) condition;
        buffer.append("(");
        boolean first = true;
        final String operator = multipleCondition.getOperator();
        for (final QueryValue subCondition : multipleCondition.getQueryValues()) {
            if (first) {
                first = false;
            } else {
                buffer.append(" ");
                buffer.append(operator);
                buffer.append(" ");
            }
            appendQueryValue(query, buffer, subCondition);
        }
        buffer.append(")");
    } else if (condition instanceof In) {
        final In in = (In) condition;
        if (in.isEmpty()) {
            buffer.append("1==0");
        } else {
            final QueryValue left = in.getLeft();
            appendQueryValue(query, buffer, left);
            buffer.append(" IN (");
            appendQueryValue(query, buffer, in.getValues());
            buffer.append(")");
        }
    } else if (condition instanceof Value) {
        final Value valueCondition = (Value) condition;
        Object value = valueCondition.getValue();
        if (value instanceof Identifier) {
            final Identifier identifier = (Identifier) value;
            value = identifier.getValue(0);
        }
        appendValue(buffer, value);
    } else if (condition instanceof CollectionValue) {
        final CollectionValue collectionValue = (CollectionValue) condition;
        final List<Object> values = collectionValue.getValues();
        boolean first = true;
        for (final Object value : values) {
            if (first) {
                first = false;
            } else {
                buffer.append(", ");
            }
            appendValue(buffer, value);
        }
    } else if (condition instanceof Column) {
        final Column column = (Column) condition;
        final Object name = column.getName();
        buffer.append(name);
    } else if (condition instanceof SqlCondition) {
        final SqlCondition sqlCondition = (SqlCondition) condition;
        final String where = sqlCondition.getSql();
        final List<Object> parameters = sqlCondition.getParameterValues();
        if (parameters.isEmpty()) {
            if (where.indexOf('?') > -1) {
                throw new IllegalArgumentException("No arguments specified for a where clause with placeholders: " + where);
            } else {
                buffer.append(where);
            }
        } else {
            final Matcher matcher = PLACEHOLDER_PATTERN.matcher(where);
            int i = 0;
            while (matcher.find()) {
                if (i >= parameters.size()) {
                    throw new IllegalArgumentException("Not enough arguments for where clause with placeholders: " + where);
                }
                final Object argument = parameters.get(i);
                final StringBuffer replacement = new StringBuffer();
                matcher.appendReplacement(replacement, DataTypes.toString(argument));
                buffer.append(replacement);
                appendValue(buffer, argument);
                i++;
            }
            final StringBuffer tail = new StringBuffer();
            matcher.appendTail(tail);
            buffer.append(tail);
        }
    } else if (condition instanceof EnvelopeIntersects) {
        buffer.append("1 = 1");
    } else if (condition instanceof WithinDistance) {
        buffer.append("1 = 1");
    } else {
        condition.appendDefaultSql(query, this, buffer);
    }
}
Also used : In(com.revolsys.record.query.In) Matcher(java.util.regex.Matcher) ILike(com.revolsys.record.query.ILike) VectorOfWString(com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString) QueryValue(com.revolsys.record.query.QueryValue) SqlCondition(com.revolsys.record.query.SqlCondition) RightUnaryCondition(com.revolsys.record.query.RightUnaryCondition) CollectionValue(com.revolsys.record.query.CollectionValue) Like(com.revolsys.record.query.Like) ILike(com.revolsys.record.query.ILike) Identifier(com.revolsys.identifier.Identifier) SingleIdentifier(com.revolsys.identifier.SingleIdentifier) BinaryCondition(com.revolsys.record.query.BinaryCondition) Column(com.revolsys.record.query.Column) EnvelopeIntersects(com.revolsys.record.query.functions.EnvelopeIntersects) WithinDistance(com.revolsys.record.query.functions.WithinDistance) CollectionValue(com.revolsys.record.query.CollectionValue) Value(com.revolsys.record.query.Value) QueryValue(com.revolsys.record.query.QueryValue) List(java.util.List) LeftUnaryCondition(com.revolsys.record.query.LeftUnaryCondition) AbstractMultiCondition(com.revolsys.record.query.AbstractMultiCondition)

Example 4 with Value

use of com.revolsys.record.query.Value in project com.revolsys.open by revolsys.

the class OgrRecordStore method appendQueryValue.

@Override
public void appendQueryValue(final Query query, final StringBuilder sql, final QueryValue condition) {
    if (condition instanceof Like || condition instanceof ILike) {
        final BinaryCondition like = (BinaryCondition) condition;
        final QueryValue left = like.getLeft();
        final QueryValue right = like.getRight();
        sql.append("UPPER(");
        appendQueryValue(query, sql, left);
        sql.append(") LIKE ");
        if (right instanceof Value) {
            final Value valueCondition = (Value) right;
            final Object value = valueCondition.getValue();
            sql.append("'");
            if (value != null) {
                final String string = DataTypes.toString(value);
                sql.append(string.toUpperCase());
            }
            sql.append("'");
        } else {
            appendQueryValue(query, sql, right);
        }
    } else if (condition instanceof LeftUnaryCondition) {
        final LeftUnaryCondition unaryCondition = (LeftUnaryCondition) condition;
        final String operator = unaryCondition.getOperator();
        final QueryValue right = unaryCondition.getValue();
        sql.append(operator);
        sql.append(" ");
        appendQueryValue(query, sql, right);
    } else if (condition instanceof RightUnaryCondition) {
        final RightUnaryCondition unaryCondition = (RightUnaryCondition) condition;
        final QueryValue left = unaryCondition.getValue();
        final String operator = unaryCondition.getOperator();
        appendQueryValue(query, sql, left);
        sql.append(" ");
        sql.append(operator);
    } else if (condition instanceof BinaryCondition) {
        final BinaryCondition binaryCondition = (BinaryCondition) condition;
        final QueryValue left = binaryCondition.getLeft();
        final String operator = binaryCondition.getOperator();
        final QueryValue right = binaryCondition.getRight();
        appendQueryValue(query, sql, left);
        sql.append(" ");
        sql.append(operator);
        sql.append(" ");
        appendQueryValue(query, sql, right);
    } else if (condition instanceof AbstractMultiCondition) {
        final AbstractMultiCondition multipleCondition = (AbstractMultiCondition) condition;
        sql.append("(");
        boolean first = true;
        final String operator = multipleCondition.getOperator();
        for (final QueryValue subCondition : multipleCondition.getQueryValues()) {
            if (first) {
                first = false;
            } else {
                sql.append(" ");
                sql.append(operator);
                sql.append(" ");
            }
            appendQueryValue(query, sql, subCondition);
        }
        sql.append(")");
    } else if (condition instanceof Value) {
        final Value valueCondition = (Value) condition;
        final Object value = valueCondition.getValue();
        appendValue(sql, value);
    } else if (condition instanceof CollectionValue) {
        final CollectionValue collectionValue = (CollectionValue) condition;
        final List<Object> values = collectionValue.getValues();
        boolean first = true;
        for (final Object value : values) {
            if (first) {
                first = false;
            } else {
                sql.append(", ");
            }
            appendValue(sql, value);
        }
    } else if (condition instanceof Column) {
        final Column column = (Column) condition;
        final Object name = column.getName();
        sql.append(name);
    } else if (condition instanceof SqlCondition) {
        final SqlCondition sqlCondition = (SqlCondition) condition;
        final String where = sqlCondition.getSql();
        final List<Object> parameters = sqlCondition.getParameterValues();
        if (parameters.isEmpty()) {
            if (where.indexOf('?') > -1) {
                throw new IllegalArgumentException("No arguments specified for a where clause with placeholders: " + where);
            } else {
                sql.append(where);
            }
        } else {
            final Matcher matcher = PLACEHOLDER_PATTERN.matcher(where);
            int i = 0;
            while (matcher.find()) {
                if (i >= parameters.size()) {
                    throw new IllegalArgumentException("Not enough arguments for where clause with placeholders: " + where);
                }
                final Object argument = parameters.get(i);
                final StringBuffer replacement = new StringBuffer();
                matcher.appendReplacement(replacement, DataTypes.toString(argument));
                sql.append(replacement);
                appendValue(sql, argument);
                i++;
            }
            final StringBuffer tail = new StringBuffer();
            matcher.appendTail(tail);
            sql.append(tail);
        }
    } else if (condition instanceof EnvelopeIntersects) {
        final EnvelopeIntersects envelopeIntersects = (EnvelopeIntersects) condition;
        final QueryValue boundingBox1Value = envelopeIntersects.getBoundingBox1Value();
        final QueryValue boundingBox2Value = envelopeIntersects.getBoundingBox2Value();
        if (boundingBox1Value == null || boundingBox2Value == null) {
            sql.append("1 = 0");
        } else {
            sql.append("Intersects(");
            boundingBox1Value.appendSql(query, this, sql);
            sql.append(",");
            boundingBox2Value.appendSql(query, this, sql);
            sql.append(")");
        }
    } else if (condition instanceof WithinDistance) {
        final WithinDistance withinDistance = (WithinDistance) condition;
        final QueryValue geometry1Value = withinDistance.getGeometry1Value();
        final QueryValue geometry2Value = withinDistance.getGeometry2Value();
        final QueryValue distanceValue = withinDistance.getDistanceValue();
        if (geometry1Value == null || geometry2Value == null || distanceValue == null) {
            sql.append("1 = 0");
        } else {
            sql.append("Distance(");
            geometry1Value.appendSql(query, this, sql);
            sql.append(", ");
            geometry2Value.appendSql(query, this, sql);
            sql.append(") <= ");
            distanceValue.appendSql(query, this, sql);
            sql.append(")");
        }
    } else {
        condition.appendDefaultSql(query, this, sql);
    }
}
Also used : Matcher(java.util.regex.Matcher) ILike(com.revolsys.record.query.ILike) QueryValue(com.revolsys.record.query.QueryValue) SqlCondition(com.revolsys.record.query.SqlCondition) RightUnaryCondition(com.revolsys.record.query.RightUnaryCondition) CollectionValue(com.revolsys.record.query.CollectionValue) ILike(com.revolsys.record.query.ILike) Like(com.revolsys.record.query.Like) BinaryCondition(com.revolsys.record.query.BinaryCondition) Column(com.revolsys.record.query.Column) EnvelopeIntersects(com.revolsys.record.query.functions.EnvelopeIntersects) WithinDistance(com.revolsys.record.query.functions.WithinDistance) QueryValue(com.revolsys.record.query.QueryValue) CollectionValue(com.revolsys.record.query.CollectionValue) Value(com.revolsys.record.query.Value) LeftUnaryCondition(com.revolsys.record.query.LeftUnaryCondition) AbstractMultiCondition(com.revolsys.record.query.AbstractMultiCondition)

Example 5 with Value

use of com.revolsys.record.query.Value in project com.revolsys.open by revolsys.

the class F method envelopeIntersects.

public static EnvelopeIntersects envelopeIntersects(final FieldDefinition attribute, final BoundingBox boundingBox) {
    if (attribute == null) {
        return null;
    } else {
        final Column column = new Column(attribute);
        final Value value = new Value(attribute, boundingBox);
        return new EnvelopeIntersects(column, value);
    }
}
Also used : Column(com.revolsys.record.query.Column) QueryValue(com.revolsys.record.query.QueryValue) Value(com.revolsys.record.query.Value)

Aggregations

Column (com.revolsys.record.query.Column)8 QueryValue (com.revolsys.record.query.QueryValue)8 Value (com.revolsys.record.query.Value)8 ILike (com.revolsys.record.query.ILike)4 BinaryCondition (com.revolsys.record.query.BinaryCondition)3 CollectionValue (com.revolsys.record.query.CollectionValue)3 RightUnaryCondition (com.revolsys.record.query.RightUnaryCondition)3 AbstractMultiCondition (com.revolsys.record.query.AbstractMultiCondition)2 In (com.revolsys.record.query.In)2 IsNotNull (com.revolsys.record.query.IsNotNull)2 IsNull (com.revolsys.record.query.IsNull)2 LeftUnaryCondition (com.revolsys.record.query.LeftUnaryCondition)2 Like (com.revolsys.record.query.Like)2 Not (com.revolsys.record.query.Not)2 SqlCondition (com.revolsys.record.query.SqlCondition)2 EnvelopeIntersects (com.revolsys.record.query.functions.EnvelopeIntersects)2 WithinDistance (com.revolsys.record.query.functions.WithinDistance)2 FieldDefinition (com.revolsys.record.schema.FieldDefinition)2 Matcher (java.util.regex.Matcher)2 BetweenOperatorNode (com.akiban.sql.parser.BetweenOperatorNode)1