Search in sources :

Example 6 with InvalidTypeException

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

the class DataTypeTest method serializeDeserializeTest.

public void serializeDeserializeTest(ProtocolVersion version) {
    for (DataType dt : DataType.allPrimitiveTypes()) {
        if (exclude(dt))
            continue;
        Object value = TestUtils.getFixedValue(dt);
        TypeCodec<Object> codec = codecRegistry.codecFor(dt);
        assertEquals(codec.deserialize(codec.serialize(value, version), version), value);
    }
    TypeCodec<Long> codec = codecRegistry.codecFor(DataType.bigint());
    try {
        ByteBuffer badValue = ByteBuffer.allocate(4);
        codec.deserialize(badValue, version);
        fail("This should not have worked");
    } catch (InvalidTypeException e) {
    /* That's what we want */
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) InvalidTypeException(com.datastax.driver.core.exceptions.InvalidTypeException)

Example 7 with InvalidTypeException

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

the class SimpleStatement method convert.

/*
     * This method performs a best-effort heuristic to guess which codec to use.
     * Note that this is not particularly efficient as the codec registry needs to iterate over
     * the registered codecs until it finds a suitable one.
     */
private static ByteBuffer[] convert(Object[] values, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
    ByteBuffer[] serializedValues = new ByteBuffer[values.length];
    for (int i = 0; i < values.length; i++) {
        Object value = values[i];
        if (value == null) {
            // impossible to locate the right codec when object is null,
            // so forcing the result to null
            serializedValues[i] = null;
        } else {
            if (value instanceof Token) {
                // bypass CodecRegistry for Token instances
                serializedValues[i] = ((Token) value).serialize(protocolVersion);
            } else {
                try {
                    TypeCodec<Object> codec = codecRegistry.codecFor(value);
                    serializedValues[i] = codec.serialize(value, protocolVersion);
                } catch (Exception e) {
                    // Catch and rethrow to provide a more helpful error message (one that include which value is bad)
                    throw new InvalidTypeException(String.format("Value %d of type %s does not correspond to any CQL3 type", i, value.getClass()), e);
                }
            }
        }
    }
    return serializedValues;
}
Also used : ByteBuffer(java.nio.ByteBuffer) InvalidTypeException(com.datastax.driver.core.exceptions.InvalidTypeException) InvalidTypeException(com.datastax.driver.core.exceptions.InvalidTypeException)

Example 8 with InvalidTypeException

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

the class Utils method convert.

/**
 * Utility method to serialize user-provided values.
 * <p/>
 * This method is a copy of the one declared in {@link com.datastax.driver.core.SimpleStatement}, it was duplicated
 * to avoid having to make it public.
 * <p/>
 * It is useful in situations where there is no metadata available and the underlying CQL
 * type for the values is not known.
 * <p/>
 * This situation happens when a {@link com.datastax.driver.core.SimpleStatement}
 * or a {@link com.datastax.driver.core.querybuilder.BuiltStatement} (Query Builder) contain values;
 * in these places, the driver has no way to determine the right CQL type to use.
 * <p/>
 * This method performs a best-effort heuristic to guess which codec to use.
 * Note that this is not particularly efficient as the codec registry needs to iterate over
 * the registered codecs until it finds a suitable one.
 *
 * @param values          The values to convert.
 * @param protocolVersion The protocol version to use.
 * @param codecRegistry   The {@link CodecRegistry} to use.
 * @return The converted values.
 */
static ByteBuffer[] convert(Object[] values, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
    ByteBuffer[] serializedValues = new ByteBuffer[values.length];
    for (int i = 0; i < values.length; i++) {
        Object value = values[i];
        if (value == null) {
            // impossible to locate the right codec when object is null,
            // so forcing the result to null
            serializedValues[i] = null;
        } else {
            if (value instanceof Token) {
                // bypass CodecRegistry for Token instances
                serializedValues[i] = ((Token) value).serialize(protocolVersion);
            } else {
                try {
                    TypeCodec<Object> codec = codecRegistry.codecFor(value);
                    serializedValues[i] = codec.serialize(value, protocolVersion);
                } catch (Exception e) {
                    // Catch and rethrow to provide a more helpful error message (one that include which value is bad)
                    throw new InvalidTypeException(String.format("Value %d of type %s does not correspond to any CQL3 type", i, value.getClass()), e);
                }
            }
        }
    }
    return serializedValues;
}
Also used : ByteBuffer(java.nio.ByteBuffer) InvalidTypeException(com.datastax.driver.core.exceptions.InvalidTypeException) InvalidTypeException(com.datastax.driver.core.exceptions.InvalidTypeException)

Example 9 with InvalidTypeException

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

the class LocalDateCodec method parse.

@Override
public LocalDate parse(String value) {
    if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
        return null;
    // strip enclosing single quotes, if any
    if (isQuoted(value)) {
        value = unquote(value);
    }
    if (isLongLiteral(value)) {
        long raw;
        try {
            raw = parseLong(value);
        } catch (NumberFormatException e) {
            throw new InvalidTypeException(String.format("Cannot parse date value from \"%s\"", value));
        }
        int days;
        try {
            days = fromCqlDateToDaysSinceEpoch(raw);
        } catch (IllegalArgumentException e) {
            throw new InvalidTypeException(String.format("Cannot parse date value from \"%s\"", value));
        }
        return EPOCH.plusDays(days);
    }
    try {
        return LocalDate.parse(value, FORMATTER);
    } catch (RuntimeException e) {
        throw new InvalidTypeException(String.format("Cannot parse date value from \"%s\"", value));
    }
}
Also used : InvalidTypeException(com.datastax.driver.core.exceptions.InvalidTypeException)

Example 10 with InvalidTypeException

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

the class Jsr353JsonCodec method format.

@Override
public String format(JsonStructure value) throws InvalidTypeException {
    if (value == null)
        return "NULL";
    String json;
    StringWriter sw = new StringWriter();
    try {
        JsonWriter writer = writerFactory.createWriter(sw);
        writer.write(value);
        json = sw.toString();
    } catch (JsonException e) {
        throw new InvalidTypeException(e.getMessage(), e);
    } finally {
        try {
            sw.close();
        } catch (IOException e) {
            // cannot happen
            throw new InvalidTypeException(e.getMessage(), e);
        }
    }
    return ParseUtils.quote(json);
}
Also used : InvalidTypeException(com.datastax.driver.core.exceptions.InvalidTypeException)

Aggregations

InvalidTypeException (com.datastax.driver.core.exceptions.InvalidTypeException)14 ByteBuffer (java.nio.ByteBuffer)7 Map (java.util.Map)2 BoundStatement (com.datastax.driver.core.BoundStatement)1 PreparedStatement (com.datastax.driver.core.PreparedStatement)1 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)1 Session (com.datastax.driver.core.Session)1 NoHostAvailableException (com.datastax.driver.core.exceptions.NoHostAvailableException)1 QueryExecutionException (com.datastax.driver.core.exceptions.QueryExecutionException)1 QueryValidationException (com.datastax.driver.core.exceptions.QueryValidationException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 BufferUnderflowException (java.nio.BufferUnderflowException)1 Charset (java.nio.charset.Charset)1 HashMap (java.util.HashMap)1 TimeoutException (java.util.concurrent.TimeoutException)1 Matcher (java.util.regex.Matcher)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 ComponentLog (org.apache.nifi.logging.ComponentLog)1 ProcessSession (org.apache.nifi.processor.ProcessSession)1