main
黄海 9 months ago
parent 4f6f474952
commit 3b4a0b7d4b

@ -15,6 +15,8 @@ import java.util.ArrayList;
import java.util.List;
public class InitDataEaseDataSet {
//DataEase数据库名称
public static final String DB_NAME = "dataease";
/**
*
@ -28,27 +30,6 @@ public class InitDataEaseDataSet {
return Db.findFirst(sqlPara).getStr("table_name");
}
/**
*
*
* @param table_name
* @return
*/
public static List<Record> getTableColumns(String table_name) {
String sql = "desc dataease.`" + table_name + "`";
return Db.find(sql);
}
/**
*
*
* @param table_name
* @return
*/
public static List<Record> getTableData(String table_name) {
String sql = "select * from dataease.`" + table_name+"`";
return Db.find(sql);
}
/**
*
@ -72,6 +53,89 @@ public class InitDataEaseDataSet {
record.set("owner_id", 3);
}
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<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("去掉不允许为空的限制成功");
}
}
}
}
@ -79,14 +143,21 @@ public class InitDataEaseDataSet {
//加载配置文件
String configFile = "application_dev.yaml";
Prop PropKit = new YamlProp(configFile);
HikariCpPlugin arpPlugin = new HikariCpPlugin(PropKit.get("mysql.jdbcUrl"), PropKit.get("mysql.user"),
HikariCpPlugin masterPlugin = new HikariCpPlugin(PropKit.get("mysql.jdbcUrl"), PropKit.get("mysql.user"),
PropKit.get("mysql.password").trim(), PropKit.get("mysql.driverClassName"));
arpPlugin.start();
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", arpPlugin);
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();
@ -96,9 +167,11 @@ public class InitDataEaseDataSet {
//只加载.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");
@ -122,18 +195,10 @@ public class InitDataEaseDataSet {
//将这些数据集扫描到表中,然后标识这个数据集由谁来维护
addDataSet(parent_name, table_name, dataset_name);
//获取表有哪些列
List<Record> columns = getTableColumns(table_name);
System.out.println(parent_name + " " + dataset_name + " " + table_name);
List<String> cols = new ArrayList<>();
for (Record column : columns) {
cols.add(column.getStr("Field"));
}
System.out.println(cols);
//获取表中的数据
List<Record> data = getTableData(table_name);
System.out.println(data);
}
//加上主键
addPrimaryKey();
//将所有非空列去掉不允许为空的限制
updateNotNullColumns();
}
}

@ -1,77 +0,0 @@
package com.dsideal.base.Tools;
import com.dsideal.base.Plugin.YamlProp;
import com.jfinal.kit.Prop;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
import java.io.File;
import java.util.List;
public class addDataEaseExcelPrimary {
public static void main(String[] args) {
//加载配置文件
String configFile = "application_dev.yaml";
Prop PropKit = new YamlProp(configFile);
HikariCpPlugin arpPlugin = new HikariCpPlugin(PropKit.get("mysql.jdbcUrl"), PropKit.get("mysql.user"),
PropKit.get("mysql.password").trim(), PropKit.get("mysql.driverClassName"));
arpPlugin.start();
HikariCpPlugin dataEasePlugin = new HikariCpPlugin(PropKit.get("mysql.jdbcUrl").replace("ds_db", "dataease"), PropKit.get("mysql.user"),
PropKit.get("mysql.password").trim(), PropKit.get("mysql.driverClassName"));
dataEasePlugin.start();
// 配置ActiveRecord插件
ActiveRecordPlugin arp = new ActiveRecordPlugin("master", arpPlugin);
arp.setDialect(new MysqlDialect());
ActiveRecordPlugin arpDataEase = new ActiveRecordPlugin("dataease", dataEasePlugin);
arpDataEase.setDialect(new MysqlDialect());
//遍历sql目录下所有的sql文件
File sqlDir;
String basePath = addDataEaseExcelPrimary.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();
//处理表,增加列
// 查询所有以 excel_ 开头的表
List<Record> tables = Db.use("dataease").find("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'dataease' AND TABLE_NAME LIKE 'excel\\_%'");
for (Record table : tables) {
String tableName = table.getStr("TABLE_NAME");
if (!hasPrimaryKey(tableName)) {
addPrimaryKeyAndTimestamp(tableName);
System.out.println("Processed table: " + tableName);
}
}
}
// 检查表是否存在主键
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, "dataease", tableName) > 0;
}
// 添加主键列和时间戳列,并设置为主键
public static void addPrimaryKeyAndTimestamp(String tableName) {
// 添加 id 列
String alterTableSql = "ALTER TABLE `" + tableName + "` ADD COLUMN `id` int(11) primary key auto_increment first";
Db.use("dataease").update(alterTableSql);
}
}
Loading…
Cancel
Save