|
@@ -325,6 +325,48 @@ public static class SqlSugarSetup
|
|
|
db.Aop.OnDiffLogEvent = AopOnDiffLogEvent;
|
|
db.Aop.OnDiffLogEvent = AopOnDiffLogEvent;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 初始化视图
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="dbProvider"></param>
|
|
|
|
|
+ private static void InitView(SqlSugarScopeProvider dbProvider)
|
|
|
|
|
+ {
|
|
|
|
|
+ var totalWatch = Stopwatch.StartNew(); // 开始总计时
|
|
|
|
|
+ Log.Information($"初始化视图 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId}");
|
|
|
|
|
+ var viewTypeList = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarView)))).ToList();
|
|
|
|
|
+
|
|
|
|
|
+ int taskIndex = 0, size = viewTypeList.Count;
|
|
|
|
|
+ var taskList = viewTypeList.Select(viewType => Task.Run(() =>
|
|
|
|
|
+ {
|
|
|
|
|
+ // 开始计时
|
|
|
|
|
+ var stopWatch = Stopwatch.StartNew();
|
|
|
|
|
+
|
|
|
|
|
+ // 获取视图实体和配置信息
|
|
|
|
|
+ var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(viewType) ?? throw new Exception("获取视图实体配置有误");
|
|
|
|
|
+
|
|
|
|
|
+ // 如果视图存在,则删除视图
|
|
|
|
|
+ if (dbProvider.DbMaintenance.GetViewInfoList(false).Any(it => it.Name.EqualIgnoreCase(entityInfo.DbTableName)))
|
|
|
|
|
+ dbProvider.DbMaintenance.DropView(entityInfo.DbTableName);
|
|
|
|
|
+
|
|
|
|
|
+ // 获取初始化视图查询SQL
|
|
|
|
|
+ var sql = viewType.GetMethod(nameof(ISqlSugarView.GetQueryableSqlString))?.Invoke(Activator.CreateInstance(viewType), [dbProvider]) as string;
|
|
|
|
|
+ if (string.IsNullOrWhiteSpace(sql)) throw new Exception("视图初始化Sql语句不能为空");
|
|
|
|
|
+
|
|
|
|
|
+ // 创建视图
|
|
|
|
|
+ dbProvider.Ado.ExecuteCommand($"CREATE VIEW {entityInfo.DbTableName} AS " + Environment.NewLine + " " + sql);
|
|
|
|
|
+
|
|
|
|
|
+ // 停止计时
|
|
|
|
|
+ stopWatch.Stop();
|
|
|
|
|
+ Console.ForegroundColor = ConsoleColor.Green;
|
|
|
|
|
+ Console.WriteLine($"初始化视图 {viewType.FullName,-58} ({dbProvider.CurrentConnectionConfig.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{size:D003},耗时:{stopWatch.ElapsedMilliseconds:N0} ms)");
|
|
|
|
|
+ }));
|
|
|
|
|
+ Task.WaitAll(taskList.ToArray());
|
|
|
|
|
+
|
|
|
|
|
+ totalWatch.Stop(); // 停止总计时
|
|
|
|
|
+ Console.ForegroundColor = ConsoleColor.Green;
|
|
|
|
|
+ Console.WriteLine($"初始化视图 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId} 总耗时:{totalWatch.ElapsedMilliseconds:N0} ms");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
/// 初始化数据库
|
|
/// 初始化数据库
|
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -349,6 +391,9 @@ public static class SqlSugarSetup
|
|
|
InitializeTables(dbProvider, entityTypes, config);
|
|
InitializeTables(dbProvider, entityTypes, config);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // 初始化视图
|
|
|
|
|
+ if (config.DbSettings.EnableInitView) InitView(dbProvider);
|
|
|
|
|
+
|
|
|
// 初始化种子数据
|
|
// 初始化种子数据
|
|
|
if (config.SeedSettings.EnableInitSeed) InitSeedData(db, config);
|
|
if (config.SeedSettings.EnableInitSeed) InitSeedData(db, config);
|
|
|
}
|
|
}
|
|
@@ -402,6 +447,15 @@ public static class SqlSugarSetup
|
|
|
/// <param name="config">数据库连接配置</param>
|
|
/// <param name="config">数据库连接配置</param>
|
|
|
private static void InitializeTables(SqlSugarScopeProvider dbProvider, List<Type> entityTypes, DbConnectionConfig config)
|
|
private static void InitializeTables(SqlSugarScopeProvider dbProvider, List<Type> entityTypes, DbConnectionConfig config)
|
|
|
{
|
|
{
|
|
|
|
|
+ // 删除视图再初始化表结构,防止因为视图导致无法同步表结构
|
|
|
|
|
+ var viewTypeList = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarView)))).ToList();
|
|
|
|
|
+ foreach (var viewType in viewTypeList)
|
|
|
|
|
+ {
|
|
|
|
|
+ var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(viewType) ?? throw new Exception("获取视图实体配置有误");
|
|
|
|
|
+ if (dbProvider.DbMaintenance.GetViewInfoList(false).Any(it => it.Name.EqualIgnoreCase(entityInfo.DbTableName)))
|
|
|
|
|
+ dbProvider.DbMaintenance.DropView(entityInfo.DbTableName);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
int count = 0, sum = entityTypes.Count;
|
|
int count = 0, sum = entityTypes.Count;
|
|
|
var tasks = entityTypes.Select(entityType => Task.Run(() =>
|
|
var tasks = entityTypes.Select(entityType => Task.Run(() =>
|
|
|
{
|
|
{
|