private SourceFragmentDefinition GetMasterTable(SourceFragmentDefinition sf, out List <SourceFragmentRefDefinition.Condition> conditions) { SourceConstraint fk = SourceView.GetSourceFields(sf) .Where(item => item.IsPK) .SelectMany(item => item.Constraints) .Single(item => item.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName); SourceFragmentDefinition m = null; conditions = new List <SourceFragmentRefDefinition.Condition>(); foreach (SourceReferences rel in SourceView.GetFKRelations(fk)) { if (m == null) { m = rel.PKField.SourceFragment; } conditions.Add(new SourceFragmentRefDefinition.Condition( rel.PKField.SourceFieldExpression, rel.FKField.SourceFieldExpression )); } return(m); }
private static bool CreateFKDefinition(IEnumerable <ScalarPropertyDefinition> fpk, SelfRelationTarget rt, SourceFragmentDefinition relSF, StringBuilder script, ISourceProvider provider, out FKDefinition f) { f = new FKDefinition() { cols = rt.FieldName, refCols = fpk.Select(item => item.SourceFieldExpression).ToArray(), refTbl = fpk.First().SourceFragment }; if (relSF != null) { SourceConstraint fk = relSF.Constraints.SingleOrDefault(item => item.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName && rt.FieldName.All(p => item.SourceFields.Any(pkf => pkf.SourceFieldExpression == p)) ); if (fk != null) { //if (!rt.FieldName.All(item => fk.SourceFields.Any(pkf => pkf.SourceFieldExpression == item))) // provider.GenerateDropConstraintScript(relSF, fk.ConstraintName, script); //else return(false); //f.constraintName = fk.ConstraintName; } } return(true); }
public void AddSourceFragment(SourceFragmentDefinition newsf) { if (GetSourceFragments().Any(item => item.Identifier == newsf.Identifier)) { throw new ArgumentException(string.Format("SourceFragment {0} already in collection", newsf.Identifier)); } _sourceFragments.Add(newsf); }
public override void GenerateAddColumnsScript(IEnumerable <PropDefinition> props, StringBuilder script, bool unicodeStrings) { SourceFragmentDefinition sf = props.First().Field.SourceFragment; script.AppendFormat("ALTER TABLE {0}.{1} ADD ", sf.Selector, sf.Name); GenerateColumns(props, script, unicodeStrings); script.Length -= 2; script.AppendLine().AppendLine(); }
public SourceFragmentDefinition GetOrCreateSourceFragment(string selector, string sourceName) { SourceFragmentDefinition sf = GetSourceFragments().FirstOrDefault(item => item.Selector == selector && item.Name == sourceName); if (sf == null) { sf = new SourceFragmentDefinition(selector + "." + sourceName, sourceName, selector); _sourceFragments.Add(sf); } return(sf); }
public void TestO2MSimilarRelations() { SourceView sv = new SourceView(); SourceFragmentDefinition sf1 = new SourceFragmentDefinition("tbl1", "tbl1", "dbo"); SourceFragmentDefinition sf2 = new SourceFragmentDefinition("tbl2", "tbl2", "dbo"); SourceFieldDefinition pkField = new SourceFieldDefinition(sf1, "id", "int") { IsNullable = false }; sv.SourceFields.Add(pkField); sv.SourceFields.Add(new SourceFieldDefinition(sf2, "id", "int") { IsNullable = false }); sv.SourceFields.Add(new SourceFieldDefinition(sf2, "prop1_id", "int")); sv.SourceFields.Add(new SourceFieldDefinition(sf2, "prop2_id", "int")); SourceConstraint pk = new SourceConstraint(SourceConstraint.PrimaryKeyConstraintTypeName, "pk1"); pk.SourceFields.Add(pkField); SourceConstraint pk2 = new SourceConstraint(SourceConstraint.PrimaryKeyConstraintTypeName, "pk2"); pk2.SourceFields.Add(sv.GetSourceFields(sf2).Single(item => item.SourceFieldExpression == "id")); SourceConstraint fk1 = new SourceConstraint(SourceConstraint.ForeignKeyConstraintTypeName, "fk1"); fk1.SourceFields.Add(sv.GetSourceFields(sf2).Single(item => item.SourceFieldExpression == "prop1_id")); SourceConstraint fk2 = new SourceConstraint(SourceConstraint.ForeignKeyConstraintTypeName, "fk2"); fk2.SourceFields.Add(sv.GetSourceFields(sf2).Single(item => item.SourceFieldExpression == "prop2_id")); sf1.Constraints.Add(pk); sf2.Constraints.Add(pk2); sf2.Constraints.Add(fk1); sf2.Constraints.Add(fk2); sv.References.Add(new SourceReferences(pk, fk1, pkField, sv.GetSourceFields(sf2).Single(item => item.SourceFieldExpression == "prop1_id") )); sv.References.Add(new SourceReferences(pk, fk2, pkField, sv.GetSourceFields(sf2).Single(item => item.SourceFieldExpression == "prop2_id") )); WXMLModel model = new WXMLModel(); SourceToModelConnector c = new SourceToModelConnector(sv, model); c.ApplySourceViewToModel(); }
private SourceFragmentDefinition GetSourceFragment(SourceFragmentDefinition sf) { var t = Model.GetSourceFragment(sf.Identifier); if (t == null) { t = sf; Model.AddSourceFragment(t); } return(t); }
public override void GenerateAddColumnsScript(IEnumerable <SourceFieldDefinition> props, StringBuilder script, bool unicodeStrings) { SourceFragmentDefinition sf = props.First().SourceFragment; script.AppendFormat("ALTER TABLE {0}.{1} ADD ", sf.Selector, sf.Name); GenerateColumns(props.Select((item) => new ColDef() { Field = item, type = GetType(item, null, default(Field2DbRelations), unicodeStrings) }), script, unicodeStrings); script.Length -= 2; script.AppendLine().AppendLine(); }
public void TestCreateTable() { var p = new MSSQLProvider(); var sf = new SourceFragmentDefinition("sdfdsf", "tbl", "dbo"); StringBuilder script = new StringBuilder(); p.GenerateCreateScript(new[] { new ScalarPropertyDefinition(null, "Prop", "Prop", Field2DbRelations.None, null, new TypeDefinition("dfg", typeof(int)), new SourceFieldDefinition(sf, "col1"), AccessLevel.Private, AccessLevel.Public) }, script, false); Assert.AreEqual(string.Format("CREATE TABLE dbo.tbl(col1 int NULL);{0}{0}", Environment.NewLine), script.ToString()); }
public override void GenerateCreateIndexScript(SourceFragmentDefinition table, IndexDefinition index, StringBuilder script) { if (index.cols.Count() > 0) { script.AppendFormat("CREATE INDEX {0} ON {1}.{2}(", index.indexName, table.Selector, table.Name); foreach (string c in index.cols) { script.Append(c).Append(", "); } script.Length -= 2; script.AppendLine(");"); script.AppendLine(); } }
public override void GenerateCreateFKsScript(SourceFragmentDefinition sf, IEnumerable <FKDefinition> fks, StringBuilder script) { script.AppendFormat("ALTER TABLE {0}.{1} ADD ", sf.Selector, sf.Name); foreach (FKDefinition fk in fks) { script.AppendFormat("CONSTRAINT {0} FOREIGN KEY({1}) REFERENCES {2}.{3}({4})", fk.constraintName, string.Join(",", fk.cols), fk.refTbl.Selector, fk.refTbl.Name, string.Join(",", fk.refCols) ).Append(", "); } script.Length -= 2; script.AppendLine(";"); script.AppendLine(); }
public override void GenerateCreatePKScript(IEnumerable <SourceFieldDefinition> pks, string constraintName, StringBuilder script, bool pk, bool clustered) { SourceFragmentDefinition sf = pks.First().SourceFragment; script.AppendFormat("ALTER TABLE {0}.{1} ADD CONSTRAINT {2} {3} {4}(", sf.Selector, sf.Name, constraintName, pk?"PRIMARY KEY":"UNIQUE", clustered?"CLUSTERED":"NONCLUSTERED"); foreach (SourceFieldDefinition sp in pks) { script.Append(sp.SourceFieldExpression).Append(", "); } script.Length -= 2; script.AppendLine(");"); script.AppendLine(); }
private void AlterTables(StringBuilder script, IEnumerable <PropertyDefinition> props, ISourceProvider provider, bool unicodeStrings) { bool hdr = false; foreach (SourceFragmentDefinition s in props.Select(item => item.SourceFragment).Distinct()) { SourceFragmentDefinition sf = s; var targetSF = SourceView.GetSourceFragments().SingleOrDefault(item => item.Name == sf.Name && item.Selector == sf.Selector); if (targetSF != null) { List <PropDefinition> tableProps = new List <PropDefinition>(); foreach (PropertyDefinition prop in props.Where(item => item.SourceFragment == sf)) { if (prop is ScalarPropertyDefinition) { tableProps.Add(new PropDefinition { Attr = prop.Attributes, Field = ((ScalarPropertyDefinition)prop).SourceField, PropType = prop.PropertyType }); } else { tableProps.AddRange(((EntityPropertyDefinition)prop).SourceFields .Select(item => new PropDefinition { Attr = Field2DbRelations.None, Field = item, PropType = prop.PropertyType.Entity.GetProperties().Single(p => p.PropertyAlias == item.PropertyAlias).PropertyType })); } } var props2Add = tableProps.Where(item => !SourceView.GetSourceFields(targetSF).Any(fld => fld.SourceFieldExpression == item.Field.SourceFieldExpression)); if (props2Add.Count() > 0) { if (!hdr) { script.AppendLine("--Altering tables"); hdr = true; } provider.GenerateAddColumnsScript(props2Add, script, unicodeStrings); } } } }
private void DropConstraints(StringBuilder script, ISourceProvider provider) { var uks = Model.GetActiveEntities().SelectMany(e => e.GetActiveProperties()) .Where(item => item.SourceFragment != null && item.SourceFragment.Constraints.Any(cns => cns.ConstraintType != SourceConstraint.ForeignKeyConstraintTypeName)); if (uks.Count() == 0) { return; } bool hdr = false; foreach (SourceFragmentDefinition s in SourceView.GetSourceFragments()) { var targetSF = s; SourceFragmentDefinition sf = uks.Select(item => item.SourceFragment).Distinct() .SingleOrDefault(item => item.Name == s.Name && item.Selector == s.Selector); if (sf != null) { foreach (SourceConstraint constraint in targetSF.Constraints.Where(item => item.ConstraintType != SourceConstraint.ForeignKeyConstraintTypeName)) { if (!sf.Constraints.Any(item => item.SourceFields.Count == constraint.SourceFields.Count && item.ConstraintType == constraint.ConstraintType && constraint.SourceFields.All(fld => item.SourceFields.Any(sfld => sfld.SourceFieldExpression == fld.SourceFieldExpression)))) { if (!hdr) { script.AppendLine("--Drop constraints"); hdr = true; } provider.GenerateDropConstraintScript(targetSF, constraint.ConstraintName, script); } } } } }
private void MergeTables(WXMLModel mergeWith) { foreach (SourceFragmentDefinition newsf in mergeWith.GetSourceFragments()) { string newsfIdentifier = newsf.Identifier; SourceFragmentDefinition sf = GetSourceFragments().SingleOrDefault(item => item.Identifier == newsfIdentifier); if (sf != null) { if (!string.IsNullOrEmpty(newsf.Name)) { sf.Name = newsf.Name; } if (!string.IsNullOrEmpty(newsf.Selector)) { sf.Selector = newsf.Selector; } } else { AddSourceFragment(newsf); } } }
private void CreateTables(StringBuilder script, IEnumerable <PropertyDefinition> props, ISourceProvider provider, bool unicodeStrings) { bool hdr = false; foreach (SourceFragmentDefinition s in props.Select(item => item.SourceFragment).Distinct()) { SourceFragmentDefinition sf = s; var targetSF = SourceView.GetSourceFragments().SingleOrDefault(item => item.Name == sf.Name && item.Selector == sf.Selector); if (targetSF == null) { if (!hdr) { script.AppendLine("--Creating tables"); hdr = true; } provider.GenerateCreateScript(props.Where(item => item.SourceFragment == sf), script, unicodeStrings); } } foreach (RelationDefinitionBase rel in Model.GetActiveRelations()) { if (!SourceView.GetSourceFragments().Any(item => item.Name == rel.SourceFragment.Name && item.Selector == rel.SourceFragment.Selector)) { if (!hdr) { script.AppendLine("--Creating tables"); hdr = true; } provider.GenerateCreateScript(rel, script, unicodeStrings); } } }
private EntityDefinition GetEntity(SourceFragmentDefinition sf, out bool created, bool capitalizeNames) { created = false; string entityId = GetEntityIdentifier(sf.Selector, sf.Name); EntityDefinition e = Model.GetEntity(entityId); if (e == null) { string ename = GetName(sf.Name); if (capitalizeNames) { ename = Capitalize(ename); } e = new EntityDefinition(entityId, ename, string.Empty, string.Format("Auto generated from {0}", string.IsNullOrEmpty(sf.Selector) ? sf.Name : $"{sf.Selector}.{sf.Name}"), Model); var t = new SourceFragmentRefDefinition(GetSourceFragment(sf)); e.AddSourceFragment(t); //odef.AddEntity(e); created = true; } return(e); }
public abstract void GenerateDropTableScript(SourceFragmentDefinition table, StringBuilder script);
public abstract void GenerateDropIndexScript(SourceFragmentDefinition table, string indexName, StringBuilder script);
public abstract void GenerateCreateIndexScript(SourceFragmentDefinition table, IndexDefinition indexes, StringBuilder script);
private void CreatePrimaryKeys(StringBuilder script, ISourceProvider provider) { var pks = Model.GetActiveEntities().SelectMany(item => item.GetPkProperties()); var rels = Model.GetActiveRelations().Where(item => item.Constraint != RelationConstraint.None); if (pks.Count() == 0 && rels.Count() == 0) { return; } bool hdr = false; foreach (SourceFragmentDefinition s in pks.Select(item => item.SourceFragment).Distinct()) { SourceFragmentDefinition sf = s; var targetSF = SourceView.GetSourceFragments().SingleOrDefault(item => item.Name == sf.Name && item.Selector == sf.Selector); var tablePKs = pks.Where(item => item.SourceFragment == sf); //string constraintName = null; const bool isPK = true; if (targetSF != null) { SourceConstraint pk = targetSF.Constraints.SingleOrDefault(item => item.ConstraintType == SourceConstraint.PrimaryKeyConstraintTypeName); //if (pk == null) //{ // isPK = false; // pk = targetSF.Constraints.SingleOrDefault(item => item.ConstraintType == SourceConstraint.UniqueConstraintTypeName); //} if (pk != null) { if (tablePKs.All(item => pk.SourceFields.Any(pkf => pkf.SourceFieldExpression == item.SourceFieldExpression))) { continue; } } //else // isPK = true; } //if (string.IsNullOrEmpty(constraintName)) //{ // constraintName = "PK_" + sf.Name.Trim(']', '['); //} if (!hdr) { script.AppendLine("--Creating primary keys"); hdr = true; } provider.GenerateCreatePKScript(tablePKs.Select(item => new PropDefinition { Field = item.SourceField, Attr = item.Attributes, PropType = item.PropertyType }), "PK_" + sf.Name.Trim(']', '['), script, isPK, true); } foreach (RelationDefinitionBase rel in rels) { var targetSF = SourceView.GetSourceFragments().SingleOrDefault(item => item.Name == rel.SourceFragment.Name && item.Selector == rel.SourceFragment.Selector); bool isPK = rel.Constraint == RelationConstraint.PrimaryKey; if (targetSF != null) { if (isPK) { SourceConstraint pk = targetSF.Constraints.SingleOrDefault(item => item.ConstraintType == SourceConstraint.PrimaryKeyConstraintTypeName); if (pk != null) { if (rel.Left.FieldName.Union(rel.Right.FieldName).All(item => pk.SourceFields.Any(pkf => pkf.SourceFieldExpression == item))) { continue; } } } else { if (targetSF.Constraints.Any(item => item.ConstraintType == SourceConstraint.UniqueConstraintTypeName && rel.Left.FieldName.Union(rel.Right.FieldName).All(fld => item.SourceFields.Any(pkf => pkf.SourceFieldExpression == fld)) )) { continue; } } } if (!hdr) { script.AppendLine("--Creating primary keys"); hdr = true; } provider.GenerateCreatePKScript(rel.Left.FieldName.Union(rel.Right.FieldName) .Select(item => new PropDefinition { Field = new SourceFieldDefinition(rel.SourceFragment, item) }) , isPK?"PK_":"UQ_" + rel.SourceFragment.Name.Trim(']', '['), script, isPK, true); } }
protected EntityPropertyDefinition AppendFK(EntityDefinition e, SourceFragmentDefinition sf, SourceConstraint fk, relation1to1 rb, out bool created, bool transforRawNamesToReadableForm, bool capitalizeNames, bool caseSensitive) { created = false; var rels = SourceView.GetFKRelations(fk); SourceFragmentDefinition m = rels.First().PKField.SourceFragment; EntityDefinition re = e; if (sf != m) { re = GetEntity(m, rb, transforRawNamesToReadableForm, capitalizeNames, caseSensitive); } string rid = "t" + re.Name; TypeDefinition td = Model.GetType(rid, false); if (td == null) { td = new TypeDefinition(rid, re); Model.AddType(td); } string propAlias = td.Entity.Name; if (rels.Count() == 1) { propAlias = Trim(GetName(rels.First().FKField.SourceFieldExpression), true); } if (capitalizeNames) { propAlias = Capitalize(propAlias); } string propName = propAlias; EntityPropertyDefinition ep = null; //try //{ ep = (EntityPropertyDefinition)e.OwnProperties .SingleOrDefault(item => item.Identifier == propAlias); if (ep == null) { ep = e.OwnProperties.OfType <EntityPropertyDefinition>().SingleOrDefault(item => fk.SourceFields.All(sf2 => item.SourceFields.Any(sff => sf2.SourceFieldExpression.Trim('[', ']') == sff.SourceFieldExpression.Trim('[', ']')) )); } if (ep == null) { var one2one = fk.SourceFields.All(sss => sss.IsPK); if (one2one && e.BaseEntity == re) { foreach (var sfd_ in fk.SourceFields) { bool x; var xx = AppendColumn(e, sfd_, out x, transforRawNamesToReadableForm, capitalizeNames, caseSensitive); RaiseOnPropertyCreated(xx, x); } return(null); } int cnt = e.OwnProperties.Count(p => p.Name == propName); if (cnt > 0) { propName = propName + cnt; propAlias = propAlias + cnt; } SourceFragmentDefinition sfd = GetSourceFragment(sf); ep = new EntityPropertyDefinition(propName, propAlias, Field2DbRelations.None, "Auto generated from constraint " + fk.ConstraintName, AccessLevel.Private, AccessLevel.Public, td, sfd, e); e.AddProperty(ep); created = true; foreach (SourceReferences rel in rels) { SourceFieldDefinition fld = SourceView.GetSourceFields(sf).Single(item => item.SourceFieldExpression == rel.FKField.SourceFieldExpression); ScalarPropertyDefinition pk = re.GetPkProperties().SingleOrDefault(item => item.SourceFieldExpression == rel.PKField.SourceFieldExpression); if (pk == null) { if (rel.PKConstraint.ConstraintType != SourceConstraint.UniqueConstraintTypeName) { throw new WXMLException(string.Format("Cannot find pk for constraint {0}", rel.PKConstraint.ConstraintName)); } pk = re.GetProperties().OfType <ScalarPropertyDefinition>().Single(item => item.SourceFieldExpression == rel.PKField.SourceFieldExpression); } ep.AddSourceField(pk.PropertyAlias, fld.SourceFieldExpression, null, fld.SourceType, fld.SourceTypeSize, fld.IsNullable, fld.DefaultValue ); } } else { if (ep.Description == "Auto generated from constraint " + fk.ConstraintName) { ep.PropertyType = td; } } foreach (SourceFieldDefinition pkField in SourceView.GetFKRelations(fk) .Select(item => item.FKField) .Where(item => item.IsPK)) { string pkPropAlias = GetName(pkField.SourceFieldExpression); if (!SourceView.GetSourceFields(pkField.SourceFragment).Any(item => GetName(item.SourceFieldExpression).Equals(Trim(pkPropAlias, transforRawNamesToReadableForm), StringComparison.InvariantCultureIgnoreCase))) { pkPropAlias = Trim(pkPropAlias, transforRawNamesToReadableForm); } //string pkPropAlias = Trim(GetName(pkField.SourceFieldExpression), transforRawNamesToReadableForm); if (capitalizeNames) { pkPropAlias = Capitalize(pkPropAlias); } string pkPropName = pkPropAlias; PropertyDefinition pe = e.OwnProperties .SingleOrDefault(pd => pd.Identifier == pkPropAlias); Field2DbRelations attrs = pkField.GetAttributes(); TypeDefinition pkType = GetClrType(pkField.SourceType, pkField.IsNullable, ref attrs); bool pkCreated = pe == null; if (pkCreated) { int cnt = e.OwnProperties.Count(p => p.Name == pkPropName); if (cnt > 0) { pkPropName = pkPropName + cnt; //pkPropAlias = pkPropAlias + cnt; } pe = new ScalarPropertyDefinition(e, pkPropName, pkPropAlias, attrs, "Auto generated from column " + pkField.SourceFieldExpression, pkType, pkField, AccessLevel.Private, AccessLevel.Public); e.AddProperty(pe); } else { l1: if (pe is ScalarPropertyDefinition) { pe.Attributes |= attrs; pe.PropertyType = pkType; ((ScalarPropertyDefinition)pe).SourceField = pkField; } else { int cnt = e.OwnProperties.Count(p => p.Identifier == pkPropAlias); if (cnt > 0) { if (e.OwnProperties.Any(item => item.Identifier == GetName(pkField.SourceFieldExpression))) { pkPropAlias = pkPropAlias + cnt; } else { pkPropAlias = GetName(pkField.SourceFieldExpression); } } pkPropName = pkPropAlias; pe = e.OwnProperties.SingleOrDefault(item => item.PropertyAlias == pkPropAlias); if (pe != null) { if (pe is EntityPropertyDefinition) { throw new WXMLParserException(string.Format("Property {0} expected to be of type ScalarPropertyDefinition", pe.Identifier)); } goto l1; } pe = new ScalarPropertyDefinition(e, pkPropName, pkPropAlias, attrs, "Auto generated from column " + pkField.SourceFieldExpression, pkType, pkField, AccessLevel.Private, AccessLevel.Public); e.AddProperty(pe); } } RaiseOnPropertyCreated(pe, pkCreated); } //} //catch //{ // int i = 10; //} return(ep); }
public static SourceView CreateComplexSourceView() { SourceView sv = new SourceView(); SourceFragmentDefinition sf1 = new SourceFragmentDefinition("tbl1", "tbl1", "dbo"); SourceFragmentDefinition sf2 = new SourceFragmentDefinition("tbl2", "tbl2", "dbo"); SourceFragmentDefinition sf3 = new SourceFragmentDefinition("tbl3", "tbl3", "dbo"); SourceFragmentDefinition sf4 = new SourceFragmentDefinition("tbl4", "tbl4", "dbo"); SourceFragmentDefinition sf5 = new SourceFragmentDefinition("tbl5", "tbl5", "dbo"); SourceFragmentDefinition sf6 = new SourceFragmentDefinition("tbl6", "tbl6", "dbo"); SourceFieldDefinition pkField = new SourceFieldDefinition(sf1, "id", "int") { IsNullable = false }; sv.SourceFields.Add(pkField); SourceFieldDefinition pkField2 = new SourceFieldDefinition(sf2, "id", "int") { IsNullable = false }; sv.SourceFields.Add(pkField2); sv.SourceFields.Add(new SourceFieldDefinition(sf3, "prop1_id", "int")); sv.SourceFields.Add(new SourceFieldDefinition(sf3, "prop2_id", "int")); sv.SourceFields.Add(new SourceFieldDefinition(sf4, "prop1_id", "int")); sv.SourceFields.Add(new SourceFieldDefinition(sf4, "prop2_id", "int")); sv.SourceFields.Add(new SourceFieldDefinition(sf5, "prop1_id", "int")); sv.SourceFields.Add(new SourceFieldDefinition(sf5, "prop2_id", "int")); sv.SourceFields.Add(new SourceFieldDefinition(sf6, "prop1_id", "int")); sv.SourceFields.Add(new SourceFieldDefinition(sf6, "prop2_id", "int")); SourceConstraint pk = new SourceConstraint(SourceConstraint.PrimaryKeyConstraintTypeName, "pk1"); pk.SourceFields.Add(pkField); sf1.Constraints.Add(pk); SourceConstraint pk2 = new SourceConstraint(SourceConstraint.PrimaryKeyConstraintTypeName, "pk2"); pk2.SourceFields.Add(pkField2); sf2.Constraints.Add(pk2); { SourceConstraint fk1 = new SourceConstraint(SourceConstraint.ForeignKeyConstraintTypeName, "tbl3_fk1"); fk1.SourceFields.Add(sv.GetSourceFields(sf3).Single(item => item.SourceFieldExpression == "prop1_id")); SourceConstraint fk2 = new SourceConstraint(SourceConstraint.ForeignKeyConstraintTypeName, "tbl3_fk2"); fk2.SourceFields.Add(sv.GetSourceFields(sf3).Single(item => item.SourceFieldExpression == "prop2_id")); sf3.Constraints.Add(fk1); sf3.Constraints.Add(fk2); sv.References.Add(new SourceReferences(pk, fk1, pkField, sv.GetSourceFields(sf3).Single(item => item.SourceFieldExpression == "prop1_id") )); sv.References.Add(new SourceReferences(pk2, fk2, pkField2, sv.GetSourceFields(sf3).Single(item => item.SourceFieldExpression == "prop2_id") )); } { SourceConstraint fk1 = new SourceConstraint(SourceConstraint.ForeignKeyConstraintTypeName, "tbl4_fk1"); fk1.SourceFields.Add(sv.GetSourceFields(sf4).Single(item => item.SourceFieldExpression == "prop1_id")); SourceConstraint fk2 = new SourceConstraint(SourceConstraint.ForeignKeyConstraintTypeName, "tbl4_fk2"); fk2.SourceFields.Add(sv.GetSourceFields(sf4).Single(item => item.SourceFieldExpression == "prop2_id")); sf4.Constraints.Add(fk1); sf4.Constraints.Add(fk2); sv.References.Add(new SourceReferences(pk, fk1, pkField, sv.GetSourceFields(sf4).Single(item => item.SourceFieldExpression == "prop1_id") )); sv.References.Add(new SourceReferences(pk2, fk2, pkField2, sv.GetSourceFields(sf4).Single(item => item.SourceFieldExpression == "prop2_id") )); } { SourceConstraint fk1 = new SourceConstraint(SourceConstraint.ForeignKeyConstraintTypeName, "tbl5_fk1"); fk1.SourceFields.Add(sv.GetSourceFields(sf5).Single(item => item.SourceFieldExpression == "prop1_id")); SourceConstraint fk2 = new SourceConstraint(SourceConstraint.ForeignKeyConstraintTypeName, "tbl5_fk2"); fk2.SourceFields.Add(sv.GetSourceFields(sf5).Single(item => item.SourceFieldExpression == "prop2_id")); sf5.Constraints.Add(fk1); sf5.Constraints.Add(fk2); sv.References.Add(new SourceReferences(pk, fk1, pkField, sv.GetSourceFields(sf5).Single(item => item.SourceFieldExpression == "prop1_id") )); sv.References.Add(new SourceReferences(pk, fk2, pkField, sv.GetSourceFields(sf5).Single(item => item.SourceFieldExpression == "prop2_id") )); } { SourceConstraint fk1 = new SourceConstraint(SourceConstraint.ForeignKeyConstraintTypeName, "tbl6_fk1"); fk1.SourceFields.Add(sv.GetSourceFields(sf6).Single(item => item.SourceFieldExpression == "prop1_id")); SourceConstraint fk2 = new SourceConstraint(SourceConstraint.ForeignKeyConstraintTypeName, "tbl6_fk2"); fk2.SourceFields.Add(sv.GetSourceFields(sf6).Single(item => item.SourceFieldExpression == "prop2_id")); sf6.Constraints.Add(fk1); sf6.Constraints.Add(fk2); sv.References.Add(new SourceReferences(pk, fk1, pkField, sv.GetSourceFields(sf6).Single(item => item.SourceFieldExpression == "prop1_id") )); sv.References.Add(new SourceReferences(pk, fk2, pkField, sv.GetSourceFields(sf6).Single(item => item.SourceFieldExpression == "prop2_id") )); } return(sv); }
//private readonly LinkTarget _left; //private readonly LinkTarget _right; //private readonly TableDescription _table; //private readonly EntityDescription _underlyingEntity; //private bool _disabled; public RelationDefinition(LinkTarget left, LinkTarget right, SourceFragmentDefinition table, EntityDefinition underlyingEntity) : this(left, right, table, underlyingEntity, false) { }
private EntityDefinition GetEntity(SourceFragmentDefinition sf, relation1to1 rb, bool transforRawNamesToReadableForm, bool capitalizeNames, bool caseSensitive) { var entityIdentifier = GetEntityIdentifier(sf.Selector, sf.Name); EntityDefinition e; if (!_entities2skip.TryGetValue(entityIdentifier, out e)) { EntityDefinition masterEntity = null; SourceFragmentDefinition masterTable = null; List <SourceFragmentRefDefinition.Condition> conds = null; if (rb != relation1to1.Default && SourceView.GetSourceFields(sf) .Where(item => item.IsPK) .SelectMany(item => item.Constraints) .Count(item => item.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName) == 1 && SourceView.GetSourceFields(sf) .Where(item => item.IsPK) .All(item => item.IsFK) ) { switch (rb) { case relation1to1.Unify: case relation1to1.Hierarchy: masterTable = GetMasterTable(sf, out conds); masterEntity = GetEntity(masterTable, rb, transforRawNamesToReadableForm, capitalizeNames, caseSensitive); break; default: throw new NotSupportedException(rb.ToString()); } } e = Model.GetEntity(entityIdentifier); if (e == null) { bool entCreated; e = GetEntity(sf, out entCreated, capitalizeNames); if (entCreated) { RaiseOnEntityCreated(e); } } _tables2skip.Add(sf); _entities2skip.Add(entityIdentifier, e); foreach (SourceFieldDefinition field in SourceView.GetSourceFields(sf) .Where(item => !item.IsFK)) { bool propCreated; PropertyDefinition prop = AppendColumn(e, field, out propCreated, transforRawNamesToReadableForm, capitalizeNames, caseSensitive); RaiseOnPropertyCreated(prop, propCreated); } foreach (SourceConstraint fk in sf.Constraints.Where(item => item.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName)) { bool propCreated; PropertyDefinition prop = AppendFK(e, sf, fk, rb, out propCreated, transforRawNamesToReadableForm, capitalizeNames, caseSensitive); if (prop != null) { RaiseOnPropertyCreated(prop, propCreated); } } if (masterEntity != null) { SourceFragmentRefDefinition sfr; switch (rb) { case relation1to1.Unify: sfr = masterEntity.GetSourceFragments() .Single(item => item.Identifier == masterTable.Identifier); sfr.AnchorTable = sf; sfr.JoinType = SourceFragmentRefDefinition.JoinTypeEnum.outer; sfr.Conditions.AddRange(conds); masterEntity.AddSourceFragment(new SourceFragmentRefDefinition(sf)); foreach (PropertyDefinition property in e.GetProperties() .Where(item => !item.HasAttribute(Field2DbRelations.PK) /* && * item.SourceFragment != masterTable*/)) { if (masterEntity.GetProperties().Any(item => item.PropertyAlias == property.PropertyAlias)) { property.PropertyAlias = e.Name + "_" + property.PropertyAlias; property.Name = e.Name + "_" + property.Name; } if (!masterEntity.GetProperties().Any(item => item.PropertyAlias == property.PropertyAlias)) { masterEntity.AddProperty(property); } } Model.RemoveEntity(e); break; case relation1to1.Hierarchy: sfr = e.GetSourceFragments().Single(); sfr.AnchorTable = masterTable; sfr.JoinType = SourceFragmentRefDefinition.JoinTypeEnum.inner; foreach (SourceFragmentRefDefinition.Condition cond in conds) { sfr.Conditions.Add(new SourceFragmentRefDefinition.Condition(cond.RightColumn, cond.LeftColumn)); } e.BaseEntity = masterEntity; e.InheritsBaseTables = true; break; } } } return(e); }
public abstract void GenerateDropConstraintScript(SourceFragmentDefinition table, string constraintName, StringBuilder script);
protected void ProcessMany2Many() { foreach (SourceFragmentDefinition sf in SourceView.GetSourceFragments() .Where(item => SourceView.GetSourceFields(item).All(clm => clm.IsFK) && item.Constraints.Count(citem => citem.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName) == 2)) { List <LinkTarget> targets = new List <LinkTarget>(); foreach (SourceConstraint fk in sf.Constraints.Where(item => item.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName)) { var rels = SourceView.GetFKRelations(fk); SourceFragmentDefinition m = rels.First().PKField.SourceFragment; EntityDefinition e = Model.GetEntity(GetEntityIdentifier(m.Selector, m.Name)); LinkTarget lt = new LinkTarget( e, rels.Select(item => item.FKField.SourceFieldExpression).ToArray(), rels.Select(item => e.GetPkProperties().Single(p => p.SourceFieldExpression == item.PKField.SourceFieldExpression).PropertyAlias).ToArray(), rels.First().DeleteAction == SourceConstraint.CascadeAction ); targets.Add(lt); } if (targets.Count != 2) { continue; } if (targets[0].Entity.Name == targets[1].Entity.Name) { LinkTarget t = targets[0]; SelfRelationDefinition newRel = new SelfRelationDefinition( t.Entity, t.EntityProperties, targets[0], targets[1], GetSourceFragment(sf), null); if (sf.Constraints.Any(item => item.ConstraintType == SourceConstraint.PrimaryKeyConstraintTypeName)) { newRel.Constraint = RelationConstraint.PrimaryKey; } else if (sf.Constraints.Any(item => item.ConstraintType == SourceConstraint.UniqueConstraintTypeName)) { newRel.Constraint = RelationConstraint.Unique; } if (Model.GetSimilarRelation(newRel) == null) { string postFix = string.Empty; if (string.IsNullOrEmpty(newRel.Left.AccessorName)) { if (newRel.Left.FieldName.Length == 1) { if (newRel.Left.FieldName[0].EndsWith("_id", StringComparison.InvariantCultureIgnoreCase)) { newRel.Left.AccessorName = newRel.Left.FieldName[0] .Substring(0, newRel.Left.FieldName[0].Length - 3); } else if (newRel.Left.FieldName[0].EndsWith("id", StringComparison.InvariantCultureIgnoreCase)) { newRel.Left.AccessorName = newRel.Left.FieldName[0] .Substring(0, newRel.Left.FieldName[0].Length - 2); } } if (string.IsNullOrEmpty(newRel.Left.AccessorName)) { newRel.Left.AccessorName = newRel.Entity.Name; postFix = "1"; } } if (string.IsNullOrEmpty(newRel.Right.AccessorName)) { if (newRel.Left.FieldName.Length == 1) { if (newRel.Right.FieldName[0].EndsWith("_id", StringComparison.InvariantCultureIgnoreCase)) { newRel.Right.AccessorName = newRel.Right.FieldName[0] .Substring(0, newRel.Right.FieldName[0].Length - 3); } else if (newRel.Right.FieldName[0].EndsWith("id", StringComparison.InvariantCultureIgnoreCase)) { newRel.Right.AccessorName = newRel.Right.FieldName[0] .Substring(0, newRel.Right.FieldName[0].Length - 2); } } if (string.IsNullOrEmpty(newRel.Right.AccessorName)) { newRel.Right.AccessorName = newRel.Entity.Name + postFix; } } Model.AddRelation(newRel); } } else { RelationDefinition newRel = new RelationDefinition( targets[0], targets[1], GetSourceFragment(sf), null); if (sf.Constraints.Any(item => item.ConstraintType == SourceConstraint.PrimaryKeyConstraintTypeName)) { newRel.Constraint = RelationConstraint.PrimaryKey; } else if (sf.Constraints.Any(item => item.ConstraintType == SourceConstraint.UniqueConstraintTypeName)) { newRel.Constraint = RelationConstraint.Unique; } if (!Model.GetRelations().OfType <RelationDefinition>().Any(m => m.Equals(newRel))) { if (Model.HasSimilarRelationM2M(newRel)) { if (string.IsNullOrEmpty(newRel.Left.AccessorName) || string.IsNullOrEmpty(newRel.Right.AccessorName)) { var lst = from r in Model.GetRelations().OfType <RelationDefinition>() where !ReferenceEquals(r.Left, newRel.Left) && !ReferenceEquals(r.Right, newRel.Right) && ( ((r.Left.Entity == newRel.Left.Entity && string.IsNullOrEmpty(r.Right.AccessorName)) && (r.Right.Entity == newRel.Right.Entity && string.IsNullOrEmpty(r.Left.AccessorName))) || ((r.Left.Entity == newRel.Right.Entity && string.IsNullOrEmpty(r.Right.AccessorName)) && (r.Right.Entity == newRel.Left.Entity && string.IsNullOrEmpty(r.Left.AccessorName))) ) select r; if (lst.Count() > 0) { foreach (RelationDefinition r in lst) { if (string.IsNullOrEmpty(r.Left.AccessorName)) { r.Left.AccessorName = r.SourceFragment.Name.TrimEnd(']').TrimStart('[') + r.Right.Entity.Name; } if (string.IsNullOrEmpty(r.Right.AccessorName)) { r.Right.AccessorName = r.SourceFragment.Name.TrimEnd(']').TrimStart('[') + r.Left.Entity.Name; } } if (string.IsNullOrEmpty(newRel.Left.AccessorName)) { newRel.Left.AccessorName = newRel.SourceFragment.Name.TrimEnd(']').TrimStart('[') + newRel.Right.Entity.Name; } if (string.IsNullOrEmpty(newRel.Right.AccessorName)) { newRel.Right.AccessorName = newRel.SourceFragment.Name.TrimEnd(']').TrimStart('[') + newRel.Left.Entity.Name; } } } } Model.AddRelation(newRel); } } } foreach (SelfRelationDefinition rdb in Model.GetActiveRelations().OfType <SelfRelationDefinition>()) { NormalizeRelationAccessors(rdb, rdb.Right.AccessorName, rdb.Entity); NormalizeRelationAccessors(rdb, rdb.Left.AccessorName, rdb.Entity); } foreach (RelationDefinition rdb in Model.GetActiveRelations().OfType <RelationDefinition>()) { NormalizeRelationAccessors(rdb, rdb.Right.AccessorName, rdb.Right.Entity); NormalizeRelationAccessors(rdb, rdb.Left.AccessorName, rdb.Left.Entity); } }
public abstract void GenerateCreateFKsScript(SourceFragmentDefinition table, IEnumerable <FKDefinition> fks, StringBuilder script);
public RelationDefinition(LinkTarget left, LinkTarget right, SourceFragmentDefinition table, EntityDefinition underlyingEntity, bool disabled) : base(table, underlyingEntity, left, right, disabled) { //_left = left; //_right = right; }
private void CreateUniqueConstraints(StringBuilder script, ISourceProvider provider) { var uks = Model.GetActiveEntities().SelectMany(e => e.GetActiveProperties()) .Where(item => item.SourceFragment != null && item.SourceFragment.Constraints.Any(cns => cns.ConstraintType == SourceConstraint.UniqueConstraintTypeName)); if (uks.Count() == 0) { return; } bool hdr = false; foreach (SourceFragmentDefinition s in uks.Select(item => item.SourceFragment).Distinct()) { SourceFragmentDefinition sf = s; var targetSF = SourceView.GetSourceFragments().SingleOrDefault(item => item.Name == sf.Name && item.Selector == sf.Selector); const bool isPK = false; List <string> names = new List <string>(); foreach (SourceConstraint constraint in s.Constraints.Where(item => item.ConstraintType == SourceConstraint.UniqueConstraintTypeName)) { if (targetSF == null || !targetSF.Constraints.Any(item => constraint.ConstraintType == item.ConstraintType && constraint.SourceFields.All(fld => item.SourceFields.Any(sfld => sfld.SourceFieldExpression == fld.SourceFieldExpression)))) { var tablePKs = uks.Where(item => item.SourceFragment == sf); List <PropDefinition> tableProps = new List <PropDefinition>(); foreach (PropertyDefinition prop in tablePKs) { if (prop is ScalarPropertyDefinition) { ScalarPropertyDefinition p = prop as ScalarPropertyDefinition; if (constraint.SourceFields.Any(item => item.SourceFieldExpression == p.SourceFieldExpression)) { tableProps.Add(new PropDefinition { Attr = prop.Attributes, Field = p.SourceField, PropType = prop.PropertyType }); } } else { EntityPropertyDefinition p = prop as EntityPropertyDefinition; foreach (EntityPropertyDefinition.SourceField field in p.SourceFields) { if (constraint.SourceFields.Any(item => item.SourceFieldExpression == field.SourceFieldExpression)) { tableProps.Add(new PropDefinition { Attr = Field2DbRelations.None, Field = field, PropType = prop.PropertyType.Entity.GetProperties().Single(pr => pr.PropertyAlias == field.PropertyAlias).PropertyType } ); } } } } string constraintName = "UK_" + sf.Name.Trim(']', '['); if (names.Contains(constraintName)) { constraintName += "_" + names.Count(item => item.StartsWith(constraintName)); } if (!hdr) { script.AppendLine("--Creating unique constraints"); hdr = true; } provider.GenerateCreatePKScript(tableProps, constraintName, script, isPK, tablePKs.First().Entity.GetPkProperties().Count() == 0); names.Add(constraintName); } } } }