import java.sql.*;
import org.json.*;
import java.nio.file.*;

public class DQLBatchTest {
    public static void main(String[] args) {
        System.out.println("=== DQL 批量测试 ===\n");
        
        try {
            // 读取配置文件
            String configStr = new String(Files.readAllBytes(Paths.get("dqltest/dqltest-config.json")));
            JSONObject config = new JSONObject(configStr);
            JSONObject jdbc = config.getJSONObject("jdbc");
            int maxRows = config.optInt("maxRows", 3);
            
            // 建立连接
            Class.forName(jdbc.getString("driver"));
            Connection conn = DriverManager.getConnection(jdbc.getString("url"), jdbc.getString("user"), jdbc.getString("password"));
            Statement stmt = conn.createStatement();
            
            // 遍历测试用例
            JSONArray categories = config.getJSONArray("testCases");
            for (int ci = 0; ci < categories.length(); ci++) {
                JSONObject cat = categories.getJSONObject(ci);
                String catName = cat.getString("category");
                System.out.println("--- " + catName + " ---");
                JSONArray cases = cat.getJSONArray("cases");
                
                for (int i = 0; i < cases.length(); i++) {
                    String sql = cases.getString(i);
                    System.out.println("\n[" + (i+1) + "] " + sql);
                    
                    try {
                        ResultSet rs = stmt.executeQuery(sql);
                        ResultSetMetaData meta = rs.getMetaData();
                        int cols = meta.getColumnCount();
                        
                        StringBuilder header = new StringBuilder("  列: ");
                        for (int c = 1; c <= cols; c++) {
                            if (c>1) header.append(" | ");
                            header.append(meta.getColumnLabel(c));
                        }
                        System.out.println(header.toString());
                        
                        int rows = 0;
                        while (rs.next() && rows < maxRows) {
                            StringBuilder row = new StringBuilder("  行" + (rows+1) + ": ");
                            for (int c = 1; c <= cols; c++) {
                                if (c>1) row.append(" | ");
                                String val = rs.getString(c);
                                row.append(val == null ? "NULL" : val);
                            }
                            System.out.println(row.toString());
                            rows++;
                        }
                        if (rows == 0) {
                            System.out.println("  (空结果)");
                        } else {
                            System.out.println("  ...(前" + maxRows + "条)");
                        }
                        rs.close();
                    } catch (Exception e) {
                        System.out.println("  [报错] " + e.getMessage());
                    }
                }
                System.out.println();
            }
            
            conn.close();
        } catch (Exception e) {
            System.out.println("[致命报错] " + e.getMessage());
            e.printStackTrace();
        }
        System.out.println("=== 测试结束 ===");
    }
}
