From ab267beccdd5850cee90b81c0ec8afd92768869e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sh=E5=B0=8F=E9=87=91=E9=B1=BC3?= Date: Sun, 9 Jul 2023 15:59:51 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E7=9A=84=E6=96=87=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chat2db-client/src/layouts/index.tsx | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/chat2db-client/src/layouts/index.tsx b/chat2db-client/src/layouts/index.tsx index 62cf2aaf..8fc4db59 100644 --- a/chat2db-client/src/layouts/index.tsx +++ b/chat2db-client/src/layouts/index.tsx @@ -24,7 +24,7 @@ import registerMessage from './init/registerMessage'; import registerNotification from './init/registerNotification'; import { NotificationInstance } from 'antd/es/notification/interface'; import { ModalStaticFunctions } from 'antd/es/modal/confirm'; -import Sub from './sub' +import Sub from './sub'; declare global { interface Window { _Lang: string; @@ -81,7 +81,6 @@ const restartCount = 200; let staticNotification: NotificationInstance; - function AppContainer() { const { token } = useToken(); const [initEnd, setInitEnd] = useState(false); @@ -92,7 +91,6 @@ function AppContainer() { // staticNotification = staticFunction.notification - useEffect(() => { let date = new Date('2030-12-30 12:30:00').toUTCString(); document.cookie = `CHAT2DB.LOCALE=${getLang()};Expires=${date}`; @@ -196,12 +194,12 @@ function AppContainer() { )}
- +
{serviceFail && ( <>
- {i18n('common.text.contactUs')}-github: + {i18n('common.text.contactUs')}: github @@ -220,4 +218,4 @@ function AppContainer() { )}
); -} \ No newline at end of file +} From fe056de42bb6ac5b866f92af05c520edc6c926b5 Mon Sep 17 00:00:00 2001 From: jipengfei-jpf <1558143046@qq.com> Date: Sun, 9 Jul 2023 17:06:23 +0800 Subject: [PATCH 2/2] fix exception --- .../main/resources/i18n/messages.properties | 5 ++- .../resources/i18n/messages_en_US.properties | 5 ++- .../resources/i18n/messages_zh_CN.properties | 5 ++- .../common/exception/ConnectionException.java | 25 ++++++++++++ .../server/tools/common/util/I18nUtils.java | 8 ++-- .../data/source/DataSourceController.java | 6 +++ .../request/DataSourceCreateRequest.java | 9 ++++- .../source/request/DataSourceTestRequest.java | 5 ++- .../data/source/vo/DataSourceVO.java | 4 ++ .../ai/chat2db/spi/sql/Chat2DBContext.java | 12 +++--- .../ai/chat2db/spi/sql/IDriverManager.java | 38 +++++-------------- .../java/ai/chat2db/spi/sql/SQLExecutor.java | 6 ++- 12 files changed, 85 insertions(+), 43 deletions(-) create mode 100644 chat2db-server/chat2db-server-tools/chat2db-server-tools-common/src/main/java/ai/chat2db/server/tools/common/exception/ConnectionException.java diff --git a/chat2db-server/chat2db-server-start/src/main/resources/i18n/messages.properties b/chat2db-server/chat2db-server-start/src/main/resources/i18n/messages.properties index 6725b57d..8a386da5 100644 --- a/chat2db-server/chat2db-server-start/src/main/resources/i18n/messages.properties +++ b/chat2db-server/chat2db-server-start/src/main/resources/i18n/messages.properties @@ -6,4 +6,7 @@ common.paramError=The parameter is incorrect common.paramDetailError=The parameter: {0} is incorrect common.paramCheckError=The following parameters are not valid: common.maxUploadSize=The file exceeds the maximum limit -dataSource.sqlAnalysisError=Invalid statements \ No newline at end of file +dataSource.sqlAnalysisError=Invalid statements +connection.error=Connection failed, please check the connection information +connection.ssh.error=SSH connection failed, please check the connection information +connection.driver.load.error=Failed to load driver class, please check the driver jar package \ No newline at end of file diff --git a/chat2db-server/chat2db-server-start/src/main/resources/i18n/messages_en_US.properties b/chat2db-server/chat2db-server-start/src/main/resources/i18n/messages_en_US.properties index 0dbbdd8e..c20e292b 100644 --- a/chat2db-server/chat2db-server-start/src/main/resources/i18n/messages_en_US.properties +++ b/chat2db-server/chat2db-server-start/src/main/resources/i18n/messages_en_US.properties @@ -6,4 +6,7 @@ common.paramError=The parameter is incorrect common.paramDetailError=The parameter: {0} is incorrect common.paramCheckError=The following parameters are not valid common.maxUploadSize=The file exceeds the maximum limit -dataSource.sqlAnalysisError=Invalid statements \ No newline at end of file +dataSource.sqlAnalysisError=Invalid statements +connection.error=Connection failed, please check the connection information +connection.ssh.error=SSH connection failed, please check the connection information +connection.driver.load.error=Failed to load driver class, please check the driver jar package \ No newline at end of file diff --git a/chat2db-server/chat2db-server-start/src/main/resources/i18n/messages_zh_CN.properties b/chat2db-server/chat2db-server-start/src/main/resources/i18n/messages_zh_CN.properties index d8f7314a..0c608b71 100644 --- a/chat2db-server/chat2db-server-start/src/main/resources/i18n/messages_zh_CN.properties +++ b/chat2db-server/chat2db-server-start/src/main/resources/i18n/messages_zh_CN.properties @@ -8,4 +8,7 @@ common.paramCheckError=请检查以下参数: common.maxUploadSize=您输入的文件超过最大限制 -dataSource.sqlAnalysisError=不合法的执行语句 \ No newline at end of file +dataSource.sqlAnalysisError=不合法的执行语句 +connection.error=数据库链接异常,请检查数据库配置 +connection.ssh.error=SSH 链接异常,请检查SSH配置 +connection.driver.load.error=数据库驱动加载异常,请检查驱动配置 \ No newline at end of file diff --git a/chat2db-server/chat2db-server-tools/chat2db-server-tools-common/src/main/java/ai/chat2db/server/tools/common/exception/ConnectionException.java b/chat2db-server/chat2db-server-tools/chat2db-server-tools-common/src/main/java/ai/chat2db/server/tools/common/exception/ConnectionException.java new file mode 100644 index 00000000..854f746f --- /dev/null +++ b/chat2db-server/chat2db-server-tools/chat2db-server-tools-common/src/main/java/ai/chat2db/server/tools/common/exception/ConnectionException.java @@ -0,0 +1,25 @@ +package ai.chat2db.server.tools.common.exception; + +import ai.chat2db.server.tools.base.excption.BusinessException; +import lombok.Getter; + +@Getter +public class ConnectionException extends BusinessException { + + + public ConnectionException() { + this("connection.error"); + } + + public ConnectionException(String code) { + this(code, null); + } + + public ConnectionException(String code, Object[] args) { + super(code,args); + } + + public ConnectionException(String code, Object[] args, Throwable throwable) { + super(code,args, throwable); + } +} diff --git a/chat2db-server/chat2db-server-tools/chat2db-server-tools-common/src/main/java/ai/chat2db/server/tools/common/util/I18nUtils.java b/chat2db-server/chat2db-server-tools/chat2db-server-tools-common/src/main/java/ai/chat2db/server/tools/common/util/I18nUtils.java index dc21a1de..7b99f1c7 100644 --- a/chat2db-server/chat2db-server-tools/chat2db-server-tools-common/src/main/java/ai/chat2db/server/tools/common/util/I18nUtils.java +++ b/chat2db-server/chat2db-server-tools/chat2db-server-tools-common/src/main/java/ai/chat2db/server/tools/common/util/I18nUtils.java @@ -1,5 +1,7 @@ package ai.chat2db.server.tools.common.util; +import java.util.Locale; + import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.InitializingBean; @@ -9,8 +11,6 @@ import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.lang.Nullable; import org.springframework.stereotype.Component; -import java.util.Locale; - /** * i18n utility * @@ -19,7 +19,7 @@ import java.util.Locale; @Slf4j @Component public class I18nUtils implements InitializingBean { - public static final String DEFAULT_message_Code=""; + public static final String DEFAULT_message_Code="common.systemError"; @Resource private MessageSource messageSource; private static MessageSource messageSourceStatic; @@ -34,7 +34,7 @@ public class I18nUtils implements InitializingBean { } catch (NoSuchMessageException e) { log.error("no message.", e); } - return messageSourceStatic.getMessage(messageCode, args, LocaleContextHolder.getLocale()); + return messageSourceStatic.getMessage(DEFAULT_message_Code, args, LocaleContextHolder.getLocale()); } /** diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/data/source/DataSourceController.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/data/source/DataSourceController.java index bc86b91a..d5c888aa 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/data/source/DataSourceController.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/data/source/DataSourceController.java @@ -38,6 +38,7 @@ import com.jcraft.jsch.Session; import jakarta.validation.Valid; import jakarta.validation.constraints.NotNull; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -179,6 +180,11 @@ public class DataSourceController { public DataResult queryById(@PathVariable("id") Long id) { DataResult dataResult = dataSourceService.queryById(id); DataSourceVO dataSourceVO = dataSourceWebConverter.dto2vo(dataResult.getData()); + if(StringUtils.isNotBlank(dataSourceVO.getUser())){ + dataSourceVO.setAuthenticationType("1"); + }else { + dataSourceVO.setAuthenticationType("2"); + } return DataResult.of(dataSourceVO); } diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/data/source/request/DataSourceCreateRequest.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/data/source/request/DataSourceCreateRequest.java index 62152e82..91a269ca 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/data/source/request/DataSourceCreateRequest.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/data/source/request/DataSourceCreateRequest.java @@ -42,9 +42,13 @@ public class DataSourceCreateRequest { @NotNull private String password; + /** + * 认证类型 + */ + private String authenticationType; + /** * 连接类型 - * @see DbTypeEnum */ @NotNull private String type; @@ -100,4 +104,7 @@ public class DataSourceCreateRequest { * 驱动配置 */ private DriverConfig driverConfig; + + + } diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/data/source/request/DataSourceTestRequest.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/data/source/request/DataSourceTestRequest.java index 083dbe2c..30b3966a 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/data/source/request/DataSourceTestRequest.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/data/source/request/DataSourceTestRequest.java @@ -47,7 +47,10 @@ public class DataSourceTestRequest { @NotNull private String type; - + /** + * 认证类型 + */ + private String authenticationType; /** * host diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/data/source/vo/DataSourceVO.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/data/source/vo/DataSourceVO.java index dd4bca44..15964537 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/data/source/vo/DataSourceVO.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/data/source/vo/DataSourceVO.java @@ -40,6 +40,10 @@ public class DataSourceVO { */ private String password; + /** + * 认证类型 + */ + private String authenticationType; /** * 连接类型 */ diff --git a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/Chat2DBContext.java b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/Chat2DBContext.java index bc67f6b8..962a5e75 100644 --- a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/Chat2DBContext.java +++ b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/Chat2DBContext.java @@ -8,6 +8,8 @@ import java.util.Map; import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; +import ai.chat2db.server.tools.base.excption.BusinessException; +import ai.chat2db.server.tools.common.exception.ConnectionException; import ai.chat2db.spi.DBManage; import ai.chat2db.spi.MetaData; import ai.chat2db.spi.Plugin; @@ -101,6 +103,10 @@ public class Chat2DBContext { if (session != null) { url = url.replace(host, "127.0.0.1").replace(port, ssh.getLocalPort()); } + }catch (Exception e){ + throw new ConnectionException("connection.ssh.error",null,e); + } + try { DriverConfig config = connectInfo.getDriverConfig(); if (config == null) { config = getDefaultDriverConfig(connectInfo.getDbType()); @@ -111,27 +117,23 @@ public class Chat2DBContext { connectInfo.getDriverConfig(), connectInfo.getExtendMap()); } catch (Exception e1) { - log.error("GetConnect error", e1); if (connection != null) { try { connection.close(); } catch (SQLException e) { - log.error("session close error", e); } } if (session != null) { try { session.delPortForwardingL(Integer.parseInt(ssh.getLocalPort())); } catch (JSchException e) { - log.error("session delPortForwardingL error", e); } try { session.disconnect(); } catch (Exception e) { - log.error("session disconnect error", e); } } - throw new RuntimeException("GetConnect error", e1); + throw new BusinessException("connection.error",null,e1); } connectInfo.setSession(session); connectInfo.setConnection(connection); diff --git a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/IDriverManager.java b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/IDriverManager.java index 7d126a03..e5a84958 100644 --- a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/IDriverManager.java +++ b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/IDriverManager.java @@ -1,12 +1,12 @@ package ai.chat2db.spi.sql; + import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.sql.Connection; import java.sql.Driver; -import java.sql.DriverManager; import java.sql.SQLException; import java.util.Map; import java.util.Properties; @@ -14,6 +14,7 @@ import java.util.concurrent.ConcurrentHashMap; import com.alibaba.fastjson2.JSON; +import ai.chat2db.server.tools.common.exception.ConnectionException; import ai.chat2db.spi.config.DriverConfig; import ai.chat2db.spi.model.DriverEntry; import ai.chat2db.spi.util.JdbcJarUtils; @@ -62,7 +63,13 @@ public class IDriverManager { if (StringUtils.isNotEmpty(password)) { info.put("password", password); } - info.putAll(properties); + if (properties != null && !properties.isEmpty()) { + for (Map.Entry entry : properties.entrySet()) { + if (entry.getKey() != null && entry.getValue() != null) { + info.put(entry.getKey(), entry.getValue()); + } + } + } return getConnection(url, info, driver); } @@ -91,10 +98,8 @@ public class IDriverManager { } if (reason != null) { - DriverManager.println("getConnection failed: " + reason); throw reason; } else { - DriverManager.println("getConnection: no suitable driver found for " + url); throw new SQLException("No suitable driver found for " + url, "08001"); } } @@ -123,8 +128,7 @@ public class IDriverManager { DRIVER_ENTRY_MAP.put(driver.getJdbcDriver(), driverEntry); return driverEntry; } catch (Exception e) { - log.error("getJDBCDriver error", e); - throw new SQLException("getJDBCDriver error", "08001"); + throw new ConnectionException("connection.driver.load.error", null, e); } } @@ -169,26 +173,4 @@ public class IDriverManager { } } - //private static List loadClass(String jarPath, ClassLoader classLoader) throws IOException { - // Long s1 = System.currentTimeMillis(); - // JarFile jarFile = new JarFile(getFullPath(jarPath)); - // Enumeration entries = jarFile.entries(); - // List classes = new ArrayList(); - // while (entries.hasMoreElements()) { - // JarEntry jarEntry = entries.nextElement(); - // if (jarEntry.getName().endsWith(".class") && !jarEntry.getName().contains("$")) { - // String className = jarEntry.getName().substring(0, jarEntry.getName().length() - 6).replaceAll("/", - // "."); - // try { - // classes.add(classLoader.loadClass(className)); - // // log.info("loadClass:{}", className); - // } catch (Throwable var7) { - // //log.error("getClasses error "+className, var7); - // } - // } - // } - // log.info("loadClass cost:{}", System.currentTimeMillis() - s1); - // return classes; - //} - } \ No newline at end of file diff --git a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/SQLExecutor.java b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/SQLExecutor.java index e438750a..bbea8af8 100644 --- a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/SQLExecutor.java +++ b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/SQLExecutor.java @@ -11,6 +11,7 @@ import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; +import ai.chat2db.server.tools.base.constant.EasyToolsConstant; import ai.chat2db.spi.model.*; import cn.hutool.core.date.TimeInterval; @@ -94,6 +95,7 @@ public class SQLExecutor { ExecuteResult executeResult = ExecuteResult.builder().sql(sql).success(Boolean.TRUE).build(); try (Statement stmt = connection.createStatement()) { + stmt.setFetchSize(EasyToolsConstant.MAX_PAGE_SIZE); TimeInterval timeInterval = new TimeInterval(); boolean query = stmt.execute(sql.replaceFirst(";", "")); executeResult.setDescription("执行成功"); @@ -121,7 +123,9 @@ public class SQLExecutor { List> dataList = Lists.newArrayList(); executeResult.setDataList(dataList); - while (rs.next()) { + int n = 0; + while (rs.next() && n < EasyToolsConstant.MAX_PAGE_SIZE) { + n++; List row = Lists.newArrayListWithExpectedSize(col); dataList.add(row); for (int i = 1; i <= col; i++) {