use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class ViewParser method parseView.
public ViewMetadata parseView(AdminRow viewRow, CqlIdentifier keyspaceId, Map<CqlIdentifier, UserDefinedType> userTypes) {
// Cassandra 3.0 (no views in earlier versions):
// CREATE TABLE system_schema.views (
// keyspace_name text,
// view_name text,
// base_table_id uuid,
// base_table_name text,
// bloom_filter_fp_chance double,
// caching frozen<map<text, text>>,
// cdc boolean,
// comment text,
// compaction frozen<map<text, text>>,
// compression frozen<map<text, text>>,
// crc_check_chance double,
// dclocal_read_repair_chance double,
// default_time_to_live int,
// extensions frozen<map<text, blob>>,
// gc_grace_seconds int,
// id uuid,
// include_all_columns boolean,
// max_index_interval int,
// memtable_flush_period_in_ms int,
// min_index_interval int,
// read_repair_chance double,
// speculative_retry text,
// where_clause text,
// PRIMARY KEY (keyspace_name, view_name)
// ) WITH CLUSTERING ORDER BY (view_name ASC)
CqlIdentifier viewId = CqlIdentifier.fromInternal(viewRow.getString("view_name"));
UUID uuid = viewRow.getUuid("id");
CqlIdentifier baseTableId = CqlIdentifier.fromInternal(viewRow.getString("base_table_name"));
boolean includesAllColumns = MoreObjects.firstNonNull(viewRow.getBoolean("include_all_columns"), false);
String whereClause = viewRow.getString("where_clause");
List<RawColumn> rawColumns = RawColumn.toRawColumns(rows.columns().getOrDefault(keyspaceId, ImmutableMultimap.of()).get(viewId));
if (rawColumns.isEmpty()) {
LOG.warn("[{}] Processing VIEW refresh for {}.{} but found no matching rows, skipping", logPrefix, keyspaceId, viewId);
return null;
}
Collections.sort(rawColumns);
ImmutableMap.Builder<CqlIdentifier, ColumnMetadata> allColumnsBuilder = ImmutableMap.builder();
ImmutableList.Builder<ColumnMetadata> partitionKeyBuilder = ImmutableList.builder();
ImmutableMap.Builder<ColumnMetadata, ClusteringOrder> clusteringColumnsBuilder = ImmutableMap.builder();
for (RawColumn raw : rawColumns) {
DataType dataType = rows.dataTypeParser().parse(keyspaceId, raw.dataType, userTypes, context);
ColumnMetadata column = new DefaultColumnMetadata(keyspaceId, viewId, raw.name, dataType, raw.kind.equals(RawColumn.KIND_STATIC));
switch(raw.kind) {
case RawColumn.KIND_PARTITION_KEY:
partitionKeyBuilder.add(column);
break;
case RawColumn.KIND_CLUSTERING_COLUMN:
clusteringColumnsBuilder.put(column, raw.reversed ? ClusteringOrder.DESC : ClusteringOrder.ASC);
break;
default:
}
allColumnsBuilder.put(column.getName(), column);
}
Map<CqlIdentifier, Object> options;
try {
options = parseOptions(viewRow);
} catch (Exception e) {
// Options change the most often, so be especially lenient if anything goes wrong.
Loggers.warnWithException(LOG, "[{}] Error while parsing options for {}.{}, getOptions() will be empty", logPrefix, keyspaceId, viewId, e);
options = Collections.emptyMap();
}
return new DefaultViewMetadata(keyspaceId, viewId, baseTableId, includesAllColumns, whereClause, uuid, partitionKeyBuilder.build(), clusteringColumnsBuilder.build(), allColumnsBuilder.build(), options);
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class AggregateParser method parseAggregate.
public AggregateMetadata parseAggregate(AdminRow row, CqlIdentifier keyspaceId, Map<CqlIdentifier, UserDefinedType> userDefinedTypes) {
// Cassandra < 3.0:
// CREATE TABLE system.schema_aggregates (
// keyspace_name text,
// aggregate_name text,
// signature frozen<list<text>>,
// argument_types list<text>,
// final_func text,
// initcond blob,
// return_type text,
// state_func text,
// state_type text,
// PRIMARY KEY (keyspace_name, aggregate_name, signature)
// ) WITH CLUSTERING ORDER BY (aggregate_name ASC, signature ASC)
//
// Cassandra >= 3.0:
// CREATE TABLE system.schema_aggregates (
// keyspace_name text,
// aggregate_name text,
// argument_types frozen<list<text>>,
// final_func text,
// initcond text,
// return_type text,
// state_func text,
// state_type text,
// PRIMARY KEY (keyspace_name, aggregate_name, argument_types)
// ) WITH CLUSTERING ORDER BY (aggregate_name ASC, argument_types ASC)
String simpleName = row.getString("aggregate_name");
List<String> argumentTypes = row.getListOfString("argument_types");
FunctionSignature signature = new FunctionSignature(CqlIdentifier.fromInternal(simpleName), dataTypeParser.parse(keyspaceId, argumentTypes, userDefinedTypes, context));
DataType stateType = dataTypeParser.parse(keyspaceId, row.getString("state_type"), userDefinedTypes, context);
TypeCodec<Object> stateTypeCodec = context.getCodecRegistry().codecFor(stateType);
String stateFuncSimpleName = row.getString("state_func");
FunctionSignature stateFuncSignature = new FunctionSignature(CqlIdentifier.fromInternal(stateFuncSimpleName), ImmutableList.<DataType>builder().add(stateType).addAll(signature.getParameterTypes()).build());
String finalFuncSimpleName = row.getString("final_func");
FunctionSignature finalFuncSignature = (finalFuncSimpleName == null) ? null : new FunctionSignature(CqlIdentifier.fromInternal(finalFuncSimpleName), stateType);
DataType returnType = dataTypeParser.parse(keyspaceId, row.getString("return_type"), userDefinedTypes, context);
Object initCond;
if (row.isString("initcond")) {
// Cassandra 3
String initCondString = row.getString("initcond");
initCond = (initCondString == null) ? null : stateTypeCodec.parse(initCondString);
} else {
// Cassandra 2.2
ByteBuffer initCondBlob = row.getByteBuffer("initcond");
initCond = (initCondBlob == null) ? null : stateTypeCodec.decode(initCondBlob, context.getProtocolVersion());
}
return new DefaultAggregateMetadata(keyspaceId, signature, finalFuncSignature, initCond, returnType, stateFuncSignature, stateType, stateTypeCodec);
}
use of com.datastax.oss.driver.api.core.type.DataType in project jnosql-diana-driver by eclipse.
the class QueryUtils method getUdtValue.
private static Object getUdtValue(UserDefinedType userType, Iterable elements, DataType type) {
Collection<Object> udtValues = getCollectionUdt(type);
UdtValue udtValue = userType.newValue();
final List<String> udtNames = userType.getFieldNames().stream().map(CqlIdentifier::asInternal).collect(Collectors.toList());
for (Object object : elements) {
if (Column.class.isInstance(object)) {
Column column = Column.class.cast(object);
Object convert = ValueUtil.convert(column.getValue());
final int index = udtNames.indexOf(column.getName());
if (index < 0) {
throw new CommunicationException("This field has not been found: " + column.getName() + " the fields available are " + udtNames + " in the UDT type " + userType.getName().asCql(true) + " at the keyspace " + userType.getKeyspace());
}
DataType fieldType = userType.getFieldTypes().get(index);
TypeCodec<Object> objectTypeCodec = CodecRegistry.DEFAULT.codecFor(fieldType);
if (fieldType instanceof SetType) {
udtValue.set(getName(column), new HashSet<Object>((Collection<?>) convert), objectTypeCodec);
} else {
udtValue.set(getName(column), convert, objectTypeCodec);
}
} else if (Iterable.class.isInstance(object)) {
udtValues.add(getUdtValue(userType, Iterable.class.cast(Iterable.class.cast(object)), type));
}
}
if (udtValues.isEmpty()) {
return udtValue;
}
return udtValues;
}
use of com.datastax.oss.driver.api.core.type.DataType in project thingsboard by thingsboard.
the class AbstractBufferedRateExecutor method toStringWithValues.
private static String toStringWithValues(BoundStatement boundStatement, ProtocolVersion protocolVersion) {
CodecRegistry codecRegistry = boundStatement.codecRegistry();
PreparedStatement preparedStatement = boundStatement.getPreparedStatement();
String query = preparedStatement.getQuery();
ColumnDefinitions defs = preparedStatement.getVariableDefinitions();
int index = 0;
for (ColumnDefinition def : defs) {
DataType type = def.getType();
TypeCodec<Object> codec = codecRegistry.codecFor(type);
if (boundStatement.getBytesUnsafe(index) != null) {
Object value = codec.decode(boundStatement.getBytesUnsafe(index), protocolVersion);
String replacement = Matcher.quoteReplacement(codec.format(value));
query = query.replaceFirst("\\?", replacement);
}
index++;
}
return query;
}
use of com.datastax.oss.driver.api.core.type.DataType in project jnosql-diana-driver by eclipse.
the class CassandraConverter method get.
static Object get(ColumnDefinition definition, Row row) {
String name = definition.getName().asInternal();
final DataType type = definition.getType();
if (type instanceof UserDefinedType) {
return getUDT(definition, row.getUdtValue(name));
}
final TypeCodec<Object> codec = row.codecRegistry().codecFor(type);
return row.get(name, codec);
}
Aggregations