DATACOUCH-136 - Add @N1QL query annotation.

The annotation allows to execute N1QL queries that are provided inline. For ease of use, one can use the placeholder $SELECT_ENTITY$ instead of writing the SELECT and FROM clauses.

QueryParams passed in as a method parameter will be used to enrich the Query. Other parameters on the annotated method will be used as values for positional placeholders in the statement.

The template's findByN1QL now expect enough data to reconstruct the full CouchbaseDocument (including ID and CAS), the ad hoc version of it has been renamed findByN1QLProjection.
This commit is contained in:
Simon Baslé
2015-07-06 16:17:39 +02:00
parent b9f23f3fd5
commit 9deaf6b480
14 changed files with 637 additions and 18 deletions

View File

@@ -241,7 +241,7 @@ public class CouchbaseTemplateTests {
.where(x("type").eq(s("fullFragment"))
.and(x("criteria").gt(1))));
List<Fragment> fragments = template.findByN1QL(query, Fragment.class);
List<Fragment> fragments = template.findByN1QLProjection(query, Fragment.class);
assertNotNull(fragments);
assertFalse(fragments.isEmpty());
assertEquals(1, fragments.size());

View File

@@ -27,6 +27,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.couchbase.IntegrationTestApplicationConfig;
import org.springframework.data.couchbase.core.CouchbaseQueryExecutionException;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactory;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
@@ -111,4 +112,25 @@ public class SimpleCouchbaseRepositoryTests {
assertEquals(2, size);
}
@Test
public void shouldFindByUsernameUsingN1ql() {
User user = repository.findByUsername("uname-1");
assertNotNull(user);
assertEquals("testuser-1", user.getKey());
assertEquals("uname-1", user.getUsername());
}
@Test
public void shouldFailFindByUsernameWithNoIdOrCas() {
try {
User user = repository.findByUsernameBadSelect("uname-1");
fail("shouldFailFindByUsernameWithNoIdOrCas");
} catch (CouchbaseQueryExecutionException e) {
assertTrue(e.getMessage().contains("_ID"));
assertTrue(e.getMessage().contains("_CAS"));
} catch (Exception e) {
fail("CouchbaseQueryExecutionException expected");
}
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.data.couchbase.repository;
import com.couchbase.client.java.view.ViewQuery;
import org.springframework.data.couchbase.core.view.N1QL;
import org.springframework.data.couchbase.core.view.View;
/**
@@ -28,4 +29,10 @@ public interface UserRepository extends CouchbaseRepository<User, String> {
@View(designDocument = "user", viewName = "all")
Iterable<User> customViewQuery(ViewQuery query);
@N1QL("$SELECT_ENTITY$ WHERE username = $1")
User findByUsername(String username);
@N1QL("SELECT * FROM $BUCKET$ WHERE username = $1")
User findByUsernameBadSelect(String username);
}