mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 04:02:09 +08:00
!308 feature #I9H6GYSQL脚本在配置上支持用户自定义的过滤条件
Merge pull request !308 from jay-li/issues#I9H6GY
This commit is contained in:
@@ -6,15 +6,16 @@ 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 = "SELECT * FROM {} WHERE {}='{}'";
|
||||
|
||||
public static final String SCRIPT_SQL_CHECK_PATTERN = "SELECT 1 FROM {} ";
|
||||
|
||||
public static final String SCRIPT_SQL_PATTERN = "SELECT * FROM {} WHERE {}=?";
|
||||
public static final String SCRIPT_SQL_PATTERN = "SELECT * FROM {} WHERE {}='{}'";
|
||||
|
||||
public static final String CHAIN_XML_PATTERN = "<chain id=\"{}\" namespace=\"{}\"><route><![CDATA[{}]]></route><body><![CDATA[{}]]></body></chain>";
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.yomahub.liteflow.parser.sql.polling;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.digest.DigestUtil;
|
||||
import com.yomahub.liteflow.parser.constant.ReadType;
|
||||
import com.yomahub.liteflow.parser.sql.exception.ELSQLException;
|
||||
import com.yomahub.liteflow.parser.sql.read.SqlRead;
|
||||
|
||||
@@ -11,7 +12,6 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.yomahub.liteflow.parser.sql.read;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.log.LFLog;
|
||||
import com.yomahub.liteflow.log.LFLoggerManager;
|
||||
import com.yomahub.liteflow.parser.constant.ReadType;
|
||||
import com.yomahub.liteflow.parser.constant.SqlReadConstant;
|
||||
import com.yomahub.liteflow.parser.sql.exception.ELSQLException;
|
||||
import com.yomahub.liteflow.parser.sql.util.LiteFlowJdbcUtil;
|
||||
@@ -10,9 +11,7 @@ import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* sql 读取抽象类,维护公共方法
|
||||
@@ -51,7 +50,6 @@ public abstract class AbstractSqlRead<T> implements SqlRead<T> {
|
||||
stmt = conn.prepareStatement(sqlCmd, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||
// 设置游标拉取数量
|
||||
stmt.setFetchSize(SqlReadConstant.FETCH_SIZE_MAX);
|
||||
stmt.setString(1, config.getApplicationName());
|
||||
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.yomahub.liteflow.parser.sql.read;
|
||||
import com.yomahub.liteflow.parser.constant.ReadType;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* sql 读取接口
|
||||
|
||||
@@ -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> {
|
||||
@@ -29,10 +30,10 @@ public class ChainRead extends AbstractSqlRead<ChainVO> {
|
||||
ChainVO chainVO = new ChainVO();
|
||||
chainVO.setChainId(getStringFromRsWithCheck(rs, super.config.getChainNameField()));
|
||||
chainVO.setBody(getStringFromRsWithCheck(rs, super.config.getElDataField()));
|
||||
if (StrUtil.isNotBlank(super.config.getNamespaceField())){
|
||||
if (StrUtil.isNotBlank(super.config.getNamespaceField())) {
|
||||
chainVO.setNamespace(getStringFromRs(rs, super.config.getNamespaceField()));
|
||||
}
|
||||
if (StrUtil.isNotBlank(super.config.getRouteField())){
|
||||
if (StrUtil.isNotBlank(super.config.getRouteField())) {
|
||||
chainVO.setRoute(getStringFromRs(rs, super.config.getRouteField()));
|
||||
}
|
||||
return chainVO;
|
||||
@@ -54,10 +55,15 @@ public class ChainRead extends AbstractSqlRead<ChainVO> {
|
||||
|
||||
@Override
|
||||
public String buildQuerySql() {
|
||||
if (StrUtil.isNotBlank(super.config.getChainCustomSql())) {
|
||||
return super.config.getChainCustomSql();
|
||||
}
|
||||
|
||||
String chainTableName = super.config.getChainTableName();
|
||||
String chainApplicationNameField = super.config.getChainApplicationNameField();
|
||||
String applicationName = super.config.getApplicationName();
|
||||
|
||||
return StrUtil.format(SqlReadConstant.SQL_PATTERN, chainTableName, chainApplicationNameField);
|
||||
return StrUtil.format(SqlReadConstant.SQL_PATTERN, chainTableName, chainApplicationNameField, applicationName);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -68,19 +74,19 @@ public class ChainRead extends AbstractSqlRead<ChainVO> {
|
||||
String chainApplicationNameField = super.config.getChainApplicationNameField();
|
||||
String applicationName = super.config.getApplicationName();
|
||||
|
||||
if (StrUtil.isBlank(chainTableName)){
|
||||
if (StrUtil.isBlank(chainTableName)) {
|
||||
throw new ELSQLException("You did not define the chainTableName property");
|
||||
}
|
||||
if (StrUtil.isBlank(elDataField)){
|
||||
if (StrUtil.isBlank(elDataField)) {
|
||||
throw new ELSQLException("You did not define the elDataField property");
|
||||
}
|
||||
if (StrUtil.isBlank(chainNameField)){
|
||||
if (StrUtil.isBlank(chainNameField)) {
|
||||
throw new ELSQLException("You did not define the chainNameField property");
|
||||
}
|
||||
if (StrUtil.isBlank(chainApplicationNameField)){
|
||||
if (StrUtil.isBlank(chainApplicationNameField)) {
|
||||
throw new ELSQLException("You did not define the chainApplicationNameField property");
|
||||
}
|
||||
if (StrUtil.isBlank(applicationName)){
|
||||
if (StrUtil.isBlank(applicationName)) {
|
||||
throw new ELSQLException("You did not define the applicationName property");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,13 +56,18 @@ 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();
|
||||
return StrUtil.format(
|
||||
SqlReadConstant.SCRIPT_SQL_PATTERN,
|
||||
scriptTableName,
|
||||
scriptApplicationNameField);
|
||||
scriptApplicationNameField,
|
||||
applicationName);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,10 +9,8 @@ import com.yomahub.liteflow.log.LFLog;
|
||||
import com.yomahub.liteflow.log.LFLoggerManager;
|
||||
import com.yomahub.liteflow.parser.constant.ReadType;
|
||||
|
||||
import com.yomahub.liteflow.parser.helper.NodeConvertHelper;
|
||||
import com.yomahub.liteflow.parser.sql.exception.ELSQLException;
|
||||
import com.yomahub.liteflow.parser.sql.polling.SqlReadPollTask;
|
||||
import com.yomahub.liteflow.parser.sql.read.AbstractSqlRead;
|
||||
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;
|
||||
@@ -25,8 +23,6 @@ import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static com.yomahub.liteflow.parser.constant.SqlReadConstant.*;
|
||||
|
||||
|
||||
@@ -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 {
|
||||
@@ -71,6 +72,16 @@ public class SQLParserVO {
|
||||
*/
|
||||
private String chainEnableField;
|
||||
|
||||
/**
|
||||
* chain 自定义 sql
|
||||
*/
|
||||
private String chainCustomSql;
|
||||
|
||||
/**
|
||||
* 脚本 自定义 sql
|
||||
*/
|
||||
private String scriptCustomSql;
|
||||
|
||||
/**
|
||||
* 脚本 node 表名
|
||||
*/
|
||||
@@ -336,4 +347,20 @@ public class SQLParserVO {
|
||||
public void setNamespaceField(String namespaceField) {
|
||||
this.namespaceField = namespaceField;
|
||||
}
|
||||
|
||||
public String getChainCustomSql() {
|
||||
return chainCustomSql;
|
||||
}
|
||||
|
||||
public void setChainCustomSql(String chainCustomSql) {
|
||||
this.chainCustomSql = chainCustomSql;
|
||||
}
|
||||
|
||||
public String getScriptCustomSql() {
|
||||
return scriptCustomSql;
|
||||
}
|
||||
|
||||
public void setScriptCustomSql(String scriptCustomSql) {
|
||||
this.scriptCustomSql = scriptCustomSql;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.yomahub.liteflow.test.withSuffix;
|
||||
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
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.context.annotation.ComponentScan;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* @author jay li
|
||||
* @since 2.12.0
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@TestPropertySource(value = "classpath:/application-data-source-with-suffix2-xml.properties")
|
||||
@SpringBootTest(classes = SQLWithXmlELWithSuffix2SpringbootTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.sql.cmp"})
|
||||
public class SQLWithXmlELWithSuffix2SpringbootTest extends BaseTest {
|
||||
|
||||
|
||||
@Resource
|
||||
private FlowExecutor flowExecutor;
|
||||
|
||||
@Test
|
||||
public void testSQLWithXmlChain() {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("r_chain4", "arg");
|
||||
Assertions.assertEquals("c==>b==>c", response.getExecuteStepStr());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.yomahub.liteflow.test.withSuffix;
|
||||
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
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.context.annotation.ComponentScan;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* @author jay li
|
||||
* @since 2.12.0
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@TestPropertySource(value = "classpath:/application-data-source-with-suffix-xml.properties")
|
||||
@SpringBootTest(classes = SQLWithXmlELWithSuffixSpringbootTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.sql.cmp"})
|
||||
public class SQLWithXmlELWithSuffixSpringbootTest extends BaseTest {
|
||||
|
||||
@Resource
|
||||
private FlowExecutor flowExecutor;
|
||||
|
||||
@Test
|
||||
public void testSQLWithXmlChain1() {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("r_chain3", "arg");
|
||||
Assertions.assertEquals("a==>b==>c", response.getExecuteStepStr());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
liteflow.rule-source-ext-data={\
|
||||
"applicationName":"demo",\
|
||||
"chainTableName":"EL_TABLE",\
|
||||
"chainApplicationNameField":"application_name",\
|
||||
"chainNameField":"chain_name",\
|
||||
"scriptCustomSql":" SELECT * FROM script_node_table WHERE application_name = 'demo' ",\
|
||||
"elDataField":"EL_DATA",\
|
||||
"scriptTableName":"script_node_table",\
|
||||
"scriptApplicationNameField":"application_name",\
|
||||
"scriptIdField":"script_node_id",\
|
||||
"scriptNameField":"script_node_name",\
|
||||
"scriptDataField":"script_node_data",\
|
||||
"scriptLanguageField":"script_language",\
|
||||
"scriptTypeField":"script_node_type"\
|
||||
}
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.datasource.url=jdbc:h2:mem:test_db;MODE=MySQL
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=123456
|
||||
spring.datasource.schema=classpath:/sql/schema.sql
|
||||
spring.datasource.data=classpath:/sql/data.sql
|
||||
spring.datasource.platform=h2
|
||||
@@ -0,0 +1,22 @@
|
||||
liteflow.rule-source-ext-data={\
|
||||
"applicationName":"demo",\
|
||||
"chainTableName":"EL_TABLE",\
|
||||
"chainApplicationNameField":"application_name",\
|
||||
"chainNameField":"chain_name",\
|
||||
"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",\
|
||||
"scriptIdField":"script_node_id",\
|
||||
"scriptNameField":"script_node_name",\
|
||||
"scriptDataField":"script_node_data",\
|
||||
"scriptLanguageField":"script_language",\
|
||||
"scriptTypeField":"script_node_type"\
|
||||
}
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.datasource.url=jdbc:h2:mem:test_db;MODE=MySQL
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=123456
|
||||
spring.datasource.schema=classpath:/sql/schema.sql
|
||||
spring.datasource.data=classpath:/sql/data.sql
|
||||
spring.datasource.platform=h2
|
||||
@@ -7,6 +7,8 @@ INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA) values ('demo','<chai
|
||||
INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA) values ('demo','chain4','IF(x2, IF(x0, THEN(a, b)));');
|
||||
INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA,ROUTE,NAMESPACE) values ('demo','r_chain1','THEN(a,b,c);','r1','ns');
|
||||
INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA,ROUTE,NAMESPACE) values ('demo','r_chain2','THEN(c,b,a);','OR(r1,r2)','ns');
|
||||
INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA,CUSTOM_FILTER_TYPE) values ('demo','r_chain3','THEN(a,b,c);','biz1');
|
||||
INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA,CUSTOM_FILTER_TYPE) values ('demo','r_chain4','THEN(c,b,a);','biz2');
|
||||
|
||||
DELETE FROM SCRIPT_NODE_TABLE;
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ create table IF NOT EXISTS `EL_TABLE`
|
||||
`el_data` varchar(1024) NOT NULL,
|
||||
`route` varchar(1024),
|
||||
`namespace` varchar(32),
|
||||
`CUSTOM_FILTER_TYPE` varchar(32),
|
||||
PRIMARY KEY (`id`)
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user