Search in sources :

Example 1 with CollectionDataType

use of com.revolsys.datatype.CollectionDataType in project com.revolsys.open by revolsys.

the class SaifSchemaReader method attributes.

public void attributes(final RecordDefinition type, final CsnIterator iterator) throws IOException {
    while (iterator.getNextEventType() == CsnIterator.ATTRIBUTE_NAME || iterator.getNextEventType() == CsnIterator.OPTIONAL_ATTRIBUTE) {
        boolean required = true;
        switch(iterator.next()) {
            case CsnIterator.OPTIONAL_ATTRIBUTE:
                required = false;
                iterator.next();
            case CsnIterator.ATTRIBUTE_NAME:
                final String fieldName = iterator.getStringValue();
                switch(iterator.next()) {
                    case CsnIterator.ATTRIBUTE_TYPE:
                        final String typePath = iterator.getPathValue();
                        DataType dataType = nameTypeMap.get(typePath);
                        if (typePath.equals(SPATIAL_OBJECT) || typePath.equals(TEXT_OR_SYMBOL_OBJECT)) {
                            dataType = DataTypes.GEOMETRY;
                            this.currentClass.setGeometryFieldIndex(this.currentClass.getFieldCount());
                        } else if (dataType == null) {
                            dataType = new SimpleDataType(typePath, Record.class);
                        }
                        this.currentClass.addField(fieldName, dataType, required);
                        break;
                    case CsnIterator.COLLECTION_ATTRIBUTE:
                        final String collectionType = iterator.getPathValue();
                        if (iterator.next() == CsnIterator.CLASS_NAME) {
                            final String contentTypeName = iterator.getPathValue();
                            final DataType collectionDataType = nameTypeMap.get(collectionType);
                            DataType contentDataType = nameTypeMap.get(contentTypeName);
                            if (contentDataType == null) {
                                contentDataType = DataTypes.RECORD;
                            }
                            this.currentClass.addField(fieldName, new CollectionDataType(collectionDataType.getName(), collectionDataType.getJavaClass(), contentDataType), required);
                        } else {
                            throw new IllegalStateException("Expecting attribute type");
                        }
                        break;
                    case CsnIterator.STRING_ATTRIBUTE:
                        int length = Integer.MAX_VALUE;
                        if (iterator.getEventType() == CsnIterator.STRING_ATTRIBUTE_LENGTH) {
                            length = iterator.getIntegerValue();
                        }
                        this.currentClass.addField(fieldName, DataTypes.STRING, length, required);
                        break;
                    default:
                        throw new IllegalStateException("Unknown event type: " + iterator.getEventType());
                }
                break;
            default:
                break;
        }
    }
}
Also used : SimpleDataType(com.revolsys.datatype.SimpleDataType) CollectionDataType(com.revolsys.datatype.CollectionDataType) CollectionDataType(com.revolsys.datatype.CollectionDataType) EnumerationDataType(com.revolsys.datatype.EnumerationDataType) SimpleDataType(com.revolsys.datatype.SimpleDataType) DataType(com.revolsys.datatype.DataType)

Example 2 with CollectionDataType

use of com.revolsys.datatype.CollectionDataType in project com.revolsys.open by revolsys.

the class PostgreSQLRecordStore method addField.

@Override
protected JdbcFieldDefinition addField(final JdbcRecordDefinition recordDefinition, final String dbColumnName, final String name, final String dbDataType, final int sqlType, final int length, final int scale, final boolean required, final String description) {
    final JdbcFieldDefinition field;
    if (dbDataType.charAt(0) == '_') {
        final String elementDbDataType = dbDataType.substring(1);
        final JdbcFieldAdder fieldAdder = getFieldAdder(elementDbDataType);
        final JdbcFieldDefinition elementField = fieldAdder.newField(this, recordDefinition, dbColumnName, name, elementDbDataType, sqlType, length, scale, required, description);
        final DataType elementDataType = elementField.getDataType();
        final CollectionDataType listDataType = new CollectionDataType("List" + elementDataType.getName(), List.class, elementDataType);
        field = new PostgreSQLArrayFieldDefinition(dbColumnName, name, listDataType, sqlType, length, scale, required, description, elementField, getProperties());
        recordDefinition.addField(field);
    } else {
        field = super.addField(recordDefinition, dbColumnName, name, dbDataType, sqlType, length, scale, required, description);
    }
    if (!dbColumnName.matches("[a-z_]+")) {
        field.setQuoteName(true);
    }
    return field;
}
Also used : JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) CollectionDataType(com.revolsys.datatype.CollectionDataType) PostgreSQLArrayFieldDefinition(com.revolsys.gis.postgresql.type.PostgreSQLArrayFieldDefinition) CollectionDataType(com.revolsys.datatype.CollectionDataType) DataType(com.revolsys.datatype.DataType) JdbcFieldAdder(com.revolsys.jdbc.field.JdbcFieldAdder)

Aggregations

CollectionDataType (com.revolsys.datatype.CollectionDataType)2 DataType (com.revolsys.datatype.DataType)2 EnumerationDataType (com.revolsys.datatype.EnumerationDataType)1 SimpleDataType (com.revolsys.datatype.SimpleDataType)1 PostgreSQLArrayFieldDefinition (com.revolsys.gis.postgresql.type.PostgreSQLArrayFieldDefinition)1 JdbcFieldAdder (com.revolsys.jdbc.field.JdbcFieldAdder)1 JdbcFieldDefinition (com.revolsys.jdbc.field.JdbcFieldDefinition)1