diff --git a/sql-core/src/main/java/com/easy/query/core/basic/api/insert/AbstractInsertable.java b/sql-core/src/main/java/com/easy/query/core/basic/api/insert/AbstractInsertable.java index 5f2044bbd..5231c56c1 100644 --- a/sql-core/src/main/java/com/easy/query/core/basic/api/insert/AbstractInsertable.java +++ b/sql-core/src/main/java/com/easy/query/core/basic/api/insert/AbstractInsertable.java @@ -2,7 +2,7 @@ import com.easy.query.core.basic.jdbc.executor.EntityExpressionExecutor; import com.easy.query.core.basic.jdbc.parameter.ToSQLContext; -import com.easy.query.core.basic.plugin.interceptor.InterceptorEntry; +import com.easy.query.core.basic.plugin.interceptor.Interceptor; import com.easy.query.core.configuration.QueryConfiguration; import com.easy.query.core.context.QueryRuntimeContext; import com.easy.query.core.enums.ExecuteMethodEnum; @@ -52,11 +52,11 @@ public Insertable insert(T entity) { protected void insertBefore() { //是否使用自定义插入策略 - List insertInterceptors = entityMetadata.getEntityInterceptors(); + List insertInterceptors = entityMetadata.getEntityInterceptors(); if (EasyCollectionUtil.isNotEmpty(insertInterceptors)) { QueryConfiguration easyQueryConfiguration = entityInsertExpressionBuilder.getRuntimeContext().getQueryConfiguration(); List entityInterceptors = entityInsertExpressionBuilder.getExpressionContext().getInterceptorFilter(insertInterceptors) - .map(interceptor -> (EntityInterceptor) easyQueryConfiguration.getEasyInterceptor(interceptor.getName())).collect(Collectors.toList()); + .map(interceptor -> (EntityInterceptor) easyQueryConfiguration.getEasyInterceptor(interceptor)).filter(Interceptor::enable).collect(Collectors.toList()); if (EasyCollectionUtil.isNotEmpty(entityInterceptors)) { Class entityClass = entityMetadata.getEntityClass(); for (T entity : entities) { diff --git a/sql-core/src/main/java/com/easy/query/core/basic/api/update/abstraction/AbstractEntityUpdatable.java b/sql-core/src/main/java/com/easy/query/core/basic/api/update/abstraction/AbstractEntityUpdatable.java index 5b228d929..7bfbafc56 100644 --- a/sql-core/src/main/java/com/easy/query/core/basic/api/update/abstraction/AbstractEntityUpdatable.java +++ b/sql-core/src/main/java/com/easy/query/core/basic/api/update/abstraction/AbstractEntityUpdatable.java @@ -5,7 +5,7 @@ import com.easy.query.core.basic.jdbc.executor.EntityExpressionExecutor; import com.easy.query.core.basic.jdbc.parameter.DefaultToSQLContext; import com.easy.query.core.basic.jdbc.parameter.ToSQLContext; -import com.easy.query.core.basic.plugin.interceptor.InterceptorEntry; +import com.easy.query.core.basic.plugin.interceptor.Interceptor; import com.easy.query.core.configuration.QueryConfiguration; import com.easy.query.core.context.QueryRuntimeContext; import com.easy.query.core.enums.ExecuteMethodEnum; @@ -19,7 +19,6 @@ import com.easy.query.core.expression.sql.builder.EntityTableExpressionBuilder; import com.easy.query.core.expression.sql.builder.EntityUpdateExpressionBuilder; import com.easy.query.core.basic.jdbc.executor.ExecutorContext; -import com.easy.query.core.expression.sql.builder.ExpressionContext; import com.easy.query.core.metadata.EntityMetadata; import com.easy.query.core.util.EasyCollectionUtil; @@ -65,11 +64,11 @@ public long executeRows() { } protected void updateBefore() { - List updateInterceptors = entityMetadata.getEntityInterceptors(); + List updateInterceptors = entityMetadata.getEntityInterceptors(); if (EasyCollectionUtil.isNotEmpty(updateInterceptors)) { QueryConfiguration easyQueryConfiguration = entityUpdateExpressionBuilder.getRuntimeContext().getQueryConfiguration(); List entityInterceptors = entityUpdateExpressionBuilder.getExpressionContext().getInterceptorFilter(updateInterceptors) - .map(interceptor -> (EntityInterceptor) easyQueryConfiguration.getEasyInterceptor(interceptor.getName())).collect(Collectors.toList()); + .map(interceptor -> (EntityInterceptor) easyQueryConfiguration.getEasyInterceptor(interceptor)).filter(Interceptor::enable).collect(Collectors.toList()); if (EasyCollectionUtil.isNotEmpty(entityInterceptors)) { Class entityClass = entityMetadata.getEntityClass(); for (T entity : entities) { diff --git a/sql-core/src/main/java/com/easy/query/core/basic/jdbc/con/ConnectionManager.java b/sql-core/src/main/java/com/easy/query/core/basic/jdbc/con/ConnectionManager.java index dbc55dc42..0e8eccb35 100644 --- a/sql-core/src/main/java/com/easy/query/core/basic/jdbc/con/ConnectionManager.java +++ b/sql-core/src/main/java/com/easy/query/core/basic/jdbc/con/ConnectionManager.java @@ -2,6 +2,7 @@ import com.easy.query.core.basic.jdbc.tx.Transaction; import com.easy.query.core.enums.con.ConnectionStrategyEnum; +import com.easy.query.core.exception.EasyQuerySQLCommandException; import java.util.List; @@ -26,8 +27,17 @@ default Transaction beginTransaction(){ * (Note that Connection.TRANSACTION_NONE cannot be used because it specifies that transactions are not supported.) * @param isolationLevel * @return + * @throws EasyQuerySQLCommandException repeat begin transaction */ Transaction beginTransaction(Integer isolationLevel); + + default Transaction tryBeginTransaction(Integer isolationLevel){ + Transaction transaction = getTransactionOrNull(); + if(transaction!=null){ + return transaction; + } + return beginTransaction(isolationLevel); + } List getEasyConnections(int count,String dataSourceName, ConnectionStrategyEnum connectionStrategy); default boolean isOpenTransaction(){ return getTransactionOrNull()!=null; diff --git a/sql-core/src/main/java/com/easy/query/core/basic/plugin/interceptor/Interceptor.java b/sql-core/src/main/java/com/easy/query/core/basic/plugin/interceptor/Interceptor.java index 8f241243a..78f480ff7 100644 --- a/sql-core/src/main/java/com/easy/query/core/basic/plugin/interceptor/Interceptor.java +++ b/sql-core/src/main/java/com/easy/query/core/basic/plugin/interceptor/Interceptor.java @@ -20,7 +20,7 @@ default int order(){ * 可以设置为动态值 * @return */ - default boolean defaultEnable(){return true;} + default boolean enable(){return true;} /** * 拦截器名称 * @return diff --git a/sql-core/src/main/java/com/easy/query/core/expression/sql/builder/EasyExpressionContext.java b/sql-core/src/main/java/com/easy/query/core/expression/sql/builder/EasyExpressionContext.java index 7e40b812f..692570171 100644 --- a/sql-core/src/main/java/com/easy/query/core/expression/sql/builder/EasyExpressionContext.java +++ b/sql-core/src/main/java/com/easy/query/core/expression/sql/builder/EasyExpressionContext.java @@ -145,7 +145,7 @@ public void noInterceptor() { } @Override - public Stream getInterceptorFilter(List queryInterceptors) { + public Stream getInterceptorFilter(List queryInterceptors) { boolean interceptorBehavior = getBehavior().hasBehavior(EasyBehaviorEnum.USE_INTERCEPTOR); //如果当前操作存在interceptor的behavior那么就不应该在interceptors里面 //否则应该在interceptors里面 @@ -153,10 +153,10 @@ public Stream getInterceptorFilter(List quer //如果是启用了的 if (interceptorBehavior) { //拦截器手动指定使用的或者默认要用的并且没有说不用的 - return useInterceptors.contains(o.getName()) || (o.isDefaultEnable() && !noInterceptors.contains(o.getName())); + return useInterceptors.contains(o) || !noInterceptors.contains(o); } else { //手动指定要用的并且不在不使用里面 - return useInterceptors.contains(o.getName()) && !noInterceptors.contains(o.getName()); + return useInterceptors.contains(o) && !noInterceptors.contains(o); } }); } diff --git a/sql-core/src/main/java/com/easy/query/core/expression/sql/builder/ExpressionContext.java b/sql-core/src/main/java/com/easy/query/core/expression/sql/builder/ExpressionContext.java index 340fdb9c3..561d1b71f 100644 --- a/sql-core/src/main/java/com/easy/query/core/expression/sql/builder/ExpressionContext.java +++ b/sql-core/src/main/java/com/easy/query/core/expression/sql/builder/ExpressionContext.java @@ -32,7 +32,7 @@ public interface ExpressionContext { void noInterceptor(String name); void useInterceptor(); void noInterceptor(); - Stream getInterceptorFilter(List queryInterceptors); + Stream getInterceptorFilter(List queryInterceptors); default void executeMethod(ExecuteMethodEnum executeMethod){ executeMethod(executeMethod,false); diff --git a/sql-core/src/main/java/com/easy/query/core/expression/sql/builder/impl/UpdateExpressionBuilder.java b/sql-core/src/main/java/com/easy/query/core/expression/sql/builder/impl/UpdateExpressionBuilder.java index ec0df45e6..1a183d691 100644 --- a/sql-core/src/main/java/com/easy/query/core/expression/sql/builder/impl/UpdateExpressionBuilder.java +++ b/sql-core/src/main/java/com/easy/query/core/expression/sql/builder/impl/UpdateExpressionBuilder.java @@ -141,8 +141,10 @@ protected SQLBuilderSegment buildSetSQLSegment(EntityTableExpressionBuilder tabl QueryConfiguration easyQueryConfiguration = getRuntimeContext().getQueryConfiguration(); getExpressionContext().getInterceptorFilter(entityMetadata.getUpdateSetInterceptors()) .forEach(interceptor -> { - UpdateSetInterceptor globalInterceptor = (UpdateSetInterceptor) easyQueryConfiguration.getEasyInterceptor(interceptor.getName()); - globalInterceptor.configure(entityMetadata.getEntityClass(), this, sqlColumnSetter); + UpdateSetInterceptor globalInterceptor = (UpdateSetInterceptor) easyQueryConfiguration.getEasyInterceptor(interceptor); + if(globalInterceptor.enable()){ + globalInterceptor.configure(entityMetadata.getEntityClass(), this, sqlColumnSetter); + } }); } if (entityMetadata.hasVersionColumn()) { diff --git a/sql-core/src/main/java/com/easy/query/core/expression/sql/builder/internal/AbstractPredicateEntityExpressionBuilder.java b/sql-core/src/main/java/com/easy/query/core/expression/sql/builder/internal/AbstractPredicateEntityExpressionBuilder.java index 97425d1dc..899775f77 100644 --- a/sql-core/src/main/java/com/easy/query/core/expression/sql/builder/internal/AbstractPredicateEntityExpressionBuilder.java +++ b/sql-core/src/main/java/com/easy/query/core/expression/sql/builder/internal/AbstractPredicateEntityExpressionBuilder.java @@ -34,6 +34,7 @@ public AbstractPredicateEntityExpressionBuilder(ExpressionContext expressionCont protected boolean useLogicDelete(EntityMetadata entityMetadata) { return expressionContext.getBehavior().hasBehavior(EasyBehaviorEnum.LOGIC_DELETE) && entityMetadata.enableLogicDelete(); } + /** * 存在问题 update必须要总的predicate但是如果在这边导致我手动指定where也会有这个version * @@ -73,8 +74,8 @@ protected PredicateSegment sqlPredicateFilter(EntityTableExpressionBuilder table QueryConfiguration easyQueryConfiguration = getRuntimeContext().getQueryConfiguration(); expressionContext.getInterceptorFilter(entityMetadata.getPredicateFilterInterceptors()) .forEach(interceptor -> { - PredicateFilterInterceptor globalSelectInterceptorStrategy = (PredicateFilterInterceptor) easyQueryConfiguration.getEasyInterceptor(interceptor.getName()); - if (globalSelectInterceptorStrategy != null) { + PredicateFilterInterceptor globalSelectInterceptorStrategy = (PredicateFilterInterceptor) easyQueryConfiguration.getEasyInterceptor(interceptor); + if (globalSelectInterceptorStrategy != null && globalSelectInterceptorStrategy.enable()) { globalSelectInterceptorStrategy.configure(entityMetadata.getEntityClass(), this, sqlPredicate); } }); diff --git a/sql-core/src/main/java/com/easy/query/core/metadata/EntityMetadata.java b/sql-core/src/main/java/com/easy/query/core/metadata/EntityMetadata.java index 8d6115a3f..f67169713 100644 --- a/sql-core/src/main/java/com/easy/query/core/metadata/EntityMetadata.java +++ b/sql-core/src/main/java/com/easy/query/core/metadata/EntityMetadata.java @@ -65,9 +65,9 @@ public boolean isMultiDataSourceMapping() { /** * 查询过滤器 */ - private final List predicateFilterInterceptors = new ArrayList<>(); - private final List entityInterceptors = new ArrayList<>(); - private final List updateSetInterceptors = new ArrayList<>(); + private final List predicateFilterInterceptors = new ArrayList<>(); + private final List entityInterceptors = new ArrayList<>(); + private final List updateSetInterceptors = new ArrayList<>(); private final LinkedHashMap property2ColumnMap = new LinkedHashMap<>(); private final Map keyPropertiesMap = new LinkedHashMap<>(); private final List incrementColumns = new ArrayList<>(4); @@ -291,15 +291,14 @@ protected void entityGlobalInterceptorConfigurationInit(QueryConfiguration confi List globalInterceptors = configuration.getEasyInterceptors().stream().sorted(Comparator.comparingInt(Interceptor::order)).collect(Collectors.toList()); for (Interceptor globalInterceptor : globalInterceptors) { if (globalInterceptor.apply(entityClass)) { - InterceptorEntry easyInterceptorEntry = new InterceptorEntry(globalInterceptor.name(), globalInterceptor.defaultEnable()); if (globalInterceptor instanceof PredicateFilterInterceptor) { - predicateFilterInterceptors.add(easyInterceptorEntry); + predicateFilterInterceptors.add(globalInterceptor.name()); } if (globalInterceptor instanceof EntityInterceptor) { - entityInterceptors.add(easyInterceptorEntry); + entityInterceptors.add(globalInterceptor.name()); } if (globalInterceptor instanceof UpdateSetInterceptor) { - updateSetInterceptors.add(easyInterceptorEntry); + updateSetInterceptors.add(globalInterceptor.name()); } } } @@ -421,15 +420,15 @@ public boolean enableLogicDelete() { } - public List getPredicateFilterInterceptors() { + public List getPredicateFilterInterceptors() { return predicateFilterInterceptors; } - public List getEntityInterceptors() { + public List getEntityInterceptors() { return entityInterceptors; } - public List getUpdateSetInterceptors() { + public List getUpdateSetInterceptors() { return updateSetInterceptors; }