コード例 #1
0
  @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;
  }
コード例 #2
0
 public static void failIfNotOfType(CustomQuery customQuery, Type type, int typeCode) {
   if (type.getTypeCode() != typeCode) {
     throw new IncompatibleTypesException(
         "Function "
             + customQuery.getName()
             + " only operates with "
             + TypeFactory.getTypeName(typeCode)
             + " types. "
             + TypeFactory.getTypeName(type.getTypeCode())
             + " found");
   }
 }
コード例 #3
0
 public static void failIfNotNumeric(Function function, Type type)
     throws IncompatibleTypesException {
   if (!TypeFactory.isNumerical(type.getTypeCode())) {
     throw new IncompatibleTypesException(
         "Function "
             + function.getName()
             + " only operates with numerical types. "
             + TypeFactory.getTypeName(type.getTypeCode())
             + " found");
   }
 }
コード例 #4
0
 public static void failIfNotOfTypes(CustomQuery customQuery, Type type, int... typesCodes) {
   for (int typeCode : typesCodes) {
     if (type.getTypeCode() == typeCode) {
       return;
     }
   }
   throw new IncompatibleTypesException(
       TypeFactory.getTypeName(type.getTypeCode())
           + " is not allowed with custom query "
           + customQuery.getName());
 }
コード例 #5
0
 public static void failIfNotOfTypes(Function function, Type type, int... typesCodes)
     throws IncompatibleTypesException {
   for (int typeCode : typesCodes) {
     if (type.getTypeCode() == typeCode) {
       return;
     }
   }
   throw new IncompatibleTypesException(
       TypeFactory.getTypeName(type.getTypeCode())
           + " is not allowed with function "
           + function.getName());
 }
コード例 #6
0
 public static void failIfNotNumeric(
     final CustomQuery customQuery, final Type type, final int argNumber)
     throws IncompatibleTypesException {
   if (!TypeFactory.isNumerical(type.getTypeCode())) {
     throw new IncompatibleTypesException(
         customQuery.getName()
             + " requires a numerical type as argument number "
             + argNumber
             + ". "
             + TypeFactory.getTypeName(type.getTypeCode())
             + " found");
   }
 }
コード例 #7
0
  public static void failIfFieldIsNotOfType(
      final CustomQuery customQuery,
      final String fieldName,
      final int fieldIndex,
      final int typeCodeOfField,
      final Metadata metadata)
      throws DriverException, SemanticException {
    failIfFieldDoesNotExist(customQuery, fieldName, fieldIndex, metadata);

    final Type[] fieldTypes = MetadataUtilities.getFieldTypes(metadata);
    if (typeCodeOfField != fieldTypes[fieldIndex].getTypeCode()) {
      throw new IncompatibleTypesException(
          customQuery.getName()
              + ": "
              + fieldName
              + " is not of type "
              + TypeFactory.getTypeName(typeCodeOfField));
    }
  }