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

online.sanen.unabo.sql.infomation.MySQLInfomation Maven / Gradle / Ivy

The newest version!
package online.sanen.unabo.sql.infomation;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.mhdt.Print;
import com.mhdt.toolkit.Assert;

import com.mhdt.toolkit.DateUtility;
import online.sanen.unabo.api.Bootstrap;
import online.sanen.unabo.api.structure.Column;
import online.sanen.unabo.api.structure.DataInformation;

/**
 * @author LazyToShow Date: 2018/06/12 Time: 09:17
 */
public class MySQLInfomation extends DataInformation {

    public MySQLInfomation(Bootstrap bootstrap) {
        super(bootstrap);
    }

    @Override
    public List getDatabases() {

        String sql = "SHOW DATABASES";

        return bootstrap.createSQL(sql).list();
    }

    @Override
    public boolean containsTable(String tableName) {
        List tableNames = null;

        if (tableName.contains(".")) {
            tableNames = bootstrap.createSQL(SQL_TABLE_NAMES, tableName.split("\\.")[0]).list();
            return tableNames != null && tableNames.stream().anyMatch(it -> it.toUpperCase().equals(tableName.split("\\.")[1].toUpperCase()));
        } else {
            tableNames = getTablesAndViews();
            return tableNames != null && tableNames.stream().anyMatch(it -> it.toUpperCase().equals(tableName.toUpperCase()));
        }
    }

    String SQL_TABLE_NAMES = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA =?";

    @Override
    public List getTablesAndViews() {

        return bootstrap.createSQL(SQL_TABLE_NAMES, bootstrap.manager().databaseName()).list();
    }

    @Override
    public List tables(String schema) {

        return bootstrap.createSQL(SQL_TABLE_NAMES + " AND TABLE_TYPE!='VIEW'", schema).list();
    }

    @Override
    public List> tableStatus(String schema) {

        List> maps = bootstrap.createSQL("SELECT TABLE_NAME Name,TABLE_ROWS AS 'Rows',DATA_LENGTH AS 'Data Length',ENGINE ENGINE,CREATE_TIME AS 'Create Time',UPDATE_TIME AS 'Update Time',TABLE_COLLATION AS 'Collation',TABLE_COMMENT AS COMMENT FROM " + "INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=? AND TABLE_TYPE !='VIEW'", schema).maps();

        maps.forEach(map -> {
            if (map.get("Create Time") != null && map.get("Create Time").getClass() == Timestamp.class) {
                Timestamp timestamp = (Timestamp) map.get("Create Time");
                String date = DateUtility.longToString(timestamp.getTime());
                map.put("Create Time", date);
            }


            if (map.get("Update Time") != null && map.get("Update Time").getClass() == LocalDateTime.class) {
                LocalDateTime timestamp = (LocalDateTime) map.get("Update Time");
                map.put("Update Time", timestamp.format(DateTimeFormatter.ISO_DATE));
            }
        });

        return maps;
    }

    @Override
    public List> viewStatus(String schema) {

        List> maps = bootstrap.createSQL("SELECT TABLE_NAME Name,TABLE_ROWS AS 'Rows',DATA_LENGTH AS 'Data Length',ENGINE ENGINE,CREATE_TIME AS 'Create Time',UPDATE_TIME AS 'Update Time',TABLE_COLLATION AS 'Collation',TABLE_COMMENT AS COMMENT FROM" + " INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA =? AND TABLE_TYPE='VIEW'", schema).maps();

        maps.forEach(map -> {
            if (map.get("Create Time").getClass() == Timestamp.class) {
                Timestamp timestamp = (Timestamp) map.get("Create Time");
                String date = DateUtility.longToString(timestamp.getTime());
                map.put("Create Time", date);
            }
        });

        return maps;
    }

    @Override
    public List views(String schema) {

        return bootstrap.createSQL("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME ASC", schema).list();
    }

    @Override
    public List beforeGetColumns(String tableName) {

        Assert.notNullOrEmpty(tableName, "TableName is null or empty");

        String sql = "SELECT column_name name,data_type type,character_maximum_length length,IS_NULLABLE isnullable,column_default defaultval,case when column_key=\"PRI\" THEN \"YES\" ELSE \"NO\" END ispk,column_comment comment FROM information_schema.COLUMNS WHERE TABLE_NAME=? AND TABLE_SCHEMA=?";
        List> maps = null;
        if (tableName.contains(".")) {
            String[] split = tableName.split("\\.");
            maps = bootstrap.createSQL(sql, split[1], split[0]).maps();
        } else {
            maps = bootstrap.createSQL(sql, tableName, bootstrap.manager().databaseName()).maps();
        }


        List columns = new ArrayList(maps.size());

        maps.forEach(map -> {
            Column column = new Column();
            column.setName(map.get("name").toString());
            column.setType(map.get("type").toString());
            column.setLength(map.get("length") == null ? null : map.get("length").toString());
            column.setIsnullable(map.get("isnullable").toString().toLowerCase().equals("yes"));
            column.setDefaultval(map.get("defaultval"));
            column.setIspk(map.get("ispk").toString().toLowerCase().equals("yes"));
            column.setComment(map.get("comment") == null ? null : map.get("comment").toString());
            columns.add(column);
        });

        return columns;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy