feat #I6O2YE sql 外置存储支持多脚本混合调用

This commit is contained in:
gaibu 2023-03-19 21:42:35 +08:00
parent f5380d318c
commit b9542f0fbd
10 changed files with 121 additions and 62 deletions

View File

@ -6,8 +6,7 @@ public enum ScriptTypeEnum {
QLEXPRESS("qlexpress", "qlexpress"),
JS("javascript", "js"),
PYTHON("python", "python"),
LUA("luaj", "lua")
;
LUA("luaj", "lua");
private String engineName;
private String displayName;
@ -41,4 +40,19 @@ public enum ScriptTypeEnum {
}
return null;
}
/**
* 校验脚本类型是否合法
*
* @param scriptType 脚本类型
* @return true:合法false:不合法
*/
public static boolean checkScriptType(String scriptType) {
for (ScriptTypeEnum e : ScriptTypeEnum.values()) {
if (e.getDisplayName().equals(scriptType)) {
return true;
}
}
return false;
}
}

View File

@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import com.yomahub.liteflow.enums.NodeTypeEnum;
import com.yomahub.liteflow.enums.ScriptTypeEnum;
import com.yomahub.liteflow.parser.sql.exception.ELSQLException;
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
@ -28,12 +29,12 @@ public class JDBCHelper {
private static final String SCRIPT_SQL_CHECK_PATTERN = "SELECT 1 FROM {} WHERE {}=?";
private static final String SCRIPT_SQL_PATTERN = "SELECT {},{},{},{} FROM {} WHERE {}=?";
private static final String SCRIPT_SQL_PATTERN = "SELECT {},{},{},{},{} FROM {} WHERE {}=?";
private static final String CHAIN_XML_PATTERN = "<chain name=\"{}\">{}</chain>";
private static final String NODE_XML_PATTERN = "<nodes>{}</nodes>";
private static final String NODE_ITEM_XML_PATTERN = "<node id=\"{}\" name=\"{}\" type=\"{}\"><![CDATA[{}]]></node>";
private static final String NODE_ITEM_XML_PATTERN = "<node id=\"{}\" name=\"{}\" type=\"{}\" language=\"{}\"><![CDATA[{}]]></node>";
private static final String XML_PATTERN = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><flow>{}{}</flow>";
private static final Integer FETCH_SIZE_MAX = 1000;
@ -145,6 +146,7 @@ public class JDBCHelper {
String scriptTypeField = sqlParserVO.getScriptTypeField();
String scriptApplicationNameField = sqlParserVO.getScriptApplicationNameField();
String applicationName = sqlParserVO.getApplicationName();
String scriptLanguageField = sqlParserVO.getScriptLanguageField();
if (StrUtil.isBlank(applicationName) || StrUtil.isBlank(scriptApplicationNameField)) {
throw new ELSQLException("You did not define the applicationName or scriptApplicationNameField property");
@ -156,6 +158,7 @@ public class JDBCHelper {
scriptDataField,
scriptNameField,
scriptTypeField,
scriptLanguageField,
scriptTableName,
scriptApplicationNameField
);
@ -172,6 +175,7 @@ public class JDBCHelper {
String data = getStringFromResultSet(rs, scriptDataField);
String name = getStringFromResultSet(rs, scriptNameField);
String type = getStringFromResultSet(rs, scriptTypeField);
String language = getStringFromResultSet(rs, scriptLanguageField);
NodeTypeEnum nodeTypeEnum = NodeTypeEnum.getEnumByCode(type);
if (Objects.isNull(nodeTypeEnum)) {
@ -182,7 +186,11 @@ public class JDBCHelper {
throw new ELSQLException(StrUtil.format("The type value[{}] is not a script type", type));
}
result.add(StrUtil.format(NODE_ITEM_XML_PATTERN, XmlUtil.escape(id), XmlUtil.escape(name), type, data));
if(!ScriptTypeEnum.checkScriptType(language)){
throw new ELSQLException(StrUtil.format("The language value[{}] is error", language));
}
result.add(StrUtil.format(NODE_ITEM_XML_PATTERN, XmlUtil.escape(id), XmlUtil.escape(name), type, language, data));
}
} catch (Exception e) {
throw new ELSQLException(e.getMessage());

View File

@ -83,6 +83,11 @@ public class SQLParserVO {
*/
private String scriptTypeField = "script_type";
/**
* 脚本 node language 字段
*/
private String scriptLanguageField = "script_language";
public String getUrl() {
return url;
}
@ -202,4 +207,12 @@ public class SQLParserVO {
public void setScriptTypeField(String scriptTypeField) {
this.scriptTypeField = scriptTypeField;
}
public String getScriptLanguageField() {
return scriptLanguageField;
}
public void setScriptLanguageField(String scriptLanguageField) {
this.scriptLanguageField = scriptLanguageField;
}
}

View File

@ -57,5 +57,12 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-script-javascript</artifactId>
<version>${revision}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -33,64 +33,71 @@ import java.sql.Statement;
@EnableAutoConfiguration
@ComponentScan({"com.yomahub.liteflow.test.sql.cmp"})
public class SQLWithXmlELSpringbootTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
@Resource
private FlowExecutor flowExecutor;
@Test
public void testSQLWithXml() {
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
Assert.assertEquals("a==>b==>c", response.getExecuteStepStr());
@Test
public void testSQLWithXml() {
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
Assert.assertEquals("a==>b==>c", response.getExecuteStepStr());
// 修改数据库
changeData();
// 修改数据库
changeData();
// 重新加载规则
flowExecutor.reloadRule();
Assert.assertEquals("a==>c==>b", flowExecutor.execute2Resp("chain1", "arg").getExecuteStepStr());
}
// 重新加载规则
flowExecutor.reloadRule();
Assert.assertEquals("a==>c==>b", flowExecutor.execute2Resp("chain1", "arg").getExecuteStepStr());
}
@Test
public void testSQLWithScriptXml() {
LiteflowResponse response = flowExecutor.execute2Resp("chain3", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("x0[if 脚本]==>a==>b", response.getExecuteStepStrWithoutTime());
@Test
public void testSQLWithScriptXml() {
LiteflowResponse response = flowExecutor.execute2Resp("chain3", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("x0[if 脚本]==>a==>b", response.getExecuteStepStrWithoutTime());
// 修改数据库
changeScriptData();
// 重新加载规则
flowExecutor.reloadRule();
Assert.assertEquals("x0[if 脚本]", flowExecutor.execute2Resp("chain3", "arg").getExecuteStepStr());
}
// 修改数据库
changeScriptData();
// 重新加载规则
flowExecutor.reloadRule();
Assert.assertEquals("x0[if 脚本]", flowExecutor.execute2Resp("chain3", "arg").getExecuteStepStr());
}
/**
* 修改数据库数据
*/
private void changeData() {
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
SQLParserVO sqlParserVO = JsonUtil.parseObject(liteflowConfig.getRuleSourceExtData(), SQLParserVO.class);
Connection connection;
try {
connection = DriverManager.getConnection(sqlParserVO.getUrl(), sqlParserVO.getUsername(), sqlParserVO.getPassword());
Statement statement = connection.createStatement();
statement.executeUpdate("UPDATE EL_TABLE SET EL_DATA='THEN(a, c, b);' WHERE chain_name='chain1'");
} catch (SQLException e) {
throw new ELSQLException(e.getMessage());
}
}
@Test
public void testMultiLanguage1() {
LiteflowResponse response = flowExecutor.execute2Resp("chain4", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("x2[python脚本]==>x0[if 脚本]==>a==>b", response.getExecuteStepStrWithoutTime());
}
/**
* 修改数据库数据
*/
private void changeScriptData() {
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
SQLParserVO sqlParserVO = JsonUtil.parseObject(liteflowConfig.getRuleSourceExtData(), SQLParserVO.class);
Connection connection;
try {
connection = DriverManager.getConnection(sqlParserVO.getUrl(), sqlParserVO.getUsername(), sqlParserVO.getPassword());
Statement statement = connection.createStatement();
statement.executeUpdate("UPDATE SCRIPT_NODE_TABLE SET SCRIPT_NODE_DATA='return false;' WHERE SCRIPT_NODE_ID='x0'");
} catch (SQLException e) {
throw new ELSQLException(e.getMessage());
}
}
/**
* 修改数据库数据
*/
private void changeData() {
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
SQLParserVO sqlParserVO = JsonUtil.parseObject(liteflowConfig.getRuleSourceExtData(), SQLParserVO.class);
Connection connection;
try {
connection = DriverManager.getConnection(sqlParserVO.getUrl(), sqlParserVO.getUsername(), sqlParserVO.getPassword());
Statement statement = connection.createStatement();
statement.executeUpdate("UPDATE EL_TABLE SET EL_DATA='THEN(a, c, b);' WHERE chain_name='chain1'");
} catch (SQLException e) {
throw new ELSQLException(e.getMessage());
}
}
/**
* 修改数据库数据
*/
private void changeScriptData() {
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
SQLParserVO sqlParserVO = JsonUtil.parseObject(liteflowConfig.getRuleSourceExtData(), SQLParserVO.class);
Connection connection;
try {
connection = DriverManager.getConnection(sqlParserVO.getUrl(), sqlParserVO.getUsername(), sqlParserVO.getPassword());
Statement statement = connection.createStatement();
statement.executeUpdate("UPDATE SCRIPT_NODE_TABLE SET SCRIPT_NODE_DATA='return false;' WHERE SCRIPT_NODE_ID='x0'");
} catch (SQLException e) {
throw new ELSQLException(e.getMessage());
}
}
}

View File

@ -13,6 +13,7 @@ liteflow.rule-source-ext-data={\
"scriptIdField":"script_node_id",\
"scriptNameField":"script_node_name",\
"scriptDataField":"script_node_data",\
"scriptLanguageField":"script_language",\
"scriptTypeField":"script_node_type"\
}

View File

@ -4,8 +4,11 @@ INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA) values ('demo','chain
INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA) values ('demo','chain2','THEN(a, b, c);');
INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA) values ('demo','chain3','IF(x0, THEN(a, b));');
INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA) values ('demo','<chain3>','IF(x0, THEN(a, b));');
INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA) values ('demo','chain4','IF(x2, IF(x0, THEN(a, b)));');
DELETE FROM SCRIPT_NODE_TABLE;
INSERT INTO SCRIPT_NODE_TABLE (APPLICATION_NAME,SCRIPT_NODE_ID,SCRIPT_NODE_NAME,SCRIPT_NODE_TYPE,SCRIPT_NODE_DATA) values ('demo','x0','if 脚本','if_script','return true');
INSERT INTO SCRIPT_NODE_TABLE (APPLICATION_NAME,SCRIPT_NODE_ID,SCRIPT_NODE_NAME,SCRIPT_NODE_TYPE,SCRIPT_NODE_DATA) values ('demo','x1','if 脚本','if_script','return false');
INSERT INTO SCRIPT_NODE_TABLE (APPLICATION_NAME,SCRIPT_NODE_ID,SCRIPT_NODE_NAME,SCRIPT_NODE_TYPE,SCRIPT_NODE_DATA,SCRIPT_LANGUAGE) values ('demo','x0','if 脚本','if_script','return true','groovy');
INSERT INTO SCRIPT_NODE_TABLE (APPLICATION_NAME,SCRIPT_NODE_ID,SCRIPT_NODE_NAME,SCRIPT_NODE_TYPE,SCRIPT_NODE_DATA,SCRIPT_LANGUAGE) values ('demo','x1','if 脚本','if_script','return false','groovy');
INSERT INTO SCRIPT_NODE_TABLE (APPLICATION_NAME,SCRIPT_NODE_ID,SCRIPT_NODE_NAME,SCRIPT_NODE_TYPE,SCRIPT_NODE_DATA,SCRIPT_LANGUAGE) values ('demo','x2','python脚本','if_script','return true','js');

View File

@ -15,5 +15,6 @@ create table `script_node_table`
`script_node_name` varchar(32) NOT NULL,
`script_node_type` varchar(32) NOT NULL,
`script_node_data` varchar(1024) NOT NULL,
`script_language` varchar(1024) NOT NULL,
PRIMARY KEY (`id`)
);

View File

@ -3,8 +3,12 @@ DELETE FROM EL_TABLE;
INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA) values ('demo','chain1','THEN(a, b, c);');
INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA) values ('demo','chain2','THEN(a, b, c);');
INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA) values ('demo','chain3','IF(x0, THEN(a, b));');
INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA) values ('demo','<chain3>','IF(x0, THEN(a, b));');
INSERT INTO EL_TABLE (APPLICATION_NAME,CHAIN_NAME,EL_DATA) values ('demo','chain4','IF(x2, IF(x0, THEN(a, b)));');
DELETE FROM SCRIPT_NODE_TABLE;
INSERT INTO SCRIPT_NODE_TABLE (APPLICATION_NAME,SCRIPT_NODE_ID,SCRIPT_NODE_NAME,SCRIPT_NODE_TYPE,SCRIPT_NODE_DATA) values ('demo','x0','if 脚本','if_script','return true');
INSERT INTO SCRIPT_NODE_TABLE (APPLICATION_NAME,SCRIPT_NODE_ID,SCRIPT_NODE_NAME,SCRIPT_NODE_TYPE,SCRIPT_NODE_DATA) values ('demo','x1','if 脚本','if_script','return false');
INSERT INTO SCRIPT_NODE_TABLE (APPLICATION_NAME,SCRIPT_NODE_ID,SCRIPT_NODE_NAME,SCRIPT_NODE_TYPE,SCRIPT_NODE_DATA,SCRIPT_LANGUAGE) values ('demo','x0','if 脚本','if_script','return true','groovy');
INSERT INTO SCRIPT_NODE_TABLE (APPLICATION_NAME,SCRIPT_NODE_ID,SCRIPT_NODE_NAME,SCRIPT_NODE_TYPE,SCRIPT_NODE_DATA,SCRIPT_LANGUAGE) values ('demo','x1','if 脚本','if_script','return false','groovy');
INSERT INTO SCRIPT_NODE_TABLE (APPLICATION_NAME,SCRIPT_NODE_ID,SCRIPT_NODE_NAME,SCRIPT_NODE_TYPE,SCRIPT_NODE_DATA,SCRIPT_LANGUAGE) values ('demo','x2','python脚本','if_script','return true','js');

View File

@ -15,5 +15,6 @@ create table `script_node_table`
`script_node_name` text NOT NULL,
`script_node_type` text NOT NULL,
`script_node_data` text NOT NULL,
`script_language` varchar(1024) NOT NULL,
PRIMARY KEY (`id`)
);