@Override public Number[] getScope(int dimension) throws DriverException { if (cachedScope == null) { boolean open = isOpen(); if (!open) { open(); } for (int i = 0; i < getRowCount(); i++) { Metadata m = getMetadata(); for (int j = 0; j < m.getFieldCount(); j++) { int typeCode = m.getFieldType(j).getTypeCode(); Envelope r = null; if ((typeCode & Type.GEOMETRY) != 0) { Value v = getFieldValue(i, j); if ((v != null) && (!v.isNull())) { r = v.getAsGeometry().getEnvelopeInternal(); } } else if (typeCode == Type.RASTER) { Value v = getFieldValue(i, j); if ((v != null) && (!v.isNull())) { r = v.getAsRaster().getMetadata().getEnvelope(); } } else if (typeCode == Type.STREAM) { Value v = getFieldValue(i, j); if ((v != null) && (!v.isNull())) { r = v.getAsStream().getEnvelope(); } } if (r != null) { if (cachedScope == null) { cachedScope = new Envelope(r); } else { cachedScope.expandToInclude(r); } } } } if (!open) { close(); } } if (cachedScope == null) { return new Number[] {0, 0}; } else { if (dimension == DataSet.X) { return new Number[] {cachedScope.getMinX(), cachedScope.getMaxX()}; } else if (dimension == DataSet.Y) { return new Number[] {cachedScope.getMinY(), cachedScope.getMaxY()}; } else { throw new UnsupportedOperationException("Not yet implemented"); } } }
@Override public String check(int fieldId, Value value) throws DriverException { // In order to build pk indexes initializeEdition(); // Check special case of auto-increment not-null fields Type type = getMetadata().getFieldType(fieldId); boolean autoIncrement = type.getBooleanConstraint(Constraint.AUTO_INCREMENT); if (autoIncrement && type.getBooleanConstraint(Constraint.NOT_NULL) && value.isNull()) { return null; } int fieldType = type.getTypeCode(); // Test geometry types. if (TypeFactory.isVectorial(type.getTypeCode())) { int valueType = value.getType(); if (!checkGeometry(valueType, fieldType)) { return "Can't put a " + TypeFactory.getTypeName(valueType) + " in a " + TypeFactory.getTypeName(fieldType) + " column."; } } // Cast value Value val = castValue(type, value); int broadType = TypeFactory.getBroaderType(fieldType, val.getType()); if (val.getType() != broadType && val.getType() != Type.NULL && !checkGeometry(val.getType(), fieldType) && fieldType != Type.STRING) { return "Can't cast a " + TypeFactory.getTypeName(value.getType()) + " to a " + TypeFactory.getTypeName(fieldType); } // Check constraints String fieldName = getMetadata().getFieldName(fieldId); String error = type.check(value); if (error != null) { return "Value at field " + getFieldName(fieldId) + " is not valid:" + error; } // Check uniqueness if (type.getBooleanConstraint(Constraint.UNIQUE) || type.getBooleanConstraint(Constraint.PK)) { // We assume a geometry field can't have unique constraint IndexQuery iq = new DefaultAlphaQuery(fieldName, value); Iterator<Integer> it = queryIndex(iq); while (it.hasNext()) { if (getFieldValue(it.next(), fieldId).equals(value).getAsBoolean()) { return fieldName + " column doesn't admit duplicates: " + value; } } } return null; }
private Value castValue(Type type, Value value) { Value newValue = value; if (!value.isNull() && type.getTypeCode() != value.getType() && TypeFactory.canBeCastTo(value.getType(), type.getTypeCode())) { newValue = value.toType(type.getTypeCode()); } return newValue; }
@Override public String check(Value value) { if (!value.isNull()) { Matcher m = pattern.matcher(value.toString()); if (!m.matches()) { return "The value doesn't match the pattern '" + constraintValue + "'"; } } return null; }
protected Value testSpatialFunction( ScalarFunction function, int[] valueType, int parameterCount, Value... normalValue) throws Exception { // Test null input ColumnValue[] vals = new ColumnValue[parameterCount]; for (int i = 0; i < parameterCount; i++) { vals[i] = new ColumnValue(valueType[i], ValueFactory.createNullValue()); } Value res = evaluate(function, vals); assertTrue(res.isNull()); // Test too many parameters Value[] args = new Value[parameterCount + 1]; for (int i = 0; i < args.length; i++) { args[i] = normalValue[0]; } try { res = evaluate(function, args); fail(); } catch (IncompatibleTypesException e) { } // Test wrong parameter type try { args = new Value[parameterCount]; for (int i = 0; i < args.length; i++) { args[i] = ValueFactory.createValue(new Value[0]); } res = evaluate(function, args); fail(); } catch (IncompatibleTypesException e) { } // Test normal input value and type res = evaluate(function, normalValue); return res; }