fixed: codegen supports Schema。

This commit is contained in:
Font_C 2023-06-12 19:21:13 +08:00
parent 6f30da2821
commit 9a49c584cd
6 changed files with 65 additions and 23 deletions

View File

@ -85,8 +85,9 @@ public class Generator {
private List<Table> buildTables() throws SQLException {
StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
String schemaName = strategyConfig.getGenerateSchema();
List<Table> tables = new ArrayList<>();
try (ResultSet rs = getTablesResultSet()) {
try (ResultSet rs = getTablesResultSet(schemaName)) {
while (rs.next()) {
String tableName = rs.getString("TABLE_NAME");
if (!strategyConfig.isSupportGenerate(tableName)) {
@ -97,6 +98,7 @@ public class Generator {
table.setGlobalConfig(globalConfig);
table.setTableConfig(strategyConfig.getTableConfig(tableName));
table.setSchema(schemaName);
table.setName(tableName);
String remarks = rs.getString("REMARKS");
@ -114,11 +116,11 @@ public class Generator {
}
protected ResultSet getTablesResultSet() throws SQLException {
protected ResultSet getTablesResultSet(String schema) throws SQLException {
if (globalConfig.getStrategyConfig().isGenerateForView()) {
return dialect.getTablesResultSet(dbMeta, conn, new String[]{"TABLE", "VIEW"});
return dialect.getTablesResultSet(dbMeta, conn, schema, new String[]{"TABLE", "VIEW"});
} else {
return dialect.getTablesResultSet(dbMeta, conn, new String[]{"TABLE"});
return dialect.getTablesResultSet(dbMeta, conn, schema, new String[]{"TABLE"});
}
}
}

View File

@ -60,6 +60,10 @@ public class StrategyConfig {
*/
private Map<String, ColumnConfig> columnConfigMap;
/**
* 需要生成的表在哪个模式下
*/
private String generateSchema;
/**
* 生成哪些表白名单
*/
@ -70,6 +74,27 @@ public class StrategyConfig {
*/
private Set<String> unGenerateTables;
/**
* 设置要生成的模式
*
* @return
*/
public String getGenerateSchema() {
return generateSchema;
}
/**
* 获取要生成的模式
*
* @param generateSchema
* @return
*/
public StrategyConfig setGenerateSchema(String generateSchema) {
this.generateSchema = generateSchema;
return this;
}
/**
* 获取表配置
*/

View File

@ -17,6 +17,7 @@ package com.mybatisflex.codegen.dialect;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.core.util.StringUtil;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
@ -27,29 +28,29 @@ public interface IDialect {
IDialect DEFAULT = new JdbcDialect() {
@Override
String forBuildColumnsSql(String tableName) {
return "SELECT * FROM " + tableName + " WHERE 1 = 2";
String forBuildColumnsSql(String schema, String tableName) {
return "SELECT * FROM " + (StringUtil.isNotBlank(schema) ? schema + "." : "") + tableName + " WHERE 1 = 2";
}
};
IDialect MYSQL = new JdbcDialect() {
@Override
String forBuildColumnsSql(String tableName) {
return "SELECT * FROM `" + tableName + "` WHERE 1 = 2";
String forBuildColumnsSql(String schema, String tableName) {
return "SELECT * FROM `" + (StringUtil.isNotBlank(schema) ? schema + "`.`" : "") + tableName + "` WHERE 1 = 2";
}
};
IDialect ORACLE = new JdbcDialect() {
@Override
public String forBuildColumnsSql(String tableName) {
return "SELECT * FROM \"" + tableName + "\" WHERE rownum < 1";
public String forBuildColumnsSql(String schema, String tableName) {
return "SELECT * FROM \"" + (StringUtil.isNotBlank(schema) ? schema + "\".\"" : "") + tableName + "\" WHERE rownum < 1";
}
@Override
public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String[] types) throws SQLException {
return dbMeta.getTables(conn.getCatalog(), dbMeta.getUserName(), null, types);
public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema, String[] types) throws SQLException {
return dbMeta.getTables(conn.getCatalog(), StringUtil.isNotBlank(schema) ? schema : dbMeta.getUserName(), null, types);
}
};
@ -58,5 +59,5 @@ public interface IDialect {
void buildTableColumns(Table table, GlobalConfig globalConfig, DatabaseMetaData dbMeta, Connection conn) throws SQLException;
ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String[] types) throws SQLException;
ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema, String[] types) throws SQLException;
}

View File

@ -29,7 +29,7 @@ public abstract class JdbcDialect implements IDialect {
public void buildTableColumns(Table table, GlobalConfig globalConfig, DatabaseMetaData dbMeta, Connection conn) throws SQLException {
Map<String, String> columnRemarks = buildColumnRemarks(table, dbMeta, conn);
String sql = forBuildColumnsSql(table.getName());
String sql = forBuildColumnsSql(table.getSchema(), table.getName());
try (Statement stm = conn.createStatement(); ResultSet rs = stm.executeQuery(sql)) {
ResultSetMetaData columnMetaData = rs.getMetaData();
@ -67,10 +67,10 @@ public abstract class JdbcDialect implements IDialect {
@Override
public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String[] types) throws SQLException {
return dbMeta.getTables(conn.getCatalog(), null, null, types);
public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema, String[] types) throws SQLException {
return dbMeta.getTables(conn.getCatalog(), schema, null, types);
}
abstract String forBuildColumnsSql(String tableName);
abstract String forBuildColumnsSql(String schema, String tableName);
}

View File

@ -50,8 +50,8 @@ public class SqliteDialect implements IDialect {
@Override
public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String[] types) throws SQLException {
return dbMeta.getTables(conn.getCatalog(), null, null, types);
public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema, String[] types) throws SQLException {
return dbMeta.getTables(conn.getCatalog(), schema, null, types);
}

View File

@ -24,6 +24,7 @@ import java.util.stream.Collectors;
public class Table {
private String schema;
private String name;
private String comment;
private Set<String> primaryKeys;
@ -32,6 +33,14 @@ public class Table {
private GlobalConfig globalConfig;
private TableConfig tableConfig;
public String getSchema() {
return schema;
}
public void setSchema(String schema) {
this.schema = schema;
}
public String getName() {
return name;
}
@ -268,10 +277,14 @@ public class Table {
tableAnnotation.append("@Table(value = \"").append(name).append("\"");
if (tableConfig != null) {
if (tableConfig.getSchema() != null) {
tableAnnotation.append(", schema = \"").append(tableConfig.getSchema()).append("\"");
if (StringUtil.isNotBlank(schema)) {
tableAnnotation.append(", schema = \"").append(schema).append("\"");
}
if (tableConfig != null) {
// if (tableConfig.getSchema() != null) {
// tableAnnotation.append(", schema = \"").append(tableConfig.getSchema()).append("\"");
// }
if (tableConfig.getCamelToUnderline() != null) {
tableAnnotation.append(", camelToUnderline = \"").append(tableConfig.getCamelToUnderline()).append("\"");
}
@ -294,6 +307,7 @@ public class Table {
@Override
public String toString() {
return "Table{" +
"schema'" + schema + '\'' +
"name='" + name + '\'' +
", remarks='" + comment + '\'' +
", primaryKeys='" + primaryKeys + '\'' +