本文整理了Java中com.datastax.driver.core.TypeCodec.format()
方法的一些代码示例,展示了TypeCodec.format()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TypeCodec.format()
方法的具体详情如下:
包路径:com.datastax.driver.core.TypeCodec
类名称:TypeCodec
方法名:format
[英]Format the given value as a valid CQL literal according to the CQL type handled by this codec.
Implementors should take care of quoting and escaping the resulting CQL literal where applicable. Null values should be accepted; in most cases, implementations should return the CQL keyword "NULL" for null inputs.
Implementing this method is not strictly mandatory. It is used:
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Override
public String format(Map<K, V> value) {
if (value == null) return "NULL";
StringBuilder sb = new StringBuilder();
sb.append("{");
int i = 0;
for (Map.Entry<K, V> e : value.entrySet()) {
if (i++ != 0) sb.append(",");
sb.append(keyCodec.format(e.getKey()));
sb.append(":");
sb.append(valueCodec.format(e.getValue()));
}
sb.append("}");
return sb.toString();
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
private String formatInitCond() {
if (stateTypeCodec.accepts(initCond)) {
try {
return stateTypeCodec.format(initCond);
} catch (RuntimeException e) {
LOGGER.info("Failed to format INITCOND literal: {}", initCond);
}
}
return initCond.toString();
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Override
public String format(C value) {
if (value == null) return "NULL";
StringBuilder sb = new StringBuilder();
sb.append(getOpeningChar());
int i = 0;
for (E v : value) {
if (i++ != 0) sb.append(",");
sb.append(eltCodec.format(v));
}
sb.append(getClosingChar());
return sb.toString();
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
TypeCodec<Object> codec = getCodecRegistry().codecFor(definition);
sb.append(codec.format(this));
return sb.toString();
}
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
TypeCodec<Object> codec = getCodecRegistry().codecFor(type);
sb.append(codec.format(this));
return sb.toString();
}
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
private static void forceSchemaVersion(
Session session, InetAddress peerAddress, UUID schemaVersion) {
session.execute(
String.format(
"UPDATE system.peers SET schema_version = %s WHERE peer = %s",
TypeCodec.uuid().format(schemaVersion), TypeCodec.inet().format(peerAddress)));
}
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@SuppressWarnings("unchecked")
public TypeCodecAssert<T> cannotFormat(Object value) {
try {
actual.format((T) value);
fail("Should not have been able to format " + value + " with " + actual);
} catch (Exception e) {
// ok
}
return this;
}
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Override
public String format(NumberBox<T> value) throws InvalidTypeException {
return numberCodec.format(value.getNumber());
}
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Override
public String format(O value) throws InvalidTypeException {
return value == null ? null : innerCodec.format(serialize(value));
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Override
protected String formatField(TupleValue value, int index) {
DataType elementType = definition.getComponentTypes().get(index);
TypeCodec<Object> codec = definition.getCodecRegistry().codecFor(elementType);
return codec.format(value.get(index, codec.getJavaType()));
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Override
protected String formatField(UDTValue source, String fieldName) {
DataType elementType = definition.getFieldType(fieldName);
TypeCodec<Object> codec = definition.getCodecRegistry().codecFor(elementType);
return codec.format(source.get(fieldName, codec.getJavaType()));
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Test(groups = "unit")
public void parseFormatListTest() {
String toParse = "['Foo','Bar','Foo''bar']";
List<String> toFormat = Arrays.asList("Foo", "Bar", "Foo'bar");
DataType dt = DataType.list(DataType.text());
assertEquals(codecRegistry.codecFor(dt).parse(toParse), toFormat);
assertEquals(codecRegistry.codecFor(dt).format(toFormat), toParse);
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Test(groups = "unit")
public void formatNativeTest() {
for (DataType dt : DataType.allPrimitiveTypes()) {
if (exclude(dt)) continue;
for (TestValue value : primitiveTestValues(dt))
assertThat(codecRegistry.codecFor(dt).format(value.javaObject))
.as("Formatting a %s expecting %s", dt, value.cqlOutputString)
.isEqualTo(value.cqlOutputString);
}
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@SuppressWarnings("deprecation")
@Test(groups = "unit")
public void parseFormatTupleTest() {
String toParse = "(1,'foo',1.0)";
TupleType t =
new TupleType(
newArrayList(DataType.cint(), DataType.text(), DataType.cfloat()),
protocolVersion,
codecRegistry);
TupleValue toFormat = t.newValue(1, "foo", 1.0f);
assertEquals(codecRegistry.codecFor(t).parse(toParse), toFormat);
assertEquals(codecRegistry.codecFor(t).format(toFormat), toParse);
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
private void assertCodec(String input, Duration expected) {
// serialize + deserialize
ByteBuffer bytes = TypeCodec.duration().serialize(Duration.from(input), V4);
Duration actual = TypeCodec.duration().deserialize(bytes, V4);
assertThat(actual).isEqualTo(expected);
// format + parse
String format = TypeCodec.duration().format(Duration.from(input));
actual = TypeCodec.duration().parse(format);
assertThat(actual).isEqualTo(expected);
// parse alone
actual = TypeCodec.duration().parse(input);
assertThat(actual).isEqualTo(expected);
}
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@SuppressWarnings("serial")
@Test(groups = "unit")
public void parseFormatSetTest() {
String toParse = "{'Foo','Bar','Foo''bar'}";
Set<String> toFormat =
new LinkedHashSet<String>() {
{
add("Foo");
add("Bar");
add("Foo'bar");
}
};
DataType dt = DataType.set(DataType.text());
assertEquals(codecRegistry.codecFor(dt).parse(toParse), toFormat);
assertEquals(codecRegistry.codecFor(dt).format(toFormat), toParse);
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Test(groups = "short")
public void should_log_all_parameter_types_bound_statements() throws Exception {
// given
normal.setLevel(TRACE);
queryLogger = QueryLogger.builder().withMaxParameterValueLength(Integer.MAX_VALUE).build();
cluster().register(queryLogger);
// when
String query = "UPDATE test SET " + assignments + " WHERE pk = 42";
PreparedStatement ps = session().prepare(query);
BoundStatement bs = ps.bind(values.toArray());
session().execute(bs);
// then
String line = normalAppender.waitAndGet(10000);
assertThat(line).contains("Query completed normally").contains(ipOfNode(1)).contains(query);
CodecRegistry codecRegistry = cluster().getConfiguration().getCodecRegistry();
for (DataType type : dataTypes) {
TypeCodec<Object> codec = codecRegistry.codecFor(type);
assertThat(line).contains(codec.format(getFixedValue(type)));
}
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@SuppressWarnings("serial")
@Test(groups = "unit")
public void parseFormatMapTest() {
String toParse = "{'Foo':3,'Bar':42,'Foo''bar':-24}";
Map<String, Integer> toFormat =
new LinkedHashMap<String, Integer>() {
{
put("Foo", 3);
put("Bar", 42);
put("Foo'bar", -24);
}
};
DataType dt = DataType.map(DataType.text(), DataType.cint());
assertEquals(codecRegistry.codecFor(dt).parse(toParse), toFormat);
assertEquals(codecRegistry.codecFor(dt).format(toFormat), toParse);
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Test(groups = "short")
public void simpleValueTest() throws Exception {
TupleType t =
cluster().getMetadata().newTupleType(DataType.cint(), DataType.text(), DataType.cfloat());
TupleValue v = t.newValue();
v.setInt(0, 1);
v.setString(1, "a");
v.setFloat(2, 1.0f);
assertEquals(v.getType().getComponentTypes().size(), 3);
assertEquals(v.getType().getComponentTypes().get(0), DataType.cint());
assertEquals(v.getType().getComponentTypes().get(1), DataType.text());
assertEquals(v.getType().getComponentTypes().get(2), DataType.cfloat());
assertEquals(v.getInt(0), 1);
assertEquals(v.getString(1), "a");
assertEquals(v.getFloat(2), 1.0f);
assertEquals(TypeCodec.tuple(t).format(v), "(1,'a',1.0)");
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Test(groups = "short")
@CassandraVersion("2.0.0")
public void should_log_all_parameter_types_simple_statements() throws Exception {
// given
normal.setLevel(TRACE);
queryLogger = QueryLogger.builder().withMaxParameterValueLength(Integer.MAX_VALUE).build();
cluster().register(queryLogger);
// when
String query = "UPDATE test SET " + assignments + " WHERE pk = 42";
SimpleStatement ss = new SimpleStatement(query, values.toArray());
session().execute(ss);
// then
String line = normalAppender.waitAndGet(10000);
assertThat(line).contains("Query completed normally").contains(ipOfNode(1)).contains(query);
CodecRegistry codecRegistry = cluster().getConfiguration().getCodecRegistry();
for (DataType type : dataTypes) {
TypeCodec<Object> codec;
if (type.equals(DataType.time())) {
codec = codecRegistry.codecFor(DataType.bigint());
} else {
codec = codecRegistry.codecFor(type);
}
assertThat(line).contains(codec.format(getFixedValue(type)));
}
}
内容来源于网络,如有侵权,请联系作者删除!