feat: 实现和配置 TABLE_SCHEMA_MAP 映射表名和数据库(模式)名,感谢 csx-bill 的贡献 #734

https://github.com/Tencent/APIJSON/pull/734
This commit is contained in:
TommyLemon 2024-06-02 17:15:17 +08:00 committed by GitHub
commit 76fc227b33
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -130,6 +130,11 @@ public abstract class AbstractSQLConfig<T extends Object> implements SQLConfig<T
public static Pattern PATTERN_RANGE;
public static Pattern PATTERN_FUNCTION;
/**
* SCHEMA 映射
*/
public static Map<String, String> TABLE_SCHEMA_MAP;
/**
* 表名映射隐藏真实表名对安全要求很高的表可以这么做
*/
@ -157,6 +162,19 @@ public abstract class AbstractSQLConfig<T extends Object> implements SQLConfig<T
// TODO 改成更好的正则校验前面为单词中间为操作符后面为值
PATTERN_FUNCTION = Pattern.compile("^[A-Za-z0-9%,:_@&~`!=\\<\\>\\|\\[\\]\\{\\} /\\.\\+\\-\\*\\^\\?\\(\\)\\$]+$");
TABLE_SCHEMA_MAP = new HashMap<>();
TABLE_SCHEMA_MAP.put(Table.class.getSimpleName(), DEFAULT_SCHEMA);
TABLE_SCHEMA_MAP.put(Column.class.getSimpleName(), DEFAULT_SCHEMA);
TABLE_SCHEMA_MAP.put(PgClass.class.getSimpleName(), DEFAULT_SCHEMA);
TABLE_SCHEMA_MAP.put(PgAttribute.class.getSimpleName(), DEFAULT_SCHEMA);
TABLE_SCHEMA_MAP.put(SysTable.class.getSimpleName(), DEFAULT_SCHEMA);
TABLE_SCHEMA_MAP.put(SysColumn.class.getSimpleName(), DEFAULT_SCHEMA);
TABLE_SCHEMA_MAP.put(ExtendedProperty.class.getSimpleName(), DEFAULT_SCHEMA);
TABLE_SCHEMA_MAP.put(AllTable.class.getSimpleName(), DEFAULT_SCHEMA);
TABLE_SCHEMA_MAP.put(AllColumn.class.getSimpleName(), DEFAULT_SCHEMA);
TABLE_SCHEMA_MAP.put(AllTableComment.class.getSimpleName(), DEFAULT_SCHEMA);
TABLE_SCHEMA_MAP.put(AllColumnComment.class.getSimpleName(), DEFAULT_SCHEMA);
TABLE_KEY_MAP = new HashMap<>();
TABLE_KEY_MAP.put(Table.class.getSimpleName(), Table.TABLE_NAME);
TABLE_KEY_MAP.put(Column.class.getSimpleName(), Column.TABLE_NAME);
@ -1305,7 +1323,7 @@ public abstract class AbstractSQLConfig<T extends Object> implements SQLConfig<T
@Override
public String getSQLSchema() {
String table = getTable();
//强制避免因为全局默认的 @schema 自动填充进来导致这几个类的 schema sys 等其它值
// FIXME 全部默认填充判断是 系统表 则不填充 // 强制避免因为全局默认的 @schema 自动填充进来导致这几个类的 schema sys 等其它值
if (Table.TAG.equals(table) || Column.TAG.equals(table)) {
return SCHEMA_INFORMATION; //MySQL, PostgreSQL, SQL Server 都有的
}
@ -1320,8 +1338,11 @@ public abstract class AbstractSQLConfig<T extends Object> implements SQLConfig<T
return ""; //Oracle, Dameng all_tables, dba_tables all_tab_columns, dba_columns 表好像不属于任何 Schema
}
String sch = getSchema();
return sch == null ? DEFAULT_SCHEMA : sch;
String sch = getSchema(); // 前端传参 @schema 优先
if (sch == null) {
sch = TABLE_SCHEMA_MAP.get(table); // 其次 Access alias schema 配置
}
return sch == null ? DEFAULT_SCHEMA : sch; // 最后代码默认兜底配置
}
@Override
public AbstractSQLConfig setSchema(String schema) {