Skip to content

Commit c243ed7

Browse files
author
Gagan Gupta
committed
feat: support for UUID type
1 parent 01eafce commit c243ed7

23 files changed

+996
-1
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/AbstractResultSet.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Iterator;
3939
import java.util.List;
4040
import java.util.Objects;
41+
import java.util.UUID;
4142
import java.util.function.Function;
4243
import javax.annotation.Nonnull;
4344
import javax.annotation.Nullable;
@@ -430,6 +431,11 @@ protected Date getDateInternal(int columnIndex) {
430431
return currRow().getDateInternal(columnIndex);
431432
}
432433

434+
@Override
435+
protected UUID getUuidInternal(int columnIndex) {
436+
return currRow().getUuidInternal(columnIndex);
437+
}
438+
433439
@Override
434440
protected Value getValueInternal(int columnIndex) {
435441
return currRow().getValueInternal(columnIndex);
@@ -522,6 +528,11 @@ protected List<Date> getDateListInternal(int columnIndex) {
522528
return currRow().getDateListInternal(columnIndex);
523529
}
524530

531+
@Override
532+
protected List<UUID> getUuidListInternal(int columnIndex) {
533+
return currRow().getUuidListInternal(columnIndex);
534+
}
535+
525536
@Override
526537
protected List<Struct> getStructListInternal(int columnIndex) {
527538
return currRow().getStructListInternal(columnIndex);

google-cloud-spanner/src/main/java/com/google/cloud/spanner/AbstractStructReader.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Arrays;
2929
import java.util.Collections;
3030
import java.util.List;
31+
import java.util.UUID;
3132
import java.util.function.Function;
3233

3334
/**
@@ -67,6 +68,8 @@ protected String getPgJsonbInternal(int columnIndex) {
6768

6869
protected abstract Date getDateInternal(int columnIndex);
6970

71+
protected abstract UUID getUuidInternal(int columnIndex);
72+
7073
protected <T extends AbstractMessage> T getProtoMessageInternal(int columnIndex, T message) {
7174
throw new UnsupportedOperationException("Not implemented");
7275
}
@@ -128,6 +131,8 @@ protected List<String> getPgJsonbListInternal(int columnIndex) {
128131

129132
protected abstract List<Date> getDateListInternal(int columnIndex);
130133

134+
protected abstract List<UUID> getUuidListInternal(int columnIndex);
135+
131136
protected abstract List<Struct> getStructListInternal(int columnIndex);
132137

133138
@Override
@@ -299,6 +304,19 @@ public Date getDate(String columnName) {
299304
return getDateInternal(columnIndex);
300305
}
301306

307+
@Override
308+
public UUID getUuid(int columnIndex) {
309+
checkNonNullOfType(columnIndex, Type.uuid(), columnIndex);
310+
return getUuidInternal(columnIndex);
311+
}
312+
313+
@Override
314+
public UUID getUuid(String columnName) {
315+
final int columnIndex = getColumnIndex(columnName);
316+
checkNonNullOfType(columnIndex, Type.uuid(), columnName);
317+
return getUuid(columnIndex);
318+
}
319+
302320
@Override
303321
public <T extends ProtocolMessageEnum> T getProtoEnum(
304322
int columnIndex, Function<Integer, ProtocolMessageEnum> method) {
@@ -583,6 +601,19 @@ public List<Date> getDateList(String columnName) {
583601
return getDateListInternal(columnIndex);
584602
}
585603

604+
@Override
605+
public List<UUID> getUuidList(int columnIndex) {
606+
checkNonNullOfType(columnIndex, Type.array(Type.uuid()), columnIndex);
607+
return getUuidListInternal(columnIndex);
608+
}
609+
610+
@Override
611+
public List<UUID> getUuidList(String columnName) {
612+
final int columnIndex = getColumnIndex(columnName);
613+
checkNonNullOfType(columnIndex, Type.array(Type.uuid()), columnName);
614+
return getUuidList(columnIndex);
615+
}
616+
586617
@Override
587618
public List<Struct> getStructList(int columnIndex) {
588619
checkNonNullArrayOfStruct(columnIndex, columnIndex);

google-cloud-spanner/src/main/java/com/google/cloud/spanner/ForwardingStructReader.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.protobuf.ProtocolMessageEnum;
2727
import java.math.BigDecimal;
2828
import java.util.List;
29+
import java.util.UUID;
2930
import java.util.function.Function;
3031

3132
/** Forwarding implements of StructReader */
@@ -225,12 +226,24 @@ public Date getDate(int columnIndex) {
225226
return delegate.get().getDate(columnIndex);
226227
}
227228

229+
@Override
230+
public UUID getUuid(int columnIndex) {
231+
checkValidState();
232+
return delegate.get().getUuid(columnIndex);
233+
}
234+
228235
@Override
229236
public Date getDate(String columnName) {
230237
checkValidState();
231238
return delegate.get().getDate(columnName);
232239
}
233240

241+
@Override
242+
public UUID getUuid(String columnName) {
243+
checkValidState();
244+
return delegate.get().getUuid(columnName);
245+
}
246+
234247
@Override
235248
public boolean[] getBooleanArray(int columnIndex) {
236249
checkValidState();
@@ -409,6 +422,18 @@ public List<Date> getDateList(String columnName) {
409422
return delegate.get().getDateList(columnName);
410423
}
411424

425+
@Override
426+
public List<UUID> getUuidList(int columnIndex) {
427+
checkValidState();
428+
return delegate.get().getUuidList(columnIndex);
429+
}
430+
431+
@Override
432+
public List<UUID> getUuidList(String columnName) {
433+
checkValidState();
434+
return delegate.get().getUuidList(columnName);
435+
}
436+
412437
@Override
413438
public <T extends AbstractMessage> List<T> getProtoMessageList(int columnIndex, T message) {
414439
checkValidState();

google-cloud-spanner/src/main/java/com/google/cloud/spanner/GrpcStruct.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.Collections;
5050
import java.util.Iterator;
5151
import java.util.List;
52+
import java.util.UUID;
5253
import java.util.concurrent.atomic.AtomicBoolean;
5354
import java.util.function.Function;
5455
import java.util.stream.Collectors;
@@ -131,6 +132,9 @@ private Object writeReplace() {
131132
case DATE:
132133
builder.set(fieldName).to((Date) value);
133134
break;
135+
case UUID:
136+
builder.set(fieldName).to((UUID) value);
137+
break;
134138
case ARRAY:
135139
final Type elementType = fieldType.getArrayElementType();
136140
switch (elementType.getCode()) {
@@ -184,6 +188,9 @@ private Object writeReplace() {
184188
case DATE:
185189
builder.set(fieldName).toDateArray((Iterable<Date>) value);
186190
break;
191+
case UUID:
192+
builder.set(fieldName).toUuidArray((Iterable<UUID>) value);
193+
break;
187194
case STRUCT:
188195
builder.set(fieldName).toStructArray(elementType, (Iterable<Struct>) value);
189196
break;
@@ -298,6 +305,9 @@ private static Object decodeValue(Type fieldType, com.google.protobuf.Value prot
298305
case DATE:
299306
checkType(fieldType, proto, KindCase.STRING_VALUE);
300307
return Date.parseDate(proto.getStringValue());
308+
case UUID:
309+
checkType(fieldType, proto, KindCase.STRING_VALUE);
310+
return UUID.fromString(proto.getStringValue());
301311
case ARRAY:
302312
checkType(fieldType, proto, KindCase.LIST_VALUE);
303313
ListValue listValue = proto.getListValue();
@@ -347,6 +357,7 @@ static Object decodeArrayValue(Type elementType, ListValue listValue) {
347357
case BYTES:
348358
case TIMESTAMP:
349359
case DATE:
360+
case UUID:
350361
case STRUCT:
351362
case PROTO:
352363
return Lists.transform(listValue.getValuesList(), input -> decodeValue(elementType, input));
@@ -503,6 +514,12 @@ protected Date getDateInternal(int columnIndex) {
503514
return (Date) rowData.get(columnIndex);
504515
}
505516

517+
@Override
518+
protected UUID getUuidInternal(int columnIndex) {
519+
ensureDecoded(columnIndex);
520+
return (UUID) rowData.get(columnIndex);
521+
}
522+
506523
private boolean isUnrecognizedType(int columnIndex) {
507524
return type.getStructFields().get(columnIndex).getType().getCode() == Code.UNRECOGNIZED;
508525
}
@@ -624,6 +641,8 @@ protected Value getValueInternal(int columnIndex) {
624641
return Value.timestamp(isNull ? null : getTimestampInternal(columnIndex));
625642
case DATE:
626643
return Value.date(isNull ? null : getDateInternal(columnIndex));
644+
case UUID:
645+
return Value.uuid(isNull ? null : getUuidInternal(columnIndex));
627646
case STRUCT:
628647
return Value.struct(isNull ? null : getStructInternal(columnIndex));
629648
case UNRECOGNIZED:
@@ -664,6 +683,8 @@ protected Value getValueInternal(int columnIndex) {
664683
return Value.timestampArray(isNull ? null : getTimestampListInternal(columnIndex));
665684
case DATE:
666685
return Value.dateArray(isNull ? null : getDateListInternal(columnIndex));
686+
case UUID:
687+
return Value.uuidArray(isNull ? null : getUuidListInternal(columnIndex));
667688
case STRUCT:
668689
return Value.structArray(
669690
elementType, isNull ? null : getStructListInternal(columnIndex));
@@ -847,6 +868,12 @@ protected List<Date> getDateListInternal(int columnIndex) {
847868
return Collections.unmodifiableList((List<Date>) rowData.get(columnIndex));
848869
}
849870

871+
@Override
872+
protected List<UUID> getUuidListInternal(int columnIndex) {
873+
ensureDecoded(columnIndex);
874+
return Collections.unmodifiableList((List<UUID>) rowData.get(columnIndex));
875+
}
876+
850877
@Override
851878
@SuppressWarnings("unchecked") // We know ARRAY<STRUCT<...>> produces a List<STRUCT>.
852879
protected List<Struct> getStructListInternal(int columnIndex) {

google-cloud-spanner/src/main/java/com/google/cloud/spanner/ResultSets.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.google.spanner.v1.ResultSetStats;
3636
import java.math.BigDecimal;
3737
import java.util.List;
38+
import java.util.UUID;
3839
import java.util.function.Function;
3940

4041
/** Utility methods for working with {@link com.google.cloud.spanner.ResultSet}. */
@@ -321,11 +322,21 @@ public Date getDate(int columnIndex) {
321322
return getCurrentRowAsStruct().getDate(columnIndex);
322323
}
323324

325+
@Override
326+
public UUID getUuid(int columnIndex) {
327+
return getCurrentRowAsStruct().getUuid(columnIndex);
328+
}
329+
324330
@Override
325331
public Date getDate(String columnName) {
326332
return getCurrentRowAsStruct().getDate(columnName);
327333
}
328334

335+
@Override
336+
public UUID getUuid(String columnName) {
337+
return getCurrentRowAsStruct().getUuid(columnName);
338+
}
339+
329340
@Override
330341
public <T extends AbstractMessage> T getProtoMessage(int columnIndex, T message) {
331342
return getCurrentRowAsStruct().getProtoMessage(columnIndex, message);
@@ -508,6 +519,16 @@ public List<Date> getDateList(String columnName) {
508519
return getCurrentRowAsStruct().getDateList(columnName);
509520
}
510521

522+
@Override
523+
public List<UUID> getUuidList(int columnIndex) {
524+
return getCurrentRowAsStruct().getUuidList(columnIndex);
525+
}
526+
527+
@Override
528+
public List<UUID> getUuidList(String columnNameÏ) {
529+
return getCurrentRowAsStruct().getUuidList(columnNameÏ);
530+
}
531+
511532
@Override
512533
public <T extends AbstractMessage> List<T> getProtoMessageList(int columnIndex, T message) {
513534
return getCurrentRowAsStruct().getProtoMessageList(columnIndex, message);

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Struct.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.ArrayList;
3737
import java.util.List;
3838
import java.util.Objects;
39+
import java.util.UUID;
3940
import java.util.function.Function;
4041
import javax.annotation.concurrent.Immutable;
4142

@@ -226,6 +227,11 @@ protected Date getDateInternal(int columnIndex) {
226227
return values.get(columnIndex).getDate();
227228
}
228229

230+
@Override
231+
protected UUID getUuidInternal(int columnIndex) {
232+
return values.get(columnIndex).getUuid();
233+
}
234+
229235
@Override
230236
protected <T extends AbstractMessage> T getProtoMessageInternal(int columnIndex, T message) {
231237
return values.get(columnIndex).getProtoMessage(message);
@@ -334,6 +340,11 @@ protected List<Date> getDateListInternal(int columnIndex) {
334340
return values.get(columnIndex).getDateArray();
335341
}
336342

343+
@Override
344+
protected List<UUID> getUuidListInternal(int columnIndex) {
345+
return values.get(columnIndex).getUuidArray();
346+
}
347+
337348
@Override
338349
protected List<Struct> getStructListInternal(int columnIndex) {
339350
return values.get(columnIndex).getStructArray();
@@ -420,6 +431,8 @@ private Object getAsObject(int columnIndex) {
420431
return getTimestampInternal(columnIndex);
421432
case DATE:
422433
return getDateInternal(columnIndex);
434+
case UUID:
435+
return getUuidInternal(columnIndex);
423436
case STRUCT:
424437
return getStructInternal(columnIndex);
425438
case ARRAY:
@@ -451,6 +464,8 @@ private Object getAsObject(int columnIndex) {
451464
return getTimestampListInternal(columnIndex);
452465
case DATE:
453466
return getDateListInternal(columnIndex);
467+
case UUID:
468+
return getUuidListInternal(columnIndex);
454469
case STRUCT:
455470
return getStructListInternal(columnIndex);
456471
default:

google-cloud-spanner/src/main/java/com/google/cloud/spanner/StructReader.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.protobuf.ProtocolMessageEnum;
2424
import java.math.BigDecimal;
2525
import java.util.List;
26+
import java.util.UUID;
2627
import java.util.function.Function;
2728

2829
/**
@@ -291,12 +292,16 @@ default <T extends ProtocolMessageEnum> T getProtoEnum(
291292
*/
292293
Date getDate(int columnIndex);
293294

295+
UUID getUuid(int columnIndex);
296+
294297
/**
295298
* @param columnName name of the column
296299
* @return the value of a non-{@code NULL} column with type {@link Type#date()}.
297300
*/
298301
Date getDate(String columnName);
299302

303+
UUID getUuid(String columnName);
304+
300305
/**
301306
* @param columnIndex index of the column
302307
* @return the value of a nullable column as a {@link Value}.
@@ -625,6 +630,10 @@ default <T extends ProtocolMessageEnum> List<T> getProtoEnumList(
625630
*/
626631
List<Date> getDateList(String columnName);
627632

633+
List<UUID> getUuidList(int columnIndex);
634+
635+
List<UUID> getUuidList(String columnNameÏ);
636+
628637
/**
629638
* @param columnIndex index of the column
630639
* @return the value of a non-{@code NULL} column with type {@code Type.array(Type.struct(...))}

0 commit comments

Comments
 (0)