mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Merge pull request #2365 from FOCONIS/pr/bugfix/cache_exists_queries
Allow caching of exists-queries
This commit is contained in:
@@ -81,6 +81,11 @@ public interface SpiQuery<T> extends Query<T>, SpiQueryFetch, TxnProfileEventCod
|
||||
*/
|
||||
ID_LIST(FIND_ID_LIST, "findIds"),
|
||||
|
||||
/**
|
||||
* Find exists.
|
||||
*/
|
||||
EXISTS(FIND_EXISTS, "exists"),
|
||||
|
||||
/**
|
||||
* Find single attribute.
|
||||
*/
|
||||
|
||||
@@ -26,6 +26,7 @@ public interface TxnProfileEventCodes {
|
||||
String FIND_MANY = "fm";
|
||||
String FIND_ITERATE = "fe";
|
||||
String FIND_ID_LIST = "fi";
|
||||
String FIND_EXISTS = "ex";
|
||||
String FIND_ATTRIBUTE = "fa";
|
||||
String FIND_COUNT = "fc";
|
||||
String FIND_SUBQUERY = "fs";
|
||||
|
||||
@@ -1296,7 +1296,12 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
|
||||
@Override
|
||||
public <T> boolean exists(Query<T> ormQuery, Transaction transaction) {
|
||||
Query<T> ormQueryCopy = ormQuery.copy().setMaxRows(1);
|
||||
SpiOrmQueryRequest<?> request = createQueryRequest(Type.ID_LIST, ormQueryCopy, transaction);
|
||||
SpiOrmQueryRequest<?> request = createQueryRequest(Type.EXISTS, ormQueryCopy, transaction);
|
||||
List<Object> ids = request.getFromQueryCache();
|
||||
if (ids != null) {
|
||||
return !ids.isEmpty();
|
||||
}
|
||||
|
||||
try {
|
||||
request.initTransIfRequired();
|
||||
return !request.findIds().isEmpty();
|
||||
|
||||
@@ -16,7 +16,10 @@ import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestQueryCache extends BaseTestCase {
|
||||
|
||||
@@ -126,15 +129,52 @@ public class TestQueryCache extends BaseTestCase {
|
||||
// and now, ensure that we hit the database
|
||||
LoggedSql.start();
|
||||
int count2 = DB.find(EColAB.class)
|
||||
.setUseQueryCache(CacheMode.OFF)
|
||||
.where()
|
||||
.eq("columnB", "count")
|
||||
.findCount();
|
||||
.setUseQueryCache(CacheMode.OFF)
|
||||
.where()
|
||||
.eq("columnB", "count")
|
||||
.findCount();
|
||||
assertThat(count2).isEqualTo(count1);
|
||||
sql = LoggedSql.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exists() {
|
||||
|
||||
new EColAB("06", "exists").save();
|
||||
new EColAB("07", "exists").save();
|
||||
|
||||
LoggedSql.start();
|
||||
|
||||
boolean exists0 = DB.find(EColAB.class)
|
||||
.setUseQueryCache(CacheMode.ON)
|
||||
.where()
|
||||
.eq("columnB", "exists")
|
||||
.exists();
|
||||
|
||||
boolean exists1 = DB.find(EColAB.class)
|
||||
.setUseQueryCache(CacheMode.ON)
|
||||
.where()
|
||||
.eq("columnB", "exists")
|
||||
.exists();
|
||||
|
||||
List<String> sql = LoggedSql.stop();
|
||||
|
||||
assertThat(exists0).isEqualTo(exists1);
|
||||
assertThat(sql).hasSize(1);
|
||||
|
||||
// and now, ensure that we hit the database
|
||||
LoggedSql.start();
|
||||
boolean exists2 = DB.find(EColAB.class)
|
||||
.setUseQueryCache(CacheMode.OFF)
|
||||
.where()
|
||||
.eq("columnB", "exists")
|
||||
.exists();
|
||||
assertThat(exists2).isEqualTo(exists1);
|
||||
sql = LoggedSql.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findCountDifferentQueries() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user