Search in sources :

Example 1 with ShallowUserDefinedType

use of com.datastax.oss.driver.internal.core.metadata.schema.ShallowUserDefinedType in project java-driver by datastax.

the class DataTypeCqlNameParser method parse.

private DataType parse(String toParse, CqlIdentifier keyspaceId, boolean frozen, Map<CqlIdentifier, UserDefinedType> userTypes, InternalDriverContext context) {
    if (toParse.startsWith("'")) {
        return DataTypes.custom(toParse.substring(1, toParse.length() - 1));
    }
    Parser parser = new Parser(toParse, 0);
    String type = parser.parseTypeName();
    if (type.equalsIgnoreCase(RawColumn.THRIFT_EMPTY_TYPE)) {
        return DataTypes.custom(type);
    }
    DataType nativeType = NATIVE_TYPES_BY_NAME.get(type.toLowerCase(Locale.ROOT));
    if (nativeType != null) {
        return nativeType;
    }
    if (parser.isEOS()) {
        // No parameters => it's a UDT
        CqlIdentifier name = CqlIdentifier.fromCql(type);
        if (userTypes != null) {
            UserDefinedType userType = userTypes.get(name);
            if (userType == null) {
                throw new IllegalStateException(String.format("Can't find referenced user type %s", type));
            }
            return userType.copy(frozen);
        } else {
            return new ShallowUserDefinedType(keyspaceId, name, frozen);
        }
    }
    List<String> parameters = parser.parseTypeParameters();
    if (type.equalsIgnoreCase("list")) {
        if (parameters.size() != 1) {
            throw new IllegalArgumentException(String.format("Expecting single parameter for list, got %s", parameters));
        }
        DataType elementType = parse(parameters.get(0), keyspaceId, false, userTypes, context);
        return DataTypes.listOf(elementType, frozen);
    }
    if (type.equalsIgnoreCase("set")) {
        if (parameters.size() != 1) {
            throw new IllegalArgumentException(String.format("Expecting single parameter for set, got %s", parameters));
        }
        DataType elementType = parse(parameters.get(0), keyspaceId, false, userTypes, context);
        return DataTypes.setOf(elementType, frozen);
    }
    if (type.equalsIgnoreCase("map")) {
        if (parameters.size() != 2) {
            throw new IllegalArgumentException(String.format("Expecting two parameters for map, got %s", parameters));
        }
        DataType keyType = parse(parameters.get(0), keyspaceId, false, userTypes, context);
        DataType valueType = parse(parameters.get(1), keyspaceId, false, userTypes, context);
        return DataTypes.mapOf(keyType, valueType, frozen);
    }
    if (type.equalsIgnoreCase("frozen")) {
        if (parameters.size() != 1) {
            throw new IllegalArgumentException(String.format("Expecting single parameter for frozen keyword, got %s", parameters));
        }
        return parse(parameters.get(0), keyspaceId, true, userTypes, context);
    }
    if (type.equalsIgnoreCase("tuple")) {
        if (parameters.isEmpty()) {
            throw new IllegalArgumentException("Expecting at list one parameter for tuple, got none");
        }
        ImmutableList.Builder<DataType> componentTypesBuilder = ImmutableList.builder();
        for (String rawType : parameters) {
            componentTypesBuilder.add(parse(rawType, keyspaceId, false, userTypes, context));
        }
        return new DefaultTupleType(componentTypesBuilder.build(), context);
    }
    throw new IllegalArgumentException("Could not parse type name " + toParse);
}
Also used : DefaultTupleType(com.datastax.oss.driver.internal.core.type.DefaultTupleType) ShallowUserDefinedType(com.datastax.oss.driver.internal.core.metadata.schema.ShallowUserDefinedType) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) DataType(com.datastax.oss.driver.api.core.type.DataType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) ShallowUserDefinedType(com.datastax.oss.driver.internal.core.metadata.schema.ShallowUserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier)

Aggregations

CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 DataType (com.datastax.oss.driver.api.core.type.DataType)1 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)1 ShallowUserDefinedType (com.datastax.oss.driver.internal.core.metadata.schema.ShallowUserDefinedType)1 DefaultTupleType (com.datastax.oss.driver.internal.core.type.DefaultTupleType)1 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)1