package com.dsideal.base.Tools; import com.dsideal.base.Plugin.YamlProp; import com.jfinal.kit.Kv; import com.jfinal.kit.Prop; import com.jfinal.plugin.activerecord.ActiveRecordPlugin; import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.SqlPara; import com.jfinal.plugin.activerecord.dialect.MysqlDialect; import com.jfinal.plugin.hikaricp.HikariCpPlugin; import com.jfinal.plugin.activerecord.Record; import java.io.File; import java.util.ArrayList; import java.util.List; public class InitDataEaseDataSet { //DataEase数据库名称 public static final String DB_NAME = "dataease"; /** * 获取数据集的表名 * * @param dataset_group_id 数据集的id * @return 表名 */ public static String getTableName(long dataset_group_id) { Kv kv = Kv.by("dataset_group_id", dataset_group_id); SqlPara sqlPara = Db.getSqlPara("DataEase.getTableName", kv); return Db.findFirst(sqlPara).getStr("table_name"); } /** * 将数据集填充到数据库表中,用于配置此数据集让谁来维护 * * @param parent_name 数据集的父名称 * @param table_name 表名 * @param dataset_name 数据集名 */ public static void addDataSet(String parent_name, String table_name, String dataset_name,long sort_id) { String sql = "select count(1) from t_dp_dataset where table_name=?"; if (Db.queryInt(sql, table_name) == 0) { Record record = new Record(); record.set("parent_name", parent_name); record.set("table_name", table_name); record.set("dataset_name", dataset_name); if (parent_name.contains("省")) { record.set("owner_id", 1); } else if (parent_name.contains("市") || parent_name.contains("州")) { record.set("owner_id", 2); } else if (parent_name.contains("县")) { record.set("owner_id", 3); } record.set("sort_id",sort_id); Db.save("t_dp_dataset", "id", record); System.out.println("添加数据集成功,parent_name=" + parent_name + ",table_name=" + table_name + ",dataset_name=" + dataset_name); } } /** * 获取表中不允许为空的列名 * * @param tableName 表名 * @return 列名列表 */ public static List getNotNullColumns(String tableName) { List columns = new ArrayList<>(); String sql = "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND IS_NULLABLE = 'NO'"; List results = Db.find(sql, DB_NAME, tableName); for (Record result : results) { columns.add(result.get("COLUMN_NAME").toString()); } return columns; } // 检查表是否存在主键 public static boolean hasPrimaryKey(String tableName) { String sql = "SELECT COUNT(*) as c FROM information_schema.TABLE_CONSTRAINTS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND CONSTRAINT_TYPE = 'PRIMARY KEY'"; return Db.use("dataease").queryInt(sql, DB_NAME, tableName) > 0; } // 添加主键列,并设置为主键 public static void addPrimaryKey(String tableName) { // 添加 id 列 String sql = "ALTER TABLE `" + tableName + "` ADD COLUMN `id` int(11) primary key auto_increment first"; Db.use(DB_NAME).update(sql); } /** * 获取所有以 excel_ 开头的表 * * @return */ public static List getExcelTable() { // 查询所有以 excel_ 开头的表 String sql = "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'dataease' AND TABLE_NAME LIKE 'excel\\_%'"; return Db.use(DB_NAME).find(sql); } /** * 添加主键 */ public static void addPrimaryKey() { // 查询所有以 excel_ 开头的表 List tables = getExcelTable(); for (Record table : tables) { String tableName = table.getStr("TABLE_NAME"); //没有主键的表,添加上主键 if (!hasPrimaryKey(tableName)) { System.out.println("表" + tableName + "没有主键,正在添加主键..."); addPrimaryKey(tableName); System.out.println("添加主键成功"); } } } /** * 将Excel表中不允许为空的列改为允许为空 */ public static void updateNotNullColumns() { // 查询所有以 excel_ 开头的表 List tables = getExcelTable(); for (Record table : tables) { String tableName = table.getStr("TABLE_NAME"); //获取非空列 List cols = getNotNullColumns(tableName); for (String col : cols) { if (!col.equals("id")) { System.out.println("列" + col + "非空,正在去掉不允许为空的限制..."); //去掉不允许为空的限制 String sql = "ALTER TABLE `" + tableName + "` MODIFY `" + col + "` VARCHAR(255) NULL"; Db.use(DB_NAME).update(sql); System.out.println("去掉不允许为空的限制成功"); } } } } public static void main(String[] args) { //加载配置文件 String configFile = "application_dev.yaml"; Prop PropKit = new YamlProp(configFile); HikariCpPlugin masterPlugin = new HikariCpPlugin(PropKit.get("mysql.jdbcUrl"), PropKit.get("mysql.user"), PropKit.get("mysql.password").trim(), PropKit.get("mysql.driverClassName")); masterPlugin.start(); HikariCpPlugin dataEasePlugin = new HikariCpPlugin(PropKit.get("mysql.jdbcUrl").replace("ds_db", DB_NAME), PropKit.get("mysql.user"), PropKit.get("mysql.password").trim(), PropKit.get("mysql.driverClassName")); dataEasePlugin.start(); // 配置ActiveRecord插件 ActiveRecordPlugin arp = new ActiveRecordPlugin("master", masterPlugin); arp.setDialect(new MysqlDialect()); ActiveRecordPlugin arpDataEase = new ActiveRecordPlugin(DB_NAME, dataEasePlugin); arpDataEase.setDialect(new MysqlDialect()); //遍历sql目录下所有的sql文件 File sqlDir; String basePath = InitDataEaseDataSet.class.getResource("/").getPath(); sqlDir = new File(basePath + "/Sql"); File[] sqlFiles = sqlDir.listFiles(); for (File sqlFile : sqlFiles != null ? sqlFiles : new File[0]) { //只加载.sql文件 if (sqlFile.getName().indexOf(".sql") > 0) { arp.addSqlTemplate("/Sql/" + sqlFile.getName()); arpDataEase.addSqlTemplate("/Sql/" + sqlFile.getName()); } } arp.start(); arpDataEase.start(); //1、获取树根 SqlPara sqlPara = Db.getSqlPara("DataEase.getTreeRoot"); Record rRoot = Db.findFirst(sqlPara); long rootId = rRoot.getLong("id"); //2、查询有哪些数据集 Kv kv = Kv.by("id", rootId); kv.set("dataset", true); sqlPara = Db.getSqlPara("DataEase.getAllDataSet", kv); List list = Db.find(sqlPara); for (Record record : list) { long id = record.getLong("id"); //数据集父名称 String parent_name = record.getStr("parent_name"); //数据集名称 String dataset_name = record.getStr("name"); //对应的表名 String table_name = getTableName(id); //将这些数据集扫描到表中,然后标识这个数据集由谁来维护 addDataSet(parent_name, table_name, dataset_name,id); } //加上主键 //addPrimaryKey(); //将所有非空列去掉不允许为空的限制 updateNotNullColumns(); } }