Search in sources :

Example 91 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class ArcGisRestServerRecordLayer method newUniqueValueRenderer.

@SuppressWarnings("unchecked")
private FilterMultipleRenderer newUniqueValueRenderer(final MapEx rendererProperties) {
    final FilterMultipleRenderer filterRenderer = new FilterMultipleRenderer(this);
    final String fieldName = rendererProperties.getString("field1");
    filterRenderer.setName(fieldName);
    for (final MapEx valueProperties : (List<MapEx>) rendererProperties.getValue("uniqueValueInfos")) {
        final AbstractRecordLayerRenderer valueRenderer = newSymbolRenderer(valueProperties, "symbol");
        if (valueRenderer != null) {
            final String valueLabel = valueProperties.getString("label");
            if (valueLabel != null) {
                valueRenderer.setName(valueLabel);
            }
            final String value = valueProperties.getString("value");
            if (value != null) {
                final FieldDefinition fieldDefinition = getFieldDefinition(fieldName);
                if (fieldDefinition.getDataType().isRequiresQuotes()) {
                    valueRenderer.setQueryFilter(fieldName + "='" + value + '\'');
                } else {
                    valueRenderer.setQueryFilter(fieldName + " = " + value);
                }
            }
            filterRenderer.addRenderer(valueRenderer);
        }
    }
    final AbstractRecordLayerRenderer defaultRenderer = newSymbolRenderer(rendererProperties, "defaultSymbol");
    if (defaultRenderer != null) {
        final String defaultLabel = rendererProperties.getString("defaultLabel", "Default");
        defaultRenderer.setName(defaultLabel);
        filterRenderer.addRenderer(defaultRenderer);
    }
    return filterRenderer;
}
Also used : AbstractRecordLayerRenderer(com.revolsys.swing.map.layer.record.renderer.AbstractRecordLayerRenderer) MapEx(com.revolsys.collection.map.MapEx) FieldDefinition(com.revolsys.record.schema.FieldDefinition) ArrayList(java.util.ArrayList) List(java.util.List) FilterMultipleRenderer(com.revolsys.swing.map.layer.record.renderer.FilterMultipleRenderer)

Example 92 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class Q method equalId.

public static Condition equalId(final List<?> fields, final Identifier identifier) {
    final And and = new And();
    List<Object> values;
    if (identifier == null) {
        values = Arrays.asList(new Object[fields.size()]);
    } else {
        values = identifier.getValues();
    }
    if (fields.size() == values.size()) {
        for (int i = 0; i < fields.size(); i++) {
            final Object fieldKey = fields.get(i);
            Object value = values.get(i);
            Condition condition;
            if (value == null) {
                if (fieldKey instanceof FieldDefinition) {
                    condition = isNull((FieldDefinition) fieldKey);
                } else {
                    condition = isNull(fieldKey.toString());
                }
            } else {
                if (fieldKey instanceof FieldDefinition) {
                    final FieldDefinition fieldDefinition = (FieldDefinition) fieldKey;
                    value = fieldDefinition.toFieldValue(value);
                    condition = equal(fieldDefinition, value);
                } else {
                    condition = equal(fieldKey.toString(), value);
                }
            }
            and.and(condition);
        }
    } else {
        throw new IllegalArgumentException("Field count for " + fields + " != count for values " + values);
    }
    return and;
}
Also used : FieldDefinition(com.revolsys.record.schema.FieldDefinition)

Example 93 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class QueryValue method toQueryValue.

@SuppressWarnings("unchecked")
static <V extends QueryValue> V toQueryValue(final RecordDefinition recordDefinition, 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)) {
            throw new IllegalArgumentException("Between operator must use a column name not: " + leftValueNode);
        }
        if (!(betweenExpressionStart instanceof NumericConstantNode)) {
            throw new IllegalArgumentException("Between min value must be a number not: " + betweenExpressionStart);
        }
        if (!(betweenExpressionEnd instanceof NumericConstantNode)) {
            throw new IllegalArgumentException("Between max value must be a number not: " + betweenExpressionEnd);
        }
        final Column column = toQueryValue(recordDefinition, leftValueNode);
        final Value min = toQueryValue(recordDefinition, betweenExpressionStart);
        final Value max = toQueryValue(recordDefinition, betweenExpressionEnd);
        if (recordDefinition != null) {
            final FieldDefinition field = recordDefinition.getField(column.getName());
            min.convert(field);
            max.convert(field);
        }
        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 Condition leftCondition = toQueryValue(recordDefinition, leftValueNode);
        final Condition rightCondition = toQueryValue(recordDefinition, rightValueNode);
        if ("AND".equals(operator)) {
            return (V) new And(leftCondition, rightCondition);
        } else if ("OR".equals(operator)) {
            return (V) new Or(leftCondition, rightCondition);
        } else {
            throw new IllegalArgumentException("Binary logical operator " + operator + " not supported.");
        }
    } 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 (SUPPORTED_BINARY_OPERATORS.contains(operator.toUpperCase())) {
            final QueryValue leftCondition = toQueryValue(recordDefinition, leftValueNode);
            QueryValue rightCondition = toQueryValue(recordDefinition, rightValueNode);
            if (leftCondition instanceof Column) {
                if (rightCondition instanceof Value) {
                    final Column column = (Column) leftCondition;
                    final String name = column.getName();
                    final Object value = ((Value) rightCondition).getValue();
                    if (value == null) {
                        throw new IllegalArgumentException("Values can't be null for " + operator + " use IS NULL or IS NOT NULL instead.");
                    } else if (recordDefinition != null) {
                        final FieldDefinition fieldDefinition = recordDefinition.getField(name);
                        final CodeTable codeTable = recordDefinition.getCodeTableByFieldName(name);
                        if (codeTable == null || fieldDefinition == recordDefinition.getIdField()) {
                            final Object convertedValue = fieldDefinition.toFieldValueException(value);
                            if (convertedValue == null) {
                                throw new IllegalArgumentException("Values can't be null for " + operator + " use IS NULL or IS NOT NULL instead.");
                            } else {
                                rightCondition = new Value(fieldDefinition, convertedValue);
                            }
                        } 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) {
                                throw new IllegalArgumentException(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 {
            throw new IllegalArgumentException("Unsupported binary operator " + operator);
        }
    } else if (expression instanceof ColumnReference) {
        final ColumnReference column = (ColumnReference) expression;
        String columnName = column.getColumnName();
        columnName = columnName.replaceAll("\"", "");
        if (recordDefinition == null) {
            return (V) new Column(columnName);
        } else {
            final FieldDefinition fieldDefinition = recordDefinition.getField(columnName);
            if (fieldDefinition == null) {
                recordDefinition.getField(columnName);
                throw new IllegalArgumentException("Invalid column 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(recordDefinition, leftValueNode);
        final QueryValue rightCondition = toQueryValue(recordDefinition, 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(recordDefinition, 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(recordDefinition, leftOperand);
        final List<QueryValue> conditions = new ArrayList<>();
        final RowConstructorNode itemsList = inListOperatorNode.getRightOperandList();
        for (final ValueNode itemValueNode : itemsList.getNodeList()) {
            final QueryValue itemCondition = toQueryValue(recordDefinition, 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(recordDefinition, 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(recordDefinition, 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(recordDefinition, 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(recordDefinition, operand);
        return (V) new Cast(condition, typeName);
    } else if (expression instanceof JavaToSQLValueNode) {
        final JavaToSQLValueNode node = (JavaToSQLValueNode) expression;
        final JavaValueNode javaValueNode = node.getJavaValueNode();
        if (javaValueNode instanceof StaticMethodCallNode) {
            final StaticMethodCallNode methodNode = (StaticMethodCallNode) javaValueNode;
            final List<QueryValue> parameters = new ArrayList<>();
            final String methodName = methodNode.getMethodName();
            for (final JavaValueNode parameter : methodNode.getMethodParameters()) {
                if (parameter instanceof SQLToJavaValueNode) {
                    final SQLToJavaValueNode sqlNode = (SQLToJavaValueNode) parameter;
                    final QueryValue param = toQueryValue(recordDefinition, sqlNode.getSQLValueNode());
                    parameters.add(param);
                }
            }
            if (methodName.equals("get_map_value")) {
                return (V) new GetMapValue(parameters);
            }
        }
        return null;
    } else if (expression == null) {
        return null;
    } else {
        throw new IllegalArgumentException("Unsupported expression" + expression.getClass() + " " + expression);
    }
}
Also used : NotNode(com.akiban.sql.parser.NotNode) FieldDefinition(com.revolsys.record.schema.FieldDefinition) ArrayList(java.util.ArrayList) SQLToJavaValueNode(com.akiban.sql.parser.SQLToJavaValueNode) JavaValueNode(com.akiban.sql.parser.JavaValueNode) BinaryArithmeticOperatorNode(com.akiban.sql.parser.BinaryArithmeticOperatorNode) UserTypeConstantNode(com.akiban.sql.parser.UserTypeConstantNode) Function(com.revolsys.record.query.functions.Function) NumericConstantNode(com.akiban.sql.parser.NumericConstantNode) ConstantNode(com.akiban.sql.parser.ConstantNode) UserTypeConstantNode(com.akiban.sql.parser.UserTypeConstantNode) ValueNodeList(com.akiban.sql.parser.ValueNodeList) NumericConstantNode(com.akiban.sql.parser.NumericConstantNode) SimpleStringOperatorNode(com.akiban.sql.parser.SimpleStringOperatorNode) InListOperatorNode(com.akiban.sql.parser.InListOperatorNode) ArrayList(java.util.ArrayList) ValueNodeList(com.akiban.sql.parser.ValueNodeList) List(java.util.List) BinaryOperatorNode(com.akiban.sql.parser.BinaryOperatorNode) BetweenOperatorNode(com.akiban.sql.parser.BetweenOperatorNode) CodeTable(com.revolsys.record.code.CodeTable) GetMapValue(com.revolsys.record.query.functions.GetMapValue) BinaryLogicalOperatorNode(com.akiban.sql.parser.BinaryLogicalOperatorNode) StaticMethodCallNode(com.akiban.sql.parser.StaticMethodCallNode) JavaToSQLValueNode(com.akiban.sql.parser.JavaToSQLValueNode) CastNode(com.akiban.sql.parser.CastNode) SQLToJavaValueNode(com.akiban.sql.parser.SQLToJavaValueNode) IsNullNode(com.akiban.sql.parser.IsNullNode) SQLToJavaValueNode(com.akiban.sql.parser.SQLToJavaValueNode) JavaValueNode(com.akiban.sql.parser.JavaValueNode) JavaToSQLValueNode(com.akiban.sql.parser.JavaToSQLValueNode) ValueNode(com.akiban.sql.parser.ValueNode) GetMapValue(com.revolsys.record.query.functions.GetMapValue) RowConstructorNode(com.akiban.sql.parser.RowConstructorNode) ColumnReference(com.akiban.sql.parser.ColumnReference) LikeEscapeOperatorNode(com.akiban.sql.parser.LikeEscapeOperatorNode)

Example 94 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class SqlCondition method appendParameters.

@Override
public int appendParameters(int index, final PreparedStatement statement) {
    for (int i = 0; i < this.parameterValues.size(); i++) {
        final Object value = this.parameterValues.get(i);
        JdbcFieldDefinition jdbcAttribute = null;
        if (i < this.parameterAttributes.size()) {
            final FieldDefinition attribute = this.parameterAttributes.get(i);
            if (attribute instanceof JdbcFieldDefinition) {
                jdbcAttribute = (JdbcFieldDefinition) attribute;
            }
        }
        if (jdbcAttribute == null) {
            jdbcAttribute = JdbcFieldDefinition.newFieldDefinition(value);
        }
        try {
            index = jdbcAttribute.setPreparedStatementValue(statement, index, value);
        } catch (final SQLException e) {
            throw new RuntimeException("Unable to set value: " + value, e);
        }
    }
    return index;
}
Also used : JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) SQLException(java.sql.SQLException) FieldDefinition(com.revolsys.record.schema.FieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition)

Example 95 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class Query method intersects.

public static Query intersects(final RecordDefinition recordDefinition, final BoundingBox boundingBox) {
    final FieldDefinition geometryField = recordDefinition.getGeometryField();
    if (geometryField == null) {
        return null;
    } else {
        final EnvelopeIntersects intersects = F.envelopeIntersects(geometryField, boundingBox);
        final Query query = new Query(recordDefinition, intersects);
        return query;
    }
}
Also used : FieldDefinition(com.revolsys.record.schema.FieldDefinition) EnvelopeIntersects(com.revolsys.record.query.functions.EnvelopeIntersects)

Aggregations

FieldDefinition (com.revolsys.record.schema.FieldDefinition)133 RecordDefinition (com.revolsys.record.schema.RecordDefinition)38 DataType (com.revolsys.datatype.DataType)32 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)23 JdbcFieldDefinition (com.revolsys.jdbc.field.JdbcFieldDefinition)19 PathName (com.revolsys.io.PathName)15 Record (com.revolsys.record.Record)15 ArrayList (java.util.ArrayList)15 Geometry (com.revolsys.geometry.model.Geometry)13 CodeTable (com.revolsys.record.code.CodeTable)9 Query (com.revolsys.record.query.Query)8 LineString (com.revolsys.geometry.model.LineString)7 ArrayRecord (com.revolsys.record.ArrayRecord)7 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)7 SQLException (java.sql.SQLException)7 CoordinateSystem (com.revolsys.geometry.cs.CoordinateSystem)4 IOException (java.io.IOException)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 BadLocationException (javax.swing.text.BadLocationException)4