com.datastax.driver.core.TypeCodec.format()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(10.3k)|赞(0)|评价(0)|浏览(128)

本文整理了Java中com.datastax.driver.core.TypeCodec.format()方法的一些代码示例,展示了TypeCodec.format()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TypeCodec.format()方法的具体详情如下:
包路径:com.datastax.driver.core.TypeCodec
类名称:TypeCodec
方法名:format

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:

  1. in the query builder, when values are inlined in the query string (see com.datastax.driver.core.querybuilder.BuiltStatement for a detailed explanation of when this happens);
  2. in the QueryLogger, if parameter logging is enabled;
  3. to format the INITCOND in AggregateMetadata#asCQLQuery(boolean);
  4. in the toString() implementation of some objects ( UDTValue, TupleValue, and the internal representation of a ROWS response), which may appear in driver logs.
    If you choose not to implement this method, you should not throw an exception but instead return a constant string (for example "XxxCodec.format not implemented").
    [中]根据此编解码器处理的CQL类型,将给定值格式化为有效的CQL文本。
    在适用的情况下,实现者应该注意引用和转义得到的CQL文本。应接受空值;在大多数情况下,对于空输入,实现应该返回CQL关键字“NULL”。
    实施这种方法并不是严格强制的。它用于:
    1.在查询生成器中,当值内联到查询字符串中时(请参阅com.datastax.driver.core.querybuilder.BuiltStatement以了解发生这种情况的详细说明);
    1.在QueryLogger中,如果参数记录已启用;
    1.在AggregateMetadata#asCQLQuery(布尔)中格式化INITCOND;
    1.在某些对象(UDTValue、TupleValue和行响应的内部表示)的toString()实现中,这些对象可能会出现在驱动程序日志中。
    如果选择不实现此方法,则不应引发异常,而应返回常量字符串(例如“XxxCodec.format not implemented”)。

代码示例

代码示例来源: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)));
 }
}

相关文章