Skip to content

Commit b066df2

Browse files
snazyolim7t
authored andcommitted
Support new C* 2.2 CQL date and time types (JAVA-404).
1 parent c1f9386 commit b066df2

25 files changed

Lines changed: 1142 additions & 17 deletions

‎driver-core/src/main/java/com/datastax/driver/core/AbstractAddressableByIndexData.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,24 @@ public T setLong(int i, long v) {
6868
}
6969

7070
public T setDate(int i, Date v) {
71+
return setTimestamp(i, v);
72+
}
73+
74+
public T setTimestamp(int i, Date v) {
7175
checkType(i, DataType.Name.TIMESTAMP);
7276
return setValue(i, v == null ? null : TypeCodec.DateCodec.instance.serialize(v));
7377
}
7478

79+
public T setDateWithoutTime(int i, DateWithoutTime v) {
80+
checkType(i, DataType.Name.DATE);
81+
return setValue(i, TypeCodec.SimpleDateCodec.instance.serialize(v));
82+
}
83+
84+
public T setTime(int i, long v) {
85+
checkType(i, DataType.Name.TIME);
86+
return setValue(i, TypeCodec.TimeCodec.instance.serializeNoBoxing(v));
87+
}
88+
7589
public T setFloat(int i, float v) {
7690
checkType(i, DataType.Name.FLOAT);
7791
return setValue(i, TypeCodec.FloatCodec.instance.serializeNoBoxing(v));

‎driver-core/src/main/java/com/datastax/driver/core/AbstractData.java‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,19 @@ public T setLong(String name, long v) {
139139
}
140140

141141
public T setDate(int i, Date v) {
142+
return setTimestamp(i, v);
143+
}
144+
145+
public T setDate(String name, Date v) {
146+
return setTimestamp(name, v);
147+
}
148+
149+
public T setTimestamp(int i, Date v) {
142150
checkType(i, DataType.Name.TIMESTAMP);
143151
return setValue(i, v == null ? null : TypeCodec.DateCodec.instance.serialize(v));
144152
}
145153

146-
public T setDate(String name, Date v) {
154+
public T setTimestamp(String name, Date v) {
147155
int[] indexes = getAllIndexesOf(name);
148156
ByteBuffer value = v == null ? null : TypeCodec.DateCodec.instance.serialize(v);
149157
for (int i = 0; i < indexes.length; i++) {
@@ -153,6 +161,36 @@ public T setDate(String name, Date v) {
153161
return wrapped;
154162
}
155163

164+
public T setDateWithoutTime(int i, DateWithoutTime v) {
165+
checkType(i, DataType.Name.DATE);
166+
return setValue(i, TypeCodec.SimpleDateCodec.instance.serialize(v));
167+
}
168+
169+
public T setDateWithoutTime(String name, DateWithoutTime v) {
170+
int[] indexes = getAllIndexesOf(name);
171+
ByteBuffer value = TypeCodec.SimpleDateCodec.instance.serialize(v);
172+
for (int i = 0; i < indexes.length; i++) {
173+
checkType(indexes[i], DataType.Name.DATE);
174+
setValue(indexes[i], value);
175+
}
176+
return wrapped;
177+
}
178+
179+
public T setTime(int i, long v) {
180+
checkType(i, DataType.Name.TIME);
181+
return setValue(i, TypeCodec.TimeCodec.instance.serializeNoBoxing(v));
182+
}
183+
184+
public T setTime(String name, long v) {
185+
int[] indexes = getAllIndexesOf(name);
186+
ByteBuffer value = TypeCodec.TimeCodec.instance.serializeNoBoxing(v);
187+
for (int i = 0; i < indexes.length; i++) {
188+
checkType(indexes[i], DataType.Name.TIME);
189+
setValue(indexes[i], value);
190+
}
191+
return wrapped;
192+
}
193+
156194
public T setFloat(int i, float v) {
157195
checkType(i, DataType.Name.FLOAT);
158196
return setValue(i, TypeCodec.FloatCodec.instance.serializeNoBoxing(v));

‎driver-core/src/main/java/com/datastax/driver/core/AbstractGettableByIndexData.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ public long getLong(int i) {
174174
*/
175175
@Override
176176
public Date getDate(int i) {
177+
return getTimestamp(i);
178+
}
179+
180+
/**
181+
* {@inheritDoc}
182+
*/
183+
@Override
184+
public Date getTimestamp(int i)
185+
{
177186
checkType(i, DataType.Name.TIMESTAMP);
178187

179188
ByteBuffer value = getValue(i);
@@ -183,6 +192,34 @@ public Date getDate(int i) {
183192
return TypeCodec.DateCodec.instance.deserialize(value);
184193
}
185194

195+
/**
196+
* {@inheritDoc}
197+
*/
198+
@Override
199+
public DateWithoutTime getDateWithoutTime(int i) {
200+
checkType(i, DataType.Name.DATE);
201+
202+
ByteBuffer value = getValue(i);
203+
if (value == null || value.remaining() == 0)
204+
return null;
205+
206+
return TypeCodec.SimpleDateCodec.instance.deserialize(value);
207+
}
208+
209+
/**
210+
* {@inheritDoc}
211+
*/
212+
@Override
213+
public long getTime(int i) {
214+
checkType(i, DataType.Name.TIME);
215+
216+
ByteBuffer value = getValue(i);
217+
if (value == null || value.remaining() == 0)
218+
return 0L;
219+
220+
return TypeCodec.TimeCodec.instance.deserializeNoBoxing(value);
221+
}
222+
186223
/**
187224
* {@inheritDoc}
188225
*/

‎driver-core/src/main/java/com/datastax/driver/core/AbstractGettableData.java‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,34 @@ public long getLong(String name) {
100100
/**
101101
* {@inheritDoc}
102102
*/
103+
@Deprecated
103104
@Override
104105
public Date getDate(String name) {
105-
return getDate(getIndexOf(name));
106+
return getTimestamp(getIndexOf(name));
107+
}
108+
109+
/**
110+
* {@inheritDoc}
111+
*/
112+
@Override
113+
public Date getTimestamp(String name) {
114+
return getTimestamp(getIndexOf(name));
115+
}
116+
117+
/**
118+
* {@inheritDoc}
119+
*/
120+
@Override
121+
public DateWithoutTime getDateWithoutTime(String name) {
122+
return getDateWithoutTime(getIndexOf(name));
123+
}
124+
125+
/**
126+
* {@inheritDoc}
127+
*/
128+
@Override
129+
public long getTime(String name) {
130+
return getTime(getIndexOf(name));
106131
}
107132

108133
/**

‎driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java‎

Lines changed: 145 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,11 @@ public BoundStatement setLong(String name, long v) {
459459
*
460460
* @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.preparedStatement().variables().size()}.
461461
* @throws InvalidTypeException if column {@code i} is not of type TIMESTAMP.
462+
* @deprecated deprecated in favor of {@link #setTimestamp(int, Date)}
462463
*/
464+
@Deprecated
463465
public BoundStatement setDate(int i, Date v) {
464-
return wrapper.setDate(i, v);
466+
return wrapper.setTimestamp(i, v);
465467
}
466468

467469
/**
@@ -477,9 +479,105 @@ public BoundStatement setDate(int i, Date v) {
477479
* variable, that is, if {@code !this.preparedStatement().variables().names().contains(name)}.
478480
* @throws InvalidTypeException if (any occurrence of) {@code name} is
479481
* not of type TIMESTAMP.
482+
* @deprecated deprecated in favor of {@link #setTimestamp(String, Date)}
480483
*/
484+
@Deprecated
481485
public BoundStatement setDate(String name, Date v) {
482-
return wrapper.setDate(name, v);
486+
return wrapper.setTimestamp(name, v);
487+
}
488+
489+
/**
490+
* Set the {@code i}th value to the provided date.
491+
*
492+
* @param i the index of the variable to set.
493+
* @param v the value to set.
494+
* @return this BoundStatement.
495+
*
496+
* @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.preparedStatement().variables().size()}.
497+
* @throws InvalidTypeException if column {@code i} is not of type TIMESTAMP.
498+
*/
499+
public BoundStatement setTimestamp(int i, Date v) {
500+
return wrapper.setTimestamp(i, v);
501+
}
502+
503+
/**
504+
* Sets the value for (all occurrences of) variable {@code name} to the
505+
* provided date.
506+
*
507+
* @param name the name of the variable to set; if multiple variables
508+
* {@code name} are prepared, all of them are set.
509+
* @param v the value to set.
510+
* @return this BoundStatement.
511+
*
512+
* @throws IllegalArgumentException if {@code name} is not a prepared
513+
* variable, that is, if {@code !this.preparedStatement().variables().names().contains(name)}.
514+
* @throws InvalidTypeException if (any occurrence of) {@code name} is
515+
* not of type TIMESTAMP.
516+
*/
517+
public BoundStatement setTimestamp(String name, Date v) {
518+
return wrapper.setTimestamp(name, v);
519+
}
520+
521+
/**
522+
* Set the {@code i}th value to the provided date as an int in days since epoch.
523+
*
524+
* @param i the index of the value to set.
525+
* @param v the value to set.
526+
* @return this object.
527+
*
528+
* @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object.
529+
* @throws InvalidTypeException if value {@code i} is not of type DATE.
530+
*/
531+
public BoundStatement setDateWithoutTime(int i, DateWithoutTime v) {
532+
return wrapper.setDateWithoutTime(i, v);
533+
}
534+
535+
/**
536+
* Sets the value for (all occurrences of) variable {@code name} to the
537+
* provided date as an int in days since epoch.
538+
*
539+
* @param name the name of the value to set; if {@code name} is present multiple
540+
* times, all its values are set.
541+
* @param v the value to set.
542+
* @return this object.
543+
*
544+
* @throws IllegalArgumentException if {@code name} is not a valid name for this object.
545+
* @throws InvalidTypeException if (any occurrence of) {@code name} is
546+
* not of type DATE.
547+
*/
548+
public BoundStatement setDateWithoutTime(String name, DateWithoutTime v) {
549+
return wrapper.setDateWithoutTime(name, v);
550+
}
551+
552+
/**
553+
* Set the {@code i}th value to the provided time as a long in nanoseconds since midnight.
554+
*
555+
* @param i the index of the value to set.
556+
* @param v the value to set.
557+
* @return this object.
558+
*
559+
* @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object.
560+
* @throws InvalidTypeException if value {@code i} is not of type TIME.
561+
*/
562+
public BoundStatement setTime(int i, long v) {
563+
return wrapper.setTime(i, v);
564+
}
565+
566+
/**
567+
* Sets the value for (all occurrences of) variable {@code name} to the
568+
* provided time as a long in nanoseconds since midnight.
569+
*
570+
* @param name the name of the value to set; if {@code name} is present multiple
571+
* times, all its values are set.
572+
* @param v the value to set.
573+
* @return this object.
574+
*
575+
* @throws IllegalArgumentException if {@code name} is not a valid name for this object.
576+
* @throws InvalidTypeException if (any occurrence of) {@code name} is
577+
* not of type TIME.
578+
*/
579+
public BoundStatement setTime(String name, long v) {
580+
return wrapper.setTime(name, v);
483581
}
484582

485583
/**
@@ -1186,15 +1284,58 @@ public long getLong(String name) {
11861284
/**
11871285
* {@inheritDoc}
11881286
*/
1287+
@Deprecated
11891288
public Date getDate(int i) {
1190-
return wrapper.getDate(i);
1289+
return wrapper.getTimestamp(i);
11911290
}
11921291

11931292
/**
11941293
* {@inheritDoc}
11951294
*/
11961295
public Date getDate(String name) {
1197-
return wrapper.getDate(name);
1296+
return wrapper.getTimestamp(name);
1297+
}
1298+
1299+
/**
1300+
* {@inheritDoc}
1301+
*/
1302+
public Date getTimestamp(int i) {
1303+
return wrapper.getTimestamp(i);
1304+
}
1305+
1306+
/**
1307+
* {@inheritDoc}
1308+
*/
1309+
public Date getTimestamp(String name) {
1310+
return wrapper.getTimestamp(name);
1311+
}
1312+
1313+
/**
1314+
* {@inheritDoc}
1315+
*/
1316+
public DateWithoutTime getDateWithoutTime(int i) {
1317+
return wrapper.getDateWithoutTime(i);
1318+
}
1319+
1320+
/**
1321+
* {@inheritDoc}
1322+
*/
1323+
public DateWithoutTime getDateWithoutTime(String name) {
1324+
return wrapper.getDateWithoutTime(name);
1325+
}
1326+
1327+
/**
1328+
* {@inheritDoc}
1329+
*/
1330+
public long getTime(int i) {
1331+
return wrapper.getTime(i);
1332+
}
1333+
1334+
/**
1335+
* {@inheritDoc}
1336+
*/
1337+
public long getTime(String name) {
1338+
return wrapper.getTime(name);
11981339
}
11991340

12001341
/**

‎driver-core/src/main/java/com/datastax/driver/core/CassandraTypeParser.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class CassandraTypeParser {
6363
.put("org.apache.cassandra.db.marshal.Int32Type", DataType.cint())
6464
.put("org.apache.cassandra.db.marshal.UTF8Type", DataType.text())
6565
.put("org.apache.cassandra.db.marshal.TimestampType", DataType.timestamp())
66-
.put("org.apache.cassandra.db.marshal.DateType", DataType.timestamp())
66+
.put("org.apache.cassandra.db.marshal.SimpleDateType", DataType.date())
67+
.put("org.apache.cassandra.db.marshal.TimeType", DataType.time())
6768
.put("org.apache.cassandra.db.marshal.UUIDType", DataType.uuid())
6869
.put("org.apache.cassandra.db.marshal.IntegerType", DataType.varint())
6970
.put("org.apache.cassandra.db.marshal.TimeUUIDType", DataType.timeuuid())

‎driver-core/src/main/java/com/datastax/driver/core/DataType.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public enum Name {
6464
UDT (48, UDTValue.class, ProtocolVersion.V3),
6565
TUPLE (49, TupleValue.class, ProtocolVersion.V3),
6666
CUSTOM (0, ByteBuffer.class),
67+
DATE (17, DateWithoutTime.class, ProtocolVersion.V4),
68+
TIME (18, Long.class, ProtocolVersion.V4),
6769
SMALLINT (19, Short.class, ProtocolVersion.V4),
6870
TINYINT (20, Byte.class, ProtocolVersion.V4);
6971

@@ -349,6 +351,24 @@ public static DataType timestamp() {
349351
return primitiveTypeMap.get(Name.TIMESTAMP);
350352
}
351353

354+
/**
355+
* Returns the DATE type.
356+
*
357+
* @return The DATE type.
358+
*/
359+
public static DataType date() {
360+
return primitiveTypeMap.get(Name.DATE);
361+
}
362+
363+
/**
364+
* Returns the TIME type.
365+
*
366+
* @return The TIME type.
367+
*/
368+
public static DataType time() {
369+
return primitiveTypeMap.get(Name.TIME);
370+
}
371+
352372
/**
353373
* Returns the UUID type.
354374
*

0 commit comments

Comments
 (0)