Browse Source

Add db.CodeFirst.GetDifferenceTables(types)

pull/18/head
sunkaixuan 3 years ago
parent
commit
435b889ff2
  1. 4
      Src/Asp.Net/SqlServerTest/UnitTest/UCodeFirst.cs
  2. 52
      Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs
  3. 140
      Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/TableDifferenceProvider.cs
  4. 2
      Src/Asp.Net/SqlSugar/Interface/ICodeFirst.cs
  5. 1
      Src/Asp.Net/SqlSugar/SqlSugar.csproj

4
Src/Asp.Net/SqlServerTest/UnitTest/UCodeFirst.cs

@ -72,9 +72,13 @@ namespace OrmTest
Db.CodeFirst.InitTables<UnitCodeFirst131>();
Db.Insertable(new UnitCodeFirst131() { Id = 1 }).ExecuteCommand();
Db.CodeFirst.InitTables<UNITCODEFIRST131>();
var diffInfo = db.CodeFirst.GetDifferenceTables(typeof(UNITCOdEFIRST131)).ToDiffString();
db.CodeFirst.InitTables<UNITCOdEFIRST131>();
var diffInfo2 = db.CodeFirst.GetDifferenceTables(typeof(UNITCODEFIRST131)).ToDiffString();
Db.CodeFirst.InitTables<UNITCOdEFIRST131>();
Db.CodeFirst.InitTables<UnitTableUserName>();
db.CodeFirst.InitTables<UnitTablename>();
}
public class UnitCodeFirst131
{

52
Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs

@ -118,9 +118,61 @@ namespace SqlSugar
}
}
}
public TableDifferenceProvider GetDifferenceTables<T>()
{
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);

140
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<DiffTableInfo> tableInfos = new List<DiffTableInfo>();
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<TableDifferenceInfo> ToDiffList()
{
List<TableDifferenceInfo> result = new List<TableDifferenceInfo>();
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<DiffColumsInfo> GetDeleteColumn(DiffTableInfo tableInfo)
{
List<DiffColumsInfo> result = new List<DiffColumsInfo>();
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<DiffColumsInfo> GetUpdateColumn(DiffTableInfo tableInfo)
{
List<DiffColumsInfo> result = new List<DiffColumsInfo>();
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<DiffColumsInfo> GetAddColumn(DiffTableInfo tableInfo)
{
List<DiffColumsInfo> result = new List<DiffColumsInfo>();
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<DiffColumsInfo> DeleteColums { get; set; } = new List<DiffColumsInfo>();
public List<DiffColumsInfo> UpdateColums { get; set; } = new List<DiffColumsInfo>();
public List<DiffColumsInfo> AddColums { get; set; } = new List<DiffColumsInfo>();
public List<DiffColumsInfo> UpdateRemark { get; set; } = new List<DiffColumsInfo>();
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<DbColumnInfo> OldColumnInfos { get; set; }
public List<DbColumnInfo> NewColumnInfos { get; set; }
}
}

2
Src/Asp.Net/SqlSugar/Interface/ICodeFirst.cs

@ -18,5 +18,7 @@ namespace SqlSugar
void InitTables<T, T2, T3>();
void InitTables<T, T2, T3, T4>();
SplitCodeFirstProvider SplitTables();
TableDifferenceProvider GetDifferenceTables<T>();
TableDifferenceProvider GetDifferenceTables(params Type[] types);
}
}

1
Src/Asp.Net/SqlSugar/SqlSugar.csproj

@ -71,6 +71,7 @@
<ItemGroup>
<Compile Include="Abstract\AopProvider\AopProvider.cs" />
<Compile Include="Abstract\CacheProvider\CacheProvider.cs" />
<Compile Include="Abstract\CodeFirstProvider\TableDifferenceProvider.cs" />
<Compile Include="Abstract\CodeFirstProvider\CodeFirstProvider.cs" />
<Compile Include="Abstract\AdoProvider\AdoAccessory.cs" />
<Compile Include="Abstract\CodeFirstProvider\SplitCodeFirstProvider.cs" />

Loading…
Cancel
Save