DATACOUCH-141 - Change query lookup order and make N1QL default.

The @N1QL annotation was changed to @Query, for discoverability and also
convey that this is now the preferred method.

Order is now view if @View present, N1ql inline if @Query present with
a Statement, N1ql query derivation if @Query without attribute or no
annotation.

@View can be without attribute in order to compute the view
and design document from method name.
This commit is contained in:
Simon Baslé
2015-07-13 16:20:15 +02:00
parent 2bf2d3e544
commit 4920acc214
11 changed files with 121 additions and 41 deletions

View File

@@ -18,6 +18,8 @@ package org.springframework.data.couchbase.repository;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.view.Stale;
@@ -74,9 +76,14 @@ public class CouchbaseRepositoryViewTests {
assertThat(value, is(100L));
}
@Test(expected = InvalidDataAccessResourceUsageException.class)
@Test
public void shouldTrimOffFindOnCustomFinder() {
repository.findAllSomething();
try {
repository.findAllSomething();
fail("Expected InvalidDataAccessResourceException");
} catch (InvalidDataAccessResourceUsageException e) {
assertTrue(e.getMessage(), e.getMessage().startsWith("View user/allSomething does not exist"));
}
}
}

View File

@@ -31,6 +31,7 @@ public interface CustomUserRepository extends CouchbaseRepository<User, String>
@View(designDocument = "userCustom", viewName = "customCountView")
long count();
@View
Iterable<User> findAllSomething();
}

View File

@@ -19,6 +19,7 @@ package org.springframework.data.couchbase.repository;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.view.Stale;
@@ -146,4 +147,26 @@ public class SimpleCouchbaseRepositoryTests {
assertEquals("uname-2", user.getUsername());
}
@Test
public void shouldFindContainsWithoutAnnotation() {
List<User> users = repository.findByUsernameContains("-9");
assertNotNull(users);
assertFalse(users.isEmpty());
for (User user : users) {
assertTrue(user.getUsername().startsWith("uname-9"));
}
}
@Test
public void shouldDefaultToN1qlQueryDerivation() {
try {
User u = repository.findByUsernameNear("london");
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
if (!e.getMessage().contains("N1QL")) {
fail(e.getMessage());
}
}
}
}

View File

@@ -20,7 +20,7 @@ import java.util.List;
import com.couchbase.client.java.view.ViewQuery;
import org.springframework.data.couchbase.core.view.N1QL;
import org.springframework.data.couchbase.core.view.Query;
import org.springframework.data.couchbase.core.view.View;
/**
@@ -31,13 +31,16 @@ public interface UserRepository extends CouchbaseRepository<User, String> {
@View(designDocument = "user", viewName = "all")
Iterable<User> customViewQuery(ViewQuery query);
@N1QL("$SELECT_ENTITY$ WHERE username = $1")
@Query("$SELECT_ENTITY$ WHERE username = $1")
User findByUsername(String username);
@N1QL("SELECT * FROM $BUCKET$ WHERE username = $1")
@Query("SELECT * FROM $BUCKET$ WHERE username = $1")
User findByUsernameBadSelect(String username);
@N1QL
@Query
User findByUsernameRegexAndUsernameIn(String regex, List<String> sample);
List<User> findByUsernameContains(String contains);
User findByUsernameNear(String place);//this is to check that there's a N1QL derivation AND it fails
}