Elffuss BraidHub

kikocis/java-driver public

Override toString for PreparedStatement and BoundStatement (JAVA-115).
Changeset9ec95e075f578dce7764307e03a72cfdebdec436f48fa03da5087e538e547193
Authorolim7t <omichallat+github@gmail.com> · 2014-09-02 11:45 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-09-02 11:47 +02:00
Parents4625fd285b2c

Browse the files at this changeset

4 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -12,6 +12,7 @@
1212 - [improvement] Add ResultSet.wasApplied() for conditional queries (JAVA-358)
1313 - [bug] Fix negative HostConnectionPool open count (JAVA-349)
1414 - [improvement] Log more connection details at trace and debug levels (JAVA-436)
15+- [improvement] Override toString for PreparedStatement and BoundStatement (JAVA-115)
1516
1617 2.0.5:
1718 ------
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +30 −2
@@ -21,13 +21,14 @@
2121 import java.nio.ByteBuffer;
2222 import java.util.*;
2323
24+import com.datastax.driver.core.ColumnDefinitions.Definition;
2425 import com.datastax.driver.core.exceptions.InvalidTypeException;
2526
2627 /**
2728 * A prepared statement with values bound to the bind variables.
2829 * <p>
2930 * Once values has been provided for the variables of the {@link PreparedStatement}
30- * it has been created from, such BoundStatement can be executed (through
31+ * it has been created from, such BoundStatement can be executed (through
3132 * {@link Session#execute(Statement)}).
3233 * <p>
3334 * The values of a BoundStatement can be set by either index or name. When
@@ -89,7 +90,7 @@
8990 * bound to a non-null value.
9091 *
9192 * @param name the name of the variable to check.
92- * @return whether the first occurrence of variable {@code name} has been
93+ * @return whether the first occurrence of variable {@code name} has been
9394 * bound to a non-null value.
9495 *
9596 * @throws IllegalArgumentException if {@code name} is not a prepared
@@ -991,6 +992,33 @@
991992 return this;
992993 }
993994
995+ /**
996+ * {@inheritDoc}
997+ *
998+ * Note that this implementation for {@code BoundStatement} is intended for debugging purposes. It deserializes the statement's variables,
999+ * which could have a performance impact.
1000+ */
1001+ @Override
1002+ public String toString() {
1003+ StringBuilder builder = new StringBuilder("BoundStatement[");
1004+ builder.append(statement.getQueryString());
1005+ int i = 0;
1006+ for (Definition definition : statement.getVariables()) {
1007+ builder.append(", ");
1008+ String name = definition.getName();
1009+ DataType type = definition.getType();
1010+ String value = type.codec().deserialize(values[i]).toString();
1011+ if (value.length() > 50) {
1012+ value = String.format("%s...[truncated %s of %d characters]",
1013+ value.substring(0, 50), type.getName(), value.length());
1014+ }
1015+ builder.append(name + " = " + value);
1016+ i += 1;
1017+ }
1018+ builder.append("]");
1019+ return builder.toString();
1020+ }
1021+
9941022 private ColumnDefinitions metadata() {
9951023 return statement.getVariables();
9961024 }
modified driver-core/src/main/java/com/datastax/driver/core/DefaultPreparedStatement.java +5 −0
@@ -175,4 +175,9 @@
175175 public PreparedId getPreparedId() {
176176 return preparedId;
177177 }
178+
179+ @Override
180+ public String toString() {
181+ return "PreparedStatement[" + query + "]";
182+ }
178183 }
modified driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java +12 −0
@@ -15,8 +15,11 @@
1515 */
1616 package com.datastax.driver.core;
1717
18+import java.nio.ByteBuffer;
1819 import java.util.*;
1920
21+import com.google.common.base.Strings;
22+
2023 import org.testng.annotations.Test;
2124 import static org.testng.Assert.assertEquals;
2225 import static org.testng.Assert.assertTrue;
@@ -408,4 +411,13 @@
408411 throw e;
409412 }
410413 }
414+
415+ @Test(groups = "short")
416+ public void toStringTest() {
417+ PreparedStatement ps = session.prepare("INSERT INTO " + SIMPLE_TABLE2 + " (k, v) VALUES (?, ?)");
418+ BoundStatement bs = ps.bind("key", Strings.repeat("a", 100));
419+ assertEquals(ps.toString(), "PreparedStatement[INSERT INTO test2 (k, v) VALUES (?, ?)]");
420+ assertEquals(bs.toString(), "BoundStatement[INSERT INTO test2 (k, v) VALUES (?, ?), "
421+ + "k = key, v = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...[truncated varchar of 100 characters]]");
422+ }
411423 }