feature #I9H6GY SQL脚本在配置上支持用户自定义的过滤条件

This commit is contained in:
jay li
2024-08-24 13:52:08 +08:00
parent 6c0d8cc922
commit 689ef2e6d5
10 changed files with 170 additions and 10 deletions

View File

@@ -12,6 +12,8 @@ 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 {}=?";

View File

@@ -10,9 +10,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 读取抽象类,维护公共方法
@@ -53,6 +51,10 @@ public abstract class AbstractSqlRead<T> implements SqlRead<T> {
stmt.setFetchSize(SqlReadConstant.FETCH_SIZE_MAX);
stmt.setString(1, config.getApplicationName());
if (sqlCmd.contains(config.getChainSuffixTypeField()) && StrUtil.isNotBlank(config.getChainSuffixType())) {
stmt.setString(2, config.getChainSuffixType());
}
rs = stmt.executeQuery();
while (rs.next()) {

View File

@@ -29,10 +29,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;
@@ -56,8 +56,14 @@ public class ChainRead extends AbstractSqlRead<ChainVO> {
public String buildQuerySql() {
String chainTableName = super.config.getChainTableName();
String chainApplicationNameField = super.config.getChainApplicationNameField();
String chainSuffixTypeField = super.config.getChainSuffixTypeField();
return StrUtil.format(SqlReadConstant.SQL_PATTERN, chainTableName, chainApplicationNameField);
if (StrUtil.isNotBlank(chainSuffixTypeField)) {
return StrUtil.format(SqlReadConstant.SQL_PATTERN_WITH_SUFFIX, chainTableName,
chainApplicationNameField, chainSuffixTypeField);
} else {
return StrUtil.format(SqlReadConstant.SQL_PATTERN, chainTableName, chainApplicationNameField);
}
}
@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");
}
}

View File

@@ -71,6 +71,16 @@ public class SQLParserVO {
*/
private String chainEnableField;
/**
* chain 后缀
*/
private String chainSuffixType;
/**
* chain 后缀字段名
*/
private String chainSuffixTypeField = "chain_suffix_type";
/**
* 脚本 node 表名
*/
@@ -336,4 +346,20 @@ public class SQLParserVO {
public void setNamespaceField(String namespaceField) {
this.namespaceField = namespaceField;
}
public String getChainSuffixType() {
return chainSuffixType;
}
public void setChainSuffixType(String chainSuffixType) {
this.chainSuffixType = chainSuffixType;
}
public String getChainSuffixTypeField() {
return chainSuffixTypeField;
}
public void setChainSuffixTypeField(String chainSuffixTypeField) {
this.chainSuffixTypeField = chainSuffixTypeField;
}
}

View File

@@ -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;
import javax.sql.DataSource;
/**
* @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 testSQLWithXmlChain1() {
LiteflowResponse response = flowExecutor.execute2Resp("r_chain1", "arg");
Assertions.assertEquals("c==>b==>a", response.getExecuteStepStr());
}
}

View File

@@ -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_chain1", "arg");
Assertions.assertEquals("a==>b==>c", response.getExecuteStepStr());
}
}

View File

@@ -0,0 +1,23 @@
liteflow.rule-source-ext-data={\
"applicationName":"demo",\
"chainTableName":"EL_TABLE",\
"chainApplicationNameField":"application_name",\
"chainNameField":"chain_name",\
"chainSuffixTypeField":"chain_suffix_type",\
"chainSuffixType":"biz1",\
"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

View File

@@ -0,0 +1,23 @@
liteflow.rule-source-ext-data={\
"applicationName":"demo",\
"chainTableName":"EL_TABLE",\
"chainApplicationNameField":"application_name",\
"chainNameField":"chain_name",\
"chainSuffixTypeField":"chain_suffix_type",\
"chainSuffixType":"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

View File

@@ -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,CHAIN_SUFFIX_TYPE) values ('demo','r_chain1','THEN(a,b,c);','biz1');
INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA,CHAIN_SUFFIX_TYPE) values ('demo','r_chain1','THEN(c,b,a);','biz2');
DELETE FROM SCRIPT_NODE_TABLE;

View File

@@ -6,6 +6,7 @@ create table IF NOT EXISTS `EL_TABLE`
`el_data` varchar(1024) NOT NULL,
`route` varchar(1024),
`namespace` varchar(32),
`chain_suffix_type` varchar(32),
PRIMARY KEY (`id`)
);