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

cn.cliveyuan.robin.generator.db.MysqlTableIntrospect Maven / Gradle / Ivy

package cn.cliveyuan.robin.generator.db;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * MySQL 表信息探测器
 *
 * @author Clive Yuan
 * @date 2020/10/28
 */
@Slf4j
public class MysqlTableIntrospect implements TableIntrospect {

    private final Connection connection;

    public MysqlTableIntrospect(Connection connection) {
        this.connection = connection;
    }

    @Override
    public TableInfo introspect(String tableName) {
        try {
            TableInfo tableInfo = new TableInfo();
            tableInfo.setName(tableName);
            DatabaseMetaData metaData = connection.getMetaData();
            // introspect table info
            try (ResultSet tableRs = metaData.getTables(connection.getCatalog(), null, tableName, null)) {
                if (tableRs.next()) {
                    String remarks = tableRs.getString("REMARKS");
                    if (StringUtils.isBlank(remarks)) {
                        remarks = tableName;
                    }
                    tableInfo.setComment(remarks);
                } else {
                    return null;
                }
            }
            List columns = new ArrayList();
            tableInfo.setColumns(columns);

            // introspect column info
            try (ResultSet columnRs = metaData.getColumns(connection.getCatalog(), null, tableName, null)) {
                while (columnRs.next()) {
                    String columnName = columnRs.getString("COLUMN_NAME");
                    String typeName = columnRs.getString("TYPE_NAME");
                    int columnSize = columnRs.getInt("COLUMN_SIZE");
                    int nullable = columnRs.getInt("NULLABLE");
                    String comment = columnRs.getString("REMARKS");
                    ColumnInfo column = new ColumnInfo();
                    column.setName(columnName);
                    column.setType(typeName);
                    column.setLength(columnSize);
                    column.setNullable(nullable == 1);
                    column.setComment(StringUtils.isNotBlank(comment) ? comment : columnName);
                    columns.add(column);
                }
            }
            return tableInfo;
        } catch (SQLException e) {
            log.error("introspect", e);
            throw new RuntimeException("Fail to introspect table: " + tableName);
        }
    }

    @Override
    public List getAllTables() {
        List list = new ArrayList<>();
        try {
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet tableRs = metaData.getTables(connection.getCatalog(), null, null, null);
            while (tableRs.next()) {
                list.add(tableRs.getString("TABLE_NAME"));
            }
        } catch (SQLException e) {
            log.error("introspect", e);
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public void closeConnection() {
        if (Objects.nonNull(connection)) {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy