1
0
Эх сурвалжийг харах

#IB0SJ1 新增read(chainId)接口

jay li 5 сар өмнө
parent
commit
5cb9df347c

+ 2 - 0
liteflow-rule-plugin/liteflow-rule-sql/src/main/java/com/yomahub/liteflow/parser/constant/SqlReadConstant.java

@@ -26,6 +26,8 @@ public class SqlReadConstant {
 
     public static final String SQL_PATTERN = "SELECT * FROM {} WHERE {}='{}'";
 
+    public static final String SQL_PATTERN_WITH_CHAIN_ID = "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 {}='{}'";

+ 23 - 3
liteflow-rule-plugin/liteflow-rule-sql/src/main/java/com/yomahub/liteflow/parser/sql/read/AbstractSqlRead.java

@@ -30,14 +30,32 @@ public abstract class AbstractSqlRead<T> implements SqlRead<T> {
     }
 
     @Override
-    public List<T> read(String... exceptions) {
+    public List<T> read(String chainId) {
+        if (!needRead()) {
+            return new ArrayList<>();
+        }
+
+        checkConfig();
+        String sqlCmd = buildQuerySql(chainId);
+        return readList(sqlCmd);
+    }
+
+
+    @Override
+    public List<T> read() {
         // 如果不需要读取直接返回
         if (!needRead()) {
             return new ArrayList<>();
         }
 
         checkConfig();
-        String sqlCmd = buildQuerySql(exceptions);
+        String sqlCmd = buildQuerySql();
+
+        return readList(sqlCmd);
+    }
+
+
+    private List<T> readList(String sqlCmd) {
         // 如果允许,就打印 sql 语句
         logSqlIfEnable(sqlCmd);
 
@@ -85,7 +103,9 @@ public abstract class AbstractSqlRead<T> implements SqlRead<T> {
      */
     public abstract boolean getEnableFiledValue(ResultSet rs) throws SQLException;
 
-    public abstract String buildQuerySql(String... exceptions);
+    public abstract String buildQuerySql();
+
+    public abstract String buildQuerySql(String chainId);
 
     public abstract void checkConfig();
 

+ 11 - 1
liteflow-rule-plugin/liteflow-rule-sql/src/main/java/com/yomahub/liteflow/parser/sql/read/SqlRead.java

@@ -9,6 +9,7 @@ import java.util.List;
  *
  * @author tangkc
  * @author houxinyu
+ * @author Jay li
  * @since 2.11.1
  */
 public interface SqlRead<T> {
@@ -18,7 +19,16 @@ public interface SqlRead<T> {
      *
      * @return 返回读取到的数据
      */
-    List<T> read(String... exceptions);
+    List<T> read();
+
+
+    /**
+     * 根据chainId 读取
+     *
+     * @return 返回读取到的数据
+     */
+    List<T> read(String chainId);
+
 
     /**
      * 类型

+ 15 - 1
liteflow-rule-plugin/liteflow-rule-sql/src/main/java/com/yomahub/liteflow/parser/sql/read/impl/ChainRead.java

@@ -54,7 +54,7 @@ public class ChainRead extends AbstractSqlRead<ChainVO> {
     }
 
     @Override
-    public String buildQuerySql(String... exceptions) {
+    public String buildQuerySql() {
         if (StrUtil.isNotBlank(super.config.getChainCustomSql())) {
             return super.config.getChainCustomSql();
         }
@@ -66,6 +66,20 @@ public class ChainRead extends AbstractSqlRead<ChainVO> {
         return StrUtil.format(SqlReadConstant.SQL_PATTERN, chainTableName, chainApplicationNameField, applicationName);
     }
 
+    @Override
+    public String buildQuerySql(String chainId) {
+        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_WITH_CHAIN_ID, chainTableName, chainApplicationNameField, applicationName,
+                super.config.getChainNameField(), chainId);
+    }
+
     @Override
     public void checkConfig() {
         String chainTableName = super.config.getChainTableName();

+ 19 - 6
liteflow-rule-plugin/liteflow-rule-sql/src/main/java/com/yomahub/liteflow/parser/sql/read/impl/InstanceIdRead.java

@@ -1,6 +1,5 @@
 package com.yomahub.liteflow.parser.sql.read.impl;
 
-import cn.hutool.core.util.ArrayUtil;
 import cn.hutool.core.util.StrUtil;
 import com.yomahub.liteflow.parser.constant.ReadType;
 import com.yomahub.liteflow.parser.constant.SqlReadConstant;
@@ -8,13 +7,14 @@ import com.yomahub.liteflow.parser.sql.exception.ELSQLException;
 import com.yomahub.liteflow.parser.sql.read.AbstractSqlRead;
 import com.yomahub.liteflow.parser.sql.read.vo.InstanceIdVO;
 import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
+import org.apache.commons.lang.StringUtils;
 
 import java.sql.ResultSet;
 import java.sql.SQLException;
 
 /**
  * @author Jay li
- * @since 2.12.4
+ * @since 2.13.0
  */
 
 public class InstanceIdRead extends AbstractSqlRead<InstanceIdVO> {
@@ -43,14 +43,27 @@ public class InstanceIdRead extends AbstractSqlRead<InstanceIdVO> {
     }
 
     @Override
-    public String buildQuerySql(String... exceptions) {
+    public String buildQuerySql() {
+        String tableName = super.config.getInstanceIdTableName();
+        String applicationNameField = super.config.getInstanceIdApplicationNameField();
+        String applicationName = super.config.getApplicationName();
+
+        return StrUtil.format(SqlReadConstant.SQL_PATTERN, tableName,
+                applicationNameField, applicationName);
+    }
+
+    @Override
+    public String buildQuerySql(String chainId) {
         String tableName = super.config.getInstanceIdTableName();
         String chainNameField = super.config.getInstanceChainIdField();
+        String instanceIdApplicationNameField = super.config.getInstanceIdApplicationNameField();
+        String applicationName = super.config.getApplicationName();
 
-        if (ArrayUtil.isEmpty(exceptions)) {
-            throw new IllegalArgumentException("You did not define the chainName");
+        if (StringUtils.isEmpty(chainId)) {
+            throw new IllegalArgumentException("You did not define the chainId");
         }
-        return StrUtil.format(SqlReadConstant.SQL_PATTERN, tableName, chainNameField, exceptions[0]);
+        return StrUtil.format(SqlReadConstant.SQL_PATTERN_WITH_CHAIN_ID, tableName, instanceIdApplicationNameField
+                , applicationName, chainNameField, chainId);
     }
 
     @Override

+ 21 - 6
liteflow-rule-plugin/liteflow-rule-sql/src/main/java/com/yomahub/liteflow/parser/sql/read/impl/ScriptRead.java

@@ -55,7 +55,7 @@ public class ScriptRead extends AbstractSqlRead<ScriptVO> {
     }
 
     @Override
-    public String buildQuerySql(String... exceptions) {
+    public String buildQuerySql() {
         if (StringUtils.isNotBlank(super.config.getScriptCustomSql())) {
             return super.config.getScriptCustomSql();
         }
@@ -70,6 +70,21 @@ public class ScriptRead extends AbstractSqlRead<ScriptVO> {
                 applicationName);
     }
 
+    @Override
+    public String buildQuerySql(String scriptNodeId) {
+        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();
+        String scriptIdField = super.config.getScriptIdField();
+        return StrUtil.format(SqlReadConstant.SQL_PATTERN_WITH_CHAIN_ID,
+                scriptTableName, scriptApplicationNameField, applicationName,
+                scriptIdField, scriptNodeId);
+    }
+
     @Override
     public void checkConfig() {
         String scriptTableName = super.config.getScriptTableName();
@@ -78,19 +93,19 @@ public class ScriptRead extends AbstractSqlRead<ScriptVO> {
         String scriptTypeField = super.config.getScriptTypeField();
         String scriptApplicationNameField = super.config.getScriptApplicationNameField();
 
-        if(StrUtil.isBlank(scriptTableName)){
+        if (StrUtil.isBlank(scriptTableName)) {
             throw new ELSQLException("You did not define the scriptTableName property");
         }
-        if(StrUtil.isBlank(scriptIdField)){
+        if (StrUtil.isBlank(scriptIdField)) {
             throw new ELSQLException("You did not define the scriptIdField property");
         }
-        if(StrUtil.isBlank(scriptDataField)){
+        if (StrUtil.isBlank(scriptDataField)) {
             throw new ELSQLException("You did not define the scriptDataField property");
         }
-        if(StrUtil.isBlank(scriptTypeField)){
+        if (StrUtil.isBlank(scriptTypeField)) {
             throw new ELSQLException("You did not define the scriptTypeField property");
         }
-        if(StrUtil.isBlank(scriptApplicationNameField)){
+        if (StrUtil.isBlank(scriptApplicationNameField)) {
             throw new ELSQLException("You did not define the scriptApplicationNameField property");
         }
     }