Merge pull request #1948 from ebean-orm/feature/queryUsingDatabase

ENH: Add query.usingDatabase(database)
This commit is contained in:
Rob Bygrave
2020-02-20 16:42:25 +13:00
committed by GitHub
5 changed files with 67 additions and 3 deletions
+5
View File
@@ -656,6 +656,11 @@ public interface Query<T> {
*/
Query<T> usingConnection(Connection connection);
/**
* Execute the query using the given database.
*/
Query<T> usingDatabase(Database database);
/**
* Execute the query returning the list of Id's.
* <p>
@@ -2,6 +2,7 @@ package io.ebeaninternal.server.query;
import io.ebean.CacheMode;
import io.ebean.CountDistinctOrder;
import io.ebean.Database;
import io.ebean.DtoQuery;
import io.ebean.Expression;
import io.ebean.ExpressionFactory;
@@ -227,6 +228,11 @@ class DefaultFetchGroupQuery<T> implements SpiFetchGroupQuery<T> {
throw new RuntimeException("EB102: Only select() and fetch() clause is allowed on FetchGroup");
}
@Override
public Query<T> usingDatabase(Database database) {
throw new RuntimeException("EB102: Only select() and fetch() clause is allowed on FetchGroup");
}
@Nonnull
@Override
public <A> List<A> findIds() {
@@ -2,6 +2,7 @@ package io.ebeaninternal.server.querydefn;
import io.ebean.CacheMode;
import io.ebean.CountDistinctOrder;
import io.ebean.Database;
import io.ebean.DtoQuery;
import io.ebean.Expression;
import io.ebean.ExpressionFactory;
@@ -89,13 +90,13 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
private final Class<T> beanType;
private final ExpressionFactory expressionFactory;
private final BeanDescriptor<T> rootBeanDescriptor;
private BeanDescriptor<T> beanDescriptor;
private final SpiEbeanServer server;
private final ExpressionFactory expressionFactory;
private SpiEbeanServer server;
private SpiTransaction transaction;
@@ -1456,6 +1457,12 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
return this;
}
@Override
public Query<T> usingDatabase(Database database) {
this.server = (SpiEbeanServer) database;
return this;
}
@Override
public int delete() {
return server.delete(this, transaction);
@@ -1664,6 +1671,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
}
@Override
@Deprecated
public OrderBy<T> orderBy() {
return order();
}
@@ -1677,6 +1685,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
}
@Override
@Deprecated
public DefaultOrmQuery<T> orderBy(String orderByClause) {
return order(orderByClause);
}
@@ -1692,6 +1701,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
}
@Override
@Deprecated
public DefaultOrmQuery<T> setOrderBy(OrderBy<T> orderBy) {
return setOrder(orderBy);
}
@@ -103,6 +103,33 @@ public class DefaultTransactionThreadLocalTest extends BaseTestCase {
}
}
@ForPlatform(Platform.H2)
@Test
public void usingDatabase() {
// setup
Database otherDb = createOtherDatabase();
EBasicVer bean = new EBasicVer("TestUsingDatabase");
otherDb.save(bean);
// act
final EBasicVer foundOtherDb = DB.find(EBasicVer.class)
.usingDatabase(otherDb)
.where().eq("name", "TestUsingDatabase")
.findOne();
assertNotNull(foundOtherDb);
assertThat(foundOtherDb.getId()).isEqualTo(bean.getId());
final EBasicVer foundDefaultDb = DB.find(EBasicVer.class)
.where().eq("name", "TestUsingDatabase")
.findOne();
assertNull(foundDefaultDb);
otherDb.delete(bean);
}
private Database createOtherDatabase() {
@@ -2,6 +2,7 @@ package org.tests.basic;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.Database;
import io.ebean.Transaction;
import io.ebean.annotation.IgnorePlatform;
import io.ebean.annotation.Platform;
@@ -18,6 +19,21 @@ import static org.junit.Assert.assertEquals;
public class TestQueryUsingConnection extends BaseTestCase {
@Test
public void usingDatabase() {
ResetBasicData.reset();
final Database database = DB.getDefault();
int count =
DB.find(Country.class)
.usingDatabase(database)
.findCount();
assertThat(count).isGreaterThan(0);
}
@Test
public void usingConnection() throws SQLException {