package com.test;

import cn.hutool.core.lang.Tuple;
import cn.hutool.core.util.StrUtil;
import java.sql.*;
import org.eclipse.collections.api.factory.Lists;

/**
 * @author lanyuanxiaoyao
 * @date 2023-07-12
 */
public class GenerateFields {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        try (Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3307/main?useSSL=false", "test", "test")) {
            for (Tuple tablePath : Lists.immutable.of(
                    new Tuple("main", "tb_app_collect_table_info", "tacti"),
                    new Tuple("main", "tb_app_hudi_sync_state", "tahss"),
                    new Tuple("main", "tb_app_flink_job_config", "tafjc"),
                    new Tuple("main", "tb_app_hudi_job_config", "tajhc"),
                    new Tuple("main", "tb_app_yarn_job_config", "tayjc_sync"),
                    new Tuple("main", "tb_app_yarn_job_config", "tayjc_compaction"),
                    new Tuple("main", "tb_app_global_config", "tagc"),
                    new Tuple("main", "tb_app_hudi_compaction_schedule", "tahcs"),
                    new Tuple("main", "data_source_table_field", "dstf"),
                    new Tuple("main", "data_source_table", "dst"),
                    new Tuple("main", "data_source", "ds")
            )) {
                String schema = tablePath.get(0);
                String table = tablePath.get(1);
                String alias = tablePath.get(2);
                try (PreparedStatement statement = connection.prepareStatement(StrUtil.format("show full columns from {}.{}", schema, table))) {
                    System.out.println(StrUtil.format("private static final Alias {}_{} = Alias.of(\"`{}`.`{}`\", \"{}\");", table.toUpperCase(), alias.toUpperCase(), schema, table, alias));
                    ResultSet resultSet = statement.executeQuery();
                    while (resultSet.next()) {
                        String name = resultSet.getString(1);
                        String type = resultSet.getString(2);
                        String comment = resultSet.getString("Comment");
                        System.out.println(StrUtil.format("/**\n  * {}.{}.{} {} ({})\n  */", schema, table, name, type, comment));
                        System.out.println(StrUtil.format("private static final String {}_{}_{} = {}_{}.getAlias() + \".`{}`\";", table.toUpperCase(), alias.toUpperCase(), name.toUpperCase(), table.toUpperCase(), alias.toUpperCase(), name));
                    }
                    System.out.println();
                    System.out.println();
                }
            }
        }
    }
}
