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;
 }
 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");
   }
 }
 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());
 }
 private String getTypeExpr(Type fieldType) {
   int precision = fieldType.getIntConstraint(Constraint.PRECISION);
   int scale = fieldType.getIntConstraint(Constraint.SCALE);
   if (scale == -1) {
     return "double precision";
   } else if (scale < 15) {
     return "double precision";
   } else {
     return "numeric(" + precision + ", " + scale + ")";
   }
 }
Example #5
0
 private void testOpen(File file) throws Exception {
   GeoRaster gr = GeoRasterFactory.createGeoRaster(file.getAbsolutePath());
   gr.open();
   int rasterType = gr.getType();
   DataSource ds = dsf.getDataSource(file);
   ds.open();
   Metadata metadata = ds.getMetadata();
   Type fieldType = metadata.getFieldType(0);
   assertTrue(fieldType.getIntConstraint(Constraint.RASTER_TYPE) == rasterType);
   ds.getFieldValue(0, 0);
   ds.close();
 }
 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");
   }
 }
 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());
 }
 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");
   }
 }
 /**
  * @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);
   }
 }
Example #10
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;
  }
Example #11
0
  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;
    }
  }
 /**
  * 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;
     }
   }
 }
Example #13
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();
  }
Example #14
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;
 }