13 files changed

+700
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -592,4 +592,17 @@
592592
<className>com/google/cloud/spanner/AsyncTransactionManager$CommitTimestampFuture</className>
593593
<method>java.lang.Object get()</method>
594594
</difference>
595+
596+
<!-- Adds getValue to ResultSet -->
597+
<!-- These are not breaking changes, since we provide default interface implementation -->
598+
<difference>
599+
<differenceType>7012</differenceType>
600+
<className>com/google/cloud/spanner/StructReader</className>
601+
<method>com.google.cloud.spanner.Value getValue(int)</method>
602+
</difference>
603+
<difference>
604+
<differenceType>7012</differenceType>
605+
<className>com/google/cloud/spanner/StructReader</className>
606+
<method>com.google.cloud.spanner.Value getValue(java.lang.String)</method>
607+
</difference>
595608
</differences>
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.cloud.ByteArray;
2929
import com.google.cloud.Date;
3030
import com.google.cloud.Timestamp;
31+
import com.google.cloud.spanner.Type.StructField;
3132
import com.google.cloud.spanner.spi.v1.SpannerRpc;
3233
import com.google.cloud.spanner.v1.stub.SpannerStubSettings;
3334
import com.google.common.annotations.VisibleForTesting;
@@ -679,6 +680,62 @@ protected Date getDateInternal(int columnIndex) {
679680
return (Date) rowData.get(columnIndex);
680681
}
681682

683+
@Override
684+
protected Value getValueInternal(int columnIndex) {
685+
final List<Type.StructField> structFields = getType().getStructFields();
686+
final StructField structField = structFields.get(columnIndex);
687+
final Type columnType = structField.getType();
688+
final boolean isNull = rowData.get(columnIndex) == null;
689+
switch (columnType.getCode()) {
690+
case BOOL:
691+
return Value.bool(isNull ? null : getBooleanInternal(columnIndex));
692+
case INT64:
693+
return Value.int64(isNull ? null : getLongInternal(columnIndex));
694+
case NUMERIC:
695+
return Value.numeric(isNull ? null : getBigDecimalInternal(columnIndex));
696+
case FLOAT64:
697+
return Value.float64(isNull ? null : getDoubleInternal(columnIndex));
698+
case STRING:
699+
return Value.string(isNull ? null : getStringInternal(columnIndex));
700+
case BYTES:
701+
return Value.bytes(isNull ? null : getBytesInternal(columnIndex));
702+
case TIMESTAMP:
703+
return Value.timestamp(isNull ? null : getTimestampInternal(columnIndex));
704+
case DATE:
705+
return Value.date(isNull ? null : getDateInternal(columnIndex));
706+
case STRUCT:
707+
return Value.struct(isNull ? null : getStructInternal(columnIndex));
708+
case ARRAY:
709+
switch (columnType.getArrayElementType().getCode()) {
710+
case BOOL:
711+
return Value.boolArray(isNull ? null : getBooleanListInternal(columnIndex));
712+
case INT64:
713+
return Value.int64Array(isNull ? null : getLongListInternal(columnIndex));
714+
case NUMERIC:
715+
return Value.numericArray(isNull ? null : getBigDecimalListInternal(columnIndex));
716+
case FLOAT64:
717+
return Value.float64Array(isNull ? null : getDoubleListInternal(columnIndex));
718+
case STRING:
719+
return Value.stringArray(isNull ? null : getStringListInternal(columnIndex));
720+
case BYTES:
721+
return Value.bytesArray(isNull ? null : getBytesListInternal(columnIndex));
722+
case TIMESTAMP:
723+
return Value.timestampArray(isNull ? null : getTimestampListInternal(columnIndex));
724+
case DATE:
725+
return Value.dateArray(isNull ? null : getDateListInternal(columnIndex));
726+
case STRUCT:
727+
return Value.structArray(
728+
columnType.getArrayElementType(),
729+
isNull ? null : getStructListInternal(columnIndex));
730+
default:
731+
throw new IllegalArgumentException(
732+
"Invalid array value type " + this.type.getArrayElementType());
733+
}
734+
default:
735+
throw new IllegalArgumentException("Invalid value type " + this.type);
736+
}
737+
}
738+
682739
@Override
683740
protected Struct getStructInternal(int columnIndex) {
684741
return (Struct) rowData.get(columnIndex);
@@ -1280,6 +1337,11 @@ protected Date getDateInternal(int columnIndex) {
12801337
return currRow().getDateInternal(columnIndex);
12811338
}
12821339

1340+
@Override
1341+
protected Value getValueInternal(int columnIndex) {
1342+
return currRow().getValueInternal(columnIndex);
1343+
}
1344+
12831345
@Override
12841346
protected boolean[] getBooleanArrayInternal(int columnIndex) {
12851347
return currRow().getBooleanArrayInternal(columnIndex);
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public abstract class AbstractStructReader implements StructReader {
4949

5050
protected abstract Date getDateInternal(int columnIndex);
5151

52+
protected Value getValueInternal(int columnIndex) {
53+
throw new UnsupportedOperationException("method should be overwritten");
54+
}
55+
5256
protected abstract boolean[] getBooleanArrayInternal(int columnIndex);
5357

5458
protected abstract List<Boolean> getBooleanListInternal(int columnIndex);
@@ -197,6 +201,18 @@ public Date getDate(String columnName) {
197201
return getDateInternal(columnIndex);
198202
}
199203

204+
@Override
205+
public Value getValue(int columnIndex) {
206+
checkNonNull(columnIndex, columnIndex);
207+
return getValueInternal(columnIndex);
208+
}
209+
210+
@Override
211+
public Value getValue(String columnName) {
212+
int columnIndex = getColumnIndex(columnName);
213+
return getValueInternal(columnIndex);
214+
}
215+
200216
@Override
201217
public boolean[] getBooleanArray(int columnIndex) {
202218
checkNonNullOfType(columnIndex, Type.array(Type.bool()), columnIndex);
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,16 @@ public List<Struct> getStructList(String columnName) {
333333
checkValidState();
334334
return delegate.get().getStructList(columnName);
335335
}
336+
337+
@Override
338+
public Value getValue(int columnIndex) {
339+
checkValidState();
340+
return delegate.get().getValue(columnIndex);
341+
}
342+
343+
@Override
344+
public Value getValue(String columnName) {
345+
checkValidState();
346+
return delegate.get().getValue(columnName);
347+
}
336348
}
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,16 @@ public Date getDate(String columnName) {
273273
return getCurrentRowAsStruct().getDate(columnName);
274274
}
275275

276+
@Override
277+
public Value getValue(int columnIndex) {
278+
return getCurrentRowAsStruct().getValue(columnIndex);
279+
}
280+
281+
@Override
282+
public Value getValue(String columnName) {
283+
return getCurrentRowAsStruct().getValue(columnName);
284+
}
285+
276286
@Override
277287
public boolean[] getBooleanArray(int columnIndex) {
278288
return getCurrentRowAsStruct().getBooleanArray(columnIndex);
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ protected Date getDateInternal(int columnIndex) {
207207
return values.get(columnIndex).getDate();
208208
}
209209

210+
@Override
211+
protected Value getValueInternal(int columnIndex) {
212+
return values.get(columnIndex);
213+
}
214+
210215
@Override
211216
protected Struct getStructInternal(int columnIndex) {
212217
return values.get(columnIndex).getStruct();
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ public interface StructReader {
132132
/** Returns the value of a non-{@code NULL} column with type {@link Type#date()}. */
133133
Date getDate(String columnName);
134134

135+
/** Returns the value of a nullable column as a {@link Value}. */
136+
default Value getValue(int columnIndex) {
137+
throw new UnsupportedOperationException("method should be overwritten");
138+
}
139+
140+
/** Returns the value of a nullable column as a {@link Value}. */
141+
default Value getValue(String columnName) {
142+
throw new UnsupportedOperationException("method should be overwritten");
143+
}
144+
135145
/**
136146
* Returns the value of a non-{@code NULL} column with type {@code Type.array(Type.bool())}.
137147
*
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ private Value() {}
543543
*
544544
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
545545
*/
546-
abstract List<Struct> getStructArray();
546+
public abstract List<Struct> getStructArray();
547547

548548
@Override
549549
public String toString() {
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.cloud.spanner.SpannerException;
2424
import com.google.cloud.spanner.Struct;
2525
import com.google.cloud.spanner.Type;
26+
import com.google.cloud.spanner.Value;
2627
import com.google.common.base.Preconditions;
2728
import com.google.spanner.v1.ResultSetStats;
2829
import java.math.BigDecimal;
@@ -231,6 +232,18 @@ public Date getDate(String columnName) {
231232
return delegate.getDate(columnName);
232233
}
233234

235+
@Override
236+
public Value getValue(int columnIndex) {
237+
Preconditions.checkState(nextCalledByClient, MISSING_NEXT_CALL);
238+
return delegate.getValue(columnIndex);
239+
}
240+
241+
@Override
242+
public Value getValue(String columnName) {
243+
Preconditions.checkState(nextCalledByClient, MISSING_NEXT_CALL);
244+
return delegate.getValue(columnName);
245+
}
246+
234247
@Override
235248
public boolean[] getBooleanArray(int columnIndex) {
236249
Preconditions.checkState(nextCalledByClient, MISSING_NEXT_CALL);
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.cloud.spanner.SpannerExceptionFactory;
2626
import com.google.cloud.spanner.Struct;
2727
import com.google.cloud.spanner.Type;
28+
import com.google.cloud.spanner.Value;
2829
import com.google.common.base.Preconditions;
2930
import com.google.spanner.v1.ResultSetStats;
3031
import java.math.BigDecimal;
@@ -231,6 +232,18 @@ public Date getDate(String columnName) {
231232
return delegate.getDate(columnName);
232233
}
233234

235+
@Override
236+
public Value getValue(int columnIndex) {
237+
checkClosed();
238+
return delegate.getValue(columnIndex);
239+
}
240+
241+
@Override
242+
public Value getValue(String columnName) {
243+
checkClosed();
244+
return delegate.getValue(columnName);
245+
}
246+
234247
@Override
235248
public boolean[] getBooleanArray(int columnIndex) {
236249
checkClosed();

0 commit comments

Comments
 (0)