mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-11-10 03:14:45 +08:00
feat: 增加根据表和数据源获取表结构
This commit is contained in:
@@ -85,6 +85,11 @@ namespace OpenAuth.App
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public IList<SysTableColumn> GetDbTableStructure(string tableName)
|
public IList<SysTableColumn> GetDbTableStructure(string tableName)
|
||||||
{
|
{
|
||||||
|
if(string.IsNullOrEmpty(tableName))
|
||||||
|
{
|
||||||
|
return new List<SysTableColumn>();
|
||||||
|
}
|
||||||
|
|
||||||
//如果是空,说明没有通过ProcessExternalDb设置过,直接从本地读取
|
//如果是空,说明没有通过ProcessExternalDb设置过,直接从本地读取
|
||||||
if(string.IsNullOrEmpty(_dbTypeStr)){
|
if(string.IsNullOrEmpty(_dbTypeStr)){
|
||||||
_dbTypeStr = _appConfiguration.Value.DbTypes[_httpContextAccessor.GetTenantId()];
|
_dbTypeStr = _appConfiguration.Value.DbTypes[_httpContextAccessor.GetTenantId()];
|
||||||
@@ -414,6 +419,22 @@ namespace OpenAuth.App
|
|||||||
return new List<SysTableColumn>();
|
return new List<SysTableColumn>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 通过表名和外部数据源ID获取表结构
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="tableName">表名</param>
|
||||||
|
/// <param name="externalDataSourceId">外部数据源ID</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public IList<SysTableColumn> GetDbTableStructure(string tableName, string externalDataSourceId){
|
||||||
|
var connection = SugarClient.Queryable<ExternalDataSource>().First(u => u.Id == externalDataSourceId);
|
||||||
|
if (connection != null)
|
||||||
|
{
|
||||||
|
var dbType = connection.Dbtype;
|
||||||
|
SetConnection(connection.Connectionstring, dbType);
|
||||||
|
}
|
||||||
|
return GetDbTableStructure(tableName);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 处理外部数据库
|
/// 处理外部数据库
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
using Infrastructure;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Infrastructure;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using OpenAuth.App;
|
using OpenAuth.App;
|
||||||
|
using OpenAuth.App.Response;
|
||||||
|
using OpenAuth.Repository.QueryObj;
|
||||||
|
|
||||||
namespace OpenAuth.WebApi.Controllers
|
namespace OpenAuth.WebApi.Controllers
|
||||||
{
|
{
|
||||||
@@ -12,13 +17,15 @@ namespace OpenAuth.WebApi.Controllers
|
|||||||
[Route("api/[controller]/[action]")]
|
[Route("api/[controller]/[action]")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[ApiExplorerSettings(GroupName = "系统配置_SysConf")]
|
[ApiExplorerSettings(GroupName = "系统配置_SysConf")]
|
||||||
public class SysConfController :ControllerBase
|
public class SysConfController : ControllerBase
|
||||||
{
|
{
|
||||||
private IOptions<AppSetting> _appConfiguration;
|
private IOptions<AppSetting> _appConfiguration;
|
||||||
|
private DbExtension _dbExtension;
|
||||||
|
|
||||||
public SysConfController(IOptions<AppSetting> appConfiguration)
|
public SysConfController(IOptions<AppSetting> appConfiguration, DbExtension dbExtension)
|
||||||
{
|
{
|
||||||
_appConfiguration = appConfiguration;
|
_appConfiguration = appConfiguration;
|
||||||
|
_dbExtension = dbExtension;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -34,5 +41,53 @@ namespace OpenAuth.WebApi.Controllers
|
|||||||
Result = _appConfiguration.Value.IsIdentityAuth
|
Result = _appConfiguration.Value.IsIdentityAuth
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取数据库表结构
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="tableName">表名</param>
|
||||||
|
/// <param name="externalDataSourceId">外部数据源ID</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet]
|
||||||
|
public Response<List<SysTableColumn>> GetDbTableStructure(string tableName, string externalDataSourceId)
|
||||||
|
{
|
||||||
|
var result = new Response<List<SysTableColumn>>();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
result.Result = _dbExtension.GetDbTableStructure(tableName, externalDataSourceId).ToList();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
result.Code = 500;
|
||||||
|
result.Message = ex.Message;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取数据库表结构(用于下拉框)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="tableName">表名</param>
|
||||||
|
/// <param name="externalDataSourceId">外部数据源ID</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet]
|
||||||
|
public TableData GetDbTableStructureForSelect(string tableName, string externalDataSourceId)
|
||||||
|
{
|
||||||
|
var result = new TableData();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
result.data = _dbExtension.GetDbTableStructure(tableName, externalDataSourceId).Select(u => new
|
||||||
|
{
|
||||||
|
Id = u.ColumnName,
|
||||||
|
Name = u.ColumnName
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
result.code = 500;
|
||||||
|
result.msg = ex.Message;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ create table stock
|
|||||||
|
|
||||||
选中刚刚添加的`Stock`表,依次点击【生成实体】【生成业务代码】【生成vue页面】;
|
选中刚刚添加的`Stock`表,依次点击【生成实体】【生成业务代码】【生成vue页面】;
|
||||||
|
|
||||||
如果存在子表,也进行相同的操作。即选中刚刚添加的`StockDetail`表,依次点击【生成实体】【生成业务代码】,子表不需要生成vue页面;
|
如果存在子表,也进行相同的操作。即选中刚刚添加的`StockDetail`表,依次点击【生成实体】【生成业务代码】【生成vue页面】,子表虽然不需要生成vue页面,但需要生成前端js api;
|
||||||
|
|
||||||
成功后生成的后端.Net代码位置如下:
|
成功后生成的后端.Net代码位置如下:
|
||||||
|
|
||||||
@@ -139,11 +139,13 @@ OpenAuth.App\StockApp\Request\AddOrUpdateStockReq.cs
|
|||||||
OpenAuth.App\StockApp\Request\QueryStockListReq.cs
|
OpenAuth.App\StockApp\Request\QueryStockListReq.cs
|
||||||
OpenAuth.WebApi\Controllers\StocksController.cs
|
OpenAuth.WebApi\Controllers\StocksController.cs
|
||||||
|
|
||||||
并且会在OpenAuth.Repository\OpenAuthDBContext.cs中自动添加:
|
::: warning 注意
|
||||||
|
以前代码生成器生成基于EF的代码时,会在OpenAuth.Repository\OpenAuthDBContext.cs中自动添加:
|
||||||
```
|
```
|
||||||
public virtual DbSet<Stock> Stocks { get; set; }
|
public virtual DbSet<Stock> Stocks { get; set; }
|
||||||
```
|
```
|
||||||
|
现在基于SqlSugar的代码生成器,已不需要。如果有其他开发方面需要,可自行手动添加。
|
||||||
|
:::
|
||||||
|
|
||||||
|
|
||||||
前端Vue代码:
|
前端Vue代码:
|
||||||
|
|||||||
Reference in New Issue
Block a user