Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlserver9以上版本兼容 #343

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/main/java/com/zzg/mybatis/generator/util/DbUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,56 @@ public static Connection getConnection(DatabaseConfig config) throws ClassNotFou
return connection;
}

public static List<String> 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<String> getTableNames(DatabaseConfig config, String filter) throws Exception {
Session sshSession = getSSHSession(config);
engagePortForwarding(sshSession, config);
try (Connection connection = getConnection(config)) {
List<String> 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"));
}
} 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"));
Expand Down