@Override public ByteBuffer[] getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { return statement.getValues(protocolVersion, codecRegistry); }
@Override public ByteBuffer[] getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { // If there is some non-BuiltStatement inside the batch with values, we shouldn't // use super.getValues() since it will ignore the values of said non-BuiltStatement. // If that's the case, we just collects all those values (and we know // super.getValues() == null in that case since we've explicitely set this.hasBindMarker // to true). Otherwise, we simply call super.getValues(). if (nonBuiltStatementValues == 0) return super.getValues(protocolVersion, codecRegistry); ByteBuffer[] values = new ByteBuffer[nonBuiltStatementValues]; int i = 0; for (RegularStatement statement : statements) { if (statement instanceof BuiltStatement) continue; ByteBuffer[] statementValues = statement.getValues(protocolVersion, codecRegistry); System.arraycopy(statementValues, 0, values, i, statementValues.length); i += statementValues.length; } return values; }
@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(); }
@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_include_original_cause_when_arguments_invalid() { // Collection elements in protocol v2 must be at most 65535 bytes ByteBuffer bb = ByteBuffer.allocate(65536); // too big List<ByteBuffer> value = Lists.newArrayList(bb); BuiltStatement s = insertInto("foo").value("l", value); try { s.getValues(ProtocolVersion.V2, CodecRegistry.DEFAULT_INSTANCE); fail("Expected an IllegalArgumentException"); } catch (InvalidTypeException e) { assertThat(e.getCause()).isInstanceOf(IllegalArgumentException.class); StringWriter writer = new StringWriter(); e.getCause().printStackTrace(new PrintWriter(writer)); String stackTrace = writer.toString(); assertThat(stackTrace) .contains( "Native protocol version 2 supports only elements with size up to 65535 bytes - " + "but element size is 65536 bytes"); } }
@Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) public void should_fail_if_built_statement_has_too_many_values() { List<Object> values = Collections.<Object>nCopies(65535, "a"); // If the excessive count results from successive DSL calls, we don't check it on the fly so // this statement works: BuiltStatement statement = select().all().from("foo").where(eq("bar", "a")).and(in("baz", values.toArray())); // But we still want to check it client-side, to fail fast instead of sending a bad query to // Cassandra. // getValues() is called on any RegularStatement before we send it (see // SessionManager.makeRequestMessage). statement.getValues(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE); }
@Override public ByteBuffer[] getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { return statement.getValues(protocolVersion, codecRegistry); }
@Override public ByteBuffer[] getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { return statement.getValues(protocolVersion, codecRegistry); }
@Override public ByteBuffer[] getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { return statement.getValues(protocolVersion, codecRegistry); }
@Override public ByteBuffer[] getValues() { return statement.getValues(); }
@Override public ByteBuffer[] getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { // If there is some non-BuiltStatement inside the batch with values, we shouldn't // use super.getValues() since it will ignore the values of said non-BuiltStatement. // If that's the case, we just collects all those values (and we know // super.getValues() == null in that case since we've explicitely set this.hasBindMarker // to true). Otherwise, we simply call super.getValues(). if (nonBuiltStatementValues == 0) return super.getValues(protocolVersion, codecRegistry); ByteBuffer[] values = new ByteBuffer[nonBuiltStatementValues]; int i = 0; for (RegularStatement statement : statements) { if (statement instanceof BuiltStatement) continue; ByteBuffer[] statementValues = statement.getValues(protocolVersion, codecRegistry); System.arraycopy(statementValues, 0, values, i, statementValues.length); i += statementValues.length; } return values; }
@Override public ByteBuffer[] getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { // If there is some non-BuiltStatement inside the batch with values, we shouldn't // use super.getValues() since it will ignore the values of said non-BuiltStatement. // If that's the case, we just collects all those values (and we know // super.getValues() == null in that case since we've explicitely set this.hasBindMarker // to true). Otherwise, we simply call super.getValues(). if (nonBuiltStatementValues == 0) return super.getValues(protocolVersion, codecRegistry); ByteBuffer[] values = new ByteBuffer[nonBuiltStatementValues]; int i = 0; for (RegularStatement statement : statements) { if (statement instanceof BuiltStatement) continue; ByteBuffer[] statementValues = statement.getValues(protocolVersion, codecRegistry); System.arraycopy(statementValues, 0, values, i, statementValues.length); i += statementValues.length; } return values; }
@Override public ByteBuffer[] getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { // If there is some non-BuiltStatement inside the batch with values, we shouldn't // use super.getValues() since it will ignore the values of said non-BuiltStatement. // If that's the case, we just collects all those values (and we know // super.getValues() == null in that case since we've explicitely set this.hasBindMarker // to true). Otherwise, we simply call super.getValues(). if (nonBuiltStatementValues == 0) return super.getValues(protocolVersion, codecRegistry); ByteBuffer[] values = new ByteBuffer[nonBuiltStatementValues]; int i = 0; for (RegularStatement statement : statements) { if (statement instanceof BuiltStatement) continue; ByteBuffer[] statementValues = statement.getValues(protocolVersion, codecRegistry); System.arraycopy(statementValues, 0, values, i, statementValues.length); i += statementValues.length; } return values; }
@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_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(); }
@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_include_original_cause_when_arguments_invalid() { // Collection elements in protocol v2 must be at most 65535 bytes ByteBuffer bb = ByteBuffer.allocate(65536); // too big List<ByteBuffer> value = Lists.newArrayList(bb); BuiltStatement s = insertInto("foo").value("l", value); try { s.getValues(ProtocolVersion.V2, CodecRegistry.DEFAULT_INSTANCE); fail("Expected an IllegalArgumentException"); } catch (InvalidTypeException e) { assertThat(e.getCause()).isInstanceOf(IllegalArgumentException.class); StringWriter writer = new StringWriter(); e.getCause().printStackTrace(new PrintWriter(writer)); String stackTrace = writer.toString(); assertThat(stackTrace) .contains( "Native protocol version 2 supports only elements with size up to 65535 bytes - " + "but element size is 65536 bytes"); } }
@Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) public void should_fail_if_built_statement_has_too_many_values() { List<Object> values = Collections.<Object>nCopies(65535, "a"); // If the excessive count results from successive DSL calls, we don't check it on the fly so // this statement works: BuiltStatement statement = select().all().from("foo").where(eq("bar", "a")).and(in("baz", values.toArray())); // But we still want to check it client-side, to fail fast instead of sending a bad query to // Cassandra. // getValues() is called on any RegularStatement before we send it (see // SessionManager.makeRequestMessage). statement.getValues(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE); }