/** * Creates a new {@link SimpleCQLStatementMapperBuilder} instance. * @param builtStatement a query built statements */ public SimpleCQLStatementMapperBuilder(BuiltStatement builtStatement) { this.queryString = builtStatement.getQueryString(); }
@Override public String getQueryString(CodecRegistry codecRegistry) { return statement.getQueryString(codecRegistry); }
@Override public String toString() { try { if (forceNoValues) return getQueryString(); // 1) try first with all values inlined (will not work if some values require custom codecs, // or if the required codecs are registered in a different CodecRegistry instance than the // default one) return maybeAddSemicolon(buildQueryString(null, CodecRegistry.DEFAULT_INSTANCE)).toString(); } catch (RuntimeException e1) { // 2) try next with bind markers for all values to avoid usage of custom codecs try { return maybeAddSemicolon( buildQueryString(new ArrayList<Object>(), CodecRegistry.DEFAULT_INSTANCE)) .toString(); } catch (RuntimeException e2) { // Ugly but we have absolutely no context to get the registry from return String.format( "built query (could not generate with default codec registry: %s)", e2.getMessage()); } } }
@Test(groups = "unit") public void should_serialize_collections_of_serializable_elements() { Set<UUID> set = Sets.newHashSet(UUID.randomUUID()); List<Date> list = Lists.newArrayList(new Date()); Map<BigInteger, String> map = ImmutableMap.of(new BigInteger("1"), "foo"); BuiltStatement query = insertInto("foo").value("v", set); assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES (?);"); assertThat(query.getObject(0)).isEqualTo(set); query = insertInto("foo").value("v", list); assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES (?);"); assertThat(query.getObject(0)).isEqualTo(list); query = insertInto("foo").value("v", map); assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES (?);"); assertThat(query.getObject(0)).isEqualTo(map); }
@Test(groups = "unit") public void should_infer_for_built_statement() { for (BuiltStatement statement : idempotentBuiltStatements()) assertThat(statement.isIdempotent()).as(statement.getQueryString()).isTrue(); for (BuiltStatement statement : nonIdempotentBuiltStatements()) assertThat(statement.isIdempotent()).as(statement.getQueryString()).isFalse(); }
@Test(groups = "unit") public void should_not_attempt_to_serialize_function_calls_in_collections() { BuiltStatement query = insertInto("foo").value("v", Sets.newHashSet(fcall("func", 1))); assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({func(1)});"); assertThat(query.getValues(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE)) .isNullOrEmpty(); }
@Test(groups = "unit") public void should_not_attempt_to_serialize_raw_values_in_collections() { BuiltStatement query = insertInto("foo").value("v", ImmutableMap.of(1, raw("x"))); assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({1:x});"); assertThat(query.getValues(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE)) .isNullOrEmpty(); }
@Test(groups = "unit") public void should_not_attempt_to_serialize_collections_containing_numbers() { BuiltStatement query; // lists List<Integer> list = Lists.newArrayList(1, 2, 3); query = insertInto("foo").value("v", list); assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ([1,2,3]);"); assertThat(query.hasValues()).isFalse(); // sets Set<Integer> set = Sets.newHashSet(1, 2, 3); query = insertInto("foo").value("v", set); assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({1,2,3});"); assertThat(query.hasValues()).isFalse(); // maps Map<Integer, Float> map = ImmutableMap.of(1, 12.34f); query = insertInto("foo").value("v", map); assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({1:12.34});"); assertThat(query.hasValues()).isFalse(); }
@Test(groups = "unit") public void should_not_attempt_to_serialize_bind_markers_in_collections() { BuiltStatement query = insertInto("foo").value("v", Lists.newArrayList(1, 2, bindMarker())); assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ([1,2,?]);"); assertThat(query.getValues(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE)) .isNullOrEmpty(); }
/** * Creates a new {@link SimpleCQLStatementMapperBuilder} instance. * @param builtStatement a query built statements */ public SimpleCQLStatementMapperBuilder(BuiltStatement builtStatement) { this.queryString = builtStatement.getQueryString(); }
@Override public String getQueryString(CodecRegistry codecRegistry) { return statement.getQueryString(codecRegistry); }
@Override public String getQueryString(CodecRegistry codecRegistry) { return statement.getQueryString(codecRegistry); }
@Test(groups = "short") public void prepareTest() throws Exception { // Just check we correctly avoid values when there is a bind marker String query = "INSERT INTO foo (a,b,c,d) VALUES ('foo','bar',?,0);"; BuiltStatement stmt = insertInto("foo") .value("a", "foo") .value("b", "bar") .value("c", bindMarker()) .value("d", 0); assertEquals(stmt.getQueryString(), query); query = "INSERT INTO foo (a,b,c,d) VALUES ('foo','bar',:c,0);"; stmt = insertInto("foo") .value("a", "foo") .value("b", "bar") .value("c", bindMarker("c")) .value("d", 0); assertEquals(stmt.getQueryString(), query); }
public PreparedStatement apply(PreparedStatement preparedStatment) { preparedStatementCache.put(statement.getQueryString(), preparedStatment); return preparedStatment; } };
@Override public String toString() { if (forceNoValues) return getQueryString(); return maybeAddSemicolon(buildQueryString(null)).toString(); }
public static BoundStatement prepareBoundStatement(Object id, EntityTypeMetadata emeta, BuiltStatement stmt, List<String> pkCols, Session session) { // bind parameters Object[] values = emeta.getIdValues(id).toArray(new Object[pkCols.size()]); String q = stmt.getQueryString(); PreparedStatement ps = getOrPrepareStatement(session, stmt, q); return ps.bind(values); }
@Test(groups = "unit") public void should_infer_for_built_statement() { for (BuiltStatement statement : idempotentBuiltStatements()) assertThat(statement.isIdempotent()).as(statement.getQueryString()).isTrue(); for (BuiltStatement statement : nonIdempotentBuiltStatements()) assertThat(statement.isIdempotent()).as(statement.getQueryString()).isFalse(); }
@Test(groups = "unit") public void should_not_attempt_to_serialize_function_calls_in_collections() { BuiltStatement query = insertInto("foo").value("v", Sets.newHashSet(fcall("func", 1))); assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({func(1)});"); assertThat(query.getValues(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE)) .isNullOrEmpty(); }
@Test(groups = "unit") public void should_not_attempt_to_serialize_raw_values_in_collections() { BuiltStatement query = insertInto("foo").value("v", ImmutableMap.of(1, raw("x"))); assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({1:x});"); assertThat(query.getValues(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE)) .isNullOrEmpty(); }
@Test(groups = "unit") public void should_not_attempt_to_serialize_bind_markers_in_collections() { BuiltStatement query = insertInto("foo").value("v", Lists.newArrayList(1, 2, bindMarker())); assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ([1,2,?]);"); assertThat(query.getValues(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE)) .isNullOrEmpty(); }