Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions google-cloud-spanner/clirr-ignored-differences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1008,4 +1008,24 @@
<className>com/google/cloud/spanner/TransactionManager</className>
<method>com.google.cloud.spanner.TransactionContext begin(com.google.cloud.spanner.AbortedException)</method>
</difference>
<difference>
<differenceType>7012</differenceType>
<className>com/google/cloud/spanner/StructReader</className>
<method>java.lang.Object getOrNull(int, java.util.function.BiFunction)</method>
</difference>
<difference>
<differenceType>7012</differenceType>
<className>com/google/cloud/spanner/StructReader</className>
<method>java.lang.Object getOrNull(java.lang.String, java.util.function.BiFunction)</method>
</difference>
<difference>
<differenceType>7012</differenceType>
<className>com/google/cloud/spanner/StructReader</className>
<method>java.lang.Object getOrDefault(int, java.util.function.BiFunction, java.lang.Object)</method>
</difference>
<difference>
<differenceType>7012</differenceType>
<className>com/google/cloud/spanner/StructReader</className>
<method>java.lang.Object getOrDefault(java.lang.String, java.util.function.BiFunction, java.lang.Object)</method>
</difference>
</differences>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -176,6 +177,60 @@ default float getFloat(String columnName) {
*/
String getString(String columnName);

/**
* @param columnIndex index of the column
* @return the value of a column with type T or null if the column contains a null value
* <p>Example
* <pre>{@code
* Struct row = ...
* String name = row.getOrNull(1, StructReader::getString)
* }</pre>
*/
default <T> T getOrNull(int columnIndex, BiFunction<StructReader, Integer, T> function) {
return isNull(columnIndex) ? null : function.apply(this, columnIndex);
}

/**
* @param columnName index of the column
* @return the value of a column with type T or null if the column contains a null value
* <p>Example
* <pre>{@code
* Struct row = ...
* String name = row.getOrNull("name", StructReader::getString)
* }</pre>
*/
default <T> T getOrNull(String columnName, BiFunction<StructReader, String, T> function) {
return isNull(columnName) ? null : function.apply(this, columnName);
}

/**
* @param columnIndex index of the column
* @return the value of a column with type T, or the given default if the column value is null
* <p>Example
* <pre>{@code
* Struct row = ...
* String name = row.getOrDefault(1, StructReader::getString, "")
* }</pre>
*/
default <T> T getOrDefault(
int columnIndex, BiFunction<StructReader, Integer, T> function, T defaultValue) {
return isNull(columnIndex) ? defaultValue : function.apply(this, columnIndex);
}

/**
* @param columnName name of the column
* @return the value of a column with type T, or the given default if the column value is null
* <p>Example
* <pre>{@code
* Struct row = ...
* String name = row.getOrDefault("name", StructReader::getString, "")
* }</pre>
*/
default <T> T getOrDefault(
String columnName, BiFunction<StructReader, String, T> function, T defaultValue) {
return isNull(columnName) ? defaultValue : function.apply(this, columnName);
}

/**
* @param columnIndex index of the column
* @return the value of a non-{@code NULL} column with type {@link Type#json()}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,48 @@ public void builder() {
assertThat(struct.getLong(1)).isEqualTo(2);
}

@Test
public void getOrNullTests() {
Struct struct =
Struct.newBuilder()
.set("f1")
.to("x")
.set("f2")
.to(2)
.set("f3")
.to(Value.bool(null))
.build();
String column1 = struct.getOrNull(0, StructReader::getString);
assertThat(column1).isEqualTo("x");

Long column2 = struct.getOrNull(1, StructReader::getLong);
assertThat(column2).isEqualTo(2);

String column3 = struct.getOrNull("f3", StructReader::getString);
assertThat(column3).isNull();
}

@Test
public void getOrDefaultTests() {
Struct struct =
Struct.newBuilder()
.set("f1")
.to("x")
.set("f2")
.to(2)
.set("f3")
.to(Value.bool(null))
.build();
String column1 = struct.getOrDefault(0, StructReader::getString, "");
assertThat(column1).isEqualTo("x");

Long column2 = struct.getOrDefault("f2", StructReader::getLong, -1L);
assertThat(column2).isEqualTo(2);

String column3 = struct.getOrDefault(2, StructReader::getString, "");
assertThat(column3).isEqualTo("");
}

@Test
public void duplicateFields() {
// Duplicate fields are allowed - some SQL queries produce this type of value.
Expand Down