|
|
|
|
@ -4,8 +4,7 @@ import java.io.*;
|
|
|
|
|
import java.sql.*;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 数据库迁移工具
|
|
|
|
|
@ -13,6 +12,9 @@ import java.util.List;
|
|
|
|
|
*/
|
|
|
|
|
public class DatabaseMigrationTool {
|
|
|
|
|
|
|
|
|
|
// 允许操作的表白名单(防止SQL注入)
|
|
|
|
|
private static final Set<String> ALLOWED_TABLES = Set.of("books", "users", "loans");
|
|
|
|
|
|
|
|
|
|
private final DatabaseAdapter sourceAdapter;
|
|
|
|
|
private final DatabaseAdapter targetAdapter;
|
|
|
|
|
|
|
|
|
|
@ -276,6 +278,11 @@ public class DatabaseMigrationTool {
|
|
|
|
|
* @throws SQLException 查询失败时抛出
|
|
|
|
|
*/
|
|
|
|
|
private int getRecordCount(DatabaseAdapter adapter, String tableName) throws SQLException {
|
|
|
|
|
// 白名单验证表名,防止SQL注入
|
|
|
|
|
if (!ALLOWED_TABLES.contains(tableName)) {
|
|
|
|
|
throw new IllegalArgumentException("非法表名: " + tableName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String sql = "SELECT COUNT(*) as count FROM " + tableName;
|
|
|
|
|
|
|
|
|
|
try (Connection conn = adapter.getConnection();
|
|
|
|
|
@ -336,6 +343,11 @@ public class DatabaseMigrationTool {
|
|
|
|
|
private void exportTable(Connection conn, FileWriter writer, String tableName)
|
|
|
|
|
throws SQLException, IOException {
|
|
|
|
|
|
|
|
|
|
// 白名单验证表名,防止SQL注入
|
|
|
|
|
if (!ALLOWED_TABLES.contains(tableName)) {
|
|
|
|
|
throw new IllegalArgumentException("非法表名: " + tableName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writer.write("-- 表: " + tableName + "\n");
|
|
|
|
|
|
|
|
|
|
try (Statement stmt = conn.createStatement();
|
|
|
|
|
|