@Override public boolean getBoolean(int columnIndex) { return getCurrentRowAsStruct().getBoolean(columnIndex); }
switch (fieldType.getCode()) { case BOOL: return Value.bool(value.getBoolean(fieldIndex)); case INT64: return Value.int64(value.getLong(fieldIndex));
builder.set(field, nullValue ? null : row.getBoolean(fieldName)); break; case LONG:
@Override public boolean getBoolean(String columnName) { return getCurrentRowAsStruct().getBoolean(columnName); }
switch (fieldType.getCode()) { case BOOL: return Value.bool(value.getBoolean(fieldIndex)); case INT64: return Value.int64(value.getLong(fieldIndex));
@Test public void bindBool() { Struct row = execute(Statement.newBuilder("SELECT @v").bind("v").to(true).build(), Type.bool()); assertThat(row.isNull(0)).isFalse(); assertThat(row.getBoolean(0)).isEqualTo(true); }
@Test public void writeBool() { write(baseInsert().set("BoolValue").to(true).build()); Struct row = readLastRow("BoolValue"); assertThat(row.isNull(0)).isFalse(); assertThat(row.getBoolean(0)).isTrue(); }
@Test public void bindStructWithNullStructField() { Type emptyStructType = Type.struct(new ArrayList<StructField>()); Struct p = Struct.newBuilder().set("f1").to(emptyStructType, null).build(); Struct row = execute(Statement.newBuilder("SELECT @p.f1 IS NULL").bind("p").to(p).build(), Type.bool()); assertThat(row.getBoolean(0)).isTrue(); }
@Test public void simpleInsert() { TransactionManager manager = client.transactionManager(); TransactionContext txn = manager.begin(); assertThat(manager.getState()).isEqualTo(TransactionState.STARTED); txn.buffer( Mutation.newInsertBuilder("T").set("K").to("Key1").set("BoolValue").to(true).build()); manager.commit(); assertThat(manager.getState()).isEqualTo(TransactionState.COMMITTED); Struct row = client.singleUse().readRow("T", Key.of("Key1"), Arrays.asList("K", "BoolValue")); assertThat(row.getString(0)).isEqualTo("Key1"); assertThat(row.getBoolean(1)).isTrue(); }
@Test public void bindEmptyStruct() { Struct p = Struct.newBuilder().build(); Struct row = execute(Statement.newBuilder("SELECT @p IS NULL").bind("p").to(p).build(), Type.bool()); assertThat(row.getBoolean(0)).isFalse(); }
private void assertRow(Struct actualRow, JSONArray expectedRow) throws Exception { assertThat(actualRow.getColumnCount()).isEqualTo(expectedRow.length()); for (int i = 0; i < expectedRow.length(); i++) { switch (actualRow.getColumnType(i).getCode()) { case BOOL: assertThat(actualRow.getBoolean(i)).isEqualTo(expectedRow.getBoolean(i)); break; case STRING: assertThat(actualRow.getString(i)).isEqualTo(expectedRow.getString(i)); break; case INT64: assertThat(actualRow.getLong(i)).isEqualTo(expectedRow.getLong(i)); break; case FLOAT64: assertThat(actualRow.getDouble(i)).isEqualTo(expectedRow.getDouble(i)); break; case BYTES: assertThat(actualRow.getBytes(i)) .isEqualTo(ByteArray.fromBase64(expectedRow.getString(i))); break; case ARRAY: Type elementType = actualRow.getColumnType(i).getArrayElementType(); assertArray(getRawList(actualRow, i, elementType), expectedRow.getJSONArray(i)); break; default: Assert.fail("Unexpected type code:" + actualRow.getColumnType(i).getCode()); } } }
@Test public void bindArrayOfStructNull() { Type elementType = Type.struct( asList( Type.StructField.of("f1", Type.string()), Type.StructField.of("f2", Type.float64()))); Struct row = execute( Statement.newBuilder("SELECT @p IS NULL") .bind("p") .toStructArray(elementType, null) .build(), Type.bool()); assertThat(row.getBoolean(0)).isTrue(); }
@Test public void bindStructNull() { Struct row = execute( Statement.newBuilder("SELECT @p IS NULL") .bind("p") .to( Type.struct( asList( Type.StructField.of("f1", Type.string()), Type.StructField.of("f2", Type.float64()))), null) .build(), Type.bool()); assertThat(row.getBoolean(0)).isTrue(); }
Struct row = client.singleUse().readRow("T", Key.of("Key3"), Arrays.asList("K", "BoolValue")); assertThat(row.getString(0)).isEqualTo("Key3"); assertThat(row.getBoolean(1)).isTrue();
@Override public boolean getBoolean(int columnIndex) { return getCurrentRowAsStruct().getBoolean(columnIndex); }
@Override public boolean getBoolean(String columnName) { return getCurrentRowAsStruct().getBoolean(columnName); }
case BOOL: return nullSafeColumnParser( (currentRow, columnName) -> Boolean.toString(currentRow.getBoolean(columnName))); case INT64: return nullSafeColumnParser(
@Override protected List executeRawResult(Object[] parameters) { if (isCountOrExistsQuery()) { return SpannerStatementQueryExecutor.executeQuery( (struct) -> isCountQuery() ? struct.getLong(0) : struct.getBoolean(0), this.entityType, this.tree, parameters, this.spannerTemplate, this.spannerMappingContext); } if (this.tree.isDelete()) { return this.spannerTemplate .performReadWriteTransaction(getDeleteFunction(parameters)); } return SpannerStatementQueryExecutor.executeQuery(this.entityType, this.tree, parameters, this.spannerTemplate, this.spannerMappingContext); }
@Override protected List executeRawResult(Object[] parameters) { if (isCountOrExistsQuery()) { return SpannerStatementQueryExecutor.executeQuery( (struct) -> isCountQuery() ? struct.getLong(0) : struct.getBoolean(0), this.entityType, this.tree, parameters, this.spannerTemplate, this.spannerMappingContext); } if (this.tree.isDelete()) { return this.spannerTemplate .performReadWriteTransaction(getDeleteFunction(parameters)); } return SpannerStatementQueryExecutor.executeQuery(this.entityType, this.tree, parameters, this.spannerTemplate, this.spannerMappingContext); }