use of com.google.cloud.datastore.KeyValue in project spring-cloud-gcp by spring-cloud.
the class GqlDatastoreQueryTests method compoundNameConventionTest.
@Test
public void compoundNameConventionTest() {
String gql = "SELECT * FROM " + "|org.springframework.cloud.gcp.data.datastore." + "repository.query.GqlDatastoreQueryTests$Trade|" + " WHERE price=:#{#tag6 * -1} AND price<>:#{#tag6 * -1} OR " + "price<>:#{#tag7 * -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 " + "price>@tag6 AND price<=@tag7 AND trade_ref = @tag8) ORDER BY id DESC LIMIT 3;";
String entityResolvedGql = "SELECT * FROM trades" + " WHERE price=@SpELtag1 AND price<>@SpELtag2 OR price<>@SpELtag3 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 " + "price>@tag6 AND price<=@tag7 AND trade_ref = @tag8) ORDER BY id DESC LIMIT 3";
Trade trade = new Trade();
trade.id = "tradeId1";
Object[] paramVals = new Object[] { "BUY", "abcd", // this is an array param of the non-natively supported type and will need conversion
new int[] { 1, 2 }, new double[] { 8.88, 9.99 }, // this parameter is a simple int, which is not a directly supported type and uses
3, // conversions
"blahblah", 1.11, 2.22, trade };
String[] paramNames = new String[] { "tag0", "tag1", "tag2", "tag3", "tag4", "tag5", "tag6", "tag7", "tag8" };
buildParameters(paramVals, paramNames);
KeyFactory keyFactory = new KeyFactory("proj");
keyFactory.setKind("kind");
Key key = keyFactory.newKey("tradeid1-key");
doReturn(key).when(this.datastoreTemplate).getKey(any());
EvaluationContext evaluationContext = new StandardEvaluationContext();
for (int i = 0; i < paramVals.length; i++) {
evaluationContext.setVariable(paramNames[i], paramVals[i]);
}
when(this.evaluationContextProvider.getEvaluationContext(any(), any())).thenReturn(evaluationContext);
GqlDatastoreQuery gqlDatastoreQuery = createQuery(gql, false, false);
doAnswer((invocation) -> {
GqlQuery statement = invocation.getArgument(0);
assertThat(statement.getQueryString()).isEqualTo(entityResolvedGql);
Map<String, Value> paramMap = statement.getNamedBindings();
assertThat(paramMap.get("tag0").get()).isEqualTo(paramVals[0]);
assertThat(paramMap.get("tag1").get()).isEqualTo(paramVals[1]);
// custom conversion is expected to have been used in this param
assertThat((long) ((LongValue) (((List) paramMap.get("tag2").get()).get(0))).get()).isEqualTo(1L);
assertThat((long) ((LongValue) (((List) paramMap.get("tag2").get()).get(1))).get()).isEqualTo(2L);
double actual = ((DoubleValue) (((List) paramMap.get("tag3").get()).get(0))).get();
assertThat(actual).isEqualTo(((double[]) paramVals[3])[0], DELTA);
actual = ((DoubleValue) (((List) paramMap.get("tag3").get()).get(1))).get();
assertThat(actual).isEqualTo(((double[]) paramVals[3])[1], DELTA);
// 3L is expected even though 3 int was the original param due to custom conversions
assertThat(paramMap.get("tag4").get()).isEqualTo(3L);
assertThat(paramMap.get("tag5").get()).isEqualTo(paramVals[5]);
assertThat(paramMap.get("tag6").get()).isEqualTo(paramVals[6]);
assertThat(paramMap.get("tag7").get()).isEqualTo(paramVals[7]);
assertThat((double) paramMap.get("SpELtag1").get()).isEqualTo(-1 * (double) paramVals[6], DELTA);
assertThat((double) paramMap.get("SpELtag2").get()).isEqualTo(-1 * (double) paramVals[6], DELTA);
assertThat((double) paramMap.get("SpELtag3").get()).isEqualTo(-1 * (double) paramVals[7], DELTA);
assertThat(((KeyValue) paramMap.get("tag8")).get()).isSameAs(key);
return null;
}).when(this.datastoreTemplate).queryKeysOrEntities(any(), eq(Trade.class));
doReturn(false).when(gqlDatastoreQuery).isNonEntityReturnedType(any());
gqlDatastoreQuery.execute(paramVals);
verify(this.datastoreTemplate, times(1)).queryKeysOrEntities(any(), eq(Trade.class));
}
use of com.google.cloud.datastore.KeyValue in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method getReferenceEntitiesForSave.
private List<Entity> getReferenceEntitiesForSave(Object entity, Builder builder, Set<Key> persistedEntities) {
DatastorePersistentEntity datastorePersistentEntity = this.datastoreMappingContext.getPersistentEntity(entity.getClass());
List<Entity> entitiesToSave = new ArrayList<>();
datastorePersistentEntity.doWithAssociations((AssociationHandler) (association) -> {
PersistentProperty persistentProperty = association.getInverse();
PersistentPropertyAccessor accessor = datastorePersistentEntity.getPropertyAccessor(entity);
Object val = accessor.getProperty(persistentProperty);
if (val == null) {
return;
}
Value<?> value;
if (LazyUtil.isLazyAndNotLoaded(val)) {
value = LazyUtil.getKeys(val);
} else if (persistentProperty.isCollectionLike()) {
Iterable<?> iterableVal = (Iterable<?>) ValueUtil.toListIfArray(val);
entitiesToSave.addAll(getEntitiesForSave(iterableVal, persistedEntities));
List<KeyValue> keyValues = StreamSupport.stream((iterableVal).spliterator(), false).map((o) -> KeyValue.of(this.getKey(o, false))).collect(Collectors.toList());
value = ListValue.of(keyValues);
} else {
entitiesToSave.addAll(getEntitiesForSave(Collections.singletonList(val), persistedEntities));
Key key = getKey(val, false);
value = KeyValue.of(key);
}
builder.set(((DatastorePersistentProperty) persistentProperty).getFieldName(), value);
});
return entitiesToSave;
}
Aggregations