完成外部数据源与代码生成关联处理

This commit is contained in:
wintel 2025-03-17 21:44:53 +08:00
parent 093be9500e
commit 13c7ec33e7
4 changed files with 50 additions and 24 deletions

View File

@ -137,7 +137,9 @@ namespace OpenAuth.App
obj.CreateUserId = user.Id;
obj.CreateUserName = user.Name;
UnitWork.Add(obj);
UnitWork.Save(); //需要先保存表结构,后面才能正常判定
_dbExtension.ProcessExternalDb(obj.Id); //先处理外部数据库连接
var columns = _dbExtension.GetDbTableStructure(obj.TableName);
if (!columns.Any())
{

View File

@ -82,17 +82,7 @@ namespace OpenAuth.App
/// </summary>
public void Sync(SyncStructureReq req)
{
var builderTable = SugarClient.Queryable<BuilderTable>().First(u => u.Id == req.BuilderTableId);
if (builderTable != null)
{
//如果代码生成器配置了外部数据库连接,则使用外部数据库连接
var connection = SugarClient.Queryable<ExternalDataSource>().First(u => u.Id == builderTable.ExternalDataSourceId);
if (connection != null)
{
var dbType = connection.Dbtype;
_dbExtension.SetConnection(connection.Connectionstring, dbType);
}
}
_dbExtension.ProcessExternalDb(req.Id); //先处理外部数据库连接
var columns = _dbExtension.GetDbTableStructure(req.TableName);
if (!columns.Any())
{

View File

@ -15,17 +15,10 @@ namespace OpenAuth.App.Request
/// </summary>
public class SyncStructureReq
{
/// <summary>
/// 列ID
/// <para>Id为空则为添加</para>
/// </summary>
public string Id { get; set; }
/// <summary>
/// 代码生成器主表ID
/// </summary>
public string BuilderTableId { get; set; }
public string Id { get; set; }
/// <summary>
/// 表英文全称

View File

@ -19,6 +19,7 @@ namespace OpenAuth.App
public class DbExtension
{
protected ISqlSugarClient SugarClient;
private string _dbTypeStr;
private IOptions<AppSetting> _appConfiguration;
private IHttpContextAccessor _httpContextAccessor;
@ -84,16 +85,20 @@ namespace OpenAuth.App
/// <returns></returns>
public IList<SysTableColumn> GetDbTableStructure(string tableName)
{
var dbtype = _appConfiguration.Value.DbTypes[_httpContextAccessor.GetTenantId()];
if (dbtype == Define.DBTYPE_MYSQL)
//如果是空,说明没有通过ProcessExternalDb设置过直接从本地读取
if(string.IsNullOrEmpty(_dbTypeStr)){
_dbTypeStr = _appConfiguration.Value.DbTypes[_httpContextAccessor.GetTenantId()];
}
if (_dbTypeStr == Define.DBTYPE_MYSQL)
{
return GetMySqlStructure(tableName);
}
else if (dbtype == Define.DBTYPE_SQLSERVER)
else if (_dbTypeStr == Define.DBTYPE_SQLSERVER)
{
return GetSqlServerStructure(tableName);
}
else if (dbtype == Define.DBTYPE_PostgreSQL)
else if (_dbTypeStr == Define.DBTYPE_PostgreSQL)
{
return GetPostgreStructure(tableName);
}
@ -409,13 +414,49 @@ namespace OpenAuth.App
return new List<SysTableColumn>();
}
/// <summary>
/// 处理外部数据库
/// </summary>
/// <param name="buildertableId">代码生成器表ID</param>
public void ProcessExternalDb(string buildertableId){
var builderTable = SugarClient.Queryable<BuilderTable>().First(u => u.Id == buildertableId);
if (builderTable != null)
{
//如果代码生成器配置了外部数据库连接,则使用外部数据库连接
var connection = SugarClient.Queryable<ExternalDataSource>().First(u => u.Id == builderTable.ExternalDataSourceId);
if (connection != null)
{
var dbType = connection.Dbtype;
SetConnection(connection.Connectionstring, dbType);
}
}
}
/// <summary>
/// 设置数据库连接
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="dbType">数据库类型</param>
public void SetConnection(string connectionString, int? dbType)
private void SetConnection(string connectionString, int? dbType)
{
switch ((SqlSugar.DbType)dbType)
{
case SqlSugar.DbType.MySql:
_dbTypeStr = Define.DBTYPE_MYSQL;
break;
case SqlSugar.DbType.SqlServer:
_dbTypeStr = Define.DBTYPE_SQLSERVER;
break;
case SqlSugar.DbType.Oracle:
_dbTypeStr = Define.DBTYPE_ORACLE;
break;
case SqlSugar.DbType.PostgreSQL:
_dbTypeStr = Define.DBTYPE_PostgreSQL;
break;
default:
_dbTypeStr = Define.DBTYPE_MYSQL;
break;
}
var config = new ConnectionConfig
{
ConnectionString = connectionString,