Exemplo n.º 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;
  }
Exemplo n.º 2
0
 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;
 }
Exemplo n.º 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");
   }
 }
Exemplo n.º 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());
 }
Exemplo n.º 5
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");
   }
 }
Exemplo n.º 6
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());
 }
Exemplo n.º 7
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");
   }
 }
Exemplo n.º 8
0
 /**
  * @param value String representation of the field
  * @return The corresponding value or null value if constraints are not respected.
  * @throws ParseException Unable to parse provided string
  * @throws DriverException The driver throw this exception
  */
 public Value getValue(Object value) throws ParseException, DriverException {
   Type fieldType = ds.getMetadata().getFieldType(fieldIndex);
   Value inputValue;
   if (value != null && (fieldType.getTypeCode() == Type.STRING || !value.toString().isEmpty())) {
     inputValue = ValueFactory.createValueByType(value.toString(), fieldType.getTypeCode());
   } else {
     inputValue = ValueFactory.createNullValue();
   }
   String error = ds.check(fieldIndex, inputValue);
   if (error == null || error.isEmpty()) {
     return inputValue;
   } else {
     throw new ParseException(error, 0);
   }
 }
 /**
  * Validates a type against this argument
  *
  * @param type a type
  * @return true if the type is valid, false otherwise
  */
 public boolean isValid(Type type) {
   if ((type.getTypeCode() & this.typeCode) == 0) {
     return false;
   } else {
     if (argValidator != null) {
       return argValidator.isValid(type);
     } else {
       return true;
     }
   }
 }
Exemplo n.º 10
0
  @Test
  public void testTypeInAddField() throws Exception {
    String dsName = "toto";
    sm.register(dsName, getTempCopyOf(super.getAnyNonSpatialResource()));
    DataSource d = dsf.getDataSource(dsName);

    d.open();
    int fc = d.getMetadata().getFieldCount();
    Type type = (d.getDriver()).getTypesDefinitions()[0].createType();
    d.addField("new", type);
    assertEquals(d.getMetadata().getFieldType(fc).getTypeCode(), type.getTypeCode());
    d.commit();
    d.close();

    d = dsf.getDataSource(dsName);
    d.open();
    assertEquals(d.getMetadata().getFieldCount(), fc + 1);
    assertEquals(d.getMetadata().getFieldType(fc).getTypeCode(), type.getTypeCode());
    d.close();
  }
Exemplo n.º 11
0
 public boolean canApply(Type type) {
   return type.getTypeCode() == Type.FLOAT;
 }
 @Override
 public boolean canApply(Type type) {
   return type.getTypeCode() == Type.FLOAT || type.getTypeCode() == Type.DOUBLE;
 }