From 0f6436e8fea040b9255458ca27f8cec01e2ce369 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=B0=B9=E8=88=9F?= <13007110208@163.com>
Date: Tue, 4 Mar 2025 10:08:30 +0800
Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90=E4=BA=A4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
docker-compose.yaml | 11 +++
dockerfile | 16 ++++
pom.xml | 78 ++++++++++++++++
src/main/java/sdk_996/SqlRunApplication.java | 13 +++
.../bean/cnofig/GlobalExceptionHandler.java | 93 +++++++++++++++++++
src/main/java/sdk_996/bean/em/ErrorCode.java | 35 +++++++
.../bean/exp/FormatValidationException.java | 11 +++
.../java/sdk_996/bean/exp/SqlException.java | 18 ++++
src/main/java/sdk_996/bean/vo/ResultVO.java | 60 ++++++++++++
.../controller/SqlExecuteController.java | 21 +++++
.../java/sdk_996/mapper/SqlExecuteMapper.java | 17 ++++
.../sdk_996/service/SqlExecuteService.java | 86 +++++++++++++++++
src/main/resources/application.yml | 23 +++++
src/main/resources/banner.txt | 21 +++++
src/main/resources/logback-spring.xml | 31 +++++++
15 files changed, 534 insertions(+)
create mode 100644 docker-compose.yaml
create mode 100644 dockerfile
create mode 100644 pom.xml
create mode 100644 src/main/java/sdk_996/SqlRunApplication.java
create mode 100644 src/main/java/sdk_996/bean/cnofig/GlobalExceptionHandler.java
create mode 100644 src/main/java/sdk_996/bean/em/ErrorCode.java
create mode 100644 src/main/java/sdk_996/bean/exp/FormatValidationException.java
create mode 100644 src/main/java/sdk_996/bean/exp/SqlException.java
create mode 100644 src/main/java/sdk_996/bean/vo/ResultVO.java
create mode 100644 src/main/java/sdk_996/controller/SqlExecuteController.java
create mode 100644 src/main/java/sdk_996/mapper/SqlExecuteMapper.java
create mode 100644 src/main/java/sdk_996/service/SqlExecuteService.java
create mode 100644 src/main/resources/application.yml
create mode 100644 src/main/resources/banner.txt
create mode 100644 src/main/resources/logback-spring.xml
diff --git a/docker-compose.yaml b/docker-compose.yaml
new file mode 100644
index 0000000..0b2ddeb
--- /dev/null
+++ b/docker-compose.yaml
@@ -0,0 +1,11 @@
+version: '3.4'
+services:
+ sql-runner:
+ build:
+ context: .
+ dockerfile: Dockerfile
+ restart: always
+ container_name: sql-runner
+ image: sql-runner:latest
+ ports:
+ - "8308:8308"
diff --git a/dockerfile b/dockerfile
new file mode 100644
index 0000000..b92d4af
--- /dev/null
+++ b/dockerfile
@@ -0,0 +1,16 @@
+#ARG instanceid="996sdk-yinqing-registry.cn-hangzhou.cr.aliyuncs.com/default/openjdk"
+ARG instanceid="registry.cn-hangzhou.aliyuncs.com/yinzhou_docker_hub/openjdk"
+
+FROM ${instanceid}:8-jdk-alpine
+
+# 将 JAR 文件复制到容器中
+COPY ./target/sql_run.jar /opt/sdk/sql_run.jar
+
+# 设置工作目录
+WORKDIR /opt/sdk/
+
+# 暴露应用程序的端口
+EXPOSE 8308
+
+# 运行应用程序
+ENTRYPOINT ["java", "-jar", "sql_run.jar"]
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..ddefd22
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,78 @@
+
+ 4.0.0
+ sdk_996
+ sql_run
+ 1.0
+
+ 1.8
+ 2.7.18
+ 3.5.9
+ 2.4.14
+ 42.6.0
+ 1.2.58
+ 1.18.8
+ UTF-8
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+ ${spring-boot.version}
+
+
+
+ com.baomidou
+ mybatis-plus-boot-starter
+ ${mybatis-plus-boot-starter.version}
+
+
+
+ com.baomidou
+ dynamic-datasource-spring-boot-starter
+ 4.3.0
+
+
+
+ org.postgresql
+ postgresql
+ ${postgresql.version}
+
+
+
+ com.alibaba
+ fastjson
+ ${fastjson.version}
+
+
+
+ org.projectlombok
+ lombok
+ ${lombok.version}
+ provided
+
+
+
+
+
+
+ ${artifactId}
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+ ${spring-boot.version}
+
+
+
+ repackage
+
+
+
+
+
+
+
+
diff --git a/src/main/java/sdk_996/SqlRunApplication.java b/src/main/java/sdk_996/SqlRunApplication.java
new file mode 100644
index 0000000..1edbb31
--- /dev/null
+++ b/src/main/java/sdk_996/SqlRunApplication.java
@@ -0,0 +1,13 @@
+package sdk_996;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class SqlRunApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(SqlRunApplication.class, args);
+ }
+
+}
diff --git a/src/main/java/sdk_996/bean/cnofig/GlobalExceptionHandler.java b/src/main/java/sdk_996/bean/cnofig/GlobalExceptionHandler.java
new file mode 100644
index 0000000..6dfff39
--- /dev/null
+++ b/src/main/java/sdk_996/bean/cnofig/GlobalExceptionHandler.java
@@ -0,0 +1,93 @@
+package sdk_996.bean.cnofig;
+
+import com.alibaba.fastjson.JSONObject;
+import org.postgresql.util.PSQLException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+import sdk_996.bean.em.ErrorCode;
+import sdk_996.bean.exp.FormatValidationException;
+import sdk_996.bean.exp.SqlException;
+import sdk_996.bean.vo.ResultVO;
+
+import java.net.SocketTimeoutException;
+import java.util.concurrent.CompletionException;
+
+@ControllerAdvice
+public class GlobalExceptionHandler {
+
+ private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
+
+
+ @ExceptionHandler(value = {FormatValidationException.class})
+ @ResponseBody
+ public ResultVO formatValidationException(FormatValidationException ex) {
+ logger.info("format validation exception: {}", ex);
+ return ResultVO.error(ErrorCode.PARAM_ERROR);
+ }
+
+
+ @ExceptionHandler(value = {SqlException.class, CompletionException.class})
+ @ResponseBody
+ public ResultVO handleSqlException(SqlException ex) {
+ Exception cause = ex.getEx();
+ String key_v = ex.getKey_v();
+
+ // 根据具体的异常类型做出响应
+ if (cause.getCause().getCause() instanceof SocketTimeoutException) {
+ return handleTimeoutException(key_v);
+ } else if (cause.getCause() instanceof PSQLException) {
+ return handleSqlSyntaxException(key_v, cause);
+ } else {
+ return handleGenericSqlException(key_v, cause);
+ }
+ }
+
+ private ResultVO handleTimeoutException(String key_v) {
+ logger.error("SQL query timeout for key: {}", key_v);
+ JSONObject errorDetail = createErrorDetail(key_v, ErrorCode.SQL_TIMEOUT.getMessage(), "");
+ return ResultVO.error_sql(ErrorCode.SQL_TIMEOUT, errorDetail);
+ }
+
+ private ResultVO handleSqlSyntaxException(String sqlKey, Exception cause) {
+ logger.error("SQL syntax error for key: {}", sqlKey, cause);
+ JSONObject errorDetail = createErrorDetail(sqlKey, ErrorCode.SQL_ERROR.getMessage(), cause.getMessage());
+ return ResultVO.error_sql(ErrorCode.SQL_ERROR, errorDetail);
+ }
+
+ private ResultVO handleGenericSqlException(String sqlKey, Exception cause) {
+ logger.error("SQL execution error for key: {}", sqlKey, cause);
+ return ResultVO.error(ErrorCode.SQL_EXECUTE_ERROR);
+ }
+
+ private JSONObject createErrorDetail(String sqlKey, String Message, String errorMessage) {
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.put("sql_key:sql", sqlKey);
+ jsonObject.put(Message, errorMessage);
+ return jsonObject;
+ }
+
+ @ExceptionHandler(value = ArithmeticException.class)
+ @ResponseBody
+ public ResultVO handleArithmeticException(ArithmeticException ex) {
+ logger.error("Arithmetic exception occurred: ", ex);
+ return ResultVO.error(ErrorCode.CODE_ERROR);
+ }
+
+ @ExceptionHandler(value = NullPointerException.class)
+ @ResponseBody
+ public ResultVO handleNullPointerException(NullPointerException ex) {
+ logger.error("NullPointerException occurred: ", ex);
+ return ResultVO.error(ErrorCode.NULL_POINTER_ERROR);
+ }
+
+ @ExceptionHandler(value = Exception.class)
+ @ResponseBody
+ public ResultVO handleGeneralException(Exception ex) {
+ logger.error("General exception occurred: ", ex);
+ return ResultVO.error(ErrorCode.RUNTIME_EXCEPTION);
+ }
+}
diff --git a/src/main/java/sdk_996/bean/em/ErrorCode.java b/src/main/java/sdk_996/bean/em/ErrorCode.java
new file mode 100644
index 0000000..a8bfc4c
--- /dev/null
+++ b/src/main/java/sdk_996/bean/em/ErrorCode.java
@@ -0,0 +1,35 @@
+package sdk_996.bean.em;
+
+public enum ErrorCode {
+
+ SUCCESS(200, "成功"),
+ SQL_ERROR(301, "SQL异常,请检查 SQL 语句"),
+ SQL_TIMEOUT(302, "SQL查询超时异常"),
+ SQL_EXECUTE_ERROR(303, "SQL执行异常"),
+ NULL_POINTER_ERROR(304, "空指针异常"),
+ CODE_ERROR(305, "代码运行异常,请查看报错信息"),
+ INTERNAL_ERROR(306, "内部异常"),
+
+ PARAM_NULL(310, "参数为空"),
+ PARAM_ERROR(311, "参数有误,检查请求参数后重试."),
+ DATA_EMPTY(312, "此时间段内无数据"),
+ RUNTIME_EXCEPTION(500, "服务运行异常");
+
+ private final Integer code;
+ private final String message;
+
+ ErrorCode(Integer code, String message) {
+ this.code = code;
+ this.message = message;
+ }
+
+ public Integer getCode() {
+ return code;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+
+}
diff --git a/src/main/java/sdk_996/bean/exp/FormatValidationException.java b/src/main/java/sdk_996/bean/exp/FormatValidationException.java
new file mode 100644
index 0000000..96dbe9e
--- /dev/null
+++ b/src/main/java/sdk_996/bean/exp/FormatValidationException.java
@@ -0,0 +1,11 @@
+package sdk_996.bean.exp;
+
+public class FormatValidationException extends RuntimeException{
+ private static final long serialVersionUID = 1L;
+
+ private Exception ex;
+
+ public FormatValidationException(Exception ex) {
+ this.ex = ex;
+ }
+}
diff --git a/src/main/java/sdk_996/bean/exp/SqlException.java b/src/main/java/sdk_996/bean/exp/SqlException.java
new file mode 100644
index 0000000..fe7dee3
--- /dev/null
+++ b/src/main/java/sdk_996/bean/exp/SqlException.java
@@ -0,0 +1,18 @@
+package sdk_996.bean.exp;
+
+import lombok.Data;
+
+@Data
+public class SqlException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
+
+ private String key_v;
+ private Exception ex;
+
+
+ public SqlException(String key_v, Exception ex) {
+ this.key_v = key_v;
+ this.ex = ex;
+ }
+
+}
diff --git a/src/main/java/sdk_996/bean/vo/ResultVO.java b/src/main/java/sdk_996/bean/vo/ResultVO.java
new file mode 100644
index 0000000..f8dca5b
--- /dev/null
+++ b/src/main/java/sdk_996/bean/vo/ResultVO.java
@@ -0,0 +1,60 @@
+package sdk_996.bean.vo;
+
+import com.alibaba.fastjson.JSONObject;
+import lombok.Data;
+import sdk_996.bean.em.ErrorCode;
+
+import java.time.LocalDateTime;
+
+@Data
+public class ResultVO {
+ private Integer code;
+ private Object message;
+ private Object data;
+ private LocalDateTime currentTime; // 当前时间字段
+
+ public ResultVO() {
+ this.currentTime = LocalDateTime.now(); // 在构造函数中设置当前时间
+ this.setCode(200); // 默认成功状态
+ this.setMessage("成功"); // 默认成功消息
+ }
+
+ // 提供一个静态方法来快速创建成功响应
+ public static ResultVO success() {
+ return new ResultVO();
+ }
+
+ // 提供一个静态方法来快速创建带有自定义消息的成功响应
+ public static ResultVO success(JSONObject data) {
+ ResultVO resultVO = new ResultVO();
+ resultVO.setData(data);
+ return resultVO;
+ }
+
+ // 提供一个静态方法来快速创建带有错误码和消息的响应
+ public static ResultVO error(ErrorCode errorCode) {
+ ResultVO resultVO = new ResultVO();
+ resultVO.setCode(errorCode.getCode());
+ resultVO.setMessage(errorCode.getMessage());
+ return resultVO;
+ }
+
+
+ public static ResultVO error_sql(ErrorCode errorCode, JSONObject msg) {
+ ResultVO resultVO = new ResultVO();
+ resultVO.setCode(errorCode.getCode());
+ resultVO.setMessage(msg);
+ return resultVO;
+ }
+
+ public static void main(String[] args) {
+ ResultVO resultVO = ResultVO.error(ErrorCode.SQL_ERROR);
+ System.out.println(resultVO);
+ }
+
+ @Override
+ public String toString() {
+ // 以json格式打印返回
+ return JSONObject.toJSONString(this);
+ }
+}
diff --git a/src/main/java/sdk_996/controller/SqlExecuteController.java b/src/main/java/sdk_996/controller/SqlExecuteController.java
new file mode 100644
index 0000000..297198e
--- /dev/null
+++ b/src/main/java/sdk_996/controller/SqlExecuteController.java
@@ -0,0 +1,21 @@
+package sdk_996.controller;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+import sdk_996.bean.vo.ResultVO;
+import sdk_996.service.SqlExecuteService;
+
+@RestController
+@RequestMapping("/api")
+public class SqlExecuteController {
+
+ @Autowired
+ private SqlExecuteService sqlExecuteService;
+
+ @PostMapping("/sql-query")
+ public ResultVO sqlQuery(@RequestBody String sql) {
+ return ResultVO.success(sqlExecuteService.performCustomQuery(sql));
+ }
+
+
+}
\ No newline at end of file
diff --git a/src/main/java/sdk_996/mapper/SqlExecuteMapper.java b/src/main/java/sdk_996/mapper/SqlExecuteMapper.java
new file mode 100644
index 0000000..f2f5798
--- /dev/null
+++ b/src/main/java/sdk_996/mapper/SqlExecuteMapper.java
@@ -0,0 +1,17 @@
+package sdk_996.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Select;
+
+import java.util.List;
+import java.util.Map;
+
+@Mapper
+public interface SqlExecuteMapper extends BaseMapper