use of com.google.cloud.spanner.Value in project beam by apache.
the class MutationKeyEncoder method mutationAsMap.
private static Map<String, Value> mutationAsMap(Mutation m) {
Map<String, Value> result = new HashMap<>();
Iterator<String> coli = m.getColumns().iterator();
Iterator<Value> vali = m.getValues().iterator();
while (coli.hasNext()) {
String column = coli.next();
Value val = vali.next();
result.put(column.toLowerCase(), val);
}
return result;
}
use of com.google.cloud.spanner.Value in project beam by apache.
the class TestStructMapper method recordStructFrom.
private static Struct recordStructFrom(DataChangeRecord record, boolean useJsonFields) {
final Type columnTypeType = useJsonFields ? COLUMN_TYPE_JSON_TYPE : COLUMN_TYPE_STRING_TYPE;
final Type modType = useJsonFields ? MOD_JSON_TYPE : MOD_STRING_TYPE;
final Value columnTypes = Value.structArray(columnTypeType, record.getRowType().stream().map(rowType -> TestStructMapper.columnTypeStructFrom(rowType, useJsonFields)).collect(Collectors.toList()));
final Value mods = Value.structArray(modType, record.getMods().stream().map(mod -> TestStructMapper.modStructFrom(mod, useJsonFields)).collect(Collectors.toList()));
return Struct.newBuilder().set("commit_timestamp").to(record.getCommitTimestamp()).set("record_sequence").to(record.getRecordSequence()).set("server_transaction_id").to(record.getServerTransactionId()).set("is_last_record_in_transaction_in_partition").to(record.isLastRecordInTransactionInPartition()).set("table_name").to(record.getTableName()).set("column_types").to(columnTypes).set("mods").to(mods).set("mod_type").to(record.getModType().toString()).set("value_capture_type").to(record.getValueCaptureType().toString()).set("number_of_records_in_transaction").to(record.getNumberOfRecordsInTransaction()).set("number_of_partitions_in_transaction").to(record.getNumberOfPartitionsInTransaction()).build();
}
use of com.google.cloud.spanner.Value in project spanner-jdbc by olavloite.
the class CloudSpannerPreparedStatementTest method getValues.
private static String[] getValues(Iterable<Value> values) {
List<Value> valueList = Lists.newArrayList(values);
String[] res = new String[valueList.size()];
int index = 0;
for (Value value : valueList) {
res[index] = value.toString();
index++;
}
return res;
}
use of com.google.cloud.spanner.Value in project spring-cloud-gcp by spring-cloud.
the class SqlSpannerQueryTests method compoundNameConventionTest.
@Test
public void compoundNameConventionTest() throws NoSuchMethodException {
String sql = "SELECT DISTINCT * FROM " + ":org.springframework.cloud.gcp.data.spanner.repository.query.SqlSpannerQueryTests$Trade:" + "@{index=fakeindex}" + " WHERE price=#{#tag3 * -1} AND price<>#{#tag3 * -1} OR " + "price<>#{#tag4 * -1} AND " + "( action=@tag0 AND ticker=@tag1 ) OR " + "( trader_id=@tag2 AND price<@tag3 ) OR ( price>=@tag4 AND id<>NULL AND " + "trader_id=NULL AND trader_id LIKE %@tag5 AND price=TRUE AND price=FALSE AND " + "struct_val = @tag8 AND struct_val = @tag9 " + "price>@tag6 AND price<=@tag7 and price in unnest(@tag10)) ORDER BY id DESC LIMIT 3;";
// @formatter:off
String entityResolvedSql = "SELECT *, " + "ARRAY (SELECT AS STRUCT disabled, id, childId, value, " + "ARRAY (SELECT AS STRUCT canceled, documentId, id, childId, content " + "FROM documents WHERE (documents.id = children.id AND documents.childId = children.childId) " + "AND (canceled = false)) AS documents " + "FROM children WHERE (children.id = trades.id) AND (disabled = false)) AS children FROM " + "(SELECT DISTINCT * FROM trades@{index=fakeindex}" + " WHERE price=@SpELtag1 AND price<>@SpELtag1 OR price<>@SpELtag2 AND " + "( action=@tag0 AND ticker=@tag1 ) OR " + "( trader_id=@tag2 AND price<@tag3 ) OR ( price>=@tag4 AND id<>NULL AND " + "trader_id=NULL AND trader_id LIKE %@tag5 AND price=TRUE AND price=FALSE AND " + "struct_val = @tag8 AND struct_val = @tag9 " + "price>@tag6 AND price<=@tag7 and price in unnest(@tag10)) ORDER BY id DESC LIMIT 3) trades " + "ORDER BY COLA ASC , COLB DESC LIMIT 10 OFFSET 30";
// @formatter:on
Object[] params = new Object[] { "BUY", this.pageable, "abcd", "abc123", 8.88, 3.33, "blahblah", 1.11, 2.22, Struct.newBuilder().set("symbol").to("ABCD").set("action").to("BUY").build(), new SymbolAction("ABCD", "BUY"), Arrays.asList("a", "b") };
String[] paramNames = new String[] { "tag0", "ignoredPageable", "tag1", "tag2", "tag3", "tag4", "tag5", "tag6", "tag7", "tag8", "tag9", "tag10" };
when(queryMethod.isCollectionQuery()).thenReturn(false);
when(queryMethod.getReturnedObjectType()).thenReturn((Class) Trade.class);
EvaluationContext evaluationContext = new StandardEvaluationContext();
for (int i = 0; i < params.length; i++) {
evaluationContext.setVariable(paramNames[i], params[i]);
}
when(this.evaluationContextProvider.getEvaluationContext(any(), any())).thenReturn(evaluationContext);
SqlSpannerQuery sqlSpannerQuery = createQuery(sql, Trade.class, false);
doAnswer((invocation) -> {
Statement statement = invocation.getArgument(0);
SpannerQueryOptions queryOptions = invocation.getArgument(1);
assertThat(queryOptions.isAllowPartialRead()).isTrue();
assertThat(statement.getSql()).isEqualTo(entityResolvedSql);
Map<String, Value> paramMap = statement.getParameters();
assertThat(paramMap.get("tag0").getString()).isEqualTo(params[0]);
// params[1] is this.pageable that is ignored, hence no synthetic tag is created for it
assertThat(paramMap.get("tag1").getString()).isEqualTo(params[2]);
assertThat(paramMap.get("tag2").getString()).isEqualTo(params[3]);
assertThat(paramMap.get("tag3").getFloat64()).isEqualTo(params[4]);
assertThat(paramMap.get("tag4").getFloat64()).isEqualTo(params[5]);
assertThat(paramMap.get("tag5").getString()).isEqualTo(params[6]);
assertThat(paramMap.get("tag6").getFloat64()).isEqualTo(params[7]);
assertThat(paramMap.get("tag7").getFloat64()).isEqualTo(params[8]);
assertThat(paramMap.get("tag8").getStruct()).isEqualTo(params[9]);
assertThat(paramMap.get("tag10").getStringArray()).isEqualTo(params[11]);
verify(this.spannerEntityProcessor, times(1)).write(same(params[10]), any());
assertThat(paramMap.get("SpELtag1").getFloat64()).isEqualTo(-8.88, DELTA);
assertThat(paramMap.get("SpELtag2").getFloat64()).isEqualTo(-3.33, DELTA);
return null;
}).when(this.spannerTemplate).executeQuery(any(), any());
// This dummy method was created so the metadata for the ARRAY param inner type is
// provided.
Method method = QueryHolder.class.getMethod("dummyMethod", Object.class, Pageable.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, List.class);
when(this.queryMethod.getMethod()).thenReturn(method);
Mockito.<Parameters>when(this.queryMethod.getParameters()).thenReturn(new DefaultParameters(method));
sqlSpannerQuery.execute(params);
verify(this.spannerTemplate, times(1)).executeQuery(any(), any());
}
use of com.google.cloud.spanner.Value in project spring-cloud-gcp by spring-cloud.
the class SqlSpannerQueryTests method sortParamQueryTest.
@Test
public void sortParamQueryTest() throws NoSuchMethodException {
String sql = "SELECT * FROM :org.springframework.cloud.gcp.data.spanner.repository.query.SqlSpannerQueryTests$Child:" + " WHERE id = @id AND trader_id = @trader_id";
// @formatter:off
String entityResolvedSql = "SELECT *, " + "ARRAY (SELECT AS STRUCT canceled, documentId, id, childId, content " + "FROM documents WHERE (documents.id = children.id AND documents.childId = children.childId) " + "AND (canceled = false)) AS documents " + "FROM (SELECT * FROM children WHERE id = @id AND trader_id = @trader_id) children " + "WHERE disabled = false ORDER BY trader_id ASC";
// @formatter:on
Object[] params = new Object[] { "ID", "TRADER_ID", Sort.by(Order.asc("trader_id")) };
String[] paramNames = new String[] { "id", "trader_id", "ignoredSort" };
when(queryMethod.isCollectionQuery()).thenReturn(false);
when(queryMethod.getReturnedObjectType()).thenReturn((Class) Child.class);
EvaluationContext evaluationContext = new StandardEvaluationContext();
for (int i = 0; i < params.length; i++) {
evaluationContext.setVariable(paramNames[i], params[i]);
}
when(this.evaluationContextProvider.getEvaluationContext(any(), any())).thenReturn(evaluationContext);
SqlSpannerQuery sqlSpannerQuery = createQuery(sql, Child.class, false);
doAnswer((invocation) -> {
Statement statement = invocation.getArgument(0);
SpannerQueryOptions queryOptions = invocation.getArgument(1);
assertThat(queryOptions.isAllowPartialRead()).isTrue();
assertThat(statement.getSql()).isEqualTo(entityResolvedSql);
Map<String, Value> paramMap = statement.getParameters();
assertThat(paramMap.get("id").getString()).isEqualTo(params[0]);
assertThat(paramMap.get("trader_id").getString()).isEqualTo(params[1]);
assertThat(paramMap.get("ignoredSort")).isNull();
return null;
}).when(this.spannerTemplate).executeQuery(any(), any());
// This dummy method was created so the metadata for the ARRAY param inner type is
// provided.
Method method = QueryHolder.class.getMethod("dummyMethod5", String.class, String.class, Sort.class);
when(this.queryMethod.getMethod()).thenReturn(method);
Mockito.<Parameters>when(this.queryMethod.getParameters()).thenReturn(new DefaultParameters(method));
sqlSpannerQuery.execute(params);
verify(this.spannerTemplate, times(1)).executeQuery(any(), any());
}
Aggregations