use of com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry in project java-driver by datastax.
the class UdtCodec method format.
@NonNull
@Override
public String format(@Nullable UdtValue value) {
if (value == null) {
return "NULL";
}
CodecRegistry registry = cqlType.getAttachmentPoint().getCodecRegistry();
StringBuilder sb = new StringBuilder("{");
int size = cqlType.getFieldTypes().size();
boolean first = true;
for (int i = 0; i < size; i++) {
if (first) {
first = false;
} else {
sb.append(",");
}
CqlIdentifier elementName = cqlType.getFieldNames().get(i);
sb.append(elementName.asCql(true));
sb.append(":");
DataType elementType = cqlType.getFieldTypes().get(i);
TypeCodec<Object> codec = registry.codecFor(elementType);
sb.append(codec.format(value.get(i, codec)));
}
sb.append("}");
return sb.toString();
}
use of com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry 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.codec.registry.CodecRegistry in project java-driver by datastax.
the class UdtCodec method parse.
@Nullable
@Override
public UdtValue parse(@Nullable String value) {
if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) {
return null;
}
UdtValue udt = cqlType.newValue();
int length = value.length();
int position = ParseUtils.skipSpaces(value, 0);
if (value.charAt(position) != '{') {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\" at character %d: expecting '{' but got '%c'", value, position, value.charAt(position)));
}
position++;
position = ParseUtils.skipSpaces(value, position);
if (position == length) {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\" at character %d: expecting CQL identifier or '}', got EOF", value, position));
}
CodecRegistry registry = cqlType.getAttachmentPoint().getCodecRegistry();
CqlIdentifier id = null;
while (position < length) {
if (value.charAt(position) == '}') {
position = ParseUtils.skipSpaces(value, position + 1);
if (position == length) {
return udt;
}
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", at character %d expecting EOF or blank, but got \"%s\"", value, position, value.substring(position)));
}
int n;
try {
n = ParseUtils.skipCQLId(value, position);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", cannot parse a CQL identifier at character %d", value, position), e);
}
id = CqlIdentifier.fromInternal(value.substring(position, n));
position = n;
if (!cqlType.contains(id)) {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", unknown CQL identifier at character %d: \"%s\"", value, position, id));
}
position = ParseUtils.skipSpaces(value, position);
if (position == length) {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", at field %s (character %d) expecting ':', but got EOF", value, id, position));
}
if (value.charAt(position) != ':') {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", at field %s (character %d) expecting ':', but got '%c'", value, id, position, value.charAt(position)));
}
position++;
position = ParseUtils.skipSpaces(value, position);
try {
n = ParseUtils.skipCQLValue(value, position);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", invalid CQL value at field %s (character %d)", value, id, position), e);
}
String fieldValue = value.substring(position, n);
// This works because ids occur at most once in UDTs
DataType fieldType = cqlType.getFieldTypes().get(cqlType.firstIndexOf(id));
TypeCodec<Object> codec = registry.codecFor(fieldType);
Object parsed;
try {
parsed = codec.parse(fieldValue);
} catch (Exception e) {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", invalid CQL value at field %s (character %d): %s", value, id, position, e.getMessage()), e);
}
udt = udt.set(id, parsed, codec);
position = n;
position = ParseUtils.skipSpaces(value, position);
if (position == length) {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", at field %s (character %d) expecting ',' or '}', but got EOF", value, id, position));
}
if (value.charAt(position) == '}') {
continue;
}
if (value.charAt(position) != ',') {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", at field %s (character %d) expecting ',' but got '%c'", value, id, position, value.charAt(position)));
}
// skip ','
++position;
position = ParseUtils.skipSpaces(value, position);
}
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\" at field %s (character %d): expecting CQL identifier or '}', got EOF", value, id, position));
}
use of com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry in project java-driver by datastax.
the class TupleCodec method format.
@NonNull
@Override
public String format(@Nullable TupleValue value) {
if (value == null) {
return "NULL";
}
if (!value.getType().equals(cqlType)) {
throw new IllegalArgumentException(String.format("Invalid tuple type, expected %s but got %s", cqlType, value.getType()));
}
CodecRegistry registry = cqlType.getAttachmentPoint().getCodecRegistry();
StringBuilder sb = new StringBuilder("(");
boolean first = true;
for (int i = 0; i < value.size(); i++) {
if (first) {
first = false;
} else {
sb.append(",");
}
DataType elementType = cqlType.getComponentTypes().get(i);
TypeCodec<Object> codec = registry.codecFor(elementType);
sb.append(codec.format(value.get(i, codec)));
}
sb.append(")");
return sb.toString();
}
use of com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry in project java-driver by datastax.
the class TupleCodec method parse.
@Nullable
@Override
public TupleValue parse(@Nullable String value) {
if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) {
return null;
}
TupleValue tuple = cqlType.newValue();
int length = value.length();
int position = ParseUtils.skipSpaces(value, 0);
if (value.charAt(position) != '(') {
throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", at character %d expecting '(' but got '%c'", value, position, value.charAt(position)));
}
position++;
position = ParseUtils.skipSpaces(value, position);
CodecRegistry registry = cqlType.getAttachmentPoint().getCodecRegistry();
int field = 0;
while (position < length) {
if (value.charAt(position) == ')') {
position = ParseUtils.skipSpaces(value, position + 1);
if (position == length) {
return tuple;
}
throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", at character %d expecting EOF or blank, but got \"%s\"", value, position, value.substring(position)));
}
int n;
try {
n = ParseUtils.skipCQLValue(value, position);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", invalid CQL value at field %d (character %d)", value, field, position), e);
}
String fieldValue = value.substring(position, n);
DataType elementType = cqlType.getComponentTypes().get(field);
TypeCodec<Object> codec = registry.codecFor(elementType);
Object parsed;
try {
parsed = codec.parse(fieldValue);
} catch (Exception e) {
throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", invalid CQL value at field %d (character %d): %s", value, field, position, e.getMessage()), e);
}
tuple = tuple.set(field, parsed, codec);
position = n;
position = ParseUtils.skipSpaces(value, position);
if (position == length) {
throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", at field %d (character %d) expecting ',' or ')', but got EOF", value, field, position));
}
if (value.charAt(position) == ')') {
continue;
}
if (value.charAt(position) != ',') {
throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", at field %d (character %d) expecting ',' but got '%c'", value, field, position, value.charAt(position)));
}
// skip ','
++position;
position = ParseUtils.skipSpaces(value, position);
field += 1;
}
throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", at field %d (character %d) expecting CQL value or ')', got EOF", value, field, position));
}
Aggregations