From a9c3ef36a5e47624f534b2676daf763437133ae8 Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Thu, 27 Jul 2023 17:02:05 +0800 Subject: [PATCH] support run sql script file in jdbc protocol config (#1117) --- collector/pom.xml | 6 ++++++ .../collector/collect/database/JdbcCommonCollect.java | 9 +++++++++ .../common/entity/job/protocol/JdbcProtocol.java | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/collector/pom.xml b/collector/pom.xml index 3a5a4553690..b491b1920ce 100644 --- a/collector/pom.xml +++ b/collector/pom.xml @@ -56,6 +56,12 @@ org.springframework.boot spring-boot-starter-validation + + + org.springframework + spring-jdbc + 5.3.23 + org.apache.kafka diff --git a/collector/src/main/java/org/dromara/hertzbeat/collector/collect/database/JdbcCommonCollect.java b/collector/src/main/java/org/dromara/hertzbeat/collector/collect/database/JdbcCommonCollect.java index ede7f0c1a32..9993e3dc5d8 100644 --- a/collector/src/main/java/org/dromara/hertzbeat/collector/collect/database/JdbcCommonCollect.java +++ b/collector/src/main/java/org/dromara/hertzbeat/collector/collect/database/JdbcCommonCollect.java @@ -32,6 +32,8 @@ import org.dromara.hertzbeat.common.util.CommonUtil; import lombok.extern.slf4j.Slf4j; import org.postgresql.util.PSQLException; +import org.springframework.core.io.FileSystemResource; +import org.springframework.jdbc.datasource.init.ScriptUtils; import java.sql.Connection; import java.sql.DriverManager; @@ -54,6 +56,8 @@ public class JdbcCommonCollect extends AbstractCollect { private static final String QUERY_TYPE_ONE_ROW = "oneRow"; private static final String QUERY_TYPE_MULTI_ROW = "multiRow"; private static final String QUERY_TYPE_COLUMNS = "columns"; + + private static final String RUN_SCRIPT = "runScript"; public JdbcCommonCollect(){} @@ -84,6 +88,11 @@ public void collect(CollectRep.MetricsData.Builder builder, long appId, String a case QUERY_TYPE_COLUMNS: queryOneRowByMatchTwoColumns(statement, jdbcProtocol.getSql(), metrics.getAliasFields(), builder, startTime); break; + case RUN_SCRIPT: + Connection connection = statement.getConnection(); + FileSystemResource rc = new FileSystemResource(jdbcProtocol.getSql()); + ScriptUtils.executeSqlScript(connection, rc); + break; default: builder.setCode(CollectRep.Code.FAIL); builder.setMsg("Not support database query type: " + jdbcProtocol.getQueryType()); diff --git a/common/src/main/java/org/dromara/hertzbeat/common/entity/job/protocol/JdbcProtocol.java b/common/src/main/java/org/dromara/hertzbeat/common/entity/job/protocol/JdbcProtocol.java index fe0e761d828..f9e3337d094 100644 --- a/common/src/main/java/org/dromara/hertzbeat/common/entity/job/protocol/JdbcProtocol.java +++ b/common/src/main/java/org/dromara/hertzbeat/common/entity/job/protocol/JdbcProtocol.java @@ -61,7 +61,7 @@ public class JdbcProtocol { */ private String platform; /** - * SQL查询方式: oneRow, multiRow, columns + * SQL查询方式: oneRow, multiRow, columns, runScript */ private String queryType; /**