parent
4f6f474952
commit
3b4a0b7d4b
@ -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…
Reference in new issue