package com.th.demo.tools.generator;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Run {
    public static void main(String[] args) {
        String tableName = "t_ware_combine_record";
String dbName = "thware";


        Connection conn = GetConnection.getConnection();
        QueryRunner queryRunner = new QueryRunner();
        String sql = " select COLUMN_NAME from information_schema.COLUMNS where table_name = '"+tableName+"' and table_schema = '"+dbName+"'";
        System.out.println(sql);
        try {
            List<Map<String, Object>> list = queryRunner.query(conn, sql, new MapListHandler());
            System.out.println("{");
            for (Map<String, Object> map : list) {
                for (String key : map.keySet()) {
                    System.out.println("        \""+ field2porp((String)map.get(key))+"\":\"\",");
                }
            }
            System.out.println("}");
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }


    public static String field2porp(String field){
        String b = "", c = "";
        String temp = "";
        String result = "";

        if (field.contains("_")) {
            result = "";
            String[] tableArr =field.split("_");
            for (int i = 0; i < tableArr.length; i++) {
                b = "";
                c = "";
                temp = tableArr[i];
                if (i == 0) {
                    result += temp.toLowerCase();
                } else {
                    b = temp.substring(0, 1);
                    c = temp.substring(1, temp.length());
                    c = c.toLowerCase();
                    result += b + c;
                }
            }
        } else {
            result = field.toLowerCase();
        }

        return  result;

    }
}
