Elffuss BraidHub

kikocis/java-driver public

Revert back to longs for date in the query builder

JAVA-313 #fixes
Changeset64954d6d69dff4646dcb2b313e285f4f8027227d322f7227108139fc2732c4ce
AuthorSylvain Lebresne <sylvain@datastax.com> · 2014-04-22 16:07 +02:00
CommitterSylvain Lebresne <sylvain@datastax.com> · 2014-04-22 16:07 +02:00
Parents37d230659fe1

Browse the files at this changeset

3 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -8,6 +8,7 @@
88 - [new] Expose the name of the partitioner in use in the cluster metadata (JAVA-179)
99 - [new] Add new WhiteListPolicy to limit the nodes connected to a particular list
1010 - [improvement] Do not hop DC for LOCAL_* CL in DCAwareRoundRobinPolicy (JAVA-289)
11+- [bug] Revert back to longs for dates in the query builder (JAVA-313)
1112
1213
1314 1.0.6:
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +1 −12
@@ -19,7 +19,6 @@
1919 import java.nio.ByteBuffer;
2020 import java.util.*;
2121 import java.util.regex.Pattern;
22-import java.text.SimpleDateFormat;
2322
2423 import org.apache.cassandra.utils.ByteBufferUtil;
2524
@@ -28,16 +27,6 @@
2827
2928 private static final Pattern cnamePattern = Pattern.compile("\\w+(?:\\[.+\\])?");
3029
31- // We currently format date as strings instead of simply longs due to JAVA-264. We could change
32- // that back to longs (which is cheaper) once C* 1.2 gets eol (since only pre-1.2.16 versions are
33- // affected).
34- private static final ThreadLocal<SimpleDateFormat> dateFormat = new ThreadLocal<SimpleDateFormat>() {
35- @Override
36- protected SimpleDateFormat initialValue() {
37- return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
38- }
39- };
40-
4130 static StringBuilder joinAndAppend(StringBuilder sb, String separator, List<? extends Appendeable> values) {
4231 for (int i = 0; i < values.size(); i++) {
4332 if (i > 0)
@@ -120,7 +109,7 @@
120109 sb.append("'").append(((InetAddress)value).getHostAddress()).append("'");
121110 return true;
122111 } else if (value instanceof Date) {
123- sb.append("'").append(dateFormat.get().format((Date)value)).append("'");
112+ sb.append(((Date)value).getTime());
124113 return true;
125114 } else if (value instanceof ByteBuffer) {
126115 sb.append("0x");
added driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderExecutionTest.java +87 −0
@@ -0,0 +1,87 @@
1+/*
2+ * Copyright (C) 2012 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core.querybuilder;
17+
18+import java.util.Arrays;
19+import java.util.Collection;
20+import java.util.Date;
21+import java.util.List;
22+
23+import org.testng.annotations.Test;
24+import static org.testng.Assert.*;
25+
26+import com.datastax.driver.core.CCMBridge;
27+import com.datastax.driver.core.Statement;
28+import com.datastax.driver.core.Row;
29+import com.datastax.driver.core.TestUtils;
30+import static com.datastax.driver.core.querybuilder.QueryBuilder.*;
31+
32+public class QueryBuilderExecutionTest extends CCMBridge.PerClassSingleNodeCluster {
33+
34+ private static final String TABLE1 = "test1";
35+
36+ @Override
37+ protected Collection<String> getTableDefinitions() {
38+ return Arrays.asList(String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, TABLE1),
39+ "CREATE TABLE dateTest (t timestamp PRIMARY KEY)");
40+ }
41+
42+ @Test(groups = "short")
43+ public void executeTest() throws Exception {
44+
45+ session.execute(insertInto(TABLE1).value("k", "k1").value("t", "This is a test").value("i", 3).value("f", 0.42));
46+ session.execute(update(TABLE1).with(set("t", "Another test")).where(eq("k", "k2")));
47+
48+ List<Row> rows = session.execute(select().from(TABLE1).where(in("k", "k1", "k2"))).all();
49+
50+ assertEquals(2, rows.size());
51+
52+ Row r1 = rows.get(0);
53+ assertEquals("k1", r1.getString("k"));
54+ assertEquals("This is a test", r1.getString("t"));
55+ assertEquals(3, r1.getInt("i"));
56+ assertFalse(r1.isNull("f"));
57+
58+ Row r2 = rows.get(1);
59+ assertEquals("k2", r2.getString("k"));
60+ assertEquals("Another test", r2.getString("t"));
61+ assertTrue(r2.isNull("i"));
62+ assertTrue(r2.isNull("f"));
63+ }
64+
65+ @Test(groups = "short")
66+ public void dateHandlingTest() throws Exception {
67+
68+ Date d = new Date();
69+ session.execute(insertInto("dateTest").value("t", d));
70+ String query = select().from("dateTest").where(eq(token("t"), fcall("token", d))).toString();
71+ List<Row> rows = session.execute(query).all();
72+
73+ assertEquals(1, rows.size());
74+
75+ Row r1 = rows.get(0);
76+ assertEquals(d, r1.getDate("t"));
77+ }
78+
79+ @Test(groups = "unit")
80+ public void prepareTest() throws Exception {
81+
82+ // Just check we correctly avoid values when there is a bind marker
83+ String query = "INSERT INTO foo(a,b,c,d) VALUES ('foo','bar',?,0);";
84+ Statement stmt = insertInto("foo").value("a", "foo").value("b", "bar").value("c", bindMarker()).value("d", 0);
85+ assertEquals(query, stmt.getQueryString());
86+ }
87+}