@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 void initializeEdition() throws DriverException { if (!initialized) { long rowCount = getDataSource().getRowCount(); // build alpha indexes on unique fields Metadata m = getMetadata(); for (int i = 0; i < m.getFieldCount(); i++) { Type type = m.getFieldType(i); if (type.getBooleanConstraint(Constraint.UNIQUE) || type.getBooleanConstraint(Constraint.PK)) { IndexManager indexManager = getDataSourceFactory().getIndexManager(); try { if (!indexManager.isIndexed(getName(), m.getFieldName(i))) { indexManager.buildIndex(getName(), m.getFieldName(i), new NullProgressMonitor()); } } catch (NoSuchTableException e) { throw new DriverException("table not found: " + getName(), e); } catch (IndexException e) { throw new DriverException("Cannot create index on unique fields", e); } } } // initialize directions rowsAddresses = new ArrayList<PhysicalRowAddress>(); for (int i = 0; i < rowCount; i++) { PhysicalRowAddress dir = new OriginalRowAddress(getDataSource(), i); rowsAddresses.add(dir); editionActions.add(new NoEditionInfo(getPK(i), i)); } Number[] xScope = getDataSource().getScope(DataSet.X); Number[] yScope = getDataSource().getScope(DataSet.Y); if ((xScope != null) && (yScope != null)) { cachedScope = new Envelope( new Coordinate(xScope[0].doubleValue(), yScope[0].doubleValue()), new Coordinate(xScope[1].doubleValue(), yScope[1].doubleValue())); } else { cachedScope = null; } initialized = true; } }