All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wind.matrix.DBMarkDownMain Maven / Gradle / Ivy

package com.wind.matrix;

import com.wind.matrix.bean.markdown.MarkDownDBTable;
import com.wind.matrix.bean.ShowTableColumns;
import com.wind.matrix.bean.ShowTableStructure;
import com.wind.matrix.bean.ShowTables;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;


/**
 *
 * create by lee
 */
public class DBMarkDownMain {

    /**
     * load database driverClassName
     */
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException classNotFoundException) {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException classNotFoundException1) {
                classNotFoundException1.printStackTrace();
            }
        }
    }


    /**
     * create markdown document
     */
    public static void create(String ip, int port, String username, String password, String databaseName, String outPutPath) {
        try {
            System.out.println("------ (•ᴗ•)c 开始连接数据库... ------");
            Connection conn = DriverManager.getConnection("jdbc:mysql://" + ip + ":" + port, username, password);
            run(databaseName, outPutPath, conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * create markdown document
     */
    public static void create(String url, String username, String password, String databaseName, String outPutPath) {
        try {
            System.out.println("------ (•ᴗ•)c 开始连接数据库... ------");
            Connection conn = DriverManager.getConnection(url, username, password);
            run(databaseName, outPutPath, conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * to create markdown document
     */
    private static void run(String databaseName, String outPutPath, Connection conn) {

        String markStr = "# " + databaseName + "数据库文档" + "\n";

        Statement stat = null;

        ResultSet rs = null;

        try {

            // SQL执行器
            stat = conn.createStatement();

            // 获取所有表
            ArrayList tables = new ArrayList<>();
            String showTablesSql = "select table_name from information_schema.tables where table_schema = '" + databaseName + "'";
            rs = stat.executeQuery(showTablesSql); // 执行SQL
            while (rs.next()) { // 封装结果
                String tableName = rs.getString("table_name");

                ShowTables showTables = new ShowTables();
                showTables.setTableName(tableName);
                tables.add(showTables);
            }

            // 所有表
            for (ShowTables table : tables) {

                String tableName = table.getTableName(); // 表名

                // 获取当前表 所有字段  (字段名+字段注释)
                ArrayList tableColumns = new ArrayList<>();
                String tableColumnsSql = "select column_name, column_comment from information_schema.columns where table_schema = '" + databaseName + "' and table_name = '" + tableName + "' order by ORDINAL_POSITION asc";
                rs = stat.executeQuery(tableColumnsSql); // 执行SQL
                while (rs.next()) { // 封装结果
                    String columnName = rs.getString("column_name");
                    String columnComment = rs.getString("column_comment");

                    ShowTableColumns showTableColumns = new ShowTableColumns();
                    showTableColumns.setColumnName(columnName);
                    showTableColumns.setColumnComment(columnComment);
                    tableColumns.add(showTableColumns);
                }

                // 获取当前表 表结构
                ArrayList tableStructures = new ArrayList<>();
                String tableStructureSql = "DESC " + databaseName + "." + tableName;
                rs = stat.executeQuery(tableStructureSql); // 执行SQL
                while (rs.next()) { // 封装结果
                    String Field = rs.getString("Field");
                    String Type = rs.getString("Type");
                    String Null = rs.getString("Null");
                    String Key = rs.getString("Key");
                    String Default = rs.getString("Default");
                    String Extra = rs.getString("Extra");

                    ShowTableStructure showTableStructure = new ShowTableStructure();
                    showTableStructure.setDefault(Default);
                    showTableStructure.setExtra(Extra);
                    showTableStructure.setKey(Key);
                    showTableStructure.setNull(Null);
                    showTableStructure.setType(Type);
                    showTableStructure.setField(Field);
                    tableStructures.add(showTableStructure);
                }

                // 封装MarkDown格式
                String title = "### " + tableName + "\n";
                String head = markdownHead();
                String style = markdownStyle();
                StringBuilder body = new StringBuilder();
                for (ShowTableColumns tableColumn : tableColumns) {
                    ArrayList markList = new ArrayList<>();
                    tableStructures.forEach(tableStructure -> {
                        if (tableColumn.getColumnName().equals(tableStructure.getField())) {
                            MarkDownDBTable databaseTable = new MarkDownDBTable();
                            databaseTable.setColumnComment(tableColumn.getColumnComment());
                            databaseTable.setDefault(tableStructure.getDefault());
                            databaseTable.setExtra(tableStructure.getExtra());
                            databaseTable.setField(tableStructure.getField());
                            databaseTable.setKey(tableStructure.getKey());
                            databaseTable.setNull(tableStructure.getNull());
                            databaseTable.setType(tableStructure.getType());
                            markList.add(databaseTable);
                        }
                    });

                    for (MarkDownDBTable data : markList) {
                        body.append(markdownBody(data));
                    }
                }
                markStr += title + head + style + body + "\n";
                System.out.println(markStr);

            }

            // 创建文件
            File markdownFile = new File(outPutPath + "/" + databaseName + ".md");

            // 输出文件
            writeString(markStr, markdownFile);

            System.out.println("------ (≖ᴗ≖)✧ 创建成功! ------");

        } catch (Exception e) {
            System.out.println("------ ๑乛◡乛๑ 卡在了奇怪的地方! ------");
            e.printStackTrace();
        } finally {

            try {

                if (null != rs) rs.close();
                if (null != stat) stat.close();
                if (null != conn) conn.close();

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    /**
     * markdownHead
     */
    private static String markdownHead() {
        return "|字段" + "|类型" + "|null" + "|键型" + "|注释" + "|其他" + "|\n";
    }

    /**
     * markdownStyle
     */
    private static String markdownStyle() {
        return "|-|-|-|-|-|-|-|\n";
    }

    /**
     * markdownBody
     */
    private static String markdownBody(MarkDownDBTable databaseTable) {
        return databaseTable.toString();
    }

    /**
     * write markString to file
     */
    private static void writeString(String str, File file) {
        FileWriter writer;
        try {
            writer = new FileWriter(file);
            writer.write(str);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy