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

pro.shuangxi.source.MysqlTableMapSource Maven / Gradle / Ivy

The newest version!
package pro.shuangxi.source;

import cn.hutool.db.ds.simple.SimpleDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author mengshx
 **/
public class MysqlTableMapSource implements MapSource{
    String  url;
    String  username ;
    String  password;
    String  tableName;
    DataSource dataSource;
    Map map = new HashMap<>();
    public MysqlTableMapSource(DataSource dataSource,String tableName) {
        this.dataSource = dataSource;
        this.tableName = tableName;
    }

    /**
     *
     * @param url
     * @param username
     * @param password
     * @param tableName
     */
    public MysqlTableMapSource(String url, String username, String password,String tableName) {
        this.url = url;
        this.username = username;
        this.password = password;
        this.tableName = tableName;
        SimpleDataSource simpleDataSource = new SimpleDataSource(url,username,password);
        simpleDataSource.setDriver("com.mysql.jdbc.Driver");
        dataSource = simpleDataSource;
    }

    public Map getMap(){
        try {
            Connection connection = dataSource.getConnection();
            String sql = "SELECT\n" +
                    "\tcolumn_name,\n" +
                    "\t( CASE WHEN ( is_nullable = 'no' && column_key != 'PRI' ) THEN '1' ELSE NULL END ) AS is_required,\n" +
                    "\t( CASE WHEN column_key = 'PRI' THEN '1' ELSE '0' END ) AS is_pk,\n" +
                    "\tordinal_position AS sort,\n" +
                    "\tcolumn_comment,\n" +
                    "\t( CASE WHEN extra = 'auto_increment' THEN '1' ELSE '0' END ) AS is_increment,\n" +
                    "\tcolumn_type \n" +
                    "FROM\n" +
                    "\tinformation_schema.COLUMNS \n" +
                    "WHERE\n" +
                    "\ttable_schema = ( SELECT DATABASE ( ) ) \n" +
                    "\tAND table_name = (?)\n" +
                    "\t\t\n" +
                    "ORDER BY\n" +
                    "\tordinal_position";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, tableName);
            ResultSet resultSet = statement.executeQuery();

            ArrayList> columns = new ArrayList<>();
            while (resultSet.next()) {
                HashMap column = new HashMap<>();
                column.put("column_name",resultSet.getString("column_name"));
                column.put("is_required",resultSet.getString("is_required"));
                column.put("is_pk",resultSet.getString("is_pk"));
                column.put("sort",resultSet.getString("sort"));
                column.put("column_comment",resultSet.getString("column_comment"));
                column.put("is_increment",resultSet.getString("is_increment"));
                column.put("column_type",resultSet.getString("column_type"));
                columns.add(column);
            }

            map.put("columns", columns);
            map.put("name", tableName);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return map;
    }
    public void put(String key, Object value) {
        if (map.get(key) != null) {
            System.out.println("覆盖了key:" + key);
        }
        map.put(key, value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy