From ddc1e928f74462f652eda58eca0b79a548b1a460 Mon Sep 17 00:00:00 2001 From: lyon <474857849@qq.com> Date: Tue, 9 Jul 2024 17:12:48 +0900 Subject: [PATCH] =?UTF-8?q?sqlserver9=E4=BB=A5=E4=B8=8A=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sqlserver9以上版本兼容 --- .../zzg/mybatis/generator/util/DbUtil.java | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/zzg/mybatis/generator/util/DbUtil.java b/src/main/java/com/zzg/mybatis/generator/util/DbUtil.java index 23f4b448..d928cbb3 100644 --- a/src/main/java/com/zzg/mybatis/generator/util/DbUtil.java +++ b/src/main/java/com/zzg/mybatis/generator/util/DbUtil.java @@ -140,15 +140,48 @@ public static Connection getConnection(DatabaseConfig config) throws ClassNotFou return connection; } - public static List getTableNames(DatabaseConfig config, String filter) throws Exception { + public static boolean isOldVersion(Connection connection) throws SQLException { + try (Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT @@VERSION")) { + + if (rs.next()) { + String versionString = rs.getString(1); + // 提取版本号 + int majorVersion = extractMajorVersion(versionString); + + // 假设我们将SQL Server 2005(版本9)及以下视为"旧版本" + return majorVersion <= 9; + } + } + return false; // 如果无法获取版本信息,默认不是旧版本 + } + + private static int extractMajorVersion(String versionString) { + // 版本字符串通常以 "Microsoft SQL Server XXXX" 开头 + String[] parts = versionString.split(" "); + for (String part : parts) { + if (part.matches("\\d+\\.\\d+\\.\\d+\\.\\d+")) { + return Integer.parseInt(part.split("\\.")[0]); + } + } + return 0; // 无法解析版本号 + } + + public static List getTableNames(DatabaseConfig config, String filter) throws Exception { Session sshSession = getSSHSession(config); engagePortForwarding(sshSession, config); try (Connection connection = getConnection(config)) { List tables = new ArrayList<>(); DatabaseMetaData md = connection.getMetaData(); ResultSet rs; + String sql; if (DbType.valueOf(config.getDbType()) == DbType.SQL_Server) { - String sql = "select name from sysobjects where xtype='u' or xtype='v' order by name"; + boolean oldVersion = isOldVersion(connection); + if (oldVersion){ + sql = "select name from sysobjects where xtype='u' or xtype='v' order by name"; + }else { + sql = "SELECT name FROM sys.tables order by name"; + } rs = connection.createStatement().executeQuery(sql); while (rs.next()) { tables.add(rs.getString("name")); @@ -156,7 +189,7 @@ public static List getTableNames(DatabaseConfig config, String filter) t } else if (DbType.valueOf(config.getDbType()) == DbType.Oracle) { rs = md.getTables(null, config.getUsername().toUpperCase(), null, new String[]{"TABLE", "VIEW"}); } else if (DbType.valueOf(config.getDbType()) == DbType.Sqlite) { - String sql = "Select name from sqlite_master;"; + sql = "Select name from sqlite_master;"; rs = connection.createStatement().executeQuery(sql); while (rs.next()) { tables.add(rs.getString("name"));