Search in sources :

Example 1 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project thingsboard by thingsboard.

the class CassandraDbHelper method getColumnValue.

private static String getColumnValue(String column, String defaultValue, Row row) {
    int index = row.getColumnDefinitions().firstIndexOf(column);
    if (index > -1) {
        String str;
        DataType type = row.getColumnDefinitions().get(index).getType();
        try {
            if (row.isNull(index)) {
                return null;
            } else if (type.getProtocolCode() == ProtocolConstants.DataType.DOUBLE) {
                str = Double.valueOf(row.getDouble(index)).toString();
            } else if (type.getProtocolCode() == ProtocolConstants.DataType.INT) {
                str = Integer.valueOf(row.getInt(index)).toString();
            } else if (type.getProtocolCode() == ProtocolConstants.DataType.BIGINT) {
                str = Long.valueOf(row.getLong(index)).toString();
            } else if (type.getProtocolCode() == ProtocolConstants.DataType.UUID) {
                str = row.getUuid(index).toString();
            } else if (type.getProtocolCode() == ProtocolConstants.DataType.TIMEUUID) {
                str = row.getUuid(index).toString();
            } else if (type.getProtocolCode() == ProtocolConstants.DataType.FLOAT) {
                str = Float.valueOf(row.getFloat(index)).toString();
            } else if (type.getProtocolCode() == ProtocolConstants.DataType.TIMESTAMP) {
                str = "" + row.getInstant(index).toEpochMilli();
            } else if (type.getProtocolCode() == ProtocolConstants.DataType.BOOLEAN) {
                str = Boolean.valueOf(row.getBoolean(index)).toString();
            } else {
                str = row.getString(index);
            }
        } catch (Exception e) {
            str = "";
        }
        return str;
    } else {
        return defaultValue;
    }
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) IOException(java.io.IOException)

Example 2 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project thingsboard by thingsboard.

the class CassandraDbHelper method setColumnValue.

private static void setColumnValue(TableMetadata tableMetadata, String column, CSVRecord record, BoundStatementBuilder boundStatementBuilder) {
    String value = record.get(column);
    DataType type = tableMetadata.getColumn(column).get().getType();
    if (value == null) {
        boundStatementBuilder.setToNull(column);
    } else if (type.getProtocolCode() == ProtocolConstants.DataType.DOUBLE) {
        boundStatementBuilder.setDouble(column, Double.valueOf(value));
    } else if (type.getProtocolCode() == ProtocolConstants.DataType.INT) {
        boundStatementBuilder.setInt(column, Integer.valueOf(value));
    } else if (type.getProtocolCode() == ProtocolConstants.DataType.BIGINT) {
        boundStatementBuilder.setLong(column, Long.valueOf(value));
    } else if (type.getProtocolCode() == ProtocolConstants.DataType.UUID) {
        boundStatementBuilder.setUuid(column, UUID.fromString(value));
    } else if (type.getProtocolCode() == ProtocolConstants.DataType.TIMEUUID) {
        boundStatementBuilder.setUuid(column, UUID.fromString(value));
    } else if (type.getProtocolCode() == ProtocolConstants.DataType.FLOAT) {
        boundStatementBuilder.setFloat(column, Float.valueOf(value));
    } else if (type.getProtocolCode() == ProtocolConstants.DataType.TIMESTAMP) {
        boundStatementBuilder.setInstant(column, Instant.ofEpochMilli(Long.valueOf(value)));
    } else if (type.getProtocolCode() == ProtocolConstants.DataType.BOOLEAN) {
        boundStatementBuilder.setBoolean(column, Boolean.valueOf(value));
    } else {
        boundStatementBuilder.setString(column, value);
    }
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType)

Example 3 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project calcite by apache.

the class CassandraSchema method getRelDataType.

RelProtoDataType getRelDataType(String columnFamily, boolean view) {
    Map<CqlIdentifier, ColumnMetadata> columns;
    CqlIdentifier tableName = CqlIdentifier.fromInternal(columnFamily);
    if (view) {
        Optional<ViewMetadata> optionalViewMetadata = getKeyspace().getView(tableName);
        if (optionalViewMetadata.isPresent()) {
            columns = optionalViewMetadata.get().getColumns();
        } else {
            throw new IllegalStateException("Unknown view " + tableName + " in keyspace " + keyspace);
        }
    } else {
        Optional<TableMetadata> optionalTableMetadata = getKeyspace().getTable(tableName);
        if (optionalTableMetadata.isPresent()) {
            columns = optionalTableMetadata.get().getColumns();
        } else {
            throw new IllegalStateException("Unknown table " + tableName + " in keyspace " + keyspace);
        }
    }
    // Temporary type factory, just for the duration of this method. Allowable
    // because we're creating a proto-type, not a type; before being used, the
    // proto-type will be copied into a real type factory.
    final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
    for (ColumnMetadata column : columns.values()) {
        final DataType dataType = column.getType();
        final String columnName = column.getName().asInternal();
        if (dataType instanceof ListType) {
            SqlTypeName arrayInnerType = CQL_TO_SQL_TYPE.lookup(((ListType) dataType).getElementType());
            fieldInfo.add(columnName, typeFactory.createArrayType(typeFactory.createSqlType(arrayInnerType), -1)).nullable(true);
        } else if (dataType instanceof SetType) {
            SqlTypeName multiSetInnerType = CQL_TO_SQL_TYPE.lookup(((SetType) dataType).getElementType());
            fieldInfo.add(columnName, typeFactory.createMultisetType(typeFactory.createSqlType(multiSetInnerType), -1)).nullable(true);
        } else if (dataType instanceof MapType) {
            MapType columnType = (MapType) dataType;
            SqlTypeName keyType = CQL_TO_SQL_TYPE.lookup(columnType.getKeyType());
            SqlTypeName valueType = CQL_TO_SQL_TYPE.lookup(columnType.getValueType());
            fieldInfo.add(columnName, typeFactory.createMapType(typeFactory.createSqlType(keyType), typeFactory.createSqlType(valueType))).nullable(true);
        } else if (dataType instanceof TupleType) {
            List<DataType> typeArgs = ((TupleType) dataType).getComponentTypes();
            List<Map.Entry<String, RelDataType>> typesList = IntStream.range(0, typeArgs.size()).mapToObj(i -> new Pair<>(// 1 indexed (as ARRAY)
            Integer.toString(i + 1), typeFactory.createSqlType(CQL_TO_SQL_TYPE.lookup(typeArgs.get(i))))).collect(Collectors.toList());
            fieldInfo.add(columnName, typeFactory.createStructType(typesList)).nullable(true);
        } else {
            SqlTypeName typeName = CQL_TO_SQL_TYPE.lookup(dataType);
            fieldInfo.add(columnName, typeName).nullable(true);
        }
    }
    return RelDataTypeImpl.proto(fieldInfo.build());
}
Also used : TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) MapType(com.datastax.oss.driver.api.core.type.MapType) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) SetType(com.datastax.oss.driver.api.core.type.SetType) ListType(com.datastax.oss.driver.api.core.type.ListType) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) TupleType(com.datastax.oss.driver.api.core.type.TupleType) RelDataType(org.apache.calcite.rel.type.RelDataType) DataType(com.datastax.oss.driver.api.core.type.DataType) RelProtoDataType(org.apache.calcite.rel.type.RelProtoDataType) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ViewMetadata(com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata)

Example 4 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class DseAggregateMetadata method describe.

@NonNull
@Override
default String describe(boolean pretty) {
    // Easiest to just copy the OSS describe() method and add in DETERMINISTIC
    ScriptBuilder builder = new ScriptBuilder(pretty);
    builder.append("CREATE AGGREGATE ").append(getKeyspace()).append(".").append(getSignature().getName()).append("(");
    boolean first = true;
    for (int i = 0; i < getSignature().getParameterTypes().size(); i++) {
        if (first) {
            first = false;
        } else {
            builder.append(",");
        }
        DataType type = getSignature().getParameterTypes().get(i);
        builder.append(type.asCql(false, pretty));
    }
    builder.increaseIndent().append(")").newLine().append("SFUNC ").append(getStateFuncSignature().getName()).newLine().append("STYPE ").append(getStateType().asCql(false, pretty));
    if (getFinalFuncSignature().isPresent()) {
        builder.newLine().append("FINALFUNC ").append(getFinalFuncSignature().get().getName());
    }
    if (getInitCond().isPresent()) {
        Optional<String> formatInitCond = formatInitCond();
        assert formatInitCond.isPresent();
        builder.newLine().append("INITCOND ").append(formatInitCond.get());
    }
    // add DETERMINISTIC if present
    if (getDeterministic().orElse(false)) {
        builder.newLine().append("DETERMINISTIC");
    }
    return builder.append(";").build();
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) ScriptBuilder(com.datastax.oss.driver.internal.core.metadata.schema.ScriptBuilder) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 5 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class CachingCodecRegistry method createCodec.

// Try to create a codec when we haven't found it in the cache.
// Variant where the Java type is unknown.
@NonNull
protected TypeCodec<?> createCodec(@NonNull DataType cqlType) {
    if (cqlType instanceof ListType) {
        DataType elementType = ((ListType) cqlType).getElementType();
        TypeCodec<Object> elementCodec = codecFor(elementType);
        return TypeCodecs.listOf(elementCodec);
    } else if (cqlType instanceof SetType) {
        DataType elementType = ((SetType) cqlType).getElementType();
        TypeCodec<Object> elementCodec = codecFor(elementType);
        return TypeCodecs.setOf(elementCodec);
    } else if (cqlType instanceof MapType) {
        DataType keyType = ((MapType) cqlType).getKeyType();
        DataType valueType = ((MapType) cqlType).getValueType();
        TypeCodec<Object> keyCodec = codecFor(keyType);
        TypeCodec<Object> valueCodec = codecFor(valueType);
        return TypeCodecs.mapOf(keyCodec, valueCodec);
    } else if (cqlType instanceof TupleType) {
        return TypeCodecs.tupleOf((TupleType) cqlType);
    } else if (cqlType instanceof UserDefinedType) {
        return TypeCodecs.udtOf((UserDefinedType) cqlType);
    } else if (cqlType instanceof CustomType) {
        return TypeCodecs.custom(cqlType);
    }
    throw new CodecNotFoundException(cqlType, null);
}
Also used : CustomType(com.datastax.oss.driver.api.core.type.CustomType) SetType(com.datastax.oss.driver.api.core.type.SetType) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) ListType(com.datastax.oss.driver.api.core.type.ListType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) DataType(com.datastax.oss.driver.api.core.type.DataType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) MapType(com.datastax.oss.driver.api.core.type.MapType) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

DataType (com.datastax.oss.driver.api.core.type.DataType)73 NonNull (edu.umd.cs.findbugs.annotations.NonNull)21 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)19 SetType (com.datastax.oss.driver.api.core.type.SetType)10 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)10 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)10 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)9 CheckReturnValue (edu.umd.cs.findbugs.annotations.CheckReturnValue)9 ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)8 Map (java.util.Map)8 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)7 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)7 ListType (com.datastax.oss.driver.api.core.type.ListType)7 MapType (com.datastax.oss.driver.api.core.type.MapType)7 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)6 Nullable (edu.umd.cs.findbugs.annotations.Nullable)6 UUID (java.util.UUID)6 TupleType (com.datastax.oss.driver.api.core.type.TupleType)5 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)5 List (java.util.List)5