Elffuss BraidHub

kikocis/java-driver public

JAVA-1040: SimpleStatement parameters support in QueryLogger.
Changeset01295193b436039c15e180fc1c24639492dda4d58bce77f9dcf8c767ca073ff2
Authortommy stendahl <tommy.stendahl@ericsson.com> · 2016-02-25 13:05 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-04-05 18:44 +02:00
Parents2adc07b50845

Browse the files at this changeset

5 files changed

modified changelog/README.md +1 −0
@@ -3,6 +3,7 @@
33 ### 3.0.1 (in progress)
44
55 - [bug] JAVA-1132: Executing bound statement with no variables results in exception with protocol v1.
6+- [improvement] JAVA-1040: SimpleStatement parameters support in QueryLogger.
67
78 Merged from 2.1 branch:
89
modified driver-core/src/main/java/com/datastax/driver/core/QueryLogger.java +87 −6
@@ -21,6 +21,7 @@
2121 import org.slf4j.LoggerFactory;
2222
2323 import java.nio.ByteBuffer;
24+import java.util.Iterator;
2425 import java.util.List;
2526
2627 import static java.util.concurrent.TimeUnit.NANOSECONDS;
@@ -148,8 +149,10 @@
148149 * within a configurable threshold in milliseconds.
149150 * <p/>
150151 * This logger is activated by setting its level to {@code DEBUG} or {@code TRACE}.
151- * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement},
152- * then the query parameters (if any) will be logged as well (names and actual values).
152+ * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement}
153+ * or a {@link SimpleStatement}, then the query parameters (if any) will be logged. For a {@link BoundStatement}
154+ * names and actual values are logged and for a {@link SimpleStatement} values are logged in positional order
155+ * and named values are logged with names and value.
153156 * <p/>
154157 * The name of this logger is {@code com.datastax.driver.core.QueryLogger.NORMAL}.
155158 */
@@ -160,8 +163,10 @@
160163 * but whose execution time exceeded a configurable threshold in milliseconds.
161164 * <p/>
162165 * This logger is activated by setting its level to {@code DEBUG} or {@code TRACE}.
163- * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement},
164- * then the query parameters (if any) will be logged as well (names and actual values).
166+ * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement}
167+ * or a {@link SimpleStatement}, then the query parameters (if any) will be logged. For a {@link BoundStatement}
168+ * names and actual values are logged and for a {@link SimpleStatement} values are logged in positional order
169+ * and named values are logged with names and value.
165170 * <p/>
166171 * The name of this logger is {@code com.datastax.driver.core.QueryLogger.SLOW}.
167172 */
@@ -171,8 +176,10 @@
171176 * The logger used to log unsuccessful queries, i.e., queries that did not complete normally and threw an exception.
172177 * <p/>
173178 * This logger is activated by setting its level to {@code DEBUG} or {@code TRACE}.
174- * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement},
175- * then the query parameters (if any) will be logged as well (names and actual values).
179+ * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement}
180+ * or a {@link SimpleStatement}, then the query parameters (if any) will be logged. For a {@link BoundStatement}
181+ * names and actual values are logged and for a {@link SimpleStatement} values are logged in positional order
182+ * and named values are logged with names and value.
176183 * Note this this logger will also print the full stack trace of the reported exception.
177184 * <p/>
178185 * The name of this logger is {@code com.datastax.driver.core.QueryLogger.ERROR}.
@@ -650,12 +657,16 @@
650657 StringBuilder params = new StringBuilder();
651658 if (statement instanceof BoundStatement) {
652659 appendParameters((BoundStatement) statement, params, maxLoggedParameters);
660+ } else if (statement instanceof SimpleStatement) {
661+ appendParameters((SimpleStatement) statement, params, maxLoggedParameters);
653662 } else if (statement instanceof BatchStatement) {
654663 BatchStatement batchStatement = (BatchStatement) statement;
655664 int remaining = maxLoggedParameters;
656665 for (Statement inner : batchStatement.getStatements()) {
657666 if (inner instanceof BoundStatement) {
658667 remaining = appendParameters((BoundStatement) inner, params, remaining);
668+ } else if (inner instanceof SimpleStatement) {
669+ remaining = appendParameters((SimpleStatement) inner, params, remaining);
659670 }
660671 }
661672 }
@@ -677,6 +688,9 @@
677688 } else if (statement instanceof BoundStatement) {
678689 int boundValues = ((BoundStatement) statement).wrapper.values.length;
679690 sb.append("[" + boundValues + " bound values] ");
691+ } else if (statement instanceof SimpleStatement) {
692+ int boundValues = ((SimpleStatement) statement).valuesCount();
693+ sb.append("[" + boundValues + " bound values] ");
680694 }
681695
682696 append(statement, sb, maxQueryStringLength);
@@ -688,6 +702,8 @@
688702 for (Statement s : bs.getStatements()) {
689703 if (s instanceof BoundStatement)
690704 count += ((BoundStatement) s).wrapper.values.length;
705+ else if (s instanceof SimpleStatement)
706+ count += ((SimpleStatement) s).valuesCount();
691707 }
692708 return count;
693709 }
@@ -755,6 +771,71 @@
755771 return valueStr;
756772 }
757773
774+ protected int appendParameters(SimpleStatement statement, StringBuilder buffer, int remaining) {
775+ if (remaining == 0)
776+ return 0;
777+ int numberOfParameters = statement.valuesCount();
778+ if (numberOfParameters > 0) {
779+ int numberOfLoggedParameters;
780+ if (remaining == -1) {
781+ numberOfLoggedParameters = numberOfParameters;
782+ } else {
783+ numberOfLoggedParameters = remaining > numberOfParameters ? numberOfParameters : remaining;
784+ remaining -= numberOfLoggedParameters;
785+ }
786+ Iterator<String> valueNames = null;
787+ if (statement.usesNamedValues()) {
788+ valueNames = statement.getValueNames().iterator();
789+ }
790+ for (int i = 0; i < numberOfLoggedParameters; i++) {
791+ if (buffer.length() == 0)
792+ buffer.append(" [");
793+ else
794+ buffer.append(", ");
795+ if (valueNames != null && valueNames.hasNext()) {
796+ String valueName = valueNames.next();
797+ buffer.append(String.format("%s:%s", valueName, parameterValueAsString(statement.getObject(valueName))));
798+ } else {
799+ buffer.append(parameterValueAsString(statement.getObject(i)));
800+ }
801+ }
802+ if (numberOfLoggedParameters < numberOfParameters) {
803+ buffer.append(FURTHER_PARAMS_OMITTED);
804+ }
805+ }
806+ return remaining;
807+ }
808+
809+ protected String parameterValueAsString(Object value) {
810+ String valueStr;
811+ if (value == null) {
812+ valueStr = "NULL";
813+ } else {
814+ CodecRegistry codecRegistry = cluster.getConfiguration().getCodecRegistry();
815+ TypeCodec<Object> codec = codecRegistry.codecFor(value);
816+ int maxParameterValueLength = this.maxParameterValueLength;
817+ if (codec.cqlType.equals(DataType.blob()) && maxParameterValueLength != -1) {
818+ // prevent large blobs from being converted to strings
819+ ByteBuffer buf = (ByteBuffer) value;
820+ int maxBufferLength = Math.max(2, (maxParameterValueLength - 2) / 2);
821+ boolean bufferTooLarge = buf.remaining() > maxBufferLength;
822+ if (bufferTooLarge) {
823+ value = (ByteBuffer) buf.duplicate().limit(maxBufferLength);
824+ }
825+ valueStr = codec.format(value);
826+ if (bufferTooLarge) {
827+ valueStr = valueStr + TRUNCATED_OUTPUT;
828+ }
829+ } else {
830+ valueStr = codec.format(value);
831+ if (maxParameterValueLength != -1 && valueStr.length() > maxParameterValueLength) {
832+ valueStr = valueStr.substring(0, maxParameterValueLength) + TRUNCATED_OUTPUT;
833+ }
834+ }
835+ }
836+ return valueStr;
837+ }
838+
758839 private ProtocolVersion protocolVersion() {
759840 // Since the QueryLogger can be registered before the Cluster was initialized, we can't retrieve
760841 // it at construction time. Cache it field at first use (a volatile field is good enough since we
modified driver-core/src/main/java/com/datastax/driver/core/SimpleStatement.java +35 −4
@@ -18,8 +18,10 @@
1818 import com.datastax.driver.core.exceptions.InvalidTypeException;
1919
2020 import java.nio.ByteBuffer;
21+import java.util.Collections;
2122 import java.util.HashMap;
2223 import java.util.Map;
24+import java.util.Set;
2325
2426 /**
2527 * A simple {@code RegularStatement} implementation built directly from a query
@@ -177,21 +179,50 @@
177179 }
178180
179181 /**
180- * Returns the {@code i}th value as the Java type matching its CQL type.
182+ * Returns the {@code i}th positional value as the Java type matching its CQL type.
183+ * <p/>
184+ * Note that, unlike with other driver types like {@link Row}, you can't retrieve named values by position. This
185+ * getter will throw an exception if the statement was created with named values (or no values at all). Call
186+ * {@link #usesNamedValues()} to check the type of values, and {@link #getObject(String)} if they are positional.
181187 *
182188 * @param i the index to retrieve.
183- * @return the value of the {@code i}th value of this statement.
184- * @throws IllegalStateException if this statement does not have values.
189+ * @return the {@code i}th value of this statement.
190+ * @throws IllegalStateException if this statement does not have positional values.
185191 * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object.
186192 */
187193 public Object getObject(int i) {
188194 if (values == null)
189- throw new IllegalStateException("This statement does not have values");
195+ throw new IllegalStateException("This statement does not have positional values");
190196 if (i < 0 || i >= values.length)
191197 throw new ArrayIndexOutOfBoundsException(i);
192198 return values[i];
193199 }
194200
201+ /**
202+ * Returns a named value as the Java type matching its CQL type.
203+ *
204+ * @param name the name of the value to retrieve.
205+ * @return the value that matches the name, or {@code null} if there is no such name.
206+ * @throws IllegalStateException if this statement does not have named values.
207+ */
208+ public Object getObject(String name) {
209+ if (namedValues == null)
210+ throw new IllegalStateException("This statement does not have named values");
211+ return namedValues.get(name);
212+ }
213+
214+ /**
215+ * Returns the names of the named values of this statement.
216+ *
217+ * @return the names of the named values of this statement.
218+ * @throws IllegalStateException if this statement does not have named values.
219+ */
220+ public Set<String> getValueNames() {
221+ if (namedValues == null)
222+ throw new IllegalStateException("This statement does not have named values");
223+ return Collections.unmodifiableSet(namedValues.keySet());
224+ }
225+
195226 /**
196227 * Returns the routing key for the query.
197228 * <p/>
modified driver-core/src/test/java/com/datastax/driver/core/QueryLoggerTest.java +304 −11
@@ -31,7 +31,9 @@
3131
3232 import java.nio.ByteBuffer;
3333 import java.util.ArrayList;
34+import java.util.HashMap;
3435 import java.util.List;
36+import java.util.Map;
3537
3638 import static com.datastax.driver.core.BatchStatement.Type.COUNTER;
3739 import static com.datastax.driver.core.BatchStatement.Type.UNLOGGED;
@@ -377,7 +379,7 @@
377379
378380 @Test(groups = "short")
379381 @CassandraVersion(major = 2.0)
380- public void should_log_non_null_named_parameter() throws Exception {
382+ public void should_log_non_null_named_parameter_bound_statements() throws Exception {
381383 // given
382384 normal.setLevel(TRACE);
383385 queryLogger = QueryLogger.builder()
@@ -403,7 +405,7 @@
403405 }
404406
405407 @Test(groups = "short")
406- public void should_log_non_null_positional_parameter() throws Exception {
408+ public void should_log_non_null_positional_parameter_bound_statements() throws Exception {
407409 // given
408410 normal.setLevel(TRACE);
409411 queryLogger = QueryLogger.builder().build();
@@ -426,7 +428,28 @@
426428 }
427429
428430 @Test(groups = "short")
429- public void should_log_null_parameter() throws Exception {
431+ @CassandraVersion(major = 2.0)
432+ public void should_log_non_null_positional_parameter_simple_statements() throws Exception {
433+ // given
434+ normal.setLevel(TRACE);
435+ queryLogger = QueryLogger.builder().build();
436+ cluster().register(queryLogger);
437+ // when
438+ String query = "UPDATE test SET c_text = ? WHERE pk = ?";
439+ SimpleStatement ss = new SimpleStatement(query, "foo", 42);
440+ session().execute(ss);
441+ // then
442+ String line = normalAppender.waitAndGet(10000);
443+ assertThat(line)
444+ .contains("Query completed normally")
445+ .contains(ipOfNode(1))
446+ .contains(query)
447+ .contains("42")
448+ .contains("'foo'");
449+ }
450+
451+ @Test(groups = "short")
452+ public void should_log_null_parameter_bound_statements() throws Exception {
430453 // given
431454 normal.setLevel(TRACE);
432455 queryLogger = QueryLogger.builder().build();
@@ -448,6 +471,27 @@
448471 .contains("c_text:NULL");
449472 }
450473
474+ @Test(groups = "short")
475+ @CassandraVersion(major = 2.0)
476+ public void should_log_null_parameter_simple_statements() throws Exception {
477+ // given
478+ normal.setLevel(TRACE);
479+ queryLogger = QueryLogger.builder().build();
480+ cluster().register(queryLogger);
481+ // when
482+ String query = "UPDATE test SET c_text = ? WHERE pk = ?";
483+ SimpleStatement ss = new SimpleStatement(query, null, 42);
484+ session().execute(ss);
485+ // then
486+ String line = normalAppender.waitAndGet(10000);
487+ assertThat(line)
488+ .contains("Query completed normally")
489+ .contains(ipOfNode(1))
490+ .contains(query)
491+ .contains("42")
492+ .contains("NULL");
493+ }
494+
451495 @Test(groups = "short")
452496 @CassandraVersion(major = 3.0)
453497 public void should_log_unset_parameter() throws Exception {
@@ -497,10 +541,38 @@
497541 .contains("c_text:'foo'")
498542 .contains("c_int:12345");
499543 }
544+
545+ @Test(groups = "short")
546+ @CassandraVersion(major = 2.0)
547+ public void should_log_simple_statement_parameters_inside_batch_statement() throws Exception {
548+ // given
549+ normal.setLevel(TRACE);
550+ queryLogger = QueryLogger.builder().build();
551+ cluster().register(queryLogger);
552+ // when
553+ String query1 = "UPDATE test SET c_text = ? WHERE pk = ?";
554+ String query2 = "UPDATE test SET c_int = ? WHERE pk = ?";
555+ BatchStatement batch = new BatchStatement();
556+ batch.add(new SimpleStatement(query1, "foo", 42));
557+ batch.add(new SimpleStatement(query2, 12345, 43));
558+ session().execute(batch);
559+ // then
560+ String line = normalAppender.waitAndGet(10000);
561+ assertThat(line)
562+ .contains("Query completed normally")
563+ .contains(ipOfNode(1))
564+ .contains(query1)
565+ .contains(query2)
566+ .contains("42")
567+ .contains("43")
568+ .contains("'foo'")
569+ .contains("12345");
570+ }
571+
500572 // Test different CQL types
501573
502574 @Test(groups = "short")
503- public void should_log_all_parameter_types() throws Exception {
575+ public void should_log_all_parameter_types_bound_statements() throws Exception {
504576 // given
505577 normal.setLevel(TRACE);
506578 queryLogger = QueryLogger.builder()
@@ -525,6 +597,37 @@
525597 }
526598 }
527599
600+ @Test(groups = "short")
601+ @CassandraVersion(major = 2.0)
602+ public void should_log_all_parameter_types_simple_statements() throws Exception {
603+ // given
604+ normal.setLevel(TRACE);
605+ queryLogger = QueryLogger.builder()
606+ .withMaxParameterValueLength(Integer.MAX_VALUE)
607+ .build();
608+ cluster().register(queryLogger);
609+ // when
610+ String query = "UPDATE test SET " + assignments + " WHERE pk = 42";
611+ SimpleStatement ss = new SimpleStatement(query, values.toArray());
612+ session().execute(ss);
613+ // then
614+ String line = normalAppender.waitAndGet(10000);
615+ assertThat(line)
616+ .contains("Query completed normally")
617+ .contains(ipOfNode(1))
618+ .contains(query);
619+ CodecRegistry codecRegistry = cluster().getConfiguration().getCodecRegistry();
620+ for (DataType type : dataTypes) {
621+ TypeCodec<Object> codec;
622+ if (type.equals(DataType.time())) {
623+ codec = codecRegistry.codecFor(DataType.bigint());
624+ } else {
625+ codec = codecRegistry.codecFor(type);
626+ }
627+ assertThat(line).contains(codec.format(getFixedValue(type)));
628+ }
629+ }
630+
528631 // Tests for truncation of query strings and parameter values
529632
530633 @Test(groups = "short")
@@ -596,7 +699,7 @@
596699
597700 @CassandraVersion(major = 2.0)
598701 @Test(groups = "short")
599- public void should_truncate_parameter_when_max_length_exceeded() throws Exception {
702+ public void should_truncate_parameter_when_max_length_exceeded_bound_statements() throws Exception {
600703 // given
601704 normal.setLevel(TRACE);
602705 queryLogger = QueryLogger.builder()
@@ -619,8 +722,30 @@
619722 .doesNotContain("123456");
620723 }
621724
725+ @CassandraVersion(major = 2.0)
726+ @Test(groups = "short")
727+ public void should_truncate_parameter_when_max_length_exceeded_simple_statements() throws Exception {
728+ // given
729+ normal.setLevel(TRACE);
730+ queryLogger = QueryLogger.builder()
731+ .withMaxParameterValueLength(5)
732+ .build();
733+ cluster().register(queryLogger);
734+ // when
735+ String query = "UPDATE test SET c_int = ? WHERE pk = ?";
736+ SimpleStatement ss = new SimpleStatement(query, 123456, 42);
737+ session().execute(ss);
738+ // then
739+ String line = normalAppender.waitAndGet(10000);
740+ assertThat(line)
741+ .contains("Query completed normally")
742+ .contains(ipOfNode(1))
743+ .contains("12345" + TRUNCATED_OUTPUT)
744+ .doesNotContain("123456");
745+ }
746+
622747 @Test(groups = "short")
623- public void should_truncate_blob_parameter_when_max_length_exceeded() throws Exception {
748+ public void should_truncate_blob_parameter_when_max_length_exceeded_bound_statements() throws Exception {
624749 // given
625750 normal.setLevel(TRACE);
626751 queryLogger = QueryLogger.builder()
@@ -640,11 +765,33 @@
640765 .contains("Query completed normally")
641766 .contains(ipOfNode(1))
642767 .contains("c_blob:0x0102" + TRUNCATED_OUTPUT)
643- .doesNotContain("123456");
768+ .doesNotContain("0x010203");
769+ }
770+
771+ @Test(groups = "short")
772+ @CassandraVersion(major = 2.0)
773+ public void should_truncate_blob_parameter_when_max_length_exceeded_simple_statements() throws Exception {
774+ // given
775+ normal.setLevel(TRACE);
776+ queryLogger = QueryLogger.builder()
777+ .withMaxParameterValueLength(6)
778+ .build();
779+ cluster().register(queryLogger);
780+ // when
781+ String query = "UPDATE test SET c_blob = ? WHERE pk = ?";
782+ SimpleStatement ss = new SimpleStatement(query, ByteBuffer.wrap(Bytes.toArray(Lists.newArrayList(1, 2, 3))), 42);
783+ session().execute(ss);
784+ // then
785+ String line = normalAppender.waitAndGet(10000);
786+ assertThat(line)
787+ .contains("Query completed normally")
788+ .contains(ipOfNode(1))
789+ .contains("0x0102" + TRUNCATED_OUTPUT)
790+ .doesNotContain("0x010203");
644791 }
645792
646793 @Test(groups = "short")
647- public void should_not_truncate_parameter_when_max_length_unlimited() throws Exception {
794+ public void should_not_truncate_parameter_when_max_length_unlimited_bound_statements() throws Exception {
648795 // given
649796 normal.setLevel(TRACE);
650797 queryLogger = QueryLogger.builder()
@@ -668,7 +815,29 @@
668815 }
669816
670817 @Test(groups = "short")
671- public void should_not_log_exceeding_number_of_parameters() throws Exception {
818+ @CassandraVersion(major = 2.0)
819+ public void should_not_truncate_parameter_when_max_length_unlimited_simple_statements() throws Exception {
820+ // given
821+ normal.setLevel(TRACE);
822+ queryLogger = QueryLogger.builder()
823+ .withMaxParameterValueLength(-1)
824+ .build();
825+ cluster().register(queryLogger);
826+ // when
827+ String query = "UPDATE test SET c_int = ? WHERE pk = ?";
828+ SimpleStatement ss = new SimpleStatement(query, 123456, 42);
829+ session().execute(ss);
830+ // then
831+ String line = normalAppender.waitAndGet(10000);
832+ assertThat(line)
833+ .contains("Query completed normally")
834+ .contains(ipOfNode(1))
835+ .contains("123456")
836+ .doesNotContain(TRUNCATED_OUTPUT);
837+ }
838+
839+ @Test(groups = "short")
840+ public void should_not_log_exceeding_number_of_parameters_bound_statements() throws Exception {
672841 // given
673842 normal.setLevel(TRACE);
674843 queryLogger = QueryLogger.builder()
@@ -694,7 +863,56 @@
694863
695864 @Test(groups = "short")
696865 @CassandraVersion(major = 2.0)
697- public void should_not_log_exceeding_number_of_parameters_in_batch_statement() throws Exception {
866+ public void should_not_log_exceeding_number_of_parameters_simple_statements() throws Exception {
867+ // given
868+ normal.setLevel(TRACE);
869+ queryLogger = QueryLogger.builder()
870+ .withMaxLoggedParameters(1)
871+ .build();
872+ cluster().register(queryLogger);
873+ // when
874+ String query = "UPDATE test SET c_int = ? WHERE pk = ?";
875+ SimpleStatement ss = new SimpleStatement(query, 123456, 42);
876+ session().execute(ss);
877+ // then
878+ String line = normalAppender.waitAndGet(10000);
879+ assertThat(line)
880+ .contains("Query completed normally")
881+ .contains(ipOfNode(1))
882+ .contains("123456")
883+ .doesNotContain("123456, 42")
884+ .contains(FURTHER_PARAMS_OMITTED);
885+ }
886+
887+ @Test(groups = "short")
888+ @CassandraVersion(major = 2.1)
889+ public void should_not_log_exceeding_number_of_parameters_simple_statements_with_named_values() throws Exception {
890+ // given
891+ normal.setLevel(TRACE);
892+ queryLogger = QueryLogger.builder()
893+ .withMaxLoggedParameters(1)
894+ .build();
895+ cluster().register(queryLogger);
896+ // when
897+ String query = "UPDATE test SET c_int = :c_int WHERE pk = :pk";
898+ Map<String, Object> namedValues = new HashMap<String, Object>();
899+ namedValues.put("c_int", 123456);
900+ namedValues.put("pk", 42);
901+ SimpleStatement ss = new SimpleStatement(query, namedValues);
902+ session().execute(ss);
903+ // then
904+ String line = normalAppender.waitAndGet(10000);
905+ assertThat(line)
906+ .contains("Query completed normally")
907+ .contains(ipOfNode(1))
908+ .contains("pk:42")
909+ .doesNotContain("c_int:123456")
910+ .contains(FURTHER_PARAMS_OMITTED);
911+ }
912+
913+ @Test(groups = "short")
914+ @CassandraVersion(major = 2.0)
915+ public void should_not_log_exceeding_number_of_parameters_in_batch_statement_bound_statements() throws Exception {
698916 // given
699917 normal.setLevel(TRACE);
700918 queryLogger = QueryLogger.builder()
@@ -723,7 +941,35 @@
723941 }
724942
725943 @Test(groups = "short")
726- public void should_log_all_parameters_when_max_unlimited() throws Exception {
944+ @CassandraVersion(major = 2.0)
945+ public void should_not_log_exceeding_number_of_parameters_in_batch_statement_simple_statements() throws Exception {
946+ // given
947+ normal.setLevel(TRACE);
948+ queryLogger = QueryLogger.builder()
949+ .withMaxLoggedParameters(1)
950+ .build();
951+ cluster().register(queryLogger);
952+ // when
953+ String query1 = "UPDATE test SET c_text = ? WHERE pk = ?";
954+ String query2 = "UPDATE test SET c_int = ? WHERE pk = ?";
955+ BatchStatement batch = new BatchStatement();
956+ batch.add(new SimpleStatement(query1, "foo", 42));
957+ batch.add(new SimpleStatement(query2, 12345, 43));
958+ session().execute(batch);
959+ // then
960+ String line = normalAppender.waitAndGet(10000);
961+ assertThat(line)
962+ .contains("Query completed normally")
963+ .contains(ipOfNode(1))
964+ .contains(query1)
965+ .contains(query2)
966+ .contains("'foo'")
967+ .doesNotContain("42, 12345, 43")
968+ .contains(FURTHER_PARAMS_OMITTED);
969+ }
970+
971+ @Test(groups = "short")
972+ public void should_log_all_parameters_when_max_unlimited_bound_statements() throws Exception {
727973 // given
728974 normal.setLevel(TRACE);
729975 queryLogger = QueryLogger.builder()
@@ -746,6 +992,53 @@
746992 .contains("pk:42");
747993 }
748994
995+ @Test(groups = "short")
996+ @CassandraVersion(major = 2.0)
997+ public void should_log_all_parameters_when_max_unlimited_simple_statements() throws Exception {
998+ // given
999+ normal.setLevel(TRACE);
1000+ queryLogger = QueryLogger.builder()
1001+ .withMaxLoggedParameters(-1)
1002+ .build();
1003+ cluster().register(queryLogger);
1004+ // when
1005+ String query = "UPDATE test SET c_int = ? WHERE pk = ?";
1006+ SimpleStatement ss = new SimpleStatement(query, 123456, 42);
1007+ session().execute(ss);
1008+ // then
1009+ String line = normalAppender.waitAndGet(10000);
1010+ assertThat(line)
1011+ .contains("Query completed normally")
1012+ .contains(ipOfNode(1))
1013+ .contains("123456")
1014+ .contains("42");
1015+ }
1016+
1017+ @Test(groups = "short")
1018+ @CassandraVersion(major = 2.1)
1019+ public void should_log_all_parameters_when_max_unlimited_simple_statements_with_named_values() throws Exception {
1020+ // given
1021+ normal.setLevel(TRACE);
1022+ queryLogger = QueryLogger.builder()
1023+ .withMaxLoggedParameters(-1)
1024+ .build();
1025+ cluster().register(queryLogger);
1026+ // when
1027+ String query = "UPDATE test SET c_int = :c_int WHERE pk = :pk";
1028+ Map<String, Object> namedValues = new HashMap<String, Object>();
1029+ namedValues.put("c_int", 123456);
1030+ namedValues.put("pk", 42);
1031+ SimpleStatement ss = new SimpleStatement(query, namedValues);
1032+ session().execute(ss);
1033+ // then
1034+ String line = normalAppender.waitAndGet(10000);
1035+ assertThat(line)
1036+ .contains("Query completed normally")
1037+ .contains(ipOfNode(1))
1038+ .contains("123456")
1039+ .contains("42");
1040+ }
1041+
7491042 @Override
7501043 public void onTestContextInitialized() {
7511044 execute("CREATE TABLE test (pk int PRIMARY KEY, " + definitions + ")");
modified driver-core/src/test/java/com/datastax/driver/core/SimpleStatementTest.java +39 −0
@@ -18,7 +18,9 @@
1818 import org.testng.annotations.Test;
1919
2020 import java.util.Collections;
21+import java.util.HashMap;
2122 import java.util.List;
23+import java.util.Map;
2224
2325 import static org.assertj.core.api.Assertions.assertThat;
2426
@@ -47,6 +49,43 @@
4749 assertThat(actual).isSameAs(expected);
4850 }
4951
52+ @Test(groups = "unit", expectedExceptions = {IllegalStateException.class})
53+ public void should_throw_ISE_if_getObject_called_on_statement_without_named_values() {
54+ new SimpleStatement("doesn't matter").getObject("name");
55+ }
56+
57+ @Test(groups = "unit")
58+ public void should_return_null_if_getObject_called_on_statement_with_wrong_name() {
59+ Map<String, Object> namedVales = new HashMap<String, Object>();
60+ namedVales.put("name", new Object());
61+ Object actual = new SimpleStatement("doesn't matter", namedVales).getObject("wrong name");
62+ assertThat(actual).isNull();
63+ }
64+
65+ @Test(groups = "unit")
66+ public void should_return_object_with_name() {
67+ Object expected = new Object();
68+ String valueName = "name";
69+ Map<String, Object> namedVales = new HashMap<String, Object>();
70+ namedVales.put(valueName, expected);
71+ Object actual = new SimpleStatement("doesn't matter", namedVales).getObject(valueName);
72+ assertThat(actual).isSameAs(expected);
73+ }
74+
75+ @Test(groups = "unit", expectedExceptions = {IllegalStateException.class})
76+ public void should_throw_ISE_if_getValueNames_called_on_statement_without_named_values() {
77+ new SimpleStatement("doesn't matter").getValueNames();
78+ }
79+
80+ @Test(groups = "unit")
81+ public void should_return_named_value() {
82+ Object expected = new Object();
83+ Map<String, Object> namedValues = new HashMap<String, Object>();
84+ namedValues.put("name", expected);
85+ Object actual = new SimpleStatement("doesn't matter", namedValues).getObject("name");
86+ assertThat(actual).isEqualTo(expected);
87+ }
88+
5089 @Test(groups = "unit")
5190 public void should_return_number_of_values() {
5291 assertThat(