Skip to content

Commit 22be75d

Browse files
author
Alexandre Dutra
committed
Delegate Statement.toString() to StatementFormatter.format().
1 parent 048b8fc commit 22be75d

11 files changed

Lines changed: 262 additions & 278 deletions

File tree

driver-core/src/main/java/com/datastax/driver/core/RegularStatement.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,4 @@ public boolean hasValues() {
189189
return hasValues(CodecRegistry.DEFAULT_INSTANCE);
190190
}
191191

192-
/**
193-
* Returns this statement as a CQL query string.
194-
* <p/>
195-
* It is important to note that the query string is merely
196-
* a CQL representation of this statement, but it does
197-
* <em>not</em> convey all the information stored in {@link Statement}
198-
* objects.
199-
* <p/>
200-
* See the javadocs of {@link #getQueryString()} for more information.
201-
*
202-
* @return this statement as a CQL query string.
203-
* @see #getQueryString()
204-
*/
205-
@Override
206-
public String toString() {
207-
return getQueryString();
208-
}
209192
}

driver-core/src/main/java/com/datastax/driver/core/Statement.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,4 +596,17 @@ protected static Boolean isBatchIdempotent(Collection<? extends Statement> state
596596
}
597597
return (hasNullIdempotentStatements) ? null : true;
598598
}
599+
600+
@Override
601+
public String toString() {
602+
try {
603+
return StatementFormatter.DEFAULT_INSTANCE.format(
604+
this,
605+
StatementFormatter.StatementFormatVerbosity.EXTENDED,
606+
ProtocolVersion.NEWEST_SUPPORTED,
607+
CodecRegistry.DEFAULT_INSTANCE);
608+
} catch (StatementFormatter.StatementFormatException e) {
609+
return super.toString();
610+
}
611+
}
599612
}

driver-core/src/main/java/com/datastax/driver/core/querybuilder/BuiltStatement.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -311,25 +311,6 @@ public Boolean isIdempotent() {
311311
return !hasNonIdempotentOps();
312312
}
313313

314-
@Override
315-
public String toString() {
316-
try {
317-
if (forceNoValues)
318-
return getQueryString();
319-
// 1) try first with all values inlined (will not work if some values require custom codecs,
320-
// or if the required codecs are registered in a different CodecRegistry instance than the default one)
321-
return maybeAddSemicolon(buildQueryString(null, CodecRegistry.DEFAULT_INSTANCE)).toString();
322-
} catch (RuntimeException e1) {
323-
// 2) try next with bind markers for all values to avoid usage of custom codecs
324-
try {
325-
return maybeAddSemicolon(buildQueryString(new ArrayList<Object>(), CodecRegistry.DEFAULT_INSTANCE)).toString();
326-
} catch (RuntimeException e2) {
327-
// Ugly but we have absolutely no context to get the registry from
328-
return String.format("built query (could not generate with default codec registry: %s)", e2.getMessage());
329-
}
330-
}
331-
}
332-
333314
/**
334315
* Allows to force this builder to not generate values (through its {@code getValues()} method).
335316
* <p/>

driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderExecutionTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ public void onTestContextInitialized() {
4545
"CREATE TABLE dateTest (t timestamp PRIMARY KEY)",
4646
"CREATE TABLE test_coll (k int PRIMARY KEY, a list<int>, b map<int,text>, c set<text>)",
4747
"CREATE TABLE test_ppl (a int, b int, c int, PRIMARY KEY (a, b))",
48-
insertInto(TABLE2).value("k", "cast_t").value("t", "a").value("i", 1).value("f", 1.1).toString(),
49-
insertInto(TABLE2).value("k", "cast_t").value("t", "b").value("i", 2).value("f", 2.5).toString(),
50-
insertInto(TABLE2).value("k", "cast_t").value("t", "c").value("i", 3).value("f", 3.7).toString(),
51-
insertInto(TABLE2).value("k", "cast_t").value("t", "d").value("i", 4).value("f", 5.0).toString()
48+
String.format("INSERT INTO %s (k, t, i, f) VALUES ('cast_t', 'a', 1, 1.1)", TABLE2),
49+
String.format("INSERT INTO %s (k, t, i, f) VALUES ('cast_t', 'b', 2, 2.5)", TABLE2),
50+
String.format("INSERT INTO %s (k, t, i, f) VALUES ('cast_t', 'c', 3, 3.7)", TABLE2),
51+
String.format("INSERT INTO %s (k, t, i, f) VALUES ('cast_t', 'd', 4, 5.0)", TABLE2)
5252
);
5353
// for per partition limit tests
5454
for (int i = 0; i < 5; i++) {
@@ -86,8 +86,8 @@ public void dateHandlingTest() throws Exception {
8686

8787
Date d = new Date();
8888
session().execute(insertInto("dateTest").value("t", d));
89-
String query = select().from("dateTest").where(eq(token("t"), fcall("token", d))).toString();
90-
List<Row> rows = session().execute(query).all();
89+
Statement stmt = select().from("dateTest").where(eq(token("t"), fcall("token", d)));
90+
List<Row> rows = session().execute(stmt).all();
9191

9292
assertEquals(1, rows.size());
9393

driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderITest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public void onTestContextInitialized() {
3838
@Test(groups = "short")
3939
public void remainingDeleteTests() throws Exception {
4040

41-
Statement query;
41+
BuiltStatement query;
4242
TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_TEXT);
4343
assertNotNull(table);
4444

4545
String expected = String.format("DELETE k FROM %s.test_text;", keyspace);
4646
query = delete("k").from(table);
47-
assertEquals(query.toString(), expected);
47+
assertEquals(query.getQueryString(), expected);
4848
try {
4949
session().execute(query);
5050
fail();
@@ -57,17 +57,17 @@ public void remainingDeleteTests() throws Exception {
5757
public void selectInjectionTests() throws Exception {
5858

5959
String query;
60-
Statement select;
60+
BuiltStatement select;
6161
PreparedStatement ps;
6262
BoundStatement bs;
6363

6464
session().execute("CREATE TABLE foo ( k ascii PRIMARY KEY , i int, s ascii )");
6565

6666
query = "SELECT * FROM foo WHERE k=?;";
6767
select = select().all().from("foo").where(eq("k", bindMarker()));
68-
ps = session().prepare(select.toString());
68+
ps = session().prepare(select.getQueryString());
6969
bs = ps.bind();
70-
assertEquals(select.toString(), query);
70+
assertEquals(select.getQueryString(), query);
7171
session().execute(bs.setString("k", "4 AND c=5"));
7272
}
7373

driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderRoutingKeyTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,31 +169,31 @@ public void intRoutingBatchKeyTest() throws Exception {
169169
.add(insertInto(table).values(new String[]{"k", "a"}, new Object[]{42, 1}))
170170
.add(update(table).using(ttl(400)));
171171
assertEquals(batch.getRoutingKey(protocolVersion, codecRegistry), bb);
172-
assertEquals(batch.toString(), batch_query);
172+
assertEquals(batch.getQueryString(), batch_query);
173173
// TODO: rs = session().execute(batch); // Not guaranteed to be valid CQL
174174

175175
batch_query = "BEGIN BATCH ";
176176
batch_query += String.format("SELECT * FROM %s.test_int WHERE k=42;", keyspace);
177177
batch_query += "APPLY BATCH;";
178178
batch = batch(query);
179179
assertEquals(batch.getRoutingKey(protocolVersion, codecRegistry), bb);
180-
assertEquals(batch.toString(), batch_query);
180+
assertEquals(batch.getQueryString(), batch_query);
181181
// TODO: rs = session().execute(batch); // Not guaranteed to be valid CQL
182182

183183
batch_query = "BEGIN BATCH ";
184184
batch_query += "SELECT * FROM foo WHERE k=42;";
185185
batch_query += "APPLY BATCH;";
186186
batch = batch().add(select().from("foo").where(eq("k", 42)));
187187
assertEquals(batch.getRoutingKey(protocolVersion, codecRegistry), null);
188-
assertEquals(batch.toString(), batch_query);
188+
assertEquals(batch.getQueryString(), batch_query);
189189
// TODO: rs = session().execute(batch); // Not guaranteed to be valid CQL
190190

191191
batch_query = "BEGIN BATCH USING TIMESTAMP 42 ";
192192
batch_query += "INSERT INTO foo.bar (a) VALUES (123);";
193193
batch_query += "APPLY BATCH;";
194194
batch = batch().using(timestamp(42)).add(insertInto("foo", "bar").value("a", 123));
195195
assertEquals(batch.getRoutingKey(protocolVersion, codecRegistry), null);
196-
assertEquals(batch.toString(), batch_query);
196+
assertEquals(batch.getQueryString(), batch_query);
197197
// TODO: rs = session().execute(batch); // Not guaranteed to be valid CQL
198198
}
199199
}

0 commit comments

Comments
 (0)