You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
8.0 KiB

9 months ago
package com.dsideal.base.Tools;
9 months ago
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;
9 months ago
public class InitDataEaseDataSet {
9 months ago
//DataEase数据库名称
public static final String DB_NAME = "dataease";
9 months ago
/**
*
*
* @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
*/
9 months ago
public static void addDataSet(String parent_name, String table_name, String dataset_name,long sort_id) {
9 months ago
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);
}
9 months ago
record.set("sort_id",sort_id);
9 months ago
Db.save("t_dp_dataset", "id", record);
9 months ago
System.out.println("添加数据集成功,parent_name=" + parent_name + ",table_name=" + table_name + ",dataset_name=" + dataset_name);
}
}
/**
*
*
* @param tableName
* @return
*/
public static List<String> getNotNullColumns(String tableName) {
List<String> columns = new ArrayList<>();
String sql = "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND IS_NULLABLE = 'NO'";
List<Record> 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<Record> 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<Record> 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<Record> tables = getExcelTable();
for (Record table : tables) {
String tableName = table.getStr("TABLE_NAME");
//获取非空列
List<String> 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("去掉不允许为空的限制成功");
}
}
9 months ago
}
}
public static void main(String[] args) {
//加载配置文件
9 months ago
String configFile = "application_dev.yaml";
9 months ago
Prop PropKit = new YamlProp(configFile);
9 months ago
HikariCpPlugin masterPlugin = new HikariCpPlugin(PropKit.get("mysql.jdbcUrl"), PropKit.get("mysql.user"),
9 months ago
PropKit.get("mysql.password").trim(), PropKit.get("mysql.driverClassName"));
9 months ago
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();
9 months ago
// 配置ActiveRecord插件
9 months ago
ActiveRecordPlugin arp = new ActiveRecordPlugin("master", masterPlugin);
9 months ago
arp.setDialect(new MysqlDialect());
9 months ago
ActiveRecordPlugin arpDataEase = new ActiveRecordPlugin(DB_NAME, dataEasePlugin);
arpDataEase.setDialect(new MysqlDialect());
9 months ago
//遍历sql目录下所有的sql文件
File sqlDir;
9 months ago
String basePath = InitDataEaseDataSet.class.getResource("/").getPath();
9 months ago
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());
9 months ago
arpDataEase.addSqlTemplate("/Sql/" + sqlFile.getName());
9 months ago
}
}
arp.start();
9 months ago
arpDataEase.start();
9 months ago
//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<Record> 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);
//将这些数据集扫描到表中,然后标识这个数据集由谁来维护
9 months ago
addDataSet(parent_name, table_name, dataset_name,id);
9 months ago
}
9 months ago
//加上主键
9 months ago
//addPrimaryKey();
9 months ago
//将所有非空列去掉不允许为空的限制
updateNotNullColumns();
9 months ago
}
}