protected virtual void OnPopulateMembers(object sender, EventArgs e) { if ((_settings.GenerateMode.HasValue ? _settings.GenerateMode.Value : _entity.Model.GenerateMode) != GenerateModeEnum.SchemaOnly) { PopulateDontRaise(); OnPopulatePropertiesAccessors(); if (_entity.GetPkProperties().Count() > 0 && (_settings.GenerateMode.HasValue ? _settings.GenerateMode.Value : _entity.Model.GenerateMode) != GenerateModeEnum.EntityOnly) { OnPupulateEntityRelations(); OnPupulateM2MRelations(); } } if ((_settings.GenerateMode.HasValue ? _settings.GenerateMode.Value : _entity.Model.GenerateMode) != GenerateModeEnum.EntityOnly) { if (_entity.NeedOwnSchema()) { OnPopulateSchema(); } } //else // throw new NotImplementedException(); }
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); }
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); } }
private void CreateForeignKeys(StringBuilder script, ISourceProvider provider) { var fks = Model.GetActiveEntities().SelectMany(item => item.GetProperties() .OfType <EntityPropertyDefinition>() ).Where(item => item.SourceFragment != null); List <string> names = new List <string>(); bool hdr = false; foreach (SourceFragmentDefinition s in fks.Select(item => item.SourceFragment).Distinct()) { SourceFragmentDefinition sf = s; var targetSF = SourceView.GetSourceFragments().SingleOrDefault(item => item.Name == sf.Name && item.Selector == sf.Selector); List <FKDefinition> fksList = new List <FKDefinition>(); foreach (EntityPropertyDefinition prop in fks.Where(item => item.SourceFragment == sf)) { EntityDefinition re = prop.PropertyType.Entity; var fpk = re.GetPkProperties().Where(item => prop.SourceFields.Any(fld => fld.PropertyAlias == item.PropertyAlias)); if (fpk.Count() == 0) { fpk = re.GetProperties().OfType <ScalarPropertyDefinition>() .Where(item => !item.Disabled && item.SourceField.Constraints.Any(cns => cns.ConstraintType == SourceConstraint.UniqueConstraintTypeName)); } FKDefinition f = new FKDefinition { cols = prop.SourceFields.Select(item => item.SourceFieldExpression).ToArray(), refCols = prop.SourceFields.Select(item => fpk.Single(pk => pk.PropertyAlias == item.PropertyAlias).SourceFieldExpression).ToArray(), refTbl = fpk.Single(pk => pk.PropertyAlias == prop.SourceFields.First().PropertyAlias).SourceFragment }; if (targetSF != null) { if (targetSF.Constraints.Any(item => item.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName && prop.SourceFields.All(p => item.SourceFields.Any(fkf => fkf.SourceFieldExpression == p.SourceFieldExpression) ) )) { continue; } } if (string.IsNullOrEmpty(f.constraintName)) { f.constraintName = "FK_" + prop.Entity.Name + "_" + re.Name; if (names.Contains(f.constraintName)) { f.constraintName += "_" + prop.Name; } } names.Add(f.constraintName); fksList.Add(f); } if (fksList.Count > 0) { if (!hdr) { script.AppendLine("--Creating foreign keys"); hdr = true; } provider.GenerateCreateFKsScript(sf, fksList, script); } } foreach (RelationDefinitionBase rel in Model.GetActiveRelations()) { var relSF = SourceView.GetSourceFragments().SingleOrDefault(item => item.Name == rel.SourceFragment.Name && item.Selector == rel.SourceFragment.Selector); List <FKDefinition> fksList = new List <FKDefinition>(); if (rel is SelfRelationDefinition) { SelfRelationDefinition r = rel as SelfRelationDefinition; FKDefinition f; if (CreateFKDefinition(r.Properties, r.Left, relSF, script, provider, out f)) { fksList.Add(f); } if (string.IsNullOrEmpty(f.constraintName)) { f.constraintName = "FK_" + rel.SourceFragment.Name.Trim(']', '[') + "_" + r.Entity.Name; } if (names.Contains(f.constraintName)) { f.constraintName += "_" + names.Count(item => item.StartsWith(f.constraintName)); } names.Add(f.constraintName); if (CreateFKDefinition(r.Properties, r.Right, relSF, script, provider, out f)) { fksList.Add(f); } if (string.IsNullOrEmpty(f.constraintName)) { f.constraintName = "FK_" + rel.SourceFragment.Name.Trim(']', '[') + "_" + r.Entity.Name; } if (names.Contains(f.constraintName)) { f.constraintName += "_" + names.Count(item => item.StartsWith(f.constraintName)); } names.Add(f.constraintName); } else if (rel is RelationDefinition) { RelationDefinition r = rel as RelationDefinition; FKDefinition f; if (CreateFKDefinition(r.Left.Properties, r.Left, relSF, script, provider, out f)) { fksList.Add(f); } if (string.IsNullOrEmpty(f.constraintName)) { f.constraintName = "FK_" + rel.SourceFragment.Name.Trim(']', '[') + "_" + r.Left.Entity.Name; } if (names.Contains(f.constraintName)) { f.constraintName += "_" + names.Count(item => item.StartsWith(f.constraintName)); } names.Add(f.constraintName); if (CreateFKDefinition(r.Right.Properties, r.Right, relSF, script, provider, out f)) { fksList.Add(f); } if (string.IsNullOrEmpty(f.constraintName)) { f.constraintName = "FK_" + rel.SourceFragment.Name.Trim(']', '[') + "_" + r.Right.Entity.Name; } if (names.Contains(f.constraintName)) { f.constraintName += "_" + names.Count(item => item.StartsWith(f.constraintName)); } names.Add(f.constraintName); } else { throw new NotSupportedException(rel.GetType().ToString()); } if (fksList.Count > 0) { if (!hdr) { script.AppendLine("--Creating foreign keys"); hdr = true; } provider.GenerateCreateFKsScript(rel.SourceFragment, fksList, script); } } }