feat: 增加SQLServer连接

This commit is contained in:
qiufeng
2025-03-06 23:26:18 +08:00
parent c7db0905d7
commit a212f4f67f
28 changed files with 290 additions and 72 deletions
@@ -23,7 +23,7 @@ public class TableInfo implements Serializable {
private String tableName;
@Schema(description = "表描述信息")
private String tableComment;
@Schema(description = "库")
@Schema(description = "表空间")
private String tableSchema;
@Schema(description = "表类型 0表 1视图")
private Integer tableType;
@@ -39,22 +39,22 @@ public class RemoteDbConnectFallback implements FallbackFactory<RemoteDbConnectS
}
@Override
public Result<PageResult<TableInfo>> getTableList(String origin, String connectId, String tableName, ReqPage reqPage) {
public Result<PageResult<TableInfo>> getTableList(String origin, String connectId, String tableSchema, String tableName, ReqPage reqPage) {
return Result.fail("错误:查询数据库表列表出错");
}
@Override
public Result<PageResult<FieldInfo>> getFieldList(String origin, String connectId, String tableName, ReqPage reqPage) {
public Result<PageResult<FieldInfo>> getFieldList(String origin, String connectId, String tableSchema, String tableName, ReqPage reqPage) {
return Result.fail("错误:查询表字段列表出错");
}
@Override
public Result<MetaHeaderDataTable> getDataTable(String origin, String connectId, String tableName, ReqPage reqPage) {
public Result<MetaHeaderDataTable> getDataTable(String origin, String connectId, String tableSchema, String tableName, ReqPage reqPage) {
return Result.fail("错误:查询表数据失败");
}
@Override
public Result<List<MetaDataHeader>> getDataHeaders(String origin, String connectId, String tableName, ReqPage reqPage) {
public Result<List<MetaDataHeader>> getDataHeaders(String origin, String connectId, String tableSchema, String tableName, ReqPage reqPage) {
return Result.fail("错误:获取表列头失败");
}
};
@@ -35,14 +35,14 @@ public interface RemoteDbConnectService {
Result<DbConnect> queryById(@RequestHeader(RPCConstants.REQ_ORIGIN) String origin, @PathVariable("id") String id);
@GetMapping("/dbConnect/tables")
Result<PageResult<TableInfo>> getTableList(@RequestHeader(RPCConstants.REQ_ORIGIN) String origin, @RequestParam(name = "connectId") String connectId, @RequestParam(name = "tableName", required = false) String tableName, @SpringQueryMap ReqPage reqPage);
Result<PageResult<TableInfo>> getTableList(@RequestHeader(RPCConstants.REQ_ORIGIN) String origin, @RequestParam(name = "connectId") String connectId, @RequestParam(name = "tableSchema", required = false) String tableSchema, @RequestParam(name = "tableName", required = false) String tableName, @SpringQueryMap ReqPage reqPage);
@GetMapping("/dbConnect/fields")
Result<PageResult<FieldInfo>> getFieldList(@RequestHeader(RPCConstants.REQ_ORIGIN) String origin, @RequestParam(name = "connectId") String connectId, @RequestParam(name = "tableName", required = false) String tableName, @SpringQueryMap ReqPage reqPage);
Result<PageResult<FieldInfo>> getFieldList(@RequestHeader(RPCConstants.REQ_ORIGIN) String origin, @RequestParam(name = "connectId") String connectId, @RequestParam(name = "tableSchema", required = false) String tableSchema, @RequestParam(name = "tableName", required = false) String tableName, @SpringQueryMap ReqPage reqPage);
@GetMapping("/dbConnect/data")
Result<MetaHeaderDataTable> getDataTable(@RequestHeader(RPCConstants.REQ_ORIGIN) String origin, @RequestParam(name = "connectId") String connectId, @RequestParam(name = "tableName", required = false) String tableName, @SpringQueryMap ReqPage reqPage);
Result<MetaHeaderDataTable> getDataTable(@RequestHeader(RPCConstants.REQ_ORIGIN) String origin, @RequestParam(name = "connectId") String connectId, @RequestParam(name = "tableSchema", required = false) String tableSchema, @RequestParam(name = "tableName", required = false) String tableName, @SpringQueryMap ReqPage reqPage);
@GetMapping("/dbConnect/headers")
Result<List<MetaDataHeader>> getDataHeaders(@RequestHeader(RPCConstants.REQ_ORIGIN) String origin, @RequestParam(name = "connectId") String connectId, @RequestParam(name = "tableName", required = false) String tableName, @SpringQueryMap ReqPage reqPage);
Result<List<MetaDataHeader>> getDataHeaders(@RequestHeader(RPCConstants.REQ_ORIGIN) String origin, @RequestParam(name = "connectId") String connectId, @RequestParam(name = "tableSchema", required = false) String tableSchema, @RequestParam(name = "tableName", required = false) String tableName, @SpringQueryMap ReqPage reqPage);
}
@@ -68,11 +68,12 @@ public class DbConnectController {
@RequiresPermissions("sys:database:query")
@Parameters({
@Parameter(name = "connectId", description = "数据库ID", required = true),
@Parameter(name = "tableSchema", description = "表前缀"),
@Parameter(name = "tableName", description = "表名")
})
@DataScope(table = "sys_db_connect", type = DataScopeType.Tenant, excludes = "is_public=1")
public Result<PageResult<TableInfo>> getTableList(@RequestParam(name = "connectId") String connectId, @RequestParam(name = "tableName", required = false) String tableName, ReqPage reqPage) {
return Result.ok(new PageResult<>(tableService.getTableList(connectId, tableName, reqPage)), "获取表列表成功");
public Result<PageResult<TableInfo>> getTableList(@RequestParam(name = "connectId") String connectId, @RequestParam(name = "tableSchema", required = false) String tableSchema, @RequestParam(name = "tableName", required = false) String tableName, ReqPage reqPage) {
return Result.ok(new PageResult<>(tableService.getTableList(connectId, tableSchema, tableName, reqPage)), "获取表列表成功");
}
@Operation(summary = "获取数据库树形结构信息-只能分层查询")
@@ -90,10 +91,11 @@ public class DbConnectController {
}
return Result.ok(treeNodes, "查询数据库列表成功");
}
List<TableInfo> list = tableService.getTableList(parentId, null, null);
List<TableInfo> list = tableService.getTableList(parentId, null, null, null);
for (TableInfo tableInfo : list) {
String comment = StringUtils.isEmpty(tableInfo.getTableComment()) ? tableInfo.getTableName() : tableInfo.getTableName() + "[" + tableInfo.getTableComment() + "]";
treeNodes.add(new DBTreeNode().setCode(tableInfo.getTableName()).setType(1).setLabel(comment));
String name = tableInfo.getTableSchema() + "." + tableInfo.getTableName();
String comment = StringUtils.isEmpty(tableInfo.getTableComment()) ? name : name + "[" + tableInfo.getTableComment() + "]";
treeNodes.add(new DBTreeNode().setCode(name).setType(1).setLabel(comment));
}
return Result.ok(treeNodes, "查询标列表成功");
}
@@ -103,33 +105,36 @@ public class DbConnectController {
@RequiresPermissions("sys:database:query")
@Parameters({
@Parameter(name = "connectId", description = "数据库ID", required = true),
@Parameter(name = "tableSchema", description = "表前缀"),
@Parameter(name = "tableName", description = "表名")
})
@DataScope(table = "sys_db_connect", type = DataScopeType.Tenant, excludes = "is_public=1")
public Result<PageResult<FieldInfo>> getFieldList(@RequestParam(name = "connectId") String connectId, @RequestParam(name = "tableName", required = false) String tableName, ReqPage reqPage) {
return Result.ok(new PageResult<>(tableService.getFieldList(connectId, tableName, reqPage)), "获取字段列表成功");
public Result<PageResult<FieldInfo>> getFieldList(@RequestParam(name = "connectId") String connectId, @RequestParam(name = "tableSchema", required = false) String tableSchema, @RequestParam(name = "tableName", required = false) String tableName, ReqPage reqPage) {
return Result.ok(new PageResult<>(tableService.getFieldList(connectId, tableSchema, tableName, reqPage)), "获取字段列表成功");
}
@Operation(summary = "获取表数据")
@GetMapping("/data")
@Parameters({
@Parameter(name = "connectId", description = "数据库ID", required = true),
@Parameter(name = "tableSchema", description = "表前缀"),
@Parameter(name = "tableName", description = "表名")
})
@DataScope(table = "sys_db_connect", type = DataScopeType.Tenant, excludes = "is_public=1")
public Result<MetaHeaderDataTable> getDataTable(@RequestParam(name = "connectId") String connectId, @RequestParam(name = "tableName", required = false) String tableName, ReqPage reqPage) {
return tableService.getHeaderDataTable(connectId, tableName, reqPage);
public Result<MetaHeaderDataTable> getDataTable(@RequestParam(name = "connectId") String connectId, @RequestParam(name = "tableSchema", required = false) String tableSchema, @RequestParam(name = "tableName", required = false) String tableName, ReqPage reqPage) {
return tableService.getHeaderDataTable(connectId, tableSchema, tableName, reqPage);
}
@Operation(summary = "获取表列头信息")
@GetMapping("/headers")
@Parameters({
@Parameter(name = "connectId", description = "数据库ID", required = true),
@Parameter(name = "tableSchema", description = "表前缀"),
@Parameter(name = "tableName", description = "表名")
})
@DataScope(table = "sys_db_connect", type = DataScopeType.Tenant, excludes = "is_public=1")
public Result<List<MetaDataHeader>> getDataHeaders(@RequestParam(name = "connectId") String connectId, @RequestParam(name = "tableName", required = false) String tableName, ReqPage reqPage) {
List<MetaDataHeader> headers = tableService.getDataHeaders(connectId, tableName, reqPage);
public Result<List<MetaDataHeader>> getDataHeaders(@RequestParam(name = "connectId") String connectId, @RequestParam(name = "tableSchema", required = false) String tableSchema, @RequestParam(name = "tableName", required = false) String tableName, ReqPage reqPage) {
List<MetaDataHeader> headers = tableService.getDataHeaders(connectId, tableSchema, tableName, reqPage);
return Result.ok(headers, "获取表列头成功");
}
@@ -108,6 +108,12 @@ public class CodeBuildServiceImpl extends ServiceImpl<CodeBuildMapper, CodeBuild
CodeBuild codeBuild = baseMapper.selectById(id);
ReqCode reqCode = new ReqCode();
BeanUtils.copyProperties(codeBuild, reqCode);
String[] tableName = codeBuild.getTableName().split("\\.");
//如果表名包含前缀,拆分
if (tableName.length > 1) {
reqCode.setTableSchema(tableName[0]);
reqCode.setTableName(tableName[1]);
}
if (!StringUtils.isEmpty(codeBuild.getQueryParams())) {
reqCode.setSearches(JSON.parseArray(codeBuild.getQueryParams(), ReqSearch.class));
}
@@ -42,15 +42,15 @@ public class TableServiceImpl implements TableService {
private String privateKey;
@Override
public List<FieldInfo> getFieldList(String connectId, String tableName, ReqPage reqPage) {
public List<FieldInfo> getFieldList(String connectId, String tableSchema, String tableName, ReqPage reqPage) {
verifyTableName(tableName);
return queryT(connectId, FieldInfo.class, (build, dbName) -> build.getColumns(dbName, tableName), reqPage);
return queryT(connectId, FieldInfo.class, (build, dbName) -> build.getColumns(dbName, tableSchema, tableName), reqPage);
}
@Override
public TableInfo getTableInfo(String connectId, String tableName, ReqPage reqPage) {
public TableInfo getTableInfo(String connectId, String tableSchema, String tableName, ReqPage reqPage) {
verifyTableName(tableName);
List<TableInfo> list = getTableList(connectId, tableName, reqPage);
List<TableInfo> list = getTableList(connectId, tableSchema, tableName, reqPage);
if (list == null || list.isEmpty()) {
return null;
}
@@ -58,22 +58,26 @@ public class TableServiceImpl implements TableService {
}
@Override
public List<TableInfo> getTableList(String connectId, String tableName, ReqPage reqPage) {
public List<TableInfo> getTableList(String connectId, String tableSchema, String tableName, ReqPage reqPage) {
verifyTableName(tableName);
return queryT(connectId, TableInfo.class, (build, dbName) -> build.getTableInfo(dbName, tableName), reqPage);
return queryT(connectId, TableInfo.class, (build, dbName) -> build.getTableInfo(dbName, tableSchema, tableName), reqPage);
}
@Override
public Result<MetaHeaderDataTable> getHeaderDataTable(String connectId, String tableName, ReqPage reqPage) {
public Result<MetaHeaderDataTable> getHeaderDataTable(String connectId, String tableSchema, String tableName, ReqPage reqPage) {
verifyTableName(tableName);
MetaDataTable table = getDataTable(connectId, tableName, reqPage);
MetaDataTable table = getDataTable(connectId, tableSchema, tableName, reqPage);
return Result.ok(new MetaHeaderDataTable(table), "获取表数据成功");
}
@Override
public MetaDataTable getDataTable(String connectId, String tableName, ReqPage reqPage) {
public MetaDataTable getDataTable(String connectId, String tableSchema, String tableName, ReqPage reqPage) {
verifyTableName(tableName);
return query(connectId, "select * from " + tableName, reqPage);
if (StringUtils.isEmpty(tableSchema)) {
return query(connectId, "select * from " + tableName, reqPage);
}
return query(connectId, "select * from " + tableSchema + "." + tableName, reqPage);
}
private void verifyTableName(String tableName) {
@@ -86,7 +90,7 @@ public class TableServiceImpl implements TableService {
if (tableName.length() > 128) {
throw new MyRuntimeException("错误:表名称太长");
}
if (!StringUtils.isMatch("^[a-zA-Z0-9_\\.\\-]+$", tableName)) {
if (!StringUtils.isMatch("^[\\u4e00-\\u9fa5a-zA-Z0-9_\\.\\-]+$", tableName)) {
throw new MyRuntimeException("错误:表名不允许包含特殊字符");
}
}
@@ -132,8 +136,8 @@ public class TableServiceImpl implements TableService {
* @return 返回数据集列头
*/
@Override
public List<MetaDataHeader> getDataHeaders(String connectId, String tableName, ReqPage reqPage) {
List<FieldInfo> list = getFieldList(connectId, tableName, reqPage);
public List<MetaDataHeader> getDataHeaders(String connectId, String tableSchema, String tableName, ReqPage reqPage) {
List<FieldInfo> list = getFieldList(connectId, tableSchema, tableName, reqPage);
List<MetaDataHeader> headers = new ArrayList<>();
if (list == null || list.isEmpty()) {
return headers;
@@ -41,22 +41,22 @@ public class BootDbConnectService implements RemoteDbConnectService {
}
@Override
public Result<PageResult<TableInfo>> getTableList(String origin, String connectId, String tableName, ReqPage reqPage) {
return Result.ok(new PageResult<>(tableService.getTableList(connectId, tableName, reqPage)), "获取表列表成功");
public Result<PageResult<TableInfo>> getTableList(String origin, String connectId, String tableSchema, String tableName, ReqPage reqPage) {
return Result.ok(new PageResult<>(tableService.getTableList(connectId, tableSchema, tableName, reqPage)), "获取表列表成功");
}
@Override
public Result<PageResult<FieldInfo>> getFieldList(String origin, String connectId, String tableName, ReqPage reqPage) {
return Result.ok(new PageResult<>(tableService.getFieldList(connectId, tableName, reqPage)), "获取字段列表成功");
public Result<PageResult<FieldInfo>> getFieldList(String origin, String connectId, String tableSchema, String tableName, ReqPage reqPage) {
return Result.ok(new PageResult<>(tableService.getFieldList(connectId, tableSchema, tableName, reqPage)), "获取字段列表成功");
}
@Override
public Result<MetaHeaderDataTable> getDataTable(String origin, String connectId, String tableName, ReqPage reqPage) {
return tableService.getHeaderDataTable(connectId, tableName, reqPage);
public Result<MetaHeaderDataTable> getDataTable(String origin, String connectId, String tableSchema, String tableName, ReqPage reqPage) {
return tableService.getHeaderDataTable(connectId, tableSchema, tableName, reqPage);
}
@Override
public Result<List<MetaDataHeader>> getDataHeaders(String origin, String connectId, String tableName, ReqPage reqPage) {
return Result.ok(tableService.getDataHeaders(connectId, tableName, reqPage), "获取表列头成功");
public Result<List<MetaDataHeader>> getDataHeaders(String origin, String connectId, String tableSchema, String tableName, ReqPage reqPage) {
return Result.ok(tableService.getDataHeaders(connectId, tableSchema, tableName, reqPage), "获取表列头成功");
}
}
@@ -103,7 +103,7 @@ public class FreemarkerUtils {
}
if (StringUtils.isEmpty(reqCode.getTableComment())) {
reqCode.setTableComment(reqCode.getTableName());
Result<PageResult<TableInfo>> result = remoteDbConnectService.getTableList(RPCConstants.INNER, reqCode.getConnectId(), reqCode.getTableName(), new ReqPage().setPageNum(1).setPageSize(10000));
Result<PageResult<TableInfo>> result = remoteDbConnectService.getTableList(RPCConstants.INNER, reqCode.getConnectId(), reqCode.getTableSchema(), reqCode.getTableName(), new ReqPage().setPageNum(1).setPageSize(10000));
if (!result.isSuccess()) {
throw new MyRuntimeException(result.getMsg());
}
@@ -130,7 +130,7 @@ public class FreemarkerUtils {
codeInfo.setPackageName(reqCode.getPackageName());
codeInfo.setEntityName(reqCode.getEntityName());
codeInfo.setApiPrefix(reqCode.getApiPrefix());
Result<PageResult<FieldInfo>> result = remoteDbConnectService.getFieldList(RPCConstants.INNER, reqCode.getConnectId(), reqCode.getTableName(), new ReqPage().setPageNum(1).setPageSize(10000));
Result<PageResult<FieldInfo>> result = remoteDbConnectService.getFieldList(RPCConstants.INNER, reqCode.getConnectId(), reqCode.getTableSchema(), reqCode.getTableName(), new ReqPage().setPageNum(1).setPageSize(10000));
if (!result.isSuccess()) {
throw new MyRuntimeException(result.getMsg());
}
@@ -242,7 +242,7 @@ public class FreemarkerUtils {
* 参数替换
* 根据提供的映射表,对字符串中的变量进行替换
*
* @param key 待处理的原始字符串,可能包含待替换的变量
* @param key 待处理的原始字符串,可能包含待替换的变量
* @param mapValue 包含变量和其对应值的映射表
* @return 替换变量后的字符串
*/
@@ -304,7 +304,7 @@ public class FreemarkerUtils {
/**
* 保存代码到本地
*
* @param list 代码列表,包含要保存的代码信息
* @param list 代码列表,包含要保存的代码信息
* @param savePath 保存路径,用于指定代码保存的位置
* @return boolean 表示保存是否成功
*/
@@ -16,6 +16,8 @@ import java.util.List;
public class ReqCode {
@Schema(description = "数据库连接ID(必传 通过数据库列表接口查询)")
private String connectId;
@Schema(description = "表前缀(不传使用空字符串)")
private String tableSchema;
@Schema(description = "表名(必传)")
private String tableName;
@Schema(description = "表描述(不传会获取数据库表中的中文描述,如果也为空则使用表名)")
+4
View File
@@ -47,6 +47,10 @@
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@@ -34,11 +34,11 @@ public abstract class AbstractDBDialect implements DBDialect {
* 它还负责生成与数据库方案匹配的参数,考虑大小写的情况
*
* @param sql 初始SQL查询字符串该查询通常不包含具体的表名或数据库名条件
* @param dbName 数据库名称非空时,将被用作查询条件的一部分
* @param tableSchema 表前缀非空时,将被用作查询条件的一部分
* @param tableName 表名称非空时,将被用作查询条件的一部分
* @return BoundSql对象,其中包含经过条件补充的SQL查询语句及相关的参数
*/
protected BoundSql buildCondition(String sql, String dbName, String tableName) {
protected BoundSql buildCondition(String sql, String tableSchema, String tableName) {
BoundSql boundSql = new BoundSql();
boundSql.setDbType(DBType.mysql);
if (!StringUtils.isEmpty(tableName)) {
@@ -46,10 +46,10 @@ public abstract class AbstractDBDialect implements DBDialect {
boundSql.getParams().add(new QueryParam().setValue(tableName.toUpperCase()));
boundSql.getParams().add(new QueryParam().setValue(tableName.toLowerCase()));
}
if (!StringUtils.isEmpty(dbName)) {
if (!StringUtils.isEmpty(tableSchema)) {
sql += " and (table_schema = ? or table_schema = ?)";
boundSql.getParams().add(new QueryParam().setValue(dbName.toUpperCase()));
boundSql.getParams().add(new QueryParam().setValue(dbName.toLowerCase()));
boundSql.getParams().add(new QueryParam().setValue(tableSchema.toUpperCase()));
boundSql.getParams().add(new QueryParam().setValue(tableSchema.toLowerCase()));
}
return boundSql.setSql(sql);
}
@@ -22,6 +22,7 @@ public class DBAdapter {
dialectMap.put(DBType.mysql, new MysqlDBDialect());
dialectMap.put(DBType.postgre, new PostgreDBDialect());
dialectMap.put(DBType.oracle, new OracleDBDialect());
dialectMap.put(DBType.sqlserver2012, new SqlServerDBDialect());
}
/**
@@ -17,17 +17,19 @@ public interface DBDialect {
* 用于动态SQL或查询构建前的准备,确保后续操作基于正确的表结构执行
*
* @param dbName 库名,指定要查询表字段信息的数据库
* @param tableSchema 表前缀,用于筛选特定的表
* @param tableName 表名,指定要查询字段信息的具体表
* @return BoundSql 包含指定表字段信息的绑定SQL对象
*/
BoundSql getColumns(String dbName, String tableName);
BoundSql getColumns(String dbName, String tableSchema, String tableName);
/**
* 获取表信息
*
* @param dbName 库名
* @param tableSchema 表前缀
* @param tableName 表名
* @return BoundSql 对象,包含表的信息
*/
BoundSql getTableInfo(String dbName, String tableName);
BoundSql getTableInfo(String dbName, String tableSchema, String tableName);
}
@@ -1,5 +1,6 @@
package cn.com.mfish.common.dblink.db;
import cn.com.mfish.common.core.utils.StringUtils;
import cn.com.mfish.common.dblink.page.BoundSql;
/**
@@ -14,7 +15,7 @@ public class MysqlDBDialect extends AbstractDBDialect {
}
@Override
public BoundSql getColumns(String dbName, String tableName) {
public BoundSql getColumns(String dbName, String tableSchema, String tableName) {
String sql = """
select column_name field_name, data_type db_type
, if(column_key = 'PRI', true, false) is_primary
@@ -22,15 +23,18 @@ public class MysqlDBDialect extends AbstractDBDialect {
, column_type
, column_comment comment
from information_schema.columns where 1=1""";
BoundSql boundSql = buildCondition(sql, dbName, tableName);
//mysql如果没传入前缀则默认使用数据库名
String schema = StringUtils.isEmpty(tableSchema) ? dbName : tableSchema;
BoundSql boundSql = buildCondition(sql, schema, tableName);
boundSql.setSql(boundSql.getSql() + " order by ordinal_position");
return boundSql;
}
@Override
public BoundSql getTableInfo(String dbName, String tableName) {
public BoundSql getTableInfo(String dbName, String tableSchema, String tableName) {
String schema = StringUtils.isEmpty(tableSchema) ? dbName : tableSchema;
String sql = "SELECT table_name, table_comment, table_schema, if(table_type='VIEW',1,0) table_type" +
" FROM INFORMATION_SCHEMA.TABLES where 1=1";
return buildCondition(sql, dbName, tableName);
return buildCondition(sql, schema, tableName);
}
}
@@ -19,7 +19,7 @@ public class OracleDBDialect implements DBDialect {
}
@Override
public BoundSql getColumns(String dbName, String tableName) {
public BoundSql getColumns(String dbName, String tableSchema, String tableName) {
String sql = """
select a.COLUMN_NAME as "field_name",
a.DATA_TYPE as "db_type",
@@ -45,13 +45,13 @@ public class OracleDBDialect implements DBDialect {
}
@Override
public BoundSql getTableInfo(String dbName, String tableName) {
public BoundSql getTableInfo(String dbName, String tableSchema, String tableName) {
String sql = """
select u.table_name, u.comments table_comment, a.owner table_schema,DECODE(u.table_type,'VIEW', 1, 0) table_type
from user_tab_comments u
inner join all_tab_comments a
on u.table_name = a.table_name
where 1 = 1""";
where u.table_name not like 'BIN$%'""";
return buildCondition(sql, tableName);
}
@@ -1,5 +1,6 @@
package cn.com.mfish.common.dblink.db;
import cn.com.mfish.common.dblink.enums.DBType;
import cn.com.mfish.common.dblink.page.BoundSql;
/**
@@ -14,7 +15,7 @@ public class PostgreDBDialect extends AbstractDBDialect {
}
@Override
public BoundSql getColumns(String dbName, String tableName) {
public BoundSql getColumns(String dbName, String tableSchema, String tableName) {
String strSql = """
SELECT A.attname AS FIELD_NAME,
T.typname AS db_type,
@@ -31,11 +32,13 @@ public class PostgreDBDialect extends AbstractDBDialect {
LEFT JOIN pg_description d ON d.objoid = A.attrelid
AND d.objsubid = A.attnum where 1=1""";
//pg数据库table_schema是单独定义的schema不是数据库名称,此处暂时不传。参数预留
return buildCondition(strSql, "", tableName);
BoundSql boundSql = buildCondition(strSql, tableSchema, tableName);
boundSql.setDbType(DBType.postgre);
return boundSql;
}
@Override
public BoundSql getTableInfo(String dbName, String tableName) {
public BoundSql getTableInfo(String dbName, String tableSchema, String tableName) {
String strSql = """
SELECT
d.table_name,
@@ -50,6 +53,8 @@ public class PostgreDBDialect extends AbstractDBDialect {
AND ( c.relkind = 'r' OR c.relkind = 'v' )
AND c.relname = d.TABLE_NAME""";
//pg数据库table_schema是单独定义的schema不是数据库名称,此处暂时不传。参数预留
return buildCondition(strSql, "", tableName);
BoundSql boundSql = buildCondition(strSql, tableSchema, tableName);
boundSql.setDbType(DBType.postgre);
return boundSql;
}
}
@@ -0,0 +1,83 @@
package cn.com.mfish.common.dblink.db;
import cn.com.mfish.common.dblink.enums.DBType;
import cn.com.mfish.common.dblink.page.BoundSql;
/**
* @description: SQLServer数据库方言
* @author: mfish
* @date: 2025/3/4
*/
public class SqlServerDBDialect extends AbstractDBDialect {
@Override
public String getJdbc(String host, String port, String dbName) {
return "jdbc:sqlserver://" + host + ":" + port + ";databaseName=" + dbName + ";encrypt=false";
}
@Override
public BoundSql getColumns(String dbName, String tableSchema, String tableName) {
String sql = """
select * from (SELECT
t.name AS table_name,
sc.name AS table_schema,
c.name AS field_name,
s.name db_type,
CASE WHEN i.is_primary_key IS NULL THEN
0 ELSE i.is_primary_key
END AS is_primary,
c.is_nullable nullable,
concat ( s.name, '(', c.max_length, ')' ) AS column_type,
p.value AS comment
FROM
sys.columns c
JOIN sys.types s ON c.user_type_id = s.user_type_id
LEFT JOIN sys.index_columns ic ON c.object_id = ic.object_id
AND c.column_id = ic.column_id
LEFT JOIN sys.indexes i ON ic.object_id = i.object_id
AND ic.index_id = i.index_id
JOIN sys.tables t ON c.object_id = t.object_id
LEFT JOIN sys.schemas sc ON t.schema_id = sc.schema_id
LEFT JOIN sys.extended_properties p ON c.object_id = p.major_id
AND c.column_id = p.minor_id
AND p.name = 'MS_Description' ) a
WHERE 1=1""";
BoundSql boundSql = buildCondition(sql, tableSchema, tableName);
boundSql.setDbType(DBType.sqlserver2012);
return boundSql;
}
@Override
public BoundSql getTableInfo(String dbName, String tableSchema, String tableName) {
String sql = """
SELECT table_name,table_comment,table_schema,table_type FROM
(
SELECT
t.name table_name,
p.value table_comment,
s.name table_schema,
0 table_type,
t.create_date
FROM
sys.tables t
LEFT JOIN sys.extended_properties p ON t.object_id = p.major_id
AND p.name = 'MS_Description'
LEFT JOIN sys.schemas s ON t.schema_id = s.schema_id UNION
SELECT
v.name table_name,
'' table_comment,
s.name table_schema,
1 table_type,
v.create_date
FROM
sys.views v
LEFT JOIN sys.schemas s ON v.schema_id = s.schema_id
) a
where 1=1""";
BoundSql boundSql = buildCondition(sql, "", tableName);
boundSql.setSql(boundSql.getSql() + " ORDER BY a.create_date");
boundSql.setDbType(DBType.sqlserver2012);
return boundSql;
}
}
@@ -8,7 +8,8 @@ package cn.com.mfish.common.dblink.enums;
public enum DBType {
mysql(0),
postgre(1),
oracle(2);
oracle(2),
sqlserver2012(3);
private final int value;
@@ -45,6 +46,7 @@ public enum DBType {
return switch (this) {
case oracle -> "oracle.jdbc.OracleDriver";
case postgre -> "org.postgresql.Driver";
case sqlserver2012 -> "com.microsoft.sqlserver.jdbc.SQLServerDriver";
default -> "com.mysql.cj.jdbc.Driver";
};
}
@@ -27,6 +27,7 @@ public class DialectAdapter {
registerDialectAlias(DBType.mysql, MysqlDialect.class);
registerDialectAlias(DBType.oracle, OracleDialect.class);
registerDialectAlias(DBType.postgre, PostgreDialect.class);
registerDialectAlias(DBType.sqlserver2012, SqlServer2012Dialect.class);
}
private final ThreadLocal<AbstractDialect> dialectThreadLocal = new ThreadLocal<>();
@@ -69,7 +69,7 @@ public abstract class AbstractDialect implements Dialect {
public String getSimpleCountSql(final String sql, String name) {
return "SELECT COUNT(" +
name +
") FROM ( \n" +
") COUNT FROM ( \n" +
sql +
"\n ) TMP_COUNT";
}
@@ -0,0 +1,72 @@
package cn.com.mfish.common.dblink.page.dialect;
import cn.com.mfish.common.core.enums.DataType;
import cn.com.mfish.common.dblink.entity.QueryParam;
import cn.com.mfish.common.dblink.page.BoundSql;
import cn.com.mfish.common.dblink.query.BaseQuery;
import com.github.pagehelper.Page;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.OrderByElement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import java.util.List;
/**
* @description: sqlserver相关方言
* @author: mfish
* @date: 2025/3/4
*/
public class SqlServer2012Dialect extends AbstractDialect {
public SqlServer2012Dialect(BaseQuery baseQuery) {
super(baseQuery);
}
@Override
public String getSimpleCountSql(final String sql, String name) {
try {
PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(sql);
//sqlserver order不允许放在字句中
select.setOrderByElements(null);
return "SELECT COUNT(" +
name +
") COUNT FROM ( \n" +
select +
"\n ) TMP_COUNT";
} catch (JSQLParserException e) {
throw new RuntimeException(e);
}
}
@Override
protected BoundSql getPageSql(BoundSql boundSql, Page<?> page) {
try {
PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(boundSql.getSql());
List<OrderByElement> list = select.getOrderByElements();
StringBuilder sb = new StringBuilder();
if(list != null && !list.isEmpty()) {
for (OrderByElement orderByElement : list) {
sb.append(orderByElement.getExpression().toString());
if (!orderByElement.isAsc()) {
sb.append(" DESC");
}
sb.append(" ,");
}
}
if (!sb.isEmpty()) {
sb.deleteCharAt(sb.length() - 1);
} else {
sb.append("1");
}
select.setOrderByElements(null);
String sql = select +
" ORDER BY " + sb + " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
boundSql.getParams().add(new QueryParam().setValue(page.getStartRow()).setType(DataType.INTEGER));
boundSql.getParams().add(new QueryParam().setValue(page.getPageSize()).setType(DataType.INTEGER));
return boundSql.setSql(sql);
} catch (JSQLParserException e) {
throw new RuntimeException(e);
}
}
}
@@ -20,6 +20,9 @@ import lombok.extern.slf4j.Slf4j;
import oracle.sql.TIMESTAMP;
import oracle.sql.TIMESTAMPTZ;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
@@ -321,6 +324,8 @@ public class BaseQuery {
columnTypeName = columnTypeName.toUpperCase(Locale.ROOT);
if (columnTypeName.contains(DataConstant.DataType.BINARY) || columnTypeName.contains(DataConstant.DataType.BLOB)) {
value = new String((byte[]) value);
} else if (columnTypeName.contains(DataConstant.DataType.CLOB) || columnTypeName.contains(DataConstant.DataType.NCLOB)) {
value = clobToString((Clob) value);
} else if (columnTypeName.contains(DataConstant.DataType.GEOMETRY)) {
value = value.toString();
} else if (columnTypeName.contains(DataConstant.DataType.BIT)) {
@@ -338,6 +343,22 @@ public class BaseQuery {
return value;
}
private static String clobToString(Clob clob) {
StringBuilder stringBuilder = new StringBuilder();
try (Reader reader = clob.getCharacterStream();
StringWriter writer = new StringWriter()) {
char[] buffer = new char[1024];
int bytesRead;
while ((bytesRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, bytesRead);
}
stringBuilder.append(writer.toString());
} catch (SQLException | IOException e) {
log.error("nclob数据类型转换异常", e);
}
return stringBuilder.toString();
}
/**
* 获取连接
*
@@ -23,7 +23,7 @@ public interface TableService {
* @param tableName 表名
* @return 返回字段列表
*/
List<FieldInfo> getFieldList(String connectId, String tableName, ReqPage reqPage);
List<FieldInfo> getFieldList(String connectId, String tableSchema, String tableName, ReqPage reqPage);
/**
* 获取表信息
@@ -32,7 +32,7 @@ public interface TableService {
* @param tableName 表名
* @return 返回表信息
*/
TableInfo getTableInfo(String connectId, String tableName, ReqPage reqPage);
TableInfo getTableInfo(String connectId, String tableSchema, String tableName, ReqPage reqPage);
/**
* 获取表列表
@@ -41,17 +41,17 @@ public interface TableService {
* @param tableName 表名
* @return 返回表列表
*/
List<TableInfo> getTableList(String connectId, String tableName, ReqPage reqPage);
List<TableInfo> getTableList(String connectId, String tableSchema, String tableName, ReqPage reqPage);
/**
* 获取带头信息数据
*
* @param connectId 数据库连接ID
* @param tableName 表名
* @param reqPage 翻页参数
* @param reqPage 翻页参数
* @return 返回头信息数据
*/
Result<MetaHeaderDataTable> getHeaderDataTable(String connectId, String tableName, ReqPage reqPage);
Result<MetaHeaderDataTable> getHeaderDataTable(String connectId, String tableSchema, String tableName, ReqPage reqPage);
/**
* 获取数据
@@ -60,7 +60,7 @@ public interface TableService {
* @param tableName 表名
* @return 返回数据
*/
MetaDataTable getDataTable(String connectId, String tableName, ReqPage reqPage);
MetaDataTable getDataTable(String connectId, String tableSchema, String tableName, ReqPage reqPage);
/**
* 获取字段
@@ -70,5 +70,5 @@ public interface TableService {
* @param reqPage 分页
* @return 返回字段
*/
List<MetaDataHeader> getDataHeaders(String connectId, String tableName, ReqPage reqPage);
List<MetaDataHeader> getDataHeaders(String connectId, String tableSchema, String tableName, ReqPage reqPage);
}
Binary file not shown.
Binary file not shown.
+6
View File
@@ -51,6 +51,7 @@
<bcprov.version>1.79</bcprov.version>
<oracle.version>23.5.0.24.07</oracle.version>
<pgsql.version>42.7.4</pgsql.version>
<mssql.version>12.8.1.jre11</mssql.version>
<fastexcel.version>1.1.0</fastexcel.version>
</properties>
<dependencyManagement>
@@ -301,6 +302,11 @@
<artifactId>postgresql</artifactId>
<version>${pgsql.version}</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>${mssql.version}</version>
</dependency>
<dependency>
<groupId>cn.com.mfish</groupId>
<artifactId>mf-common-core</artifactId>