From 435b889ff2c48f44b32f6bcf5e58b346fd96abc4 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Thu, 5 May 2022 21:39:31 +0800 Subject: [PATCH] Add db.CodeFirst.GetDifferenceTables(types) --- .../SqlServerTest/UnitTest/UCodeFirst.cs | 4 + .../CodeFirstProvider/CodeFirstProvider.cs | 52 +++++++ .../TableDifferenceProvider.cs | 140 ++++++++++++++++++ Src/Asp.Net/SqlSugar/Interface/ICodeFirst.cs | 2 + Src/Asp.Net/SqlSugar/SqlSugar.csproj | 1 + 5 files changed, 199 insertions(+) create mode 100644 Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/TableDifferenceProvider.cs diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UCodeFirst.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UCodeFirst.cs index faba79d13..f760723e2 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/UCodeFirst.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UCodeFirst.cs @@ -72,9 +72,13 @@ namespace OrmTest Db.CodeFirst.InitTables(); Db.Insertable(new UnitCodeFirst131() { Id = 1 }).ExecuteCommand(); Db.CodeFirst.InitTables(); + var diffInfo = db.CodeFirst.GetDifferenceTables(typeof(UNITCOdEFIRST131)).ToDiffString(); + db.CodeFirst.InitTables(); + var diffInfo2 = db.CodeFirst.GetDifferenceTables(typeof(UNITCODEFIRST131)).ToDiffString(); Db.CodeFirst.InitTables(); Db.CodeFirst.InitTables(); db.CodeFirst.InitTables(); + } public class UnitCodeFirst131 { diff --git a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index 7c78012c8..4d2df7c25 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs @@ -118,9 +118,61 @@ namespace SqlSugar } } } + public TableDifferenceProvider GetDifferenceTables() + { + var type = typeof(T); + return GetDifferenceTables(type); + } + + public TableDifferenceProvider GetDifferenceTables(params Type[] types) + { + TableDifferenceProvider result = new TableDifferenceProvider(); + foreach (var type in types) + { + GetDifferenceTables(result, type); + } + return result; + } #endregion #region Core Logic + private void GetDifferenceTables(TableDifferenceProvider result, Type type) + { + var tempTableName = "TempDiff" + DateTime.Now.ToString("yyMMssHHmmssfff"); + var oldTableName = this.Context.EntityMaintenance.GetEntityInfo(type).DbTableName; + var db = new SqlSugarProvider(UtilMethods.CopyConfig(this.Context.CurrentConnectionConfig)); + UtilMethods.IsNullReturnNew(db.CurrentConnectionConfig.ConfigureExternalServices); + db.CurrentConnectionConfig.ConfigureExternalServices.EntityNameService += (x, p) => + { + p.IsDisabledUpdateAll = true;//Disabled update + }; + db.MappingTables = new MappingTableList(); + db.MappingTables.Add(type.Name, tempTableName); + try + { + db.CodeFirst.InitTables(type); + var tables = db.DbMaintenance.GetTableInfoList(false); + var oldTableInfo = tables.FirstOrDefault(it=>it.Name.EqualCase(oldTableName)); + var newTableInfo = tables.FirstOrDefault(it => it.Name.EqualCase(oldTableName)); + var oldTable = db.DbMaintenance.GetColumnInfosByTableName(oldTableName, false); + var tempTable = db.DbMaintenance.GetColumnInfosByTableName(tempTableName, false); + result.tableInfos.Add(new DiffTableInfo() + { + OldTableInfo= oldTableInfo, + NewTableInfo = newTableInfo, + OldColumnInfos = oldTable, + NewColumnInfos = tempTable + }); + } + catch (Exception ex) + { + throw ex; + } + finally + { + db.DbMaintenance.DropTable(tempTableName); + } + } protected virtual void Execute(Type entityType) { var entityInfo = this.Context.EntityMaintenance.GetEntityInfoNoCache(entityType); diff --git a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/TableDifferenceProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/TableDifferenceProvider.cs new file mode 100644 index 000000000..bb73b159d --- /dev/null +++ b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/TableDifferenceProvider.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +namespace SqlSugar +{ + public class TableDifferenceProvider + { + internal List tableInfos = new List(); + public string ToDiffString() + { + StringBuilder sb = new StringBuilder(); + sb.AppendLine(); + var diffTables = this.ToDiffList(); + if (diffTables.IsNullOrEmpty()) + { + sb.AppendLine("No change"); + } + else + { + foreach (var item in diffTables) + { + sb.AppendLine($"----Table:{ item.TableName }----"); + if (item.AddColums.HasValue()) + { + sb.AppendLine($"Add column: "); + foreach (var addItem in item.AddColums) + { + sb.AppendLine($"{addItem.Message} "); + } + } + if (item.UpdateColums.HasValue()) + { + sb.AppendLine($"Update column: "); + foreach (var addItem in item.UpdateColums) + { + sb.AppendLine($"{addItem.Message} "); + } + } + if (item.DeleteColums.HasValue()) + { + sb.AppendLine($"Delete column: "); + foreach (var addItem in item.DeleteColums) + { + sb.AppendLine($"{addItem.Message} "); + } + } + } + } + sb.AppendLine(); + sb.AppendLine(); + return sb.ToString(); + } + + public List ToDiffList() + { + List result = new List(); + foreach (var tableInfo in tableInfos) + { + TableDifferenceInfo addItem = new TableDifferenceInfo(); + addItem.TableName = tableInfo.OldTableInfo.Name; + addItem.AddColums = GetAddColumn(tableInfo); + addItem.UpdateColums = GetUpdateColumn(tableInfo); + addItem.DeleteColums = GetDeleteColumn(tableInfo); + if (addItem.IsDiff) + result.Add(addItem); + } + return result; + } + + private static List GetDeleteColumn(DiffTableInfo tableInfo) + { + List result = new List(); + var columns = tableInfo.OldColumnInfos.Where(z => !tableInfo.NewColumnInfos.Any(y => y.DbColumnName.EqualCase(z.DbColumnName))).ToList(); + return columns.Select(it => new DiffColumsInfo() { Message= GetColumnString(it) }).ToList(); + } + + private List GetUpdateColumn(DiffTableInfo tableInfo) + { + List result = new List(); + result = tableInfo.NewColumnInfos + .Where(z => tableInfo.OldColumnInfos.Any(y => y.DbColumnName.EqualCase(z.DbColumnName) && ( + z.Length != y.Length || + z.ColumnDescription != y.ColumnDescription || + z.DataType != y.DataType || + z.DecimalDigits != y.DecimalDigits + ))).Select(it => new DiffColumsInfo() + { + Message=GetColumnString(it) + }).ToList(); + return result; + } + + private static List GetAddColumn(DiffTableInfo tableInfo) + { + List result = new List(); + var columns = tableInfo.NewColumnInfos.Where(z => !tableInfo.OldColumnInfos.Any(y => y.DbColumnName.EqualCase(z.DbColumnName))).ToList(); + return columns.Select(it => new DiffColumsInfo() { Message = GetColumnString(it) }).ToList(); + } + + private static string GetColumnString(DbColumnInfo it) + { + return $"{it.DbColumnName} {it.DataType} {it.Length} {it.Scale} default:{it.DefaultValue} description:{it.ColumnDescription} pk:{it.IsPrimarykey} nullable:{it.IsNullable} identity:{it.IsIdentity} "; + } + } + public class TableDifferenceInfo + { + public List DeleteColums { get; set; } = new List(); + public List UpdateColums { get; set; } = new List(); + public List AddColums { get; set; } = new List(); + public List UpdateRemark { get; set; } = new List(); + public bool IsDiff + { + get + { + return + (DeleteColums.Count>0 || + UpdateColums.Count > 0 || + AddColums.Count > 0 || + UpdateRemark.Count > 0) ; + } + } + + public string TableName { get; set; } + } + + public class DiffColumsInfo + { + public string SqlTemplate { get; set; } + public string Message { get; set; } + } + + public class DiffTableInfo + { + public DbTableInfo OldTableInfo { get; set; } + public DbTableInfo NewTableInfo { get; set; } + public List OldColumnInfos { get; set; } + public List NewColumnInfos { get; set; } + } +} diff --git a/Src/Asp.Net/SqlSugar/Interface/ICodeFirst.cs b/Src/Asp.Net/SqlSugar/Interface/ICodeFirst.cs index 6eb0e6a4c..9483e7b36 100644 --- a/Src/Asp.Net/SqlSugar/Interface/ICodeFirst.cs +++ b/Src/Asp.Net/SqlSugar/Interface/ICodeFirst.cs @@ -18,5 +18,7 @@ namespace SqlSugar void InitTables(); void InitTables(); SplitCodeFirstProvider SplitTables(); + TableDifferenceProvider GetDifferenceTables(); + TableDifferenceProvider GetDifferenceTables(params Type[] types); } } diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index 807776160..470250b22 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -71,6 +71,7 @@ +