mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-06-13 11:14:38 +08:00
采用自定义全sql逻辑
This commit is contained in:
@@ -6,14 +6,13 @@ package com.yomahub.liteflow.parser.constant;
|
||||
* @author tangkc
|
||||
* @author houxinyu
|
||||
* @author Bryan.Zhang
|
||||
* @author jay li
|
||||
* @since 2.11.1
|
||||
*/
|
||||
public class SqlReadConstant {
|
||||
|
||||
public static final String SQL_PATTERN = "SELECT * FROM {} WHERE {}='{}'";
|
||||
|
||||
public static final String SQL_PATTERN_WITH_SUFFIX = "SELECT * FROM {} WHERE {}='{}' AND {}";
|
||||
|
||||
public static final String SCRIPT_SQL_CHECK_PATTERN = "SELECT 1 FROM {} ";
|
||||
|
||||
public static final String SCRIPT_SQL_PATTERN = "SELECT * FROM {} WHERE {}='{}'";
|
||||
|
||||
@@ -9,16 +9,13 @@ import com.yomahub.liteflow.core.FlowInitHook;
|
||||
import com.yomahub.liteflow.parser.constant.ReadType;
|
||||
import com.yomahub.liteflow.parser.el.ClassXmlFlowELParser;
|
||||
import com.yomahub.liteflow.parser.sql.exception.ELSQLException;
|
||||
import com.yomahub.liteflow.parser.sql.read.CustomSqlRead;
|
||||
import com.yomahub.liteflow.parser.sql.read.SqlReadFactory;
|
||||
import com.yomahub.liteflow.parser.sql.util.JDBCHelper;
|
||||
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
|
||||
import com.yomahub.liteflow.property.LiteflowConfig;
|
||||
import com.yomahub.liteflow.property.LiteflowConfigGetter;
|
||||
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
|
||||
import com.yomahub.liteflow.util.JsonUtil;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@@ -52,17 +49,6 @@ public class SQLXmlELParser extends ClassXmlFlowELParser {
|
||||
throw new ELSQLException(ERROR_COMMON_MSG);
|
||||
}
|
||||
|
||||
// 自定义sql
|
||||
Map<String, CustomSqlRead> beansOfType = ContextAwareHolder.loadContextAware().getBeansOfType(CustomSqlRead.class);
|
||||
for (Map.Entry<String,CustomSqlRead> entry : beansOfType.entrySet()) {
|
||||
CustomSqlRead customSqlRead = entry.getValue();
|
||||
String customChainSql = customSqlRead.getCustomChainSql();
|
||||
|
||||
if (StrUtil.isNotBlank(customChainSql)) {
|
||||
sqlParserVO.setCustomSql(customChainSql);
|
||||
}
|
||||
}
|
||||
|
||||
// 检查配置文件
|
||||
checkParserVO(sqlParserVO);
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import java.sql.SQLException;
|
||||
*
|
||||
* @author tangkc
|
||||
* @author houxinyu
|
||||
* @author jay li
|
||||
* @since 2.11.1
|
||||
*/
|
||||
public class ChainRead extends AbstractSqlRead<ChainVO> {
|
||||
@@ -54,30 +55,15 @@ public class ChainRead extends AbstractSqlRead<ChainVO> {
|
||||
|
||||
@Override
|
||||
public String buildQuerySql() {
|
||||
if (StrUtil.isNotBlank(super.config.getCustomSql())) {
|
||||
return super.config.getCustomSql();
|
||||
if (StrUtil.isNotBlank(super.config.getChainCustomSql())) {
|
||||
return super.config.getChainCustomSql();
|
||||
}
|
||||
|
||||
String chainTableName = super.config.getChainTableName();
|
||||
String chainApplicationNameField = super.config.getChainApplicationNameField();
|
||||
String customFilterSql = super.config.getCustomFilterSql();
|
||||
String applicationName = super.config.getApplicationName();
|
||||
|
||||
if (StrUtil.isNotBlank(customFilterSql)) {
|
||||
String customFilterSqlTrim = customFilterSql.trim();
|
||||
if (customFilterSqlTrim.startsWith("where ") || customFilterSqlTrim.startsWith("WHERE ")) {
|
||||
customFilterSqlTrim = customFilterSqlTrim.substring(5).trim();
|
||||
}
|
||||
|
||||
if (customFilterSqlTrim.startsWith("AND ") || customFilterSqlTrim.startsWith("and ")) {
|
||||
customFilterSqlTrim = customFilterSqlTrim.substring(3);
|
||||
}
|
||||
|
||||
return StrUtil.format(SqlReadConstant.SQL_PATTERN_WITH_SUFFIX, chainTableName,
|
||||
chainApplicationNameField, applicationName, customFilterSqlTrim);
|
||||
} else {
|
||||
return StrUtil.format(SqlReadConstant.SQL_PATTERN, chainTableName, chainApplicationNameField, applicationName);
|
||||
}
|
||||
return StrUtil.format(SqlReadConstant.SQL_PATTERN, chainTableName, chainApplicationNameField, applicationName);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package com.yomahub.liteflow.parser.sql.read.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.enums.NodeTypeEnum;
|
||||
import com.yomahub.liteflow.enums.ScriptTypeEnum;
|
||||
import com.yomahub.liteflow.parser.constant.ReadType;
|
||||
import com.yomahub.liteflow.parser.constant.SqlReadConstant;
|
||||
import com.yomahub.liteflow.parser.sql.exception.ELSQLException;
|
||||
@@ -11,18 +8,19 @@ import com.yomahub.liteflow.parser.sql.read.AbstractSqlRead;
|
||||
import com.yomahub.liteflow.parser.sql.read.vo.ScriptVO;
|
||||
import com.yomahub.liteflow.parser.sql.util.LiteFlowJdbcUtil;
|
||||
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
/**
|
||||
* 脚本读取
|
||||
*
|
||||
* @author tangkc
|
||||
* @author houxinyu
|
||||
* @author jay li
|
||||
* @since 2.11.1
|
||||
*/
|
||||
public class ScriptRead extends AbstractSqlRead<ScriptVO> {
|
||||
@@ -58,6 +56,10 @@ public class ScriptRead extends AbstractSqlRead<ScriptVO> {
|
||||
|
||||
@Override
|
||||
public String buildQuerySql() {
|
||||
if (StringUtils.isNotBlank(super.config.getScriptCustomSql())) {
|
||||
return super.config.getScriptCustomSql();
|
||||
}
|
||||
|
||||
String scriptTableName = super.config.getScriptTableName();
|
||||
String scriptApplicationNameField = super.config.getScriptApplicationNameField();
|
||||
String applicationName = super.config.getApplicationName();
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.yomahub.liteflow.parser.sql.util;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.thread.NamedThreadFactory;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
@@ -12,13 +11,11 @@ import com.yomahub.liteflow.parser.constant.ReadType;
|
||||
|
||||
import com.yomahub.liteflow.parser.sql.exception.ELSQLException;
|
||||
import com.yomahub.liteflow.parser.sql.polling.SqlReadPollTask;
|
||||
import com.yomahub.liteflow.parser.sql.read.CustomSqlRead;
|
||||
import com.yomahub.liteflow.parser.sql.read.SqlRead;
|
||||
import com.yomahub.liteflow.parser.sql.read.SqlReadFactory;
|
||||
import com.yomahub.liteflow.parser.sql.read.vo.ChainVO;
|
||||
import com.yomahub.liteflow.parser.sql.read.vo.ScriptVO;
|
||||
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
|
||||
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@@ -7,6 +7,7 @@ import cn.hutool.core.util.StrUtil;
|
||||
*
|
||||
* @author tangkc
|
||||
* @author Bryan.Zhang
|
||||
* @author jay li
|
||||
* @since 2.9.0
|
||||
*/
|
||||
public class SQLParserVO {
|
||||
@@ -72,14 +73,14 @@ public class SQLParserVO {
|
||||
private String chainEnableField;
|
||||
|
||||
/**
|
||||
* 后缀 自定义过滤 sql
|
||||
* chain 自定义 sql
|
||||
*/
|
||||
private String customFilterSql;
|
||||
private String chainCustomSql;
|
||||
|
||||
/**
|
||||
* 自定义 sql
|
||||
* 脚本 自定义 sql
|
||||
*/
|
||||
private String customSql;
|
||||
private String scriptCustomSql;
|
||||
|
||||
/**
|
||||
* 脚本 node 表名
|
||||
@@ -347,19 +348,19 @@ public class SQLParserVO {
|
||||
this.namespaceField = namespaceField;
|
||||
}
|
||||
|
||||
public String getCustomFilterSql() {
|
||||
return customFilterSql;
|
||||
public String getChainCustomSql() {
|
||||
return chainCustomSql;
|
||||
}
|
||||
|
||||
public void setCustomFilterSql(String customFilterSql) {
|
||||
this.customFilterSql = customFilterSql;
|
||||
public void setChainCustomSql(String chainCustomSql) {
|
||||
this.chainCustomSql = chainCustomSql;
|
||||
}
|
||||
|
||||
public String getCustomSql() {
|
||||
return customSql;
|
||||
public String getScriptCustomSql() {
|
||||
return scriptCustomSql;
|
||||
}
|
||||
|
||||
public void setCustomSql(String customSql) {
|
||||
this.customSql = customSql;
|
||||
public void setScriptCustomSql(String scriptCustomSql) {
|
||||
this.scriptCustomSql = scriptCustomSql;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,21 +2,18 @@ package com.yomahub.liteflow.test.withSuffix;
|
||||
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
import com.yomahub.liteflow.parser.sql.read.CustomSqlRead;
|
||||
import com.yomahub.liteflow.test.BaseTest;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author jay li
|
||||
@@ -29,26 +26,12 @@ import static org.mockito.Mockito.when;
|
||||
@ComponentScan({"com.yomahub.liteflow.test.sql.cmp"})
|
||||
public class SQLWithXmlELWithSuffix2SpringbootTest extends BaseTest {
|
||||
|
||||
@MockBean(name = "CustomSqlRead")
|
||||
private static CustomSqlRead customChain;
|
||||
|
||||
@Resource
|
||||
private FlowExecutor flowExecutor;
|
||||
|
||||
@Test
|
||||
public void testSQLWithXmlChain1() {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("r_chain4", "arg");
|
||||
Assertions.assertEquals("c==>b==>a", response.getExecuteStepStr());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSQLWithXmlChain2() {
|
||||
when(customChain.getCustomChainSql())
|
||||
.thenReturn(" SELECT APPLICATION_NAME,CHAIN_NAME,'THEN(c,b,c);' as EL_DATA,CUSTOM_FILTER_TYPE FROM EL_TABLE WHERE custom_filter_type = 'biz2' ");
|
||||
|
||||
flowExecutor.init(false);
|
||||
|
||||
public void testSQLWithXmlChain() {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("r_chain4", "arg");
|
||||
Assertions.assertEquals("c==>b==>c", response.getExecuteStepStr());
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ liteflow.rule-source-ext-data={\
|
||||
"chainTableName":"EL_TABLE",\
|
||||
"chainApplicationNameField":"application_name",\
|
||||
"chainNameField":"chain_name",\
|
||||
"customFilterSql":"custom_filter_type = 'biz1'",\
|
||||
"scriptCustomSql":" SELECT * FROM script_node_table WHERE application_name = 'demo' ",\
|
||||
"elDataField":"EL_DATA",\
|
||||
"scriptTableName":"script_node_table",\
|
||||
"scriptApplicationNameField":"application_name",\
|
||||
|
||||
@@ -3,7 +3,7 @@ liteflow.rule-source-ext-data={\
|
||||
"chainTableName":"EL_TABLE",\
|
||||
"chainApplicationNameField":"application_name",\
|
||||
"chainNameField":"chain_name",\
|
||||
"customFilterSql":"custom_filter_type = 'biz2' ",\
|
||||
"chainCustomSql":" SELECT APPLICATION_NAME,CHAIN_NAME,'THEN(c,b,c);' as EL_DATA,CUSTOM_FILTER_TYPE FROM EL_TABLE WHERE custom_filter_type = 'biz2' ",\
|
||||
"elDataField":"EL_DATA",\
|
||||
"scriptTableName":"script_node_table",\
|
||||
"scriptApplicationNameField":"application_name",\
|
||||
|
||||
Reference in New Issue
Block a user