001package gudusoft.gsqlparser.sqlenv;
002
003import gudusoft.gsqlparser.EDbVendor;
004import gudusoft.gsqlparser.sqlenv.IdentifierRules.CaseCompare;
005
006/**
007 * Collation 兼容视图(将旧布尔常量映射到 IdentifierProfile)
008 *
009 * <p>此类用于在不破坏向后兼容性的前提下,将配置逻辑迁移到 IdentifierProfile。
010 *
011 * @since 3.2.0 (Phase 0)
012 */
013public class CollationCompatView {
014
015    /**
016     * 计算表名的大小写敏感性(供兼容视图调用)
017     *
018     * @param vendor 数据库厂商
019     * @return true 如果表名大小写敏感
020     */
021    public static boolean computeTableCaseSensitivity(EDbVendor vendor) {
022        IdentifierProfile profile = IdentifierProfile.forVendor(
023            vendor,
024            IdentifierProfile.VendorFlags.defaults()
025        );
026        IdentifierRules rules = profile.getRules(ESQLDataObjectType.dotTable);
027        return rules.unquotedCompare == CaseCompare.SENSITIVE;
028    }
029
030    /**
031     * 计算列名的大小写敏感性
032     *
033     * @param vendor 数据库厂商
034     * @return true 如果列名大小写敏感
035     */
036    public static boolean computeColumnCaseSensitivity(EDbVendor vendor) {
037        IdentifierProfile profile = IdentifierProfile.forVendor(vendor, IdentifierProfile.VendorFlags.defaults());
038        IdentifierRules rules = profile.getRules(ESQLDataObjectType.dotColumn);
039        return rules.unquotedCompare == CaseCompare.SENSITIVE;
040    }
041
042    /**
043     * 计算函数名的大小写敏感性
044     *
045     * @param vendor 数据库厂商
046     * @return true 如果函数名大小写敏感
047     */
048    public static boolean computeFunctionCaseSensitivity(EDbVendor vendor) {
049        IdentifierProfile profile = IdentifierProfile.forVendor(vendor, IdentifierProfile.VendorFlags.defaults());
050        IdentifierRules rules = profile.getRules(ESQLDataObjectType.dotFunction);
051        return rules.unquotedCompare == CaseCompare.SENSITIVE;
052    }
053
054    /**
055     * 计算 catalog 名的大小写敏感性
056     *
057     * @param vendor 数据库厂商
058     * @return true 如果 catalog 名大小写敏感
059     */
060    public static boolean computeCatalogCaseSensitivity(EDbVendor vendor) {
061        IdentifierProfile profile = IdentifierProfile.forVendor(vendor, IdentifierProfile.VendorFlags.defaults());
062        IdentifierRules rules = profile.getRules(ESQLDataObjectType.dotCatalog);
063        return rules.unquotedCompare == CaseCompare.SENSITIVE;
064    }
065
066    /**
067     * 验证兼容视图的一致性(测试时调用)
068     *
069     * @param vendor 数据库厂商
070     * @return true 如果所有常量与 IdentifierProfile 计算值一致
071     */
072    public static boolean verifyConsistency(EDbVendor vendor) {
073        // 验证表名
074        boolean tableMatch = TSQLEnv.tableCollationCaseSensitive.get(vendor)
075            == computeTableCaseSensitivity(vendor);
076
077        // 验证列名
078        boolean columnMatch = TSQLEnv.columnCollationCaseSensitive.get(vendor)
079            == computeColumnCaseSensitivity(vendor);
080
081        // 验证函数名
082        boolean functionMatch = TSQLEnv.functionCollationCaseSensitive.get(vendor)
083            == computeFunctionCaseSensitivity(vendor);
084
085        // 验证 catalog 名
086        boolean catalogMatch = TSQLEnv.catalogCollationCaseSensitive.get(vendor)
087            == computeCatalogCaseSensitivity(vendor);
088
089        return tableMatch && columnMatch && functionMatch && catalogMatch;
090    }
091
092    /**
093     * 生成所有数据库的一致性验证报告
094     */
095    public static void generateReport() {
096        System.out.println("Collation Compatibility View Verification Report");
097        System.out.println("============================================================");
098
099        int passed = 0;
100        int failed = 0;
101
102        for (EDbVendor vendor : EDbVendor.values()) {
103            boolean result = verifyConsistency(vendor);
104            String status = result ? "✅ PASS" : "❌ FAIL";
105            System.out.printf("%-25s: %s%n", vendor.name(), status);
106
107            if (result) {
108                passed++;
109            } else {
110                failed++;
111            }
112        }
113
114        System.out.println("============================================================");
115        System.out.printf("Total: %d, Passed: %d, Failed: %d%n",
116            EDbVendor.values().length, passed, failed);
117    }
118}