kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | da0c3d4bdee0a5ffeab0cea71394ffd88780ff919d9b99af28434a96c2ac2c98 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-06-24 16:12 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-07-20 19:50 +02:00 |
| Parents | a24f48a9b779 |
4 files changed
modified changelog/README.md +3 −1
| @@ -3,7 +3,9 @@ | ||
| 3 | 3 | ### 2.2.0-rc2 (in progress) |
| 4 | 4 | |
| 5 | 5 | - [improvement] Rename DateWithoutTime to LocalDate (JAVA-810) |
| 6 | -- [bug] TypeCodec.getDataTypeFor() does not handle LocalDate instances. (JAVA-818) | |
| 6 | +- [bug] DateCodec does not format values correctly (JAVA-816) | |
| 7 | +- [bug] TimeCodec does not format values correctly (JAVA-817) | |
| 8 | +- [bug] TypeCodec.getDataTypeFor() does not handle LocalDate instances (JAVA-818) | |
| 7 | 9 | |
| 8 | 10 | Merged from 2.1 branch: |
| 9 | 11 |
modified driver-core/src/main/java/com/datastax/driver/core/TypeCodec.java +47 −20
| @@ -908,6 +908,11 @@ | ||
| 908 | 908 | |
| 909 | 909 | @Override |
| 910 | 910 | public Date parse(String value) { |
| 911 | + // strip enclosing single quotes, if any | |
| 912 | + // enclosing quotes are optional for long literals, mandatory for date patterns | |
| 913 | + if (value.charAt(0) == '\'' && value.charAt(value.length() - 1) == '\'') | |
| 914 | + value = value.substring(1, value.length() - 1); | |
| 915 | + | |
| 911 | 916 | if (IS_LONG_PATTERN.matcher(value).matches()) { |
| 912 | 917 | try { |
| 913 | 918 | return new Date(Long.parseLong(value)); |
| @@ -952,6 +957,12 @@ | ||
| 952 | 957 | |
| 953 | 958 | @Override |
| 954 | 959 | public LocalDate parse(String value) { |
| 960 | + | |
| 961 | + // strip enclosing single quotes, if any | |
| 962 | + // single quotes are optional for long literals, mandatory for date patterns | |
| 963 | + if (value.charAt(0) == '\'' && value.charAt(value.length() - 1) == '\'') | |
| 964 | + value = value.substring(1, value.length() - 1); | |
| 965 | + | |
| 955 | 966 | if (IS_LONG_PATTERN.matcher(value).matches()) { |
| 956 | 967 | // In CQL, numeric DATE literals are longs between 0 and 2^32 - 1, with the epoch in the middle, |
| 957 | 968 | // so parse it as a long and re-center at 0 |
| @@ -985,7 +996,7 @@ | ||
| 985 | 996 | |
| 986 | 997 | @Override |
| 987 | 998 | public String format(LocalDate value) { |
| 988 | - return value.toString(); | |
| 999 | + return "'" + value.toString() + "'"; | |
| 989 | 1000 | } |
| 990 | 1001 | |
| 991 | 1002 | @Override |
| @@ -1010,7 +1021,7 @@ | ||
| 1010 | 1021 | } |
| 1011 | 1022 | } |
| 1012 | 1023 | |
| 1013 | - static class TimeCodec extends TypeCodec<Long> { | |
| 1024 | + static class TimeCodec extends LongCodec { | |
| 1014 | 1025 | |
| 1015 | 1026 | private static final Pattern IS_LONG_PATTERN = Pattern.compile("^-?\\d+$"); |
| 1016 | 1027 | |
| @@ -1082,6 +1093,11 @@ | ||
| 1082 | 1093 | |
| 1083 | 1094 | @Override |
| 1084 | 1095 | public Long parse(String value) { |
| 1096 | + // enclosing single quotes required, even for long literals | |
| 1097 | + if (value.charAt(0) != '\'' || value.charAt(value.length() - 1) != '\'') | |
| 1098 | + throw new InvalidTypeException("time values must be enclosed by single quotes"); | |
| 1099 | + value = value.substring(1, value.length() - 1); | |
| 1100 | + | |
| 1085 | 1101 | if (IS_LONG_PATTERN.matcher(value).matches()) { |
| 1086 | 1102 | try { |
| 1087 | 1103 | return Long.parseLong(value); |
| @@ -1099,26 +1115,37 @@ | ||
| 1099 | 1115 | |
| 1100 | 1116 | @Override |
| 1101 | 1117 | public String format(Long value) { |
| 1102 | - return value.toString(); | |
| 1103 | - } | |
| 1104 | - | |
| 1105 | - @Override | |
| 1106 | - public ByteBuffer serialize(Long value) { | |
| 1107 | - return LongCodec.instance.serializeNoBoxing(value); | |
| 1108 | - } | |
| 1109 | - | |
| 1110 | - @Override | |
| 1111 | - public Long deserialize(ByteBuffer bytes) { | |
| 1112 | - return LongCodec.instance.deserializeNoBoxing(bytes); | |
| 1113 | - } | |
| 1114 | - | |
| 1115 | - public ByteBuffer serializeNoBoxing(long value) { | |
| 1116 | - return LongCodec.instance.serializeNoBoxing(value); | |
| 1118 | + if (value == null) | |
| 1119 | + return "null"; | |
| 1120 | + int nano = (int)(value % 1000000000); | |
| 1121 | + value -= nano; | |
| 1122 | + value /= 1000000000; | |
| 1123 | + int seconds = (int)(value % 60); | |
| 1124 | + value -= seconds; | |
| 1125 | + value /= 60; | |
| 1126 | + int minutes = (int)(value % 60); | |
| 1127 | + value -= minutes; | |
| 1128 | + value /= 60; | |
| 1129 | + int hours = (int)(value % 24); | |
| 1130 | + value -= hours; | |
| 1131 | + value /= 24; | |
| 1132 | + assert(value == 0); | |
| 1133 | + StringBuilder sb = new StringBuilder("'"); | |
| 1134 | + leftPadZeros(hours, 2, sb); | |
| 1135 | + sb.append(":"); | |
| 1136 | + leftPadZeros(minutes, 2, sb); | |
| 1137 | + sb.append(":"); | |
| 1138 | + leftPadZeros(seconds, 2, sb); | |
| 1139 | + sb.append("."); | |
| 1140 | + leftPadZeros(nano, 9, sb); | |
| 1141 | + sb.append("'"); | |
| 1142 | + return sb.toString(); | |
| 1143 | + } | |
| 1144 | + | |
| 1145 | + private static void leftPadZeros(int value, int digits, StringBuilder sb) { | |
| 1146 | + sb.append(String.format("%0" + digits + "d", value)); | |
| 1117 | 1147 | } |
| 1118 | 1148 | |
| 1119 | - public long deserializeNoBoxing(ByteBuffer bytes) { | |
| 1120 | - return LongCodec.instance.deserializeNoBoxing(bytes); | |
| 1121 | - } | |
| 1122 | 1149 | } |
| 1123 | 1150 | |
| 1124 | 1151 | static class UUIDCodec extends TypeCodec<UUID> { |
modified driver-core/src/test/java/com/datastax/driver/core/DataTypeIntegrationTest.java +12 −1
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import java.nio.ByteBuffer; | |
| 18 | 19 | import java.util.*; |
| 19 | 20 | import java.util.concurrent.atomic.AtomicInteger; |
| 20 | 21 | |
| @@ -89,7 +90,9 @@ | ||
| 89 | 90 | session.execute(table.insertStatement.replace("?", table.testColumnType.format(table.sampleValue))); |
| 90 | 91 | break; |
| 91 | 92 | case SIMPLE_WITH_PARAM: |
| 92 | - session.execute(table.insertStatement, table.sampleValue); | |
| 93 | + SimpleStatement statement = new SimpleStatement(table.insertStatement, table.sampleValue); | |
| 94 | + checkGetValuesReturnsSerializedValue(protocolVersion, statement, table); | |
| 95 | + session.execute(statement); | |
| 93 | 96 | break; |
| 94 | 97 | case PREPARED: |
| 95 | 98 | PreparedStatement ps = session.prepare(table.insertStatement); |
| @@ -123,6 +126,14 @@ | ||
| 123 | 126 | assertThat(bs.getObject("v")).isEqualTo(table.sampleValue); |
| 124 | 127 | } |
| 125 | 128 | |
| 129 | + public void checkGetValuesReturnsSerializedValue(ProtocolVersion protocolVersion, SimpleStatement statement, TestTable table) { | |
| 130 | + ByteBuffer[] values = statement.getValues(protocolVersion); | |
| 131 | + assertThat(values.length).isEqualTo(1); | |
| 132 | + assertThat(values[0]) | |
| 133 | + .as("Value not serialized as expected for " + table.sampleValue) | |
| 134 | + .isEqualTo(DataType.serializeValue(table.sampleValue, protocolVersion)); | |
| 135 | + } | |
| 136 | + | |
| 126 | 137 | /** |
| 127 | 138 | * Abstracts information about a table (corresponding to a given column type). |
| 128 | 139 | */ |
modified driver-core/src/test/java/com/datastax/driver/core/DataTypeTest.java +78 −66
| @@ -23,7 +23,6 @@ | ||
| 23 | 23 | |
| 24 | 24 | import com.google.common.collect.Lists; |
| 25 | 25 | import com.google.common.reflect.TypeToken; |
| 26 | - | |
| 27 | 26 | import org.testng.annotations.Test; |
| 28 | 27 | |
| 29 | 28 | import static org.testng.Assert.assertEquals; |
| @@ -43,90 +42,103 @@ | ||
| 43 | 42 | return t.getName() == DataType.Name.COUNTER; |
| 44 | 43 | } |
| 45 | 44 | |
| 46 | - private static String[] getCQLStringTestData(DataType dt) { | |
| 47 | - switch (dt.getName()) { | |
| 48 | - case ASCII: | |
| 49 | - case TEXT: | |
| 50 | - case VARCHAR: | |
| 51 | - return new String[]{ "'foo'", "'fo''o'" }; | |
| 52 | - case BIGINT: | |
| 53 | - case TIMESTAMP: | |
| 54 | - return new String[]{ "42", "91294377723", "-133" }; | |
| 55 | - case DATE: | |
| 56 | - return new String[]{ "2014-01-01", "1970-01-01" }; | |
| 57 | - case TIME: | |
| 58 | - return new String[]{ "54012123450000", "0" }; | |
| 59 | - case BLOB: | |
| 60 | - return new String[]{ "0x2450", "0x" }; | |
| 61 | - case BOOLEAN: | |
| 62 | - return new String[]{ "true", "false" }; | |
| 63 | - case DECIMAL: | |
| 64 | - return new String[]{ "1.23E+8" }; | |
| 65 | - case DOUBLE: | |
| 66 | - return new String[]{ "2.39324324", "-12.0" }; | |
| 67 | - case FLOAT: | |
| 68 | - return new String[]{ "2.39", "-12.0" }; | |
| 69 | - case INET: | |
| 70 | - return new String[]{ "'128.2.12.3'" }; | |
| 71 | - case TINYINT: | |
| 72 | - return new String[]{ "-4", "44" }; | |
| 73 | - case SMALLINT: | |
| 74 | - return new String[]{ "-3", "43" }; | |
| 75 | - case INT: | |
| 76 | - return new String[]{ "-2", "42" }; | |
| 77 | - case TIMEUUID: | |
| 78 | - return new String[]{ "fe2b4360-28c6-11e2-81c1-0800200c9a66" }; | |
| 79 | - case UUID: | |
| 80 | - return new String[]{ "fe2b4360-28c6-11e2-81c1-0800200c9a66", "067e6162-3b6f-4ae2-a171-2470b63dff00" }; | |
| 81 | - case VARINT: | |
| 82 | - return new String[]{ "12387290982347987032483422342432" }; | |
| 83 | - default: | |
| 84 | - throw new RuntimeException("Missing handling of " + dt); | |
| 45 | + /** A test value for a primitive data type */ | |
| 46 | + static class TestValue { | |
| 47 | + /** The value as a Java object */ | |
| 48 | + final Object javaObject; | |
| 49 | + /** A CQL string that should parse to the value */ | |
| 50 | + final String cqlInputString; | |
| 51 | + /** How the value should be formatted in CQL */ | |
| 52 | + final String cqlOutputString; | |
| 53 | + | |
| 54 | + TestValue(Object javaObject, String cqlInputString, String cqlOutputString) { | |
| 55 | + this.javaObject = javaObject; | |
| 56 | + this.cqlInputString = cqlInputString; | |
| 57 | + this.cqlOutputString = cqlOutputString; | |
| 85 | 58 | } |
| 86 | 59 | } |
| 87 | 60 | |
| 88 | - private static Object[] getTestData(DataType dt) { | |
| 61 | + private static TestValue[] primitiveTestValues(DataType dt) { | |
| 89 | 62 | switch (dt.getName()) { |
| 90 | 63 | case ASCII: |
| 91 | 64 | case TEXT: |
| 92 | 65 | case VARCHAR: |
| 93 | - return new Object[]{ "foo", "fo'o" }; | |
| 66 | + return new TestValue[]{ | |
| 67 | + new TestValue("foo", "'foo'", "'foo'"), | |
| 68 | + new TestValue("fo'o", "'fo''o'", "'fo''o'") }; | |
| 94 | 69 | case BIGINT: |
| 95 | - return new Object[]{ 42L, 91294377723L, -133L }; | |
| 70 | + return new TestValue[]{ | |
| 71 | + new TestValue(42L, "42", "42"), | |
| 72 | + new TestValue(91294377723L, "91294377723", "91294377723"), | |
| 73 | + new TestValue(-133L, "-133", "-133") }; | |
| 96 | 74 | case TIMESTAMP: |
| 97 | - return new Object[]{ new Date(42L), new Date(91294377723L), new Date(-133L) }; | |
| 75 | + // input: single quotes are optional for long literals, mandatory for date patterns | |
| 76 | + return new TestValue[]{ | |
| 77 | + new TestValue(new Date(42L), "42", "42"), | |
| 78 | + new TestValue(new Date(91294377723L), "91294377723", "91294377723"), | |
| 79 | + new TestValue(new Date(-133L), "-133", "-133"), | |
| 80 | + new TestValue(new Date(784041330999L), "'1994-11-05T14:15:30.999+0100'", "784041330999") }; | |
| 98 | 81 | case DATE: |
| 99 | - return new Object[]{ LocalDate.fromDaysSinceEpoch(16071) /* 2014-01-01 */, LocalDate.fromDaysSinceEpoch(0) /* 1970-01-01 */ }; | |
| 82 | + // input: single quotes are optional for long literals, mandatory for date patterns | |
| 83 | + return new TestValue[]{ | |
| 84 | + new TestValue(LocalDate.fromDaysSinceEpoch(16071), "'2014-01-01'", "'2014-01-01'"), | |
| 85 | + new TestValue(LocalDate.fromDaysSinceEpoch(0), "'1970-01-01'", "'1970-01-01'"), | |
| 86 | + new TestValue(LocalDate.fromDaysSinceEpoch((int)(2147483648L - (1L << 31))), "'2147483648'", "'1970-01-01'"), | |
| 87 | + new TestValue(LocalDate.fromDaysSinceEpoch((int)(0 - (1L << 31))), "0", "'-5877641-06-23'") }; | |
| 100 | 88 | case TIME: |
| 101 | - return new Object[]{ 54012123450000L /* 15:00:12.123450000 */, 0L }; | |
| 89 | + // input: all literals must by enclosed in single quotes | |
| 90 | + return new TestValue[]{ | |
| 91 | + new TestValue(54012123450000L, "'54012123450000'", "'15:00:12.123450000'"), | |
| 92 | + new TestValue(0L, "'0'", "'00:00:00.000000000'"), | |
| 93 | + new TestValue(54012012345000L, "'15:00:12.012345000'", "'15:00:12.012345000'") }; | |
| 102 | 94 | case BLOB: |
| 103 | - return new Object[]{ Bytes.fromHexString("0x2450"), ByteBuffer.allocate(0) }; | |
| 95 | + return new TestValue[]{ | |
| 96 | + new TestValue(Bytes.fromHexString("0x2450"), "0x2450", "0x2450"), | |
| 97 | + new TestValue(ByteBuffer.allocate(0), "0x", "0x") }; | |
| 104 | 98 | case BOOLEAN: |
| 105 | - return new Object[]{ true, false }; | |
| 99 | + return new TestValue[]{ | |
| 100 | + new TestValue(true, "true", "true"), | |
| 101 | + new TestValue(false, "false", "false") }; | |
| 106 | 102 | case DECIMAL: |
| 107 | - return new Object[]{ new BigDecimal("1.23E+8") }; | |
| 103 | + return new TestValue[]{ | |
| 104 | + new TestValue(new BigDecimal("1.23E+8"), "1.23E+8", "1.23E+8") }; | |
| 108 | 105 | case DOUBLE: |
| 109 | - return new Object[]{ 2.39324324, -12. }; | |
| 106 | + return new TestValue[]{ | |
| 107 | + new TestValue(2.39324324, "2.39324324", "2.39324324"), | |
| 108 | + new TestValue(-12., "-12.0", "-12.0") }; | |
| 110 | 109 | case FLOAT: |
| 111 | - return new Object[]{ 2.39f, -12.f }; | |
| 110 | + return new TestValue[]{ | |
| 111 | + new TestValue(2.39f, "2.39", "2.39"), | |
| 112 | + new TestValue(-12.f, "-12.0", "-12.0") }; | |
| 112 | 113 | case INET: |
| 113 | 114 | try { |
| 114 | - return new Object[]{ InetAddress.getByName("128.2.12.3") }; | |
| 115 | + return new TestValue[]{ | |
| 116 | + new TestValue(InetAddress.getByName("128.2.12.3"), "'128.2.12.3'", "'128.2.12.3'") }; | |
| 115 | 117 | } catch (java.net.UnknownHostException e) { |
| 116 | 118 | throw new RuntimeException(); |
| 117 | 119 | } |
| 118 | 120 | case TINYINT: |
| 119 | - return new Object[]{ (byte)-4, (byte)44 }; | |
| 121 | + return new TestValue[]{ | |
| 122 | + new TestValue((byte)-4, "-4", "-4"), | |
| 123 | + new TestValue((byte)44, "44", "44") }; | |
| 120 | 124 | case SMALLINT: |
| 121 | - return new Object[]{ (short)-3, (short)43 }; | |
| 125 | + return new TestValue[]{ | |
| 126 | + new TestValue((short)-3, "-3", "-3"), | |
| 127 | + new TestValue((short)43, "43", "43") }; | |
| 122 | 128 | case INT: |
| 123 | - return new Object[]{ -2, 42 }; | |
| 129 | + return new TestValue[]{ | |
| 130 | + new TestValue(-2, "-2", "-2"), | |
| 131 | + new TestValue(42, "42", "42") }; | |
| 124 | 132 | case TIMEUUID: |
| 125 | - return new Object[]{ UUID.fromString("FE2B4360-28C6-11E2-81C1-0800200C9A66") }; | |
| 133 | + return new TestValue[]{ | |
| 134 | + new TestValue(UUID.fromString("FE2B4360-28C6-11E2-81C1-0800200C9A66"), "fe2b4360-28c6-11e2-81c1-0800200c9a66", "fe2b4360-28c6-11e2-81c1-0800200c9a66") }; | |
| 126 | 135 | case UUID: |
| 127 | - return new Object[]{ UUID.fromString("FE2B4360-28C6-11E2-81C1-0800200C9A66"), UUID.fromString("067e6162-3b6f-4ae2-a171-2470b63dff00") }; | |
| 136 | + return new TestValue[]{ | |
| 137 | + new TestValue(UUID.fromString("FE2B4360-28C6-11E2-81C1-0800200C9A66"), "fe2b4360-28c6-11e2-81c1-0800200c9a66", "fe2b4360-28c6-11e2-81c1-0800200c9a66"), | |
| 138 | + new TestValue(UUID.fromString("067e6162-3b6f-4ae2-a171-2470b63dff00"), "067e6162-3b6f-4ae2-a171-2470b63dff00", "067e6162-3b6f-4ae2-a171-2470b63dff00") }; | |
| 128 | 139 | case VARINT: |
| 129 | - return new Object[]{ new BigInteger("12387290982347987032483422342432") }; | |
| 140 | + return new TestValue[]{ | |
| 141 | + new TestValue(new BigInteger("12387290982347987032483422342432"), "12387290982347987032483422342432", "12387290982347987032483422342432") }; | |
| 130 | 142 | default: |
| 131 | 143 | throw new RuntimeException("Missing handling of " + dt); |
| 132 | 144 | } |
| @@ -138,10 +150,10 @@ | ||
| 138 | 150 | if (exclude(dt)) |
| 139 | 151 | continue; |
| 140 | 152 | |
| 141 | - String[] s = getCQLStringTestData(dt); | |
| 142 | - Object[] o = getTestData(dt); | |
| 143 | - for (int i = 0; i < s.length; i++) | |
| 144 | - assertEquals(dt.parse(s[i]), o[i], String.format("For input %d of %s, ", i, dt, s[i], o[i])); | |
| 153 | + for (TestValue value : primitiveTestValues(dt)) | |
| 154 | + assertThat(dt.parse(value.cqlInputString)) | |
| 155 | + .as("Parsing input %s to a %s", value.cqlInputString, dt) | |
| 156 | + .isEqualTo(value.javaObject); | |
| 145 | 157 | } |
| 146 | 158 | } |
| 147 | 159 | |
| @@ -151,10 +163,10 @@ | ||
| 151 | 163 | if (exclude(dt)) |
| 152 | 164 | continue; |
| 153 | 165 | |
| 154 | - String[] s = getCQLStringTestData(dt); | |
| 155 | - Object[] o = getTestData(dt); | |
| 156 | - for (int i = 0; i < s.length; i++) | |
| 157 | - assertEquals(dt.format(o[i]), s[i], String.format("For input %d of %s, ", i, dt, o[i], s[i])); | |
| 166 | + for (TestValue value : primitiveTestValues(dt)) | |
| 167 | + assertThat(dt.format(value.javaObject)) | |
| 168 | + .as("Formatting a %s expecting %s", dt, value.cqlOutputString) | |
| 169 | + .isEqualTo(value.cqlOutputString); | |
| 158 | 170 | } |
| 159 | 171 | } |
| 160 | 172 |