public RefBTAttribute(RefBLAttribute blAttribute, BTInterface ifa) { this.blAttribute = blAttribute; this.refBLAttribute = blAttribute; this.otherBlAttribute = null; this.ParentInterface = ifa; this.IsIdentity = blAttribute.IsIdentity; this.IsPartOfUniqueKey = blAttribute.IsPartOfUniqueKey; this.HasToUseVerionTable = GetHasToUseVersionTable(); if (HasToUseVerionTable) { // Wenn das aktuelle Interface keine FactTable ist und beide eine Historie haben, // dann wird die Beziehung über die Historientabellen aufgelöst var blModel = blAttribute.ParentInterface.ParentModel; this.ReferencedBLInterface = blModel.Interfaces.Single(i => i.Name == blAttribute.ReferencedAttribute.ParentInterface.Name + "_VERSION"); this.ReferencedBLAttribute = this.ReferencedBLInterface.Attributes.Single(a => a.IsPartOfUniqueKey && !a.IsTechnicalAttribute && a.Name != "Mandant_KNZ"); } else { // ansonsten wird die Tabelle ohne Historie verwendet. this.ReferencedBLAttribute = blAttribute.ReferencedAttribute; this.ReferencedBLInterface = blAttribute.ReferencedAttribute.ParentInterface; } this.IdAttribute = new IdSubAttribute(this); this.KnzAttribute = new KnzSubAttribute(this); }
public IdSubAttribute(RefBTAttribute refBTAttribute) { this.refBTAttribute = refBTAttribute; if (refBTAttribute.blAttribute is RefBLAttribute) { this.blAttribute = (RefBLAttribute)refBTAttribute.blAttribute; } }
// Einzelnes Foreign-Key-Constraint anlegen internal string GenerateSingleForeignKeyConstraint(RefBLAttribute attr) { string code = $"alter table {attr.ParentInterface.FullName}\n"; code += $"add constraint {attr.ParentInterface.Name}_{attr.Name}_FK\n"; // TODO: Wenn das Ganze mit Mandant ist dann kommt hier der Mandant mit rein, // und bei Historisierung zusätzlich noch das Historisierungsattribut ... // (wenn auf beiden Seiten unterstützt!) if (attr.ParentInterface.IsMandant && attr.ReferencedAttribute.ParentInterface.IsMandant) { var attrNames = new List <string>(); attrNames.Add("Mandant_KNZ"); attrNames.Add(attr.Name); code += "foreign key ("; foreach (var attrName in attrNames) { code += attrName; if (attrName != attrNames.Last()) { code += ", "; } } code += ")\n"; var attrNames2 = attr.ReferencedAttribute.ParentInterface.UniqueKeyAttributes.Select(a => a.Name); code += $"references {attr.ReferencedAttribute.ParentInterface.Name} ("; foreach (var attrName in attrNames2) { code += attrName; if (attrName != attrNames2.Last()) { code += ", "; } } code += ");\n\n"; } else { code += $"foreign key ({attr.Name})\n"; code += $"references {attr.ReferencedAttribute.ParentInterface.Name} ({attr.ReferencedAttribute.Name});\n\n"; } return(code); }
/// /// Wichtig: nur in BT zur Abbildung der History-Beziehung nutzen! /// internal RefBTAttribute(BaseBLAttribute blAttribute, IBLInterface blIfa, BTInterface ifa) { this.blAttribute = blAttribute; this.refBLAttribute = null; this.otherBlAttribute = blAttribute; this.ParentInterface = ifa; this.IsIdentity = otherBlAttribute.IsIdentity; this.IsPartOfUniqueKey = otherBlAttribute.IsPartOfUniqueKey; this.HasToUseVerionTable = false; // TODO: Das ist noch nicht sicher ! var blModel = blAttribute.ParentInterface.ParentModel; this.ReferencedBLInterface = blIfa; this.ReferencedBLAttribute = blIfa.Attributes.Single(a => a.IsPartOfUniqueKey && (a is BaseBLAttribute)); this.IdAttribute = new IdSubAttribute(this); this.KnzAttribute = new KnzSubAttribute(this); }
public KnzSubAttribute(RefBTAttribute refBTAttribute) { this.refBTAttribute = refBTAttribute; this.refBLAttribute = refBTAttribute.refBLAttribute; this.blAttribute = refBTAttribute.blAttribute; if (refBTAttribute.HasToUseVerionTable) { var core = refBTAttribute.ReferencedBLInterface.GetCoreInterface(); var knz = core.Attributes.Where(a => a.IsPrimaryKey).First(); this.ShortName = $"{core.Name}_VERSION_{knz.Name}"; this.Alias = refBLAttribute.Core.Alias; } else if (refBLAttribute == null) { this.ShortName = this.blAttribute.Name; this.Alias = null; } else { this.ShortName = this.refBLAttribute.ReferencedAttribute.Name; this.Alias = refBLAttribute.Core.Alias; } }
/// /// Kaskadierendes Auflösung von Versionen von Kind-Dimensionen in den /// Elterndimensionen für ein einzelnes Eltern-Kind-Paar auflösen /// private void GenerateCascadeVersions(DerivedBLInterface childIfa, RefBLAttribute reference, StringBuilder sb) { var parentIfa = GetDerivedForDefault(reference.ReferencedAttribute.ParentInterface); var parentNonIdentityAttributes = parentIfa.Attributes.Where(a => !a.IsIdentity); var max = GetMaxValueForHistoryAttribute(parentIfa.HistoryAttribute); sb.Append($"-- Cascade Versions for {parentIfa.Name} -> {reference.ReferencedAttribute.FullName}\n"); sb.Append($"-- {childIfa.Name}\n"); // Ermittlung der im parentIfa fehlenden Versionen sb.Append("with missing_versions as (\n"); sb.Append("select distinct \n".Indent(1)); foreach (var uk in childIfa.UniqueKeyAttributes.Where(a => childIfa.HistoryAttribute != a)) { sb.Append($"t2.{uk.Name},\n".Indent(2)); } sb.Append($"t2.{reference.Name} as {reference.ReferencedAttribute.Name},\n".Indent(2)); sb.Append($"t2.{childIfa.HistoryAttribute.Name}\n".Indent(2)); sb.Append($"from {childIfa.FullName} as t2\nleft outer join {parentIfa.FullName} as t1\n".Indent(1)); sb.Append($"on t2.{reference.Name} = t1.{reference.ReferencedAttribute.Name}\n".Indent(2)); if (childIfa.IsMandant && parentIfa.IsMandant) { sb.Append($"and t2.Mandant_KNZ = t1.Mandant_KNZ\n".Indent(2)); } sb.Append($"and coalesce(t1.{parentIfa.HistoryAttribute.Name}, 'NOW') = coalesce(t2.{childIfa.HistoryAttribute.Name}, 'NOW')\n".Indent(2)); sb.Append($"where t1.{parentIfa.PrimaryKeyAttributes.First().Name} is null\n".Indent(1)); sb.Append(")\n"); // Einfügen der fehlenden Versionen in die BL-Tabelle zu parrentIfa sb.Append($"insert into {parentIfa.FullName} (\n"); foreach (var attr in parentNonIdentityAttributes) { sb.Append(attr.Name.Indent(1)); if (attr != parentNonIdentityAttributes.Last()) { sb.Append(","); } sb.Append("\n"); } sb.Append(")\n"); sb.Append("select distinct \n"); foreach (var attr in parentNonIdentityAttributes.Where(a => !a.IsTechnicalAttribute)) { sb.Append($"t1.{attr.Name},\n".Indent(1)); } sb.Append("'I' as T_Modifikation,\n".Indent(1)); sb.Append($"cast('Cascaded for {reference.FullName}' as varchar(100)) as T_Bemerkung,\n".Indent(1)); sb.Append("SYSTEM_USER as T_Benutzer,\n".Indent(1)); sb.Append("'H' as T_System,\n".Indent(1)); sb.Append($"mv.{childIfa.HistoryAttribute.Name} as {parentIfa.HistoryAttribute.Name},\n".Indent(1)); sb.Append("GETDATE() as T_Erst_Dat,\nGETDATE() as T_Aend_Dat,\n".Indent(1)); sb.Append("t1.T_Ladelauf_NR\n".Indent(1)); sb.Append($"from {parentIfa.FullName} as t1\n"); sb.Append($"inner join missing_versions as mv\n"); var parentUkWithoutHistory = parentIfa.UniqueKeyAttributes.Where(a => parentIfa.HistoryAttribute != a); foreach (var uk in parentUkWithoutHistory) { if (uk == parentUkWithoutHistory.First()) { sb.Append($"on t1.{uk.Name} = mv.{uk.Name}\n".Indent(1)); } else { sb.Append($"and t1.{uk.Name} = mv.{uk.Name}\n".Indent(1)); } } sb.Append($"and coalesce(t1.{parentIfa.HistoryAttribute.Name}, '{max}') = (\n".Indent(1)); sb.Append($"select min(coalesce(z.{parentIfa.HistoryAttribute.Name}, '{max}'))\n".Indent(2)); sb.Append($"from {parentIfa.FullName} as z\n".Indent(2)); foreach (var uk in parentUkWithoutHistory) { if (uk == parentUkWithoutHistory.First()) { sb.Append($"where z.{uk.Name} = t1.{uk.Name}\n".Indent(2)); } else { sb.Append($"and z.{uk.Name} = t1.{uk.Name}\n".Indent(" ")); } } sb.Append($"and coalesce(z.{parentIfa.HistoryAttribute.Name}, '{max}') >= coalesce(mv.{parentIfa.HistoryAttribute.Name}, '{max}')\n".Indent(" ")); sb.Append(")\n".Indent(1)); sb.Append(";\n"); }