From d83a40e07ed4daaca8da560df51664b448eeb434 Mon Sep 17 00:00:00 2001 From: yubaolee Date: Sun, 6 Jul 2025 23:40:10 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A0=EF=B8=8Ffeat:=20=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=AE=BF=E9=97=AE=E5=A4=9A=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenAuth.App/Test/TestBase.cs | 90 ++++++++++++++++++---------- OpenAuth.Repository/Test/TestBase.cs | 83 ++++++++++++++++--------- 数据库脚本/Oracle脚本.sql | 4 +- 数据库脚本/PostgreSQL脚本.sql | 4 +- 数据库脚本/SqlServer脚本.sql | 4 +- 数据库脚本/mysql脚本.sql | 4 +- 6 files changed, 122 insertions(+), 67 deletions(-) diff --git a/OpenAuth.App/Test/TestBase.cs b/OpenAuth.App/Test/TestBase.cs index ac3902d9..9d949e24 100644 --- a/OpenAuth.App/Test/TestBase.cs +++ b/OpenAuth.App/Test/TestBase.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using System.Linq; using Autofac.Extensions.DependencyInjection; @@ -60,44 +61,71 @@ namespace OpenAuth.App.Test serviceCollection.AddDbContext(); - var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren() - .ToDictionary(x => x.Key, x => x.Value); - var connectionString = config.GetSection("ConnectionStrings")["OpenAuthDBContext"]; - - Console.WriteLine($"单元测试数据库信息:{dbtypes[httpContextAccessorMock.Object.GetTenantId()]}/{connectionString}"); - + var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren() + .ToDictionary(x => x.Key, x => x.Value);; var sqlsugarTypes = UtilMethods.EnumToDictionary(); - var dbType = sqlsugarTypes.FirstOrDefault(it => - dbtypes.ToDictionary(u => u.Key, v => v.Value.ToLower()).ContainsValue(it.Key)); - serviceCollection.AddScoped(s => { - var sqlSugar = new SqlSugarClient(new ConnectionConfig() + // 获取所有连接字符串配置 + var connectionStrings = config.GetSection("ConnectionStrings").GetChildren() + .ToDictionary(x => x.Key, x => x.Value); + + // 准备ConnectionConfig列表 + var connectionConfigs = new List(); + + // 遍历所有连接字符串 + foreach (var conn in connectionStrings) { - DbType = dbType.Value, - ConnectionString = connectionString, - IsAutoCloseConnection = true - }); - - if(dbType.Value != SqlSugar.DbType.PostgreSQL){ - return sqlSugar; - } - // 配置bool类型转换为smallint - sqlSugar.Aop.OnExecutingChangeSql = (sql, parameters) => - { - foreach (var param in parameters) + // 获取对应的数据库类型 + var connDbType = dbtypes.ContainsKey(conn.Key) ? + sqlsugarTypes.FirstOrDefault(it => dbtypes[conn.Key].ToLower().Contains(it.Key)).Value : + DbType.SqlServer; // 如果没有定义DbType,使用默认类型 + + // 创建连接配置 + var config = new ConnectionConfig { - if (param.Value is bool boolValue) - { - param.DbType = System.Data.DbType.Int16; - // 将 bool 转换为 smallint - param.Value = boolValue ? (short)1 : (short)0; - } + DbType = connDbType, + ConnectionString = conn.Value, + IsAutoCloseConnection = true, + }; + + // 如果不是默认连接,设置ConfigId + if (conn.Key != Define.DEFAULT_TENANT_ID) + { + config.ConfigId = conn.Key; } - // 返回修改后的 SQL 和参数 - return new System.Collections.Generic.KeyValuePair(sql, parameters); - }; + + connectionConfigs.Add(config); + Console.WriteLine($"添加数据库连接: {conn.Key} / {(dbtypes.ContainsKey(conn.Key) ? dbtypes[conn.Key] : "未指定类型")},连接字符串:{conn.Value}"); + } + + var sqlSugar = new SqlSugarClient(connectionConfigs); + + // 配置PostgreSQL数据库处理 + foreach (var connConfig in connectionConfigs) + { + if(connConfig.DbType == SqlSugar.DbType.PostgreSQL) + { + // 配置bool类型转换为smallint + sqlSugar.Aop.OnExecutingChangeSql = (sql, parameters) => + { + foreach (var param in parameters) + { + if (param.Value is bool boolValue) + { + param.DbType = System.Data.DbType.Int16; + // 将 bool 转换为 smallint + param.Value = boolValue ? (short)1 : (short)0; + } + } + // 返回修改后的 SQL 和参数 + return new System.Collections.Generic.KeyValuePair(sql, parameters); + }; + break; // 找到一个PostgreSQL连接后就设置一次即可 + } + } + return sqlSugar; }); diff --git a/OpenAuth.Repository/Test/TestBase.cs b/OpenAuth.Repository/Test/TestBase.cs index 714c6813..f4a70c51 100644 --- a/OpenAuth.Repository/Test/TestBase.cs +++ b/OpenAuth.Repository/Test/TestBase.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; @@ -67,44 +68,70 @@ namespace OpenAuth.Repository.Test serviceCollection.AddDbContext(); - var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren() - .ToDictionary(x => x.Key, x => x.Value); - - var connectionString = config.GetSection("ConnectionStrings")["OpenAuthDBContext"]; - Console.WriteLine($"单元测试数据库信息:{dbtypes[httpContextAccessorMock.Object.GetTenantId()]}/{connectionString}"); - + var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren() + .ToDictionary(x => x.Key, x => x.Value);; var sqlsugarTypes = UtilMethods.EnumToDictionary(); - var dbType = sqlsugarTypes.FirstOrDefault(it => - dbtypes.ToDictionary(u => u.Key, v => v.Value.ToLower()).ContainsValue(it.Key)); - serviceCollection.AddScoped(s => { - var sqlSugar = new SqlSugarClient(new ConnectionConfig() + // 获取所有连接字符串配置 + var connectionStrings = config.GetSection("ConnectionStrings").GetChildren() + .ToDictionary(x => x.Key, x => x.Value); + + // 准备ConnectionConfig列表 + var connectionConfigs = new List(); + + // 遍历所有连接字符串 + foreach (var conn in connectionStrings) { - DbType = dbType.Value, - ConnectionString = connectionString, - IsAutoCloseConnection = true - }); - - if(dbType.Value != SqlSugar.DbType.PostgreSQL){ - return sqlSugar; + // 获取对应的数据库类型 + var connDbType = dbtypes.ContainsKey(conn.Key) ? + sqlsugarTypes.FirstOrDefault(it => dbtypes[conn.Key].ToLower().Contains(it.Key)).Value : + DbType.SqlServer; // 如果没有定义DbType,使用默认类型 + + // 创建连接配置 + var config = new ConnectionConfig + { + DbType = connDbType, + ConnectionString = conn.Value, + IsAutoCloseConnection = true, + }; + + // 如果不是默认连接,设置ConfigId + if (conn.Key != Define.DEFAULT_TENANT_ID) + { + config.ConfigId = conn.Key; + } + + connectionConfigs.Add(config); + Console.WriteLine($"添加数据库连接: {conn.Key} / {(dbtypes.ContainsKey(conn.Key) ? dbtypes[conn.Key] : "未指定类型")},连接字符串:{conn.Value}"); } - // 配置bool类型转换为smallint - sqlSugar.Aop.OnExecutingChangeSql = (sql, parameters) => + var sqlSugar = new SqlSugarClient(connectionConfigs); + + // 配置PostgreSQL数据库处理 + foreach (var connConfig in connectionConfigs) { - foreach (var param in parameters) + if(connConfig.DbType == SqlSugar.DbType.PostgreSQL) { - if (param.Value is bool boolValue) + // 配置bool类型转换为smallint + sqlSugar.Aop.OnExecutingChangeSql = (sql, parameters) => { - param.DbType = System.Data.DbType.Int16; - // 将 bool 转换为 smallint - param.Value = boolValue ? (short)1 : (short)0; - } + foreach (var param in parameters) + { + if (param.Value is bool boolValue) + { + param.DbType = System.Data.DbType.Int16; + // 将 bool 转换为 smallint + param.Value = boolValue ? (short)1 : (short)0; + } + } + // 返回修改后的 SQL 和参数 + return new System.Collections.Generic.KeyValuePair(sql, parameters); + }; + break; // 找到一个PostgreSQL连接后就设置一次即可 } - // 返回修改后的 SQL 和参数 - return new System.Collections.Generic.KeyValuePair(sql, parameters); - }; + } + return sqlSugar; }); diff --git a/数据库脚本/Oracle脚本.sql b/数据库脚本/Oracle脚本.sql index e32a34d6..eca38212 100644 --- a/数据库脚本/Oracle脚本.sql +++ b/数据库脚本/Oracle脚本.sql @@ -578,8 +578,8 @@ COMMENT ON TABLE "EXTERNALDATASOURCE" IS '外部数据源管理'; -- ---------------------------- -- Records of EXTERNALDATASOURCE -- ---------------------------- -INSERT INTO "EXTERNALDATASOURCE" VALUES ('94279d93-cef3-46e0-83af-3b7dc48764fe', '本地SqlServer', '1', 'Data Source=localhost;Encrypt=false;Initial Catalog=openauthpro;User=sa;Password=000000;', 'localhost', NULL, 'openauthpro', 'sa', '000000', '1', TO_DATE('2025-03-15 20:09:35', 'SYYYY-MM-DD HH24:MI:SS'), TO_DATE('2025-03-18 11:42:08', 'SYYYY-MM-DD HH24:MI:SS'), '0', NULL, '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员'); -INSERT INTO "EXTERNALDATASOURCE" VALUES ('94279d93-cef3-46e0-83af-3b7dc48764ff', '本地mysql', '0', 'Server=127.0.0.1;Database=openauthpro;User=root;Password=000000;', '127.0.0.1', '3306', 'openauthpro', 'root', '000000', '1', TO_DATE('2025-03-15 00:02:11', 'SYYYY-MM-DD HH24:MI:SS'), TO_DATE('2025-03-15 20:12:30', 'SYYYY-MM-DD HH24:MI:SS'), '0', NULL, '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员'); +INSERT INTO "EXTERNALDATASOURCE" VALUES ('94279d93-cef3-46e0-83af-3b7dc48764fe', '非默认的SqlServer', '1', 'Data Source=localhost;Encrypt=false;Initial Catalog=openauthpro;User=sa;Password=000000;', 'localhost', NULL, 'openauthpro', 'sa', '000000', '1', TO_DATE('2025-03-15 20:09:35', 'SYYYY-MM-DD HH24:MI:SS'), TO_DATE('2025-03-18 11:42:08', 'SYYYY-MM-DD HH24:MI:SS'), '0', NULL, '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员'); +INSERT INTO "EXTERNALDATASOURCE" VALUES ('94279d93-cef3-46e0-83af-3b7dc48764ff', '非默认的mysql', '0', 'Server=127.0.0.1;Database=openauthpro;User=root;Password=000000;', '127.0.0.1', '3306', 'openauthpro', 'root', '000000', '1', TO_DATE('2025-03-15 00:02:11', 'SYYYY-MM-DD HH24:MI:SS'), TO_DATE('2025-03-15 20:12:30', 'SYYYY-MM-DD HH24:MI:SS'), '0', NULL, '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员'); -- ---------------------------- -- Table structure for FLOWAPPROVER diff --git a/数据库脚本/PostgreSQL脚本.sql b/数据库脚本/PostgreSQL脚本.sql index 5dc1bf7c..c7e8d074 100644 --- a/数据库脚本/PostgreSQL脚本.sql +++ b/数据库脚本/PostgreSQL脚本.sql @@ -480,8 +480,8 @@ COMMENT ON TABLE "public"."externaldatasource" IS '外部数据源管理'; -- ---------------------------- -- Records of externaldatasource -- ---------------------------- -INSERT INTO "public"."externaldatasource" VALUES ('94279d93-cef3-46e0-83af-3b7dc48764fe', '本地SqlServer', 1, 'Data Source=localhost;Encrypt=false;Initial Catalog=openauthpro;User=sa;Password=000000;', 'localhost', NULL, 'openauthpro', 'sa', '000000', 1, '2025-03-15 20:09:35', '2025-03-18 11:42:08', 0, '', '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员'); -INSERT INTO "public"."externaldatasource" VALUES ('94279d93-cef3-46e0-83af-3b7dc48764ff', '本地mysql', 0, 'Server=127.0.0.1;Database=openauthpro;User=root;Password=000000;', '127.0.0.1', 3306, 'openauthpro', 'root', '000000', 1, '2025-03-15 00:02:11', '2025-03-15 20:12:30', 0, '', '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员'); +INSERT INTO "public"."externaldatasource" VALUES ('94279d93-cef3-46e0-83af-3b7dc48764fe', '非默认的SqlServer', 1, 'Data Source=localhost;Encrypt=false;Initial Catalog=openauthpro;User=sa;Password=000000;', 'localhost', NULL, 'openauthpro', 'sa', '000000', 1, '2025-03-15 20:09:35', '2025-03-18 11:42:08', 0, '', '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员'); +INSERT INTO "public"."externaldatasource" VALUES ('94279d93-cef3-46e0-83af-3b7dc48764ff', '非默认的mysql', 0, 'Server=127.0.0.1;Database=openauthpro;User=root;Password=000000;', '127.0.0.1', 3306, 'openauthpro', 'root', '000000', 1, '2025-03-15 00:02:11', '2025-03-15 20:12:30', 0, '', '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员'); -- ---------------------------- -- Table structure for flowapprover diff --git a/数据库脚本/SqlServer脚本.sql b/数据库脚本/SqlServer脚本.sql index a7252f57..859b4ec4 100644 --- a/数据库脚本/SqlServer脚本.sql +++ b/数据库脚本/SqlServer脚本.sql @@ -1522,10 +1522,10 @@ GO -- ---------------------------- -- Records of ExternalDataSource -- ---------------------------- -INSERT INTO [dbo].[ExternalDataSource] ([Id], [Name], [DbType], [ConnectionString], [Server], [Port], [DatabaseName], [UserName], [Password], [Enabled], [CreateTime], [UpdateTime], [TestSuccess], [Description], [CreateUserId], [CreateUserName], [UpdateUserId], [UpdateUserName]) VALUES (N'94279d93-cef3-46e0-83af-3b7dc48764fe', N'本地SqlServer', N'1', N'Data Source=localhost;Encrypt=false;Initial Catalog=openauthpro;User=sa;Password=000000;', N'localhost', NULL, N'openauthpro', N'sa', N'000000', N'1', N'2025-03-15', N'2025-03-18', N'0', N'', N'00000000-0000-0000-0000-000000000000', N'超级管理员', N'00000000-0000-0000-0000-000000000000', N'超级管理员') +INSERT INTO [dbo].[ExternalDataSource] ([Id], [Name], [DbType], [ConnectionString], [Server], [Port], [DatabaseName], [UserName], [Password], [Enabled], [CreateTime], [UpdateTime], [TestSuccess], [Description], [CreateUserId], [CreateUserName], [UpdateUserId], [UpdateUserName]) VALUES (N'94279d93-cef3-46e0-83af-3b7dc48764fe', N'非默认的SqlServer', N'1', N'Data Source=localhost;Encrypt=false;Initial Catalog=openauthpro;User=sa;Password=000000;', N'localhost', NULL, N'openauthpro', N'sa', N'000000', N'1', N'2025-03-15', N'2025-03-18', N'0', N'', N'00000000-0000-0000-0000-000000000000', N'超级管理员', N'00000000-0000-0000-0000-000000000000', N'超级管理员') GO -INSERT INTO [dbo].[ExternalDataSource] ([Id], [Name], [DbType], [ConnectionString], [Server], [Port], [DatabaseName], [UserName], [Password], [Enabled], [CreateTime], [UpdateTime], [TestSuccess], [Description], [CreateUserId], [CreateUserName], [UpdateUserId], [UpdateUserName]) VALUES (N'94279d93-cef3-46e0-83af-3b7dc48764ff', N'本地mysql', N'0', N'Server=127.0.0.1;Database=openauthpro;User=root;Password=000000;', N'127.0.0.1', N'3306', N'openauthpro', N'root', N'000000', N'1', N'2025-03-15', N'2025-03-15', N'0', N'', N'00000000-0000-0000-0000-000000000000', N'超级管理员', N'00000000-0000-0000-0000-000000000000', N'超级管理员') +INSERT INTO [dbo].[ExternalDataSource] ([Id], [Name], [DbType], [ConnectionString], [Server], [Port], [DatabaseName], [UserName], [Password], [Enabled], [CreateTime], [UpdateTime], [TestSuccess], [Description], [CreateUserId], [CreateUserName], [UpdateUserId], [UpdateUserName]) VALUES (N'94279d93-cef3-46e0-83af-3b7dc48764ff', N'非默认的mysql', N'0', N'Server=127.0.0.1;Database=openauthpro;User=root;Password=000000;', N'127.0.0.1', N'3306', N'openauthpro', N'root', N'000000', N'1', N'2025-03-15', N'2025-03-15', N'0', N'', N'00000000-0000-0000-0000-000000000000', N'超级管理员', N'00000000-0000-0000-0000-000000000000', N'超级管理员') GO diff --git a/数据库脚本/mysql脚本.sql b/数据库脚本/mysql脚本.sql index 73dfca3f..b8f13fd3 100644 --- a/数据库脚本/mysql脚本.sql +++ b/数据库脚本/mysql脚本.sql @@ -373,8 +373,8 @@ CREATE TABLE `externaldatasource` ( -- ---------------------------- -- Records of externaldatasource -- ---------------------------- -INSERT INTO `externaldatasource` VALUES ('94279d93-cef3-46e0-83af-3b7dc48764fe', '本地SqlServer', 1, 'Data Source=localhost;Encrypt=false;Initial Catalog=openauthpro;User=sa;Password=000000;', 'localhost', NULL, 'openauthpro', 'sa', '000000', 1, '2025-03-15 20:09:35', '2025-03-18 11:42:08', 0, '', '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员'); -INSERT INTO `externaldatasource` VALUES ('94279d93-cef3-46e0-83af-3b7dc48764ff', '本地mysql', 0, 'Server=127.0.0.1;Database=openauthpro;User=root;Password=000000;', '127.0.0.1', 3306, 'openauthpro', 'root', '000000', 1, '2025-03-15 00:02:11', '2025-03-15 20:12:30', 0, '', '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员'); +INSERT INTO `externaldatasource` VALUES ('94279d93-cef3-46e0-83af-3b7dc48764fe', '非默认的SqlServer', 1, 'Data Source=localhost;Encrypt=false;Initial Catalog=openauthpro;User=sa;Password=000000;', 'localhost', NULL, 'openauthpro', 'sa', '000000', 1, '2025-03-15 20:09:35', '2025-03-18 11:42:08', 0, '', '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员'); +INSERT INTO `externaldatasource` VALUES ('94279d93-cef3-46e0-83af-3b7dc48764ff', '非默认的mysql', 0, 'Server=127.0.0.1;Database=openauthpro;User=root;Password=000000;', '127.0.0.1', 3306, 'openauthpro', 'root', '000000', 1, '2025-03-15 00:02:11', '2025-03-15 20:12:30', 0, '', '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员'); -- ---------------------------- -- Table structure for flowapprover