top.lingkang.finalsql.ui.GenerateBuildMysql Maven / Gradle / Ivy
The newest version!
package top.lingkang.finalsql.ui;
import top.lingkang.finalsql.sql.FinalSql;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;
/**
* @author lingkang
* Created by 2022/11/8
*/
public class GenerateBuildMysql implements GenerateBuild {
// 获取所有表
private final String sql_tables = "select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA=(select database());";
private final String sql_column = "select COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT,COLUMN_KEY from information_schema.COLUMNS where TABLE_SCHEMA = (select database()) and TABLE_NAME=?;";
@Override
public void build(FinalSql finalSql, GenerateProperties properties) {
GenerateUtils.checkGenerateProperties(properties);
List tables = finalSql.selectForList(sql_tables, String.class);
if (tables.isEmpty())
return;
String pageName = GenerateUtils.getPage(properties.getOutDir());
String template = GenerateUtils.readFile(
GenerateBuildMysql.class.getClassLoader().getResourceAsStream("java-template.txt")
);
if (!GenerateUtils.isBlank(pageName)) {
pageName = "package " + pageName + ";";
}
template = template.replace("#package", pageName);
String ignoreTablePrefix = properties.getIgnoreTablePrefix();
String ignoreTables = properties.getIgnoreTable();
List ignoreTable = new ArrayList<>();
if (ignoreTables != null) {
String[] s = ignoreTables.replaceAll(" ", "").split(",");
for (String str : s) {
ignoreTable.add(str.replaceAll("\\*", "[0-9a-zA-Z_]{0,}"));
}
}
HashSet hasClassName = new HashSet<>();
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
for (String table : tables) {
if (!ignoreTable.isEmpty()) {
boolean jump = false;
for (String str : ignoreTable)
if (Pattern.matches(str, table)) {
jump = true;
break;
}
if (jump)
continue;
}
String className = GenerateUtils.handlerTableName(table, ignoreTablePrefix);
if (hasClassName.contains(className)) {// 防止相同类型名冲突
className = GenerateUtils.hasSomeName(className, hasClassName);
}
hasClassName.add(className);
String t = template;
t = t.replace("#date", date);
t = t.replace("#table", table);
t = t.replace("#className", className);
HashSet impl = new HashSet<>();
// 列
List