Skip to content

Commit 5c5dde3

Browse files
authored
Collection support for predicates on meta fields. (#1338)
When a derived query is being created and there is a predicate on a meta-field ( meta().id, cas, expiry), translate that field (i.e. id -> meta().id) *without* the bucket or collection name, as it might apply to a collelction which we do not know yet (for instance, if there is a withCollection() specified).. This is fine, because in a derived query, meta() without the bucket or collection name is unambigous.
1 parent 19c9e16 commit 5c5dde3

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

src/main/java/org/springframework/data/couchbase/repository/query/N1qlQueryCreator.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public N1qlQueryCreator(final PartTree tree, final ParameterAccessor accessor, f
7676
protected QueryCriteria create(final Part part, final Iterator<Object> iterator) {
7777
PersistentPropertyPath<CouchbasePersistentProperty> path = context.getPersistentPropertyPath(part.getProperty());
7878
CouchbasePersistentProperty property = path.getLeafProperty();
79-
return from(part, property, where(addMetaIfRequired(bucketName, path, property, entity)), iterator);
79+
return from(part, property, where(addMetaIfRequired(null, path, property, entity)), iterator);
8080
}
8181

8282
@Override
@@ -187,17 +187,28 @@ private QueryCriteria from(final Part part, final CouchbasePersistentProperty pr
187187
}
188188
}
189189

190+
/**
191+
* Translate meta-fields to META(bucketName).id, cas, expiry.<br>
192+
* If bucketName is null, META().id etc, <br>
193+
* If not a meta-field, just create the corresponding path
194+
*
195+
* @param bucketName
196+
* @param persistentPropertyPath
197+
* @param property
198+
* @param entity
199+
* @return N1QLExpression
200+
*/
190201
public static N1QLExpression addMetaIfRequired(String bucketName,
191202
final PersistentPropertyPath<CouchbasePersistentProperty> persistentPropertyPath,
192203
final CouchbasePersistentProperty property, final PersistentEntity entity) {
193204
if (entity != null && property == entity.getIdProperty()) {
194-
return path(meta(i(bucketName)), i(META_ID_PROPERTY));
205+
return path(meta(bucketName != null ? i(bucketName) : x("")), i(META_ID_PROPERTY));
195206
}
196207
if (property == entity.getVersionProperty()) {
197-
return path(meta(i(bucketName)), i(META_CAS_PROPERTY));
208+
return path(meta(bucketName != null ? i(bucketName) : x("")), i(META_CAS_PROPERTY));
198209
}
199210
if (property.isExpirationProperty()) {
200-
return path(meta(i(bucketName)), i(META_EXPIRATION_PROPERTY));
211+
return path(meta(bucketName != null ? i(bucketName) : x("")), i(META_EXPIRATION_PROPERTY));
201212
}
202213
return x(persistentPropertyPath.toDotPath(cvtr));
203214
}

src/test/java/org/springframework/data/couchbase/domain/UserColRepository.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,10 +40,13 @@ public interface UserColRepository extends CouchbaseRepository<UserCol, String>,
4040

4141
// CouchbaseRepositoryQueryCollectionIntegrationTests.testScopeCollectionAnnotationSwap() relies on this
4242
// being commented out.
43-
//<S extends UserCol> S save(S var1);
43+
// <S extends UserCol> S save(S var1);
4444

4545
List<UserCol> findByFirstname(String firstname);
4646

47+
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
48+
UserCol getById(String id);
49+
4750
List<UserCol> findByFirstnameIn(String... firstnames);
4851

4952
List<UserCol> findByFirstnameIn(JsonArray firstnames);

src/test/java/org/springframework/data/couchbase/repository/query/CouchbaseRepositoryQueryCollectionIntegrationTests.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2021 the original author or authors.
2+
* Copyright 2017-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -91,6 +91,16 @@ public void afterEach() {
9191
super.afterEach();
9292
}
9393

94+
@Test
95+
void findByKey() {
96+
UserCol userCol = new UserCol("101", "userColFirst", "userColLast");
97+
userColRepository.save(userCol);
98+
UserCol found = userColRepository.getById(userCol.getId());
99+
System.err.println("found: " + found);
100+
assertEquals(userCol, found);
101+
userColRepository.delete(found);
102+
}
103+
94104
@Test
95105
public void myTest() {
96106

src/test/java/org/springframework/data/couchbase/repository/query/N1qlQueryCreatorTests.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ void createsQueryFindByIdIsNotNullAndFirstname() throws Exception {
189189
converter, bucketName);
190190
Query query = creator.createQuery();
191191

192-
assertEquals(query.export(),
193-
" WHERE " + where(x("META(`" + bucketName + "`).`id`")).isNotNull().and(i("firstname")).is("Oliver").export());
192+
assertEquals(" WHERE " + where(x("META().`id`")).isNotNull().and(i("firstname")).is("Oliver").export(),
193+
query.export());
194194
}
195195

196196
@Test // https://github.com/spring-projects/spring-data-couchbase/issues/1072
@@ -204,8 +204,9 @@ void createsQueryFindByVersionEqualsAndAndFirstname() throws Exception {
204204
getAccessor(getParameters(method), 1611287177404088320L, "Oliver"), queryMethod, converter, bucketName);
205205
Query query = creator.createQuery();
206206

207-
assertEquals(query.export(), " WHERE " + where(x("META(`" + bucketName + "`).`cas`")).is(1611287177404088320L)
208-
.and(i("firstname")).is("Oliver").export());
207+
assertEquals(
208+
" WHERE " + where(x("META().`cas`")).is(1611287177404088320L).and(i("firstname")).is("Oliver").export(),
209+
query.export());
209210
}
210211

211212
private ParameterAccessor getAccessor(Parameters<?, ?> params, Object... values) {

0 commit comments

Comments
 (0)