增加反向工程的细分开关
This commit is contained in:
parent
e5c205d804
commit
12407f8fea
|
@ -847,7 +847,7 @@ internal class SQLiteMetaData : FileDbMetaData
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override String DropIndexSQL(IDataIndex index) => $"Drop Index {index.Name}";
|
public override String DropIndexSQL(IDataIndex index) => $"Drop Index {index.Name}";
|
||||||
|
|
||||||
protected override String CheckColumnsChange(IDataTable entitytable, IDataTable dbtable, Boolean onlySql, Boolean noDelete)
|
protected override String CheckColumnsChange(IDataTable entitytable, IDataTable dbtable, Boolean onlySql, Boolean noDelete, XCodeSetting set)
|
||||||
{
|
{
|
||||||
foreach (var item in entitytable.Columns)
|
foreach (var item in entitytable.Columns)
|
||||||
{
|
{
|
||||||
|
@ -864,7 +864,7 @@ internal class SQLiteMetaData : FileDbMetaData
|
||||||
}
|
}
|
||||||
|
|
||||||
// 把onlySql设为true,让基类只产生语句而不执行
|
// 把onlySql设为true,让基类只产生语句而不执行
|
||||||
var sql = base.CheckColumnsChange(entitytable, dbtable, onlySql, true);
|
var sql = base.CheckColumnsChange(entitytable, dbtable, onlySql, true, set);
|
||||||
if (sql.IsNullOrEmpty()) return sql;
|
if (sql.IsNullOrEmpty()) return sql;
|
||||||
|
|
||||||
// SQLite 3.35.0 起支持 Drop Column
|
// SQLite 3.35.0 起支持 Drop Column
|
||||||
|
@ -879,7 +879,7 @@ internal class SQLiteMetaData : FileDbMetaData
|
||||||
|
|
||||||
Database.CreateSession().Execute(sql);
|
Database.CreateSession().Execute(sql);
|
||||||
|
|
||||||
return null;
|
return "";
|
||||||
}
|
}
|
||||||
if (sql.Contains("Drop Column") && v != null && v >= new Version(3, 35))
|
if (sql.Contains("Drop Column") && v != null && v >= new Version(3, 35))
|
||||||
{
|
{
|
||||||
|
@ -887,11 +887,11 @@ internal class SQLiteMetaData : FileDbMetaData
|
||||||
|
|
||||||
Database.CreateSession().Execute(sql);
|
Database.CreateSession().Execute(sql);
|
||||||
|
|
||||||
return null;
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
var db = Database as DbBase;
|
var db = Database as DbBase;
|
||||||
using var span = db.Tracer?.NewSpan($"db:{db.ConnName}:SetSchema:RebuildTable", sql);
|
using var span = db!.Tracer?.NewSpan($"db:{db.ConnName}:SetSchema:RebuildTable", sql);
|
||||||
|
|
||||||
var sql2 = sql;
|
var sql2 = sql;
|
||||||
|
|
||||||
|
@ -941,7 +941,7 @@ internal class SQLiteMetaData : FileDbMetaData
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return "";
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
@ -965,16 +965,17 @@ internal class SQLiteMetaData : FileDbMetaData
|
||||||
/// <param name="entitytable"></param>
|
/// <param name="entitytable"></param>
|
||||||
/// <param name="dbtable"></param>
|
/// <param name="dbtable"></param>
|
||||||
/// <param name="mode"></param>
|
/// <param name="mode"></param>
|
||||||
protected override void CheckTable(IDataTable entitytable, IDataTable dbtable, Migration mode)
|
/// <param name="set"></param>
|
||||||
|
protected override void CheckTable(IDataTable entitytable, IDataTable? dbtable, Migration mode, XCodeSetting set)
|
||||||
{
|
{
|
||||||
if (dbtable == null && (Database as SQLite).IsMemoryDatabase)
|
if (dbtable == null && (Database as SQLite)!.IsMemoryDatabase)
|
||||||
{
|
{
|
||||||
if (memoryTables.Any(t => t.TableName.EqualIgnoreCase(entitytable.TableName))) return;
|
if (memoryTables.Any(t => t.TableName.EqualIgnoreCase(entitytable.TableName))) return;
|
||||||
|
|
||||||
memoryTables.Add(entitytable);
|
memoryTables.Add(entitytable);
|
||||||
}
|
}
|
||||||
|
|
||||||
base.CheckTable(entitytable, dbtable, mode);
|
base.CheckTable(entitytable, dbtable, mode, set);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override Boolean PerformSchema(StringBuilder sb, Boolean onlySql, DDLSchema schema, params Object[] values)
|
protected override Boolean PerformSchema(StringBuilder sb, Boolean onlySql, DDLSchema schema, params Object[] values)
|
||||||
|
|
|
@ -69,14 +69,16 @@ internal partial class DbMetaData
|
||||||
{
|
{
|
||||||
if (mode == Migration.Off) return;
|
if (mode == Migration.Off) return;
|
||||||
|
|
||||||
OnSetTables(tables, mode);
|
var set = XCodeSetting.Current;
|
||||||
|
|
||||||
|
OnSetTables(tables, mode, set);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void OnSetTables(IDataTable[] tables, Migration mode)
|
protected virtual void OnSetTables(IDataTable[] tables, Migration mode, XCodeSetting set)
|
||||||
{
|
{
|
||||||
var dbExist = CheckDatabase(mode);
|
var dbExist = CheckDatabase(mode);
|
||||||
|
|
||||||
CheckAllTables(tables, mode, dbExist);
|
CheckAllTables(tables, mode, dbExist, set);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Boolean? hasCheckedDatabase;
|
private Boolean? hasCheckedDatabase;
|
||||||
|
@ -119,7 +121,7 @@ internal partial class DbMetaData
|
||||||
return dbExist;
|
return dbExist;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CheckAllTables(IDataTable[] tables, Migration mode, Boolean dbExit)
|
private void CheckAllTables(IDataTable[] tables, Migration mode, Boolean dbExit, XCodeSetting set)
|
||||||
{
|
{
|
||||||
IList<IDataTable>? dbtables = null;
|
IList<IDataTable>? dbtables = null;
|
||||||
if (dbExit)
|
if (dbExit)
|
||||||
|
@ -141,9 +143,9 @@ internal partial class DbMetaData
|
||||||
|
|
||||||
// 判断指定表是否存在于数据库中,以决定是创建表还是修改表
|
// 判断指定表是否存在于数据库中,以决定是创建表还是修改表
|
||||||
if (dbtable != null)
|
if (dbtable != null)
|
||||||
CheckTable(item, dbtable, mode);
|
CheckTable(item, dbtable, mode, set);
|
||||||
else
|
else
|
||||||
CheckTable(item, null, mode);
|
CheckTable(item, null, mode, set);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -152,7 +154,7 @@ internal partial class DbMetaData
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void CheckTable(IDataTable entitytable, IDataTable? dbtable, Migration mode)
|
protected virtual void CheckTable(IDataTable entitytable, IDataTable? dbtable, Migration mode, XCodeSetting set)
|
||||||
{
|
{
|
||||||
var @readonly = mode <= Migration.ReadOnly;
|
var @readonly = mode <= Migration.ReadOnly;
|
||||||
if (dbtable == null)
|
if (dbtable == null)
|
||||||
|
@ -174,19 +176,30 @@ internal partial class DbMetaData
|
||||||
var onlyCreate = mode < Migration.Full;
|
var onlyCreate = mode < Migration.Full;
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
var sql = CheckTableDescription(entitytable, dbtable, mode);
|
if (set.CheckComment)
|
||||||
if (!sql.IsNullOrEmpty()) Append(sb, ";" + Environment.NewLine, sql);
|
{
|
||||||
|
var sql = CheckTableDescription(entitytable, dbtable, mode);
|
||||||
|
if (!sql.IsNullOrEmpty()) Append(sb, ";" + Environment.NewLine, sql);
|
||||||
|
}
|
||||||
|
|
||||||
// 先删除索引,后面才有可能删除字段
|
if (set.CheckDeleteIndex)
|
||||||
sql = CheckDeleteIndex(entitytable, dbtable, mode);
|
{
|
||||||
if (!sql.IsNullOrEmpty()) Append(sb, ";" + Environment.NewLine, sql);
|
// 先删除索引,后面才有可能删除字段
|
||||||
|
var sql = CheckDeleteIndex(entitytable, dbtable, mode);
|
||||||
|
if (!sql.IsNullOrEmpty()) Append(sb, ";" + Environment.NewLine, sql);
|
||||||
|
}
|
||||||
|
|
||||||
sql = CheckColumnsChange(entitytable, dbtable, @readonly, onlyCreate);
|
{
|
||||||
if (!sql.IsNullOrEmpty()) Append(sb, ";" + Environment.NewLine, sql);
|
var sql = CheckColumnsChange(entitytable, dbtable, @readonly, onlyCreate, set);
|
||||||
|
if (!sql.IsNullOrEmpty()) Append(sb, ";" + Environment.NewLine, sql);
|
||||||
|
}
|
||||||
|
|
||||||
// 新增字段后,可能需要删除索引
|
if (set.CheckAddIndex)
|
||||||
sql = CheckAddIndex(entitytable, dbtable, mode);
|
{
|
||||||
if (!sql.IsNullOrEmpty()) Append(sb, ";" + Environment.NewLine, sql);
|
// 新增字段后,可能需要删除索引
|
||||||
|
var sql = CheckAddIndex(entitytable, dbtable, mode);
|
||||||
|
if (!sql.IsNullOrEmpty()) Append(sb, ";" + Environment.NewLine, sql);
|
||||||
|
}
|
||||||
|
|
||||||
if (sb.Length > 0) WriteLog($"DDL模式[{mode}],请手工修改表[{dbtable.TableName}]:{Environment.NewLine}{sb}");
|
if (sb.Length > 0) WriteLog($"DDL模式[{mode}],请手工修改表[{dbtable.TableName}]:{Environment.NewLine}{sb}");
|
||||||
}
|
}
|
||||||
|
@ -197,8 +210,9 @@ internal partial class DbMetaData
|
||||||
/// <param name="dbtable"></param>
|
/// <param name="dbtable"></param>
|
||||||
/// <param name="readonly"></param>
|
/// <param name="readonly"></param>
|
||||||
/// <param name="onlyCreate"></param>
|
/// <param name="onlyCreate"></param>
|
||||||
|
/// <param name="set"></param>
|
||||||
/// <returns>返回未执行语句</returns>
|
/// <returns>返回未执行语句</returns>
|
||||||
protected virtual String CheckColumnsChange(IDataTable entitytable, IDataTable dbtable, Boolean @readonly, Boolean onlyCreate)
|
protected virtual String CheckColumnsChange(IDataTable entitytable, IDataTable dbtable, Boolean @readonly, Boolean onlyCreate, XCodeSetting set)
|
||||||
{
|
{
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
var etdic = entitytable.Columns.ToDictionary(e => FormatName(e), e => e, StringComparer.OrdinalIgnoreCase);
|
var etdic = entitytable.Columns.ToDictionary(e => FormatName(e), e => e, StringComparer.OrdinalIgnoreCase);
|
||||||
|
@ -279,7 +293,7 @@ internal partial class DbMetaData
|
||||||
PerformSchema(sb, @readonly || onlyCreate, DDLSchema.AlterColumn, item, dbf);
|
PerformSchema(sb, @readonly || onlyCreate, DDLSchema.AlterColumn, item, dbf);
|
||||||
|
|
||||||
//if (item.Description + "" != dbf.Description + "")
|
//if (item.Description + "" != dbf.Description + "")
|
||||||
if (FormatDescription(item.Description) != FormatDescription(dbf.Description))
|
if (set.CheckComment && FormatDescription(item.Description) != FormatDescription(dbf.Description))
|
||||||
{
|
{
|
||||||
// 先删除旧注释
|
// 先删除旧注释
|
||||||
//if (dbf.Description != null) PerformSchema(sb, noDelete, DDLSchema.DropColumnDescription, dbf);
|
//if (dbf.Description != null) PerformSchema(sb, noDelete, DDLSchema.DropColumnDescription, dbf);
|
||||||
|
|
|
@ -73,6 +73,18 @@ public class XCodeSetting : Config<XCodeSetting>
|
||||||
[Description("反向工程。Off 关闭;ReadOnly 只读不执行;On 打开,仅新建;Full 完全,修改删除")]
|
[Description("反向工程。Off 关闭;ReadOnly 只读不执行;On 打开,仅新建;Full 完全,修改删除")]
|
||||||
public Migration Migration { get; set; } = Migration.On;
|
public Migration Migration { get; set; } = Migration.On;
|
||||||
|
|
||||||
|
/// <summary>检查注释。表注释或字段注释,反向工程,默认打开</summary>
|
||||||
|
[Description("检查注释。表注释或字段注释,反向工程,默认打开")]
|
||||||
|
public Boolean CheckComment { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>检查删除索引。反向工程,默认打开</summary>
|
||||||
|
[Description("检查删除索引。反向工程,默认打开")]
|
||||||
|
public Boolean CheckDeleteIndex { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>检查添加索引。反向工程,默认打开</summary>
|
||||||
|
[Description("检查添加索引。反向工程,默认打开")]
|
||||||
|
public Boolean CheckAddIndex { get; set; } = true;
|
||||||
|
|
||||||
/// <summary>是否检查索引重复。默认打开</summary>
|
/// <summary>是否检查索引重复。默认打开</summary>
|
||||||
[Description("检查索引重复。默认打开")]
|
[Description("检查索引重复。默认打开")]
|
||||||
public Boolean CheckDuplicateIndex { get; set; } = true;
|
public Boolean CheckDuplicateIndex { get; set; } = true;
|
||||||
|
|
Loading…
Reference in New Issue