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.
140 lines
5.0 KiB
140 lines
5.0 KiB
package com.dsideal.base.Test;
|
|
|
|
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 TestDataSet {
|
|
|
|
/**
|
|
* 获取数据集的表名
|
|
*
|
|
* @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 table_name 表名
|
|
* @return 表的信息
|
|
*/
|
|
public static List<Record> getTableColumns(String table_name) {
|
|
String sql = "desc `" + table_name + "`";
|
|
return Db.find(sql);
|
|
}
|
|
|
|
/**
|
|
* 获取数据表中的数据
|
|
*
|
|
* @param table_name 表名
|
|
* @return 表中的数据
|
|
*/
|
|
public static List<Record> getTableData(String table_name) {
|
|
String sql = "select * from `" + table_name+"`";
|
|
return Db.find(sql);
|
|
}
|
|
|
|
/**
|
|
* 将数据集填充到数据库表中,用于配置此数据集让谁来维护
|
|
*
|
|
* @param parent_name 数据集的父名称
|
|
* @param table_name 表名
|
|
* @param dataset_name 数据集名
|
|
*/
|
|
public static void addDataSet(String parent_name, String table_name, String dataset_name) {
|
|
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);
|
|
}
|
|
Db.save("t_dp_dataset", "id", record);
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
//加载配置文件
|
|
String configFile = "application_dataease.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();
|
|
|
|
// 配置ActiveRecord插件
|
|
ActiveRecordPlugin arp = new ActiveRecordPlugin("master", arpPlugin);
|
|
arp.setDialect(new MysqlDialect());
|
|
|
|
//遍历sql目录下所有的sql文件
|
|
File sqlDir;
|
|
String basePath = TestDataSet.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());
|
|
}
|
|
}
|
|
arp.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<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);
|
|
|
|
//将这些数据集扫描到表中,然后标识这个数据集由谁来维护
|
|
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);
|
|
}
|
|
}
|
|
}
|