kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 64954d6d69dff4646dcb2b313e285f4f8027227d322f7227108139fc2732c4ce |
|---|---|
| Author | Sylvain Lebresne <sylvain@datastax.com> · 2014-04-22 16:07 +02:00 |
| Committer | Sylvain Lebresne <sylvain@datastax.com> · 2014-04-22 16:07 +02:00 |
| Parents | 37d230659fe1 |
3 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | - [new] Expose the name of the partitioner in use in the cluster metadata (JAVA-179) |
| 9 | 9 | - [new] Add new WhiteListPolicy to limit the nodes connected to a particular list |
| 10 | 10 | - [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) | |
| 11 | 12 | |
| 12 | 13 | |
| 13 | 14 | 1.0.6: |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +1 −12
| @@ -19,7 +19,6 @@ | ||
| 19 | 19 | import java.nio.ByteBuffer; |
| 20 | 20 | import java.util.*; |
| 21 | 21 | import java.util.regex.Pattern; |
| 22 | -import java.text.SimpleDateFormat; | |
| 23 | 22 | |
| 24 | 23 | import org.apache.cassandra.utils.ByteBufferUtil; |
| 25 | 24 | |
| @@ -28,16 +27,6 @@ | ||
| 28 | 27 | |
| 29 | 28 | private static final Pattern cnamePattern = Pattern.compile("\\w+(?:\\[.+\\])?"); |
| 30 | 29 | |
| 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 | - | |
| 41 | 30 | static StringBuilder joinAndAppend(StringBuilder sb, String separator, List<? extends Appendeable> values) { |
| 42 | 31 | for (int i = 0; i < values.size(); i++) { |
| 43 | 32 | if (i > 0) |
| @@ -120,7 +109,7 @@ | ||
| 120 | 109 | sb.append("'").append(((InetAddress)value).getHostAddress()).append("'"); |
| 121 | 110 | return true; |
| 122 | 111 | } else if (value instanceof Date) { |
| 123 | - sb.append("'").append(dateFormat.get().format((Date)value)).append("'"); | |
| 112 | + sb.append(((Date)value).getTime()); | |
| 124 | 113 | return true; |
| 125 | 114 | } else if (value instanceof ByteBuffer) { |
| 126 | 115 | 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 | +} |