Search in sources :

Example 1 with UnresolvedUserTypeException

use of com.datastax.driver.core.exceptions.UnresolvedUserTypeException in project java-driver by datastax.

the class DataTypeCqlNameParser method parse.

/**
 * @param currentUserTypes if this method gets called as part of a refresh that spans multiple user types, this contains the ones
 *                         that have already been refreshed. If the type we are parsing references a user type, we want to pick its
 *                         definition from this map in priority.
 * @param oldUserTypes     this contains all the keyspace's user types as they were before the refresh started. If we can't find a
 *                         definition in {@code currentUserTypes}, we'll check this map as a fallback.
 */
static DataType parse(String toParse, Cluster cluster, String currentKeyspaceName, Map<String, UserType> currentUserTypes, Map<String, UserType> oldUserTypes, boolean frozen, boolean shallowUserTypes) {
    if (toParse.startsWith("'"))
        return custom(toParse.substring(1, toParse.length() - 1));
    Parser parser = new Parser(toParse, 0);
    String type = parser.parseTypeName();
    DataType nativeType = NATIVE_TYPES_MAP.get(type.toLowerCase());
    if (nativeType != null)
        return nativeType;
    if (type.equalsIgnoreCase(LIST)) {
        List<String> parameters = parser.parseTypeParameters();
        if (parameters.size() != 1)
            throw new DriverInternalError(String.format("Excepting single parameter for list, got %s", parameters));
        DataType elementType = parse(parameters.get(0), cluster, currentKeyspaceName, currentUserTypes, oldUserTypes, false, shallowUserTypes);
        return list(elementType, frozen);
    }
    if (type.equalsIgnoreCase(SET)) {
        List<String> parameters = parser.parseTypeParameters();
        if (parameters.size() != 1)
            throw new DriverInternalError(String.format("Excepting single parameter for set, got %s", parameters));
        DataType elementType = parse(parameters.get(0), cluster, currentKeyspaceName, currentUserTypes, oldUserTypes, false, shallowUserTypes);
        return set(elementType, frozen);
    }
    if (type.equalsIgnoreCase(MAP)) {
        List<String> parameters = parser.parseTypeParameters();
        if (parameters.size() != 2)
            throw new DriverInternalError(String.format("Excepting two parameters for map, got %s", parameters));
        DataType keyType = parse(parameters.get(0), cluster, currentKeyspaceName, currentUserTypes, oldUserTypes, false, shallowUserTypes);
        DataType valueType = parse(parameters.get(1), cluster, currentKeyspaceName, currentUserTypes, oldUserTypes, false, shallowUserTypes);
        return map(keyType, valueType, frozen);
    }
    if (type.equalsIgnoreCase(FROZEN)) {
        List<String> parameters = parser.parseTypeParameters();
        if (parameters.size() != 1)
            throw new DriverInternalError(String.format("Excepting single parameter for frozen keyword, got %s", parameters));
        return parse(parameters.get(0), cluster, currentKeyspaceName, currentUserTypes, oldUserTypes, true, shallowUserTypes);
    }
    if (type.equalsIgnoreCase(TUPLE)) {
        List<String> rawTypes = parser.parseTypeParameters();
        List<DataType> types = new ArrayList<DataType>(rawTypes.size());
        for (String rawType : rawTypes) {
            types.add(parse(rawType, cluster, currentKeyspaceName, currentUserTypes, oldUserTypes, false, shallowUserTypes));
        }
        return cluster.getMetadata().newTupleType(types);
    }
    // so that it gets detected later on, see TableMetadata
    if (type.equalsIgnoreCase(EMPTY))
        return custom(type);
    // Otherwise it's a UDT. If we only want a shallow definition build it, otherwise search known definitions.
    if (shallowUserTypes)
        return new UserType.Shallow(currentKeyspaceName, Metadata.handleId(type), frozen);
    UserType userType = null;
    if (currentUserTypes != null)
        userType = currentUserTypes.get(Metadata.handleId(type));
    if (userType == null && oldUserTypes != null)
        userType = oldUserTypes.get(Metadata.handleId(type));
    if (userType == null)
        throw new UnresolvedUserTypeException(currentKeyspaceName, type);
    else
        return userType.copy(frozen);
}
Also used : DriverInternalError(com.datastax.driver.core.exceptions.DriverInternalError) ArrayList(java.util.ArrayList) DataType(com.datastax.driver.core.DataType) UnresolvedUserTypeException(com.datastax.driver.core.exceptions.UnresolvedUserTypeException)

Aggregations

DataType (com.datastax.driver.core.DataType)1 DriverInternalError (com.datastax.driver.core.exceptions.DriverInternalError)1 UnresolvedUserTypeException (com.datastax.driver.core.exceptions.UnresolvedUserTypeException)1 ArrayList (java.util.ArrayList)1