Add support for ignoreCase in queries derived from method names.

Closes #984, #1481.
This commit is contained in:
Michael Reiche
2022-06-23 09:40:34 -07:00
parent 1d6c033a41
commit 62feffa89d
8 changed files with 219 additions and 102 deletions

View File

@@ -174,7 +174,7 @@ class QueryCriteriaTests {
@Test
void testNotContaining() {
QueryCriteria c = where(i("name")).notContaining("Elvis");
assertEquals("not( (contains(`name`, \"Elvis\")) )", c.export());
assertEquals("not (contains(`name`, \"Elvis\"))", c.export());
}
@Test
@@ -192,7 +192,7 @@ class QueryCriteriaTests {
@Test
void testNotLike() {
QueryCriteria c = where(i("name")).notLike("%Elvis%");
assertEquals("not(`name` like \"%Elvis%\")", c.export());
assertEquals("not( ( (`name` like \"%Elvis%\")) )", c.export());
}
@Test

View File

@@ -43,6 +43,8 @@ public interface UserRepository extends CouchbaseRepository<User, String> {
List<User> findByFirstname(String firstname);
List<User> findByFirstnameIgnoreCase(String firstname);
Stream<User> findByLastname(String lastname);
List<User> findByFirstnameIn(String... firstnames);

View File

@@ -255,7 +255,7 @@ public class CouchbaseRepositoryQuerydslIntegrationTests extends JavaIntegration
.filter(a -> a.getName().toUpperCase().startsWith(united.getName().toUpperCase().substring(0, 5)))
.toArray(Airline[]::new)),
"[unexpected] -> [missing]");
assertEquals(" WHERE UPPER(name) like ($1||\"%\")", bq(predicate));
assertEquals(" WHERE lower(name) like ($1||\"%\")", bq(predicate));
}
}
@@ -281,7 +281,7 @@ public class CouchbaseRepositoryQuerydslIntegrationTests extends JavaIntegration
.filter(a -> a.getName().toUpperCase().endsWith(united.getName().toUpperCase().substring(1)))
.toArray(Airline[]::new)),
"[unexpected] -> [missing]");
assertEquals(" WHERE UPPER(name) like (\"%\"||$1)", bq(predicate));
assertEquals(" WHERE lower(name) like (\"%\"||$1)", bq(predicate));
}
}
@@ -293,7 +293,7 @@ public class CouchbaseRepositoryQuerydslIntegrationTests extends JavaIntegration
assertNull(comprises(result,
Arrays.stream(saved).filter(a -> a.getName().equalsIgnoreCase(flyByNight.getName())).toArray(Airline[]::new)),
"[unexpected] -> [missing]");
assertEquals(" WHERE UPPER(name) = $1", bq(predicate));
assertEquals(" WHERE lower(name) = $1", bq(predicate));
}
{
BooleanExpression predicate = airline.name.equalsIgnoreCase(united.getName());
@@ -302,7 +302,7 @@ public class CouchbaseRepositoryQuerydslIntegrationTests extends JavaIntegration
comprises(result,
Arrays.stream(saved).filter(a -> a.getName().equalsIgnoreCase(united.getName())).toArray(Airline[]::new)),
"[unexpected] -> [missing]");
assertEquals(" WHERE UPPER(name) = $1", bq(predicate));
assertEquals(" WHERE lower(name) = $1", bq(predicate));
}
}
@@ -329,7 +329,7 @@ public class CouchbaseRepositoryQuerydslIntegrationTests extends JavaIntegration
.filter(a -> a.getName().toUpperCase(Locale.ROOT).contains("united".toUpperCase(Locale.ROOT)))
.toArray(Airline[]::new)),
"[unexpected] -> [missing]");
assertEquals(" WHERE contains(UPPER(name), $1)", bq(predicate));
assertEquals(" WHERE contains(lower(name), $1)", bq(predicate));
}
}
@@ -356,7 +356,7 @@ public class CouchbaseRepositoryQuerydslIntegrationTests extends JavaIntegration
.filter(a -> a.getName().toUpperCase(Locale.ROOT).endsWith("Airlines".toUpperCase(Locale.ROOT)))
.toArray(Airline[]::new)),
"[unexpected] -> [missing]");
assertEquals(" WHERE UPPER(name) like $1", bq(predicate));
assertEquals(" WHERE lower(name) like $1", bq(predicate));
}
}

View File

@@ -23,6 +23,7 @@ import static org.springframework.data.couchbase.core.query.QueryCriteria.where;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -81,6 +82,21 @@ class N1qlQueryCreatorTests {
assertEquals(query.export(), " WHERE " + where(i("firstname")).is("Oliver").export());
}
@Test
void createsQueryCorrectlyIgnoreCase() throws Exception {
String input = "findByFirstnameIgnoreCase";
PartTree tree = new PartTree(input, User.class);
Method method = UserRepository.class.getMethod(input, String.class);
QueryMethod queryMethod = new QueryMethod(method, new DefaultRepositoryMetadata(UserRepository.class),
new SpelAwareProxyProjectionFactory());
N1qlQueryCreator creator = new N1qlQueryCreator(tree, getAccessor(getParameters(method), "Oliver"), queryMethod,
converter, bucketName);
Query query = creator.createQuery();
assertEquals(query.export(),
" WHERE " + where("lower(" + i("firstname") + ")").is("Oliver".toLowerCase(Locale.ROOT)).export());
}
@Test
void createsQueryFieldAnnotationCorrectly() throws Exception {
String input = "findByMiddlename";