mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 12:12:08 +08:00
enhancement #I5XWL2 数据库插件支持脚本的存储
This commit is contained in:
@@ -7,6 +7,7 @@ import com.yomahub.liteflow.builder.prop.NodePropBean;
|
|||||||
import com.yomahub.liteflow.enums.NodeTypeEnum;
|
import com.yomahub.liteflow.enums.NodeTypeEnum;
|
||||||
import com.yomahub.liteflow.parser.sql.exception.ELSQLException;
|
import com.yomahub.liteflow.parser.sql.exception.ELSQLException;
|
||||||
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
|
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
|
||||||
|
import com.yomahub.liteflow.script.ScriptExecutor;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DatabaseMetaData;
|
import java.sql.DatabaseMetaData;
|
||||||
@@ -16,6 +17,7 @@ import java.sql.ResultSet;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.ServiceLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* jdbc 工具类
|
* jdbc 工具类
|
||||||
@@ -107,12 +109,14 @@ public class JDBCHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String chains = CollUtil.join(result, StrUtil.CRLF);
|
String chains = CollUtil.join(result, StrUtil.CRLF);
|
||||||
String nodes = getScriptNodes();
|
// 根据 SPI 判断是否需要添加 script node 节点
|
||||||
if (StrUtil.isEmpty(nodes)) {
|
ServiceLoader<ScriptExecutor> loader = ServiceLoader.load(ScriptExecutor.class);
|
||||||
return StrFormatter.format(XML_PATTERN, StrUtil.EMPTY, chains);
|
if (loader.iterator().hasNext()) {
|
||||||
|
String nodes = getScriptNodes();
|
||||||
|
return StrFormatter.format(XML_PATTERN, StrFormatter.format(NODES_XML_PATTERN, nodes), chains);
|
||||||
}
|
}
|
||||||
|
|
||||||
return StrFormatter.format(XML_PATTERN, StrFormatter.format(NODES_XML_PATTERN, nodes), chains);
|
return StrFormatter.format(XML_PATTERN, StrUtil.EMPTY, chains);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getScriptNodes() {
|
public String getScriptNodes() {
|
||||||
@@ -140,14 +144,6 @@ public class JDBCHelper {
|
|||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
conn = getConn();
|
conn = getConn();
|
||||||
// 判断表是否存在
|
|
||||||
// DatabaseMetaData meta = conn.getMetaData();
|
|
||||||
// String types[] = {"TABLE"};
|
|
||||||
// rs = meta.getTables(null, null, scriptNodeTableName, types);
|
|
||||||
// if (!rs.next()) {
|
|
||||||
// return StrUtil.EMPTY;
|
|
||||||
// }
|
|
||||||
|
|
||||||
stmt = conn.prepareStatement(sqlCmd, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
stmt = conn.prepareStatement(sqlCmd, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||||
// 设置游标拉取数量
|
// 设置游标拉取数量
|
||||||
stmt.setFetchSize(FETCH_SIZE_MAX);
|
stmt.setFetchSize(FETCH_SIZE_MAX);
|
||||||
@@ -164,7 +160,7 @@ public class JDBCHelper {
|
|||||||
throw new ELSQLException("is not script node type; node id: " + id);
|
throw new ELSQLException("is not script node type; node id: " + id);
|
||||||
}
|
}
|
||||||
|
|
||||||
result.add(StrFormatter.format(NODE_XML_PATTERN, id, name, type, language));
|
result.add(StrFormatter.format(NODE_XML_PATTERN, id, name, type, language, data));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ELSQLException(e.getMessage());
|
throw new ELSQLException(e.getMessage());
|
||||||
|
|||||||
@@ -52,8 +52,9 @@
|
|||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.yomahub</groupId>
|
<groupId>com.yomahub</groupId>
|
||||||
<artifactId>liteflow-script-javascript</artifactId>
|
<artifactId>liteflow-script-groovy</artifactId>
|
||||||
<version>${revision}</version>
|
<version>${revision}</version>
|
||||||
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@@ -33,35 +33,64 @@ import java.sql.Statement;
|
|||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
@ComponentScan({"com.yomahub.liteflow.test.sql.cmp"})
|
@ComponentScan({"com.yomahub.liteflow.test.sql.cmp"})
|
||||||
public class SQLWithXmlELSpringbootTest extends BaseTest {
|
public class SQLWithXmlELSpringbootTest extends BaseTest {
|
||||||
@Resource
|
@Resource
|
||||||
private FlowExecutor flowExecutor;
|
private FlowExecutor flowExecutor;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSQLWithXml() {
|
public void testSQLWithXml() {
|
||||||
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
|
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
|
||||||
Assert.assertEquals("a==>b==>c", response.getExecuteStepStr());
|
Assert.assertEquals("a==>b==>c", response.getExecuteStepStr());
|
||||||
|
|
||||||
// 修改数据库
|
// 修改数据库
|
||||||
changeData();
|
changeData();
|
||||||
|
|
||||||
// 重新加载规则
|
// 重新加载规则
|
||||||
flowExecutor.reloadRule();
|
flowExecutor.reloadRule();
|
||||||
Assert.assertEquals("a==>c==>b", flowExecutor.execute2Resp("chain1", "arg").getExecuteStepStr());
|
Assert.assertEquals("a==>c==>b", flowExecutor.execute2Resp("chain1", "arg").getExecuteStepStr());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Test
|
||||||
* 修改数据库数据
|
public void testSQLWithScriptXml() {
|
||||||
*/
|
LiteflowResponse response = flowExecutor.execute2Resp("chain3", "arg");
|
||||||
private void changeData() {
|
Assert.assertTrue(response.isSuccess());
|
||||||
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
|
Assert.assertEquals("x0[if 脚本]==>a==>b", response.getExecuteStepStrWithoutTime());
|
||||||
SQLParserVO sqlParserVO = JsonUtil.parseObject(liteflowConfig.getRuleSourceExtData(), SQLParserVO.class);
|
|
||||||
Connection connection = null;
|
// 修改数据库
|
||||||
try {
|
changeScriptData();
|
||||||
connection = DriverManager.getConnection(sqlParserVO.getUrl(), sqlParserVO.getUsername(), sqlParserVO.getPassword());
|
// 重新加载规则
|
||||||
Statement statement = connection.createStatement();
|
flowExecutor.reloadRule();
|
||||||
statement.executeUpdate("UPDATE EL_TABLE SET EL_DATA='THEN(a, c, b);' WHERE chain_name='chain1'");
|
Assert.assertEquals("x0[if 脚本]", flowExecutor.execute2Resp("chain3", "arg").getExecuteStepStr());
|
||||||
} 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 = null;
|
||||||
|
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 = null;
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
package com.yomahub.liteflow.test.sql.cmp;
|
|
||||||
|
|
||||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
|
||||||
import com.yomahub.liteflow.core.NodeComponent;
|
|
||||||
|
|
||||||
@LiteflowComponent("d")
|
|
||||||
public class DCmp extends NodeComponent {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void process() {
|
|
||||||
System.out.println("DCmp executed!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
package com.yomahub.liteflow.test.sql.cmp;
|
|
||||||
|
|
||||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
|
||||||
import com.yomahub.liteflow.core.NodeComponent;
|
|
||||||
|
|
||||||
|
|
||||||
@LiteflowComponent("e")
|
|
||||||
public class ECmp extends NodeComponent {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void process() {
|
|
||||||
System.out.println("ECmp executed!");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -2,9 +2,9 @@ DELETE FROM EL_TABLE;
|
|||||||
|
|
||||||
INSERT INTO EL_TABLE (CHAIN_NAME,EL_DATA) values ('chain1','THEN(a, b, c);');
|
INSERT INTO EL_TABLE (CHAIN_NAME,EL_DATA) values ('chain1','THEN(a, b, c);');
|
||||||
INSERT INTO EL_TABLE (CHAIN_NAME,EL_DATA) values ('chain2','THEN(a, b, c);');
|
INSERT INTO EL_TABLE (CHAIN_NAME,EL_DATA) values ('chain2','THEN(a, b, c);');
|
||||||
INSERT INTO EL_TABLE (CHAIN_NAME,EL_DATA) values ('chain3','IF(x0, THEN(d, e));');
|
INSERT INTO EL_TABLE (CHAIN_NAME,EL_DATA) values ('chain3','IF(x0, THEN(a, b));');
|
||||||
|
|
||||||
DELETE FROM SCRIPT_NODE_TABLE;
|
DELETE FROM SCRIPT_NODE_TABLE;
|
||||||
|
|
||||||
INSERT INTO SCRIPT_NODE_TABLE (SCRIPT_NODE_ID,SCRIPT_NODE_NAME,SCRIPT_NODE_TYPE,SCRIPT_NODE_LANGUAGE,SCRIPT_NODE_DATA) values ('x0','if 脚本','if_script','js','return true;');
|
INSERT INTO SCRIPT_NODE_TABLE (SCRIPT_NODE_ID,SCRIPT_NODE_NAME,SCRIPT_NODE_TYPE,SCRIPT_NODE_LANGUAGE,SCRIPT_NODE_DATA) values ('x0','if 脚本','if_script','js','return true');
|
||||||
INSERT INTO SCRIPT_NODE_TABLE (SCRIPT_NODE_ID,SCRIPT_NODE_NAME,SCRIPT_NODE_TYPE,SCRIPT_NODE_LANGUAGE,SCRIPT_NODE_DATA) values ('x1','if 脚本','if_script','js','return false;');
|
INSERT INTO SCRIPT_NODE_TABLE (SCRIPT_NODE_ID,SCRIPT_NODE_NAME,SCRIPT_NODE_TYPE,SCRIPT_NODE_LANGUAGE,SCRIPT_NODE_DATA) values ('x1','if 脚本','if_script','js','return false');
|
||||||
|
|||||||
Reference in New Issue
Block a user