mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1689 - ENH: Add query usingTransaction() and usingConnection()
This commit is contained in:
@@ -9,6 +9,7 @@ import io.ebean.search.TextSimple;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.persistence.NonUniqueResultException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
@@ -186,6 +187,16 @@ public interface ExpressionList<T> {
|
||||
*/
|
||||
Query<T> setIncludeSoftDeletes();
|
||||
|
||||
/**
|
||||
* Execute the query using the given transaction.
|
||||
*/
|
||||
Query<T> usingTransaction(Transaction transaction);
|
||||
|
||||
/**
|
||||
* Execute the query using the given connection.
|
||||
*/
|
||||
Query<T> usingConnection(Connection connection);
|
||||
|
||||
/**
|
||||
* Execute as a delete query deleting the 'root level' beans that match the predicates
|
||||
* in the query.
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.ebean;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.persistence.NonUniqueResultException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -631,6 +632,16 @@ public interface Query<T> {
|
||||
*/
|
||||
Query<T> apply(FetchPath fetchPath);
|
||||
|
||||
/**
|
||||
* Execute the query using the given transaction.
|
||||
*/
|
||||
Query<T> usingTransaction(Transaction transaction);
|
||||
|
||||
/**
|
||||
* Execute the query using the given connection.
|
||||
*/
|
||||
Query<T> usingConnection(Connection connection);
|
||||
|
||||
/**
|
||||
* Execute the query returning the list of Id's.
|
||||
* <p>
|
||||
|
||||
@@ -36,6 +36,7 @@ import io.ebeaninternal.api.SpiJunction;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -358,6 +359,16 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
|
||||
return query.apply(fetchPath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query<T> usingTransaction(Transaction transaction) {
|
||||
return query.usingTransaction(transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query<T> usingConnection(Connection connection) {
|
||||
return query.usingConnection(connection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int delete() {
|
||||
return query.delete();
|
||||
|
||||
@@ -34,6 +34,7 @@ import io.ebeaninternal.api.SpiJunction;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
@@ -330,6 +331,16 @@ class JunctionExpression<T> implements SpiJunction<T>, SpiExpression, Expression
|
||||
throw new IllegalStateException("filterMany not allowed on Junction expression list");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query<T> usingTransaction(Transaction transaction) {
|
||||
return exprList.usingTransaction(transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query<T> usingConnection(Connection connection) {
|
||||
return exprList.usingConnection(connection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int delete() {
|
||||
return exprList.delete();
|
||||
|
||||
@@ -44,6 +44,7 @@ import io.ebeaninternal.api.SpiExpressionValidation;
|
||||
import io.ebeaninternal.api.SpiNamedParam;
|
||||
import io.ebeaninternal.api.SpiQuery;
|
||||
import io.ebeaninternal.api.SpiQuerySecondary;
|
||||
import io.ebeaninternal.api.SpiTransaction;
|
||||
import io.ebeaninternal.server.autotune.ProfilingListener;
|
||||
import io.ebeaninternal.server.core.SpiOrmQueryRequest;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
@@ -56,11 +57,12 @@ import io.ebeaninternal.server.expression.SimpleExpression;
|
||||
import io.ebeaninternal.server.query.CancelableQuery;
|
||||
import io.ebeaninternal.server.query.NativeSqlQueryPlanKey;
|
||||
import io.ebeaninternal.server.rawsql.SpiRawSql;
|
||||
import io.ebeaninternal.server.transaction.ExternalJdbcTransaction;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@@ -91,6 +93,8 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
|
||||
private final ExpressionFactory expressionFactory;
|
||||
|
||||
private SpiTransaction transaction;
|
||||
|
||||
/**
|
||||
* For lazy loading of ManyToMany we need to add a join to the intersection table. This is that
|
||||
* join to the intersection table.
|
||||
@@ -812,6 +816,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
@Override
|
||||
public DefaultOrmQuery<T> copy(SpiEbeanServer server) {
|
||||
DefaultOrmQuery<T> copy = new DefaultOrmQuery<>(beanDescriptor, server, expressionFactory);
|
||||
copy.transaction = transaction;
|
||||
copy.m2mIncludeJoin = m2mIncludeJoin;
|
||||
copy.profilingListener = profilingListener;
|
||||
copy.profileLocation = profileLocation;
|
||||
@@ -1431,9 +1436,21 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query<T> usingTransaction(Transaction transaction) {
|
||||
this.transaction = (SpiTransaction)transaction;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query<T> usingConnection(Connection connection) {
|
||||
this.transaction = new ExternalJdbcTransaction(connection);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int delete() {
|
||||
return server.delete(this, null);
|
||||
return server.delete(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1443,7 +1460,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
|
||||
@Override
|
||||
public int update() {
|
||||
return server.update(this, null);
|
||||
return server.update(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1456,12 +1473,12 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
// a copy of this query is made in the server
|
||||
// as the query needs to modified (so we modify
|
||||
// the copy rather than this query instance)
|
||||
return server.findIds(this, null);
|
||||
return server.findIds(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
return server.exists(this, null);
|
||||
return server.exists(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1469,28 +1486,28 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
// a copy of this query is made in the server
|
||||
// as the query needs to modified (so we modify
|
||||
// the copy rather than this query instance)
|
||||
return server.findCount(this, null);
|
||||
return server.findCount(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void findEachWhile(Predicate<T> consumer) {
|
||||
server.findEachWhile(this, consumer, null);
|
||||
server.findEachWhile(this, consumer, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void findEach(Consumer<T> consumer) {
|
||||
server.findEach(this, consumer, null);
|
||||
server.findEach(this, consumer, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryIterator<T> findIterate() {
|
||||
return server.findIterate(this, null);
|
||||
return server.findIterate(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Version<T>> findVersions() {
|
||||
this.temporalMode = TemporalMode.VERSIONS;
|
||||
return server.findVersions(this, null);
|
||||
return server.findVersions(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1501,28 +1518,28 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
this.temporalMode = TemporalMode.VERSIONS;
|
||||
this.versionsStart = start;
|
||||
this.versionsEnd = end;
|
||||
return server.findVersions(this, null);
|
||||
return server.findVersions(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> findList() {
|
||||
return server.findList(this, null);
|
||||
return server.findList(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<T> findSet() {
|
||||
return server.findSet(this, null);
|
||||
return server.findSet(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K> Map<K, T> findMap() {
|
||||
return server.findMap(this, null);
|
||||
return server.findMap(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <A> List<A> findSingleAttributeList() {
|
||||
return (List<A>) server.findSingleAttributeList(this, null);
|
||||
return (List<A>) server.findSingleAttributeList(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1533,32 +1550,32 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
|
||||
@Override
|
||||
public T findOne() {
|
||||
return server.findOne(this, null);
|
||||
return server.findOne(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<T> findOneOrEmpty() {
|
||||
return server.findOneOrEmpty(this, null);
|
||||
return server.findOneOrEmpty(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FutureIds<T> findFutureIds() {
|
||||
return server.findFutureIds(this, null);
|
||||
return server.findFutureIds(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FutureList<T> findFutureList() {
|
||||
return server.findFutureList(this, null);
|
||||
return server.findFutureList(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FutureRowCount<T> findFutureCount() {
|
||||
return server.findFutureCount(this, null);
|
||||
return server.findFutureCount(this, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PagedList<T> findPagedList() {
|
||||
return server.findPagedList(this, null);
|
||||
return server.findPagedList(this, transaction);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user