kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 9ec95e075f578dce7764307e03a72cfdebdec436f48fa03da5087e538e547193 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-09-02 11:45 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-09-02 11:47 +02:00 |
| Parents | 4625fd285b2c |
4 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | - [improvement] Add ResultSet.wasApplied() for conditional queries (JAVA-358) |
| 13 | 13 | - [bug] Fix negative HostConnectionPool open count (JAVA-349) |
| 14 | 14 | - [improvement] Log more connection details at trace and debug levels (JAVA-436) |
| 15 | +- [improvement] Override toString for PreparedStatement and BoundStatement (JAVA-115) | |
| 15 | 16 | |
| 16 | 17 | 2.0.5: |
| 17 | 18 | ------ |
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +30 −2
| @@ -21,13 +21,14 @@ | ||
| 21 | 21 | import java.nio.ByteBuffer; |
| 22 | 22 | import java.util.*; |
| 23 | 23 | |
| 24 | +import com.datastax.driver.core.ColumnDefinitions.Definition; | |
| 24 | 25 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| 25 | 26 | |
| 26 | 27 | /** |
| 27 | 28 | * A prepared statement with values bound to the bind variables. |
| 28 | 29 | * <p> |
| 29 | 30 | * 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 | |
| 31 | 32 | * {@link Session#execute(Statement)}). |
| 32 | 33 | * <p> |
| 33 | 34 | * The values of a BoundStatement can be set by either index or name. When |
| @@ -89,7 +90,7 @@ | ||
| 89 | 90 | * bound to a non-null value. |
| 90 | 91 | * |
| 91 | 92 | * @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 | |
| 93 | 94 | * bound to a non-null value. |
| 94 | 95 | * |
| 95 | 96 | * @throws IllegalArgumentException if {@code name} is not a prepared |
| @@ -991,6 +992,33 @@ | ||
| 991 | 992 | return this; |
| 992 | 993 | } |
| 993 | 994 | |
| 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 | + | |
| 994 | 1022 | private ColumnDefinitions metadata() { |
| 995 | 1023 | return statement.getVariables(); |
| 996 | 1024 | } |
modified driver-core/src/main/java/com/datastax/driver/core/DefaultPreparedStatement.java +5 −0
| @@ -175,4 +175,9 @@ | ||
| 175 | 175 | public PreparedId getPreparedId() { |
| 176 | 176 | return preparedId; |
| 177 | 177 | } |
| 178 | + | |
| 179 | + @Override | |
| 180 | + public String toString() { | |
| 181 | + return "PreparedStatement[" + query + "]"; | |
| 182 | + } | |
| 178 | 183 | } |
modified driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java +12 −0
| @@ -15,8 +15,11 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import java.nio.ByteBuffer; | |
| 18 | 19 | import java.util.*; |
| 19 | 20 | |
| 21 | +import com.google.common.base.Strings; | |
| 22 | + | |
| 20 | 23 | import org.testng.annotations.Test; |
| 21 | 24 | import static org.testng.Assert.assertEquals; |
| 22 | 25 | import static org.testng.Assert.assertTrue; |
| @@ -408,4 +411,13 @@ | ||
| 408 | 411 | throw e; |
| 409 | 412 | } |
| 410 | 413 | } |
| 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 | + } | |
| 411 | 423 | } |