public static void Register() { new Construct(DynamicTypeOperation.Create) { Construct = (_) => new DynamicTypeEntity { BaseType = DynamicBaseType.Entity }, }.Register(); new ConstructFrom <DynamicTypeEntity>(DynamicTypeOperation.Clone) { Construct = (e, _) => { var def = e.GetDefinition(); var result = new DynamicTypeEntity { TypeName = null }; result.SetDefinition(def); return(result); }, }.Register(); new Execute(DynamicTypeOperation.Save) { AllowsNew = true, Lite = false, Execute = (e, _) => { if (!e.IsNew) { var old = e.ToLite().Retrieve(); if (e.TypeName != old.TypeName) { DynamicSqlMigrationLogic.AddDynamicRename(Replacements.KeyTables, old.TypeName, e.TypeName); } var newDef = e.GetDefinition(); var oldDef = old.GetDefinition(); var pairs = newDef.Properties .Join(oldDef.Properties, n => n.UID, o => o.UID, (n, o) => new { n, o }) .Where(a => a.n.Type == a.o.Type); foreach (var a in pairs.Where(a => a.n.Name != a.o.Name)) { DynamicSqlMigrationLogic.AddDynamicRename(Replacements.KeyColumnsForTable(old.TypeName), a.o.Name, a.n.Name); } } }, }.Register(); new Delete(DynamicTypeOperation.Delete) { Delete = (e, _) => { e.Delete(); } }.Register(); }
public static void Start(SchemaBuilder sb, DynamicQueryManager dqm) { if (sb.NotDefined(MethodInfo.GetCurrentMethod())) { sb.Include <DynamicTypeConditionSymbolEntity>() .WithQuery(dqm, () => e => new { Entity = e, e.Id, e.Name, }); sb.Include <DynamicTypeConditionEntity>() .WithSave(DynamicTypeConditionOperation.Save) .WithQuery(dqm, () => e => new { Entity = e, e.Id, e.SymbolName, e.EntityType, e.Eval.Script, }); new Graph <DynamicTypeConditionSymbolEntity> .Execute(DynamicTypeConditionSymbolOperation.Save) { Lite = false, AllowsNew = true, Execute = (e, _) => { if (!e.IsNew) { var old = e.ToLite().Retrieve(); if (old.Name != e.Name) { DynamicSqlMigrationLogic.AddDynamicRename(typeof(TypeConditionSymbol).Name, $"CodeGenTypeCondition.{old.Name}", $"CodeGenTypeCondition.{e.Name}"); } } } } .Register(); DynamicLogic.GetCodeFiles += GetCodeFiles; DynamicLogic.OnWriteDynamicStarter += WriteDynamicStarter; sb.Schema.Table <TypeEntity>().PreDeleteSqlSync += type => Administrator.UnsafeDeletePreCommand(Database.Query <DynamicTypeConditionEntity>().Where(dtc => dtc.EntityType == type)); } }
public static void Register() { new Construct(DynamicTypeOperation.Create) { Construct = (_) => new DynamicTypeEntity { BaseType = DynamicBaseType.Entity }, }.Register(); new ConstructFrom <DynamicTypeEntity>(DynamicTypeOperation.Clone) { Construct = (e, _) => { var def = e.GetDefinition(); var result = new DynamicTypeEntity { TypeName = null !, BaseType = e.BaseType }; result.SetDefinition(def); return(result); }, }.Register(); new Execute(DynamicTypeOperation.Save) { CanBeNew = true, CanBeModified = true, Execute = (e, _) => { var newDef = e.GetDefinition(); var duplicatePropertyNames = newDef.Properties .GroupToDictionary(a => a.Name.ToLower()) .Where(a => a.Value.Count() > 1) .ToList(); if (duplicatePropertyNames.Any()) { throw new InvalidOperationException(ValidationMessage._0HasSomeRepeatedElements1.NiceToString(e.TypeName, duplicatePropertyNames.Select(a => a.Key.FirstUpper()).Comma(", "))); } if (!e.IsNew) { var old = e.ToLite().RetrieveAndRemember(); if (e.TypeName != old.TypeName) { DynamicSqlMigrationLogic.AddDynamicRename(TypeNameKey, old.TypeName, e.TypeName); } if (e.BaseType == DynamicBaseType.ModelEntity) { return; } var oldDef = old.GetDefinition(); var newName = GetTableName(e, newDef); var oldName = GetTableName(old, oldDef); if (newName != oldName) { DynamicSqlMigrationLogic.AddDynamicRename(Replacements.KeyTables, oldName, newName); } var pairs = newDef.Properties .Join(oldDef.Properties, n => n.UID, o => o.UID, (n, o) => new { n, o }) .Where(a => a.n !.Type == a.o !.Type); /*CSBUG*/ { string ColName(DynamicProperty dp) => dp.ColumnName ?? dp.Name; string replacementKey = (e.BaseType != DynamicBaseType.Entity || old.BaseType != DynamicBaseType.Entity) ? UnknownColumnKey : Replacements.KeyColumnsForTable(oldName); foreach (var a in pairs.Where(a => ColName(a.n !) != ColName(a.o !))) { DynamicSqlMigrationLogic.AddDynamicRename(replacementKey, ColName(a.o !), ColName(a.n !)); } } { string replacementKey = (e.BaseType != DynamicBaseType.Entity || old.BaseType != DynamicBaseType.Entity) ? UnknownPropertyKey : PropertyRouteLogic.PropertiesFor.FormatWith(old.TypeName); foreach (var a in pairs.Where(a => a.n !.Name != a.o !.Name)) { DynamicSqlMigrationLogic.AddDynamicRename(replacementKey, a.o !.Name, a.n !.Name); } } } }, }.Register();
public static void Start(SchemaBuilder sb) { if (sb.NotDefined(MethodInfo.GetCurrentMethod())) { sb.Include <DynamicTypeConditionSymbolEntity>() .WithQuery(() => e => new { Entity = e, e.Id, e.Name, }); sb.Include <DynamicTypeConditionEntity>() .WithSave(DynamicTypeConditionOperation.Save) .WithQuery(() => e => new { Entity = e, e.Id, e.SymbolName, e.EntityType, e.Eval.Script, }); new Graph <DynamicTypeConditionEntity> .ConstructFrom <DynamicTypeConditionEntity>(DynamicTypeConditionOperation.Clone) { Construct = (e, args) => new DynamicTypeConditionEntity() { SymbolName = e.SymbolName, EntityType = e.EntityType, Eval = new DynamicTypeConditionEval() { Script = e.Eval.Script }, } } .Register(); new Graph <DynamicTypeConditionSymbolEntity> .Execute(DynamicTypeConditionSymbolOperation.Save) { CanBeModified = true, CanBeNew = true, Execute = (e, _) => { if (!e.IsNew) { var old = e.ToLite().RetrieveAndRemember(); if (old.Name != e.Name) { DynamicSqlMigrationLogic.AddDynamicRename(typeof(TypeConditionSymbol).Name, $"CodeGenTypeCondition.{old.Name}", $"CodeGenTypeCondition.{e.Name}"); } } } } .Register(); DynamicLogic.GetCodeFiles += GetCodeFiles; DynamicLogic.OnWriteDynamicStarter += WriteDynamicStarter; DynamicCode.RegisteredDynamicTypes.Add(typeof(DynamicTypeConditionEntity)); sb.Schema.Table <TypeEntity>().PreDeleteSqlSync += type => Administrator.UnsafeDeletePreCommand(Database.Query <DynamicTypeConditionEntity>().Where(dtc => dtc.EntityType == type)); sb.AddUniqueIndex((DynamicTypeConditionEntity e) => new { e.SymbolName, e.EntityType }); } }