Search in sources :

Example 1 with TypedCQLLiteral

use of com.datastax.oss.dsbulk.mapping.TypedCQLLiteral in project dsbulk by datastax.

the class SchemaSettings method appendWriteTimeAndTTL.

private void appendWriteTimeAndTTL(StringBuilder sb, @Nullable CQLFragment writetime, @Nullable CQLFragment ttl) {
    boolean hasTtl = ttlSeconds != -1 || ttl != null;
    boolean hasTimestamp = timestampMicros != -1 || writetime != null;
    if (hasTtl || hasTimestamp) {
        if (isCounterTable()) {
            throw new IllegalArgumentException("Cannot set TTL or timestamp when updating a counter table.");
        }
        sb.append(" USING ");
        if (hasTtl) {
            sb.append("TTL ");
            if (ttlSeconds != -1) {
                sb.append(ttlSeconds);
            } else {
                assert ttl != null;
                sb.append(ttl.render(NAMED_ASSIGNMENT));
            }
            if (hasTimestamp) {
                sb.append(" AND ");
            }
        }
        if (hasTimestamp) {
            sb.append("TIMESTAMP ");
            if (timestampMicros != -1) {
                sb.append(timestampMicros);
            } else {
                assert writetime != null;
                if (writetime instanceof TypedCQLLiteral) {
                    DataType dataType = ((TypedCQLLiteral) writetime).getDataType();
                    if (dataType == DataTypes.TIMESTAMP) {
                        ConvertingCodec<String, Instant> codec = codecFactory.createConvertingCodec(DataTypes.TIMESTAMP, GenericType.STRING, true);
                        String literal = ((CQLLiteral) writetime).getLiteral();
                        Instant i = codec.externalToInternal(codec.parse(literal));
                        long micros = instantToNumber(i, MICROSECONDS, EPOCH);
                        writetime = new TypedCQLLiteral(Long.toString(micros), DataTypes.BIGINT);
                    }
                }
                sb.append(writetime.render(NAMED_ASSIGNMENT));
            }
        }
    }
}
Also used : CQLLiteral(com.datastax.oss.dsbulk.mapping.CQLLiteral) TypedCQLLiteral(com.datastax.oss.dsbulk.mapping.TypedCQLLiteral) Instant(java.time.Instant) TypedCQLLiteral(com.datastax.oss.dsbulk.mapping.TypedCQLLiteral) DataType(com.datastax.oss.driver.api.core.type.DataType)

Aggregations

DataType (com.datastax.oss.driver.api.core.type.DataType)1 CQLLiteral (com.datastax.oss.dsbulk.mapping.CQLLiteral)1 TypedCQLLiteral (com.datastax.oss.dsbulk.mapping.TypedCQLLiteral)1 Instant (java.time.Instant)1