Skip to content

Collection support for predicates on meta fields. #1338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public N1qlQueryCreator(final PartTree tree, final ParameterAccessor accessor, f
protected QueryCriteria create(final Part part, final Iterator<Object> iterator) {
PersistentPropertyPath<CouchbasePersistentProperty> path = context.getPersistentPropertyPath(part.getProperty());
CouchbasePersistentProperty property = path.getLeafProperty();
return from(part, property, where(addMetaIfRequired(bucketName, path, property, entity)), iterator);
return from(part, property, where(addMetaIfRequired(null, path, property, entity)), iterator);
}

@Override
Expand Down Expand Up @@ -187,17 +187,28 @@ private QueryCriteria from(final Part part, final CouchbasePersistentProperty pr
}
}

/**
* Translate meta-fields to META(bucketName).id, cas, expiry.<br>
* If bucketName is null, META().id etc, <br>
* If not a meta-field, just create the corresponding path
*
* @param bucketName
* @param persistentPropertyPath
* @param property
* @param entity
* @return N1QLExpression
*/
public static N1QLExpression addMetaIfRequired(String bucketName,
final PersistentPropertyPath<CouchbasePersistentProperty> persistentPropertyPath,
final CouchbasePersistentProperty property, final PersistentEntity entity) {
if (entity != null && property == entity.getIdProperty()) {
return path(meta(i(bucketName)), i(META_ID_PROPERTY));
return path(meta(bucketName != null ? i(bucketName) : x("")), i(META_ID_PROPERTY));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can the bucket name be empty?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That's actually what the fix is (line 79 above). When creating the N1qlQuery (a query derived from a method name), the collection name is not yet available so it just uses meta().id instead of meta(collectionName).id. - which is fine, because in derived queries, meta().id is unambiguous - the collection/bucket is determined by the FROM clause which is constructed later when the collection name is available.

}
if (property == entity.getVersionProperty()) {
return path(meta(i(bucketName)), i(META_CAS_PROPERTY));
return path(meta(bucketName != null ? i(bucketName) : x("")), i(META_CAS_PROPERTY));
}
if (property.isExpirationProperty()) {
return path(meta(i(bucketName)), i(META_EXPIRATION_PROPERTY));
return path(meta(bucketName != null ? i(bucketName) : x("")), i(META_EXPIRATION_PROPERTY));
}
return x(persistentPropertyPath.toDotPath(cvtr));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors
* Copyright 2012-2022 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,10 +40,13 @@ public interface UserColRepository extends CouchbaseRepository<UserCol, String>,

// CouchbaseRepositoryQueryCollectionIntegrationTests.testScopeCollectionAnnotationSwap() relies on this
// being commented out.
//<S extends UserCol> S save(S var1);
// <S extends UserCol> S save(S var1);

List<UserCol> findByFirstname(String firstname);

@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
UserCol getById(String id);

List<UserCol> findByFirstnameIn(String... firstnames);

List<UserCol> findByFirstnameIn(JsonArray firstnames);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 the original author or authors.
* Copyright 2017-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -91,6 +91,16 @@ public void afterEach() {
super.afterEach();
}

@Test
void findByKey() {
UserCol userCol = new UserCol("101", "userColFirst", "userColLast");
userColRepository.save(userCol);
UserCol found = userColRepository.getById(userCol.getId());
System.err.println("found: " + found);
assertEquals(userCol, found);
userColRepository.delete(found);
}

@Test
public void myTest() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ void createsQueryFindByIdIsNotNullAndFirstname() throws Exception {
converter, bucketName);
Query query = creator.createQuery();

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

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

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

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