public void TestDropProperty() { MSSQLProvider p = new MSSQLProvider(GetTestDB(), null); SourceView sv = p.GetSourceView(null, "aspnet_Applications"); Assert.AreEqual(4, sv.SourceFields.Count); WXMLModel model = new WXMLModel(); SourceToModelConnector c = new SourceToModelConnector(sv, model); c.ApplySourceViewToModel(); Assert.AreEqual(4, model.GetActiveEntities().First().GetActiveProperties().Count()); SourceFieldDefinition fld = sv.SourceFields.Find(item => item.SourceFieldExpression == "[Description]"); sv.SourceFields.Remove(fld); Assert.AreEqual(3, sv.SourceFields.Count); c.ApplySourceViewToModel(true, relation1to1.Default, true, true, false); Assert.AreEqual(3, model.GetActiveEntities().First().GetActiveProperties().Count()); sv.SourceFields.Add(fld); Assert.AreEqual(4, sv.SourceFields.Count); c.ApplySourceViewToModel(true, relation1to1.Default, true, true, false); Assert.AreEqual(4, model.GetActiveEntities().First().GetActiveProperties().Count()); }
/// <summary> /// Begin a new field mapping by indicating the source field. /// You must call .To() or .Then() on the result of this method. /// </summary> public ISourceFieldDefinition <TTargetEntity, TSourceField> From <TSourceField>(Expression <Func <TSourceEntity, TSourceField> > sourceField) { var fieldMapping = new SourceFieldDefinition <TSourceField>(sourceField, Replace); _fieldMappings.Add(fieldMapping); return(fieldMapping); }
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 static void GenerateColumns(IEnumerable <ColDef> props, StringBuilder script, bool unicodeStrings) { foreach (ColDef prop in props) { SourceFieldDefinition sp = prop.Field; script.Append(sp.SourceFieldExpression).Append(" ").Append(prop.type); if (sp.IsAutoIncrement) { script.Append(" IDENTITY"); } script.Append(sp.IsNullable ? " NULL" : " NOT NULL"); if (!string.IsNullOrEmpty(sp.DefaultValue)) { script.AppendFormat(" DEFAULT({0})", sp.DefaultValue); } script.Append(", "); } }
protected PropertyDefinition AppendColumn(EntityDefinition e, SourceFieldDefinition c, out bool created, bool transforRawNamesToReadableForm, bool capitalizeNames, bool caseSensitive) { created = false; //var x = e.OwnProperties.OfType<ScalarPropertyDefinition>().Where(pd => // pd.SourceFieldExpression.Trim(']', '[') == c.SourceFieldExpression.Trim('[',']') //); var nsfn = c.SourceFieldExpression.Trim('[', ']'); if (!caseSensitive) { nsfn = nsfn.ToLower(); } var x = e.OwnProperties.Where(pd => { ScalarPropertyDefinition sp = pd as ScalarPropertyDefinition; if (sp != null) { var spsf = sp.SourceFieldExpression.Trim(']', '['); if (!caseSensitive) { spsf = spsf.ToLower(); } return(spsf == nsfn); } else { EntityPropertyDefinition ep = pd as EntityPropertyDefinition; if (ep != null) { return(ep.SourceFields.Any(item => { var spsf = item.SourceFieldExpression.Trim(']', '['); if (!caseSensitive) { spsf = spsf.ToLower(); } return spsf == nsfn; })); } } return(false); } ); PropertyDefinition pe = null; ScalarPropertyDefinition pes = x.OfType <ScalarPropertyDefinition>().SingleOrDefault(); if (pes != null) { pe = pes; } else { pe = x.OfType <EntityPropertyDefinition>().SingleOrDefault(); } GetSourceFragment(c.SourceFragment); if (pe == null) { Field2DbRelations attrs = c.GetAttributes(); string name = GetName(c.SourceFieldExpression); if (!SourceView.GetSourceFields(c.SourceFragment).Any(item => GetName(item.SourceFieldExpression).Equals(Trim(name, transforRawNamesToReadableForm), StringComparison.InvariantCultureIgnoreCase))) { name = Trim(name, transforRawNamesToReadableForm); } if (capitalizeNames) { name = Capitalize(name); } if ((attrs & Field2DbRelations.PK) == Field2DbRelations.PK && c.IsNullable) { throw new WXMLException(string.Format("Column {0}.{1} cannot be nullable since it's a primary key", c.SourceFragment, c.SourceFieldExpression)); } var t = GetClrType(c.SourceType, c.IsNullable, ref attrs); pe = new ScalarPropertyDefinition(e, name, name, attrs, "Auto generated from column " + c.SourceFieldExpression, t, c, AccessLevel.Private, AccessLevel.Public ); e.AddProperty(pe); created = true; } else { Field2DbRelations attrs = c.GetAttributes(); if (attrs == Field2DbRelations.None) { attrs = pe.Attributes; } ScalarPropertyDefinition sp = pe as ScalarPropertyDefinition; if (sp != null) { if (!pe.PropertyType.IsUserType && (attrs & Field2DbRelations.PK) != Field2DbRelations.PK && sp.SourceType != c.SourceType) { pe.PropertyType = GetClrType(c.SourceType, c.IsNullable, ref attrs); } sp.SourceField = c; } if ((attrs & Field2DbRelations.PK) == Field2DbRelations.PK && e.BaseEntity != null && e.BaseEntity.GetPkProperties().Any()) { attrs = attrs & ~Field2DbRelations.PK; } if (attrs == Field2DbRelations.None) { attrs = pe.Attributes; } pe.Attributes = attrs; } return(pe); }
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 SourceFieldDefinition Create(SourceView db, DbDataReader reader, bool escapeTableNames, bool escapeColumnNames, bool skipSchema) { SourceFieldDefinition c = new SourceFieldDefinition(); string table = reader.GetString(reader.GetOrdinal("table_name")); string schema = null; if (!skipSchema && !reader.IsDBNull(reader.GetOrdinal("table_schema"))) { schema = reader.GetString(reader.GetOrdinal("table_schema")); if (escapeTableNames) { if (!(table.StartsWith("[") || table.EndsWith("]"))) { table = "[" + table + "]"; } if (!(schema.StartsWith("[") || schema.EndsWith("]"))) { schema = "[" + schema + "]"; } } } c.SourceFragment = db.GetOrCreateSourceFragment(schema, table); c._column = reader.GetString(reader.GetOrdinal("column_name")); if (escapeColumnNames && !c._column.StartsWith("[") && !c._column.EndsWith("]")) { c._column = "[" + c._column + "]"; } if (!db.GetSourceFields(c.SourceFragment).Any(item => item.SourceFieldExpression == c._column)) { string yn = reader.GetString(reader.GetOrdinal("is_nullable")); if (yn == "YES") { c.IsNullable = true; } else { c.IsNullable = false; } c.SourceType = reader.GetString(reader.GetOrdinal("data_type")); try { c.IsAutoIncrement = Convert.ToBoolean(reader.GetInt32(reader.GetOrdinal("identity"))); } catch { } int dfo = reader.GetOrdinal("column_default"); if (!reader.IsDBNull(dfo)) { c._defaultValue = reader.GetString(dfo); } if (!new[] { "ntext", "text", "image" }.Any(item => item == c.SourceType.ToLower())) { int sc = reader.GetOrdinal("character_maximum_length"); if (!reader.IsDBNull(sc)) { c.SourceTypeSize = reader.GetInt32(sc); } } db.SourceFields.Add(c); } else { c = db.GetSourceFields(c.SourceFragment).Single(item => item.SourceFieldExpression == c._column); } try { int ct = reader.GetOrdinal("constraint_type"); int cn = reader.GetOrdinal("constraint_name"); if (!reader.IsDBNull(ct)) { SourceConstraint cns = c.SourceFragment.Constraints .SingleOrDefault(item => item.ConstraintName == reader.GetString(cn)); if (cns == null) { cns = new SourceConstraint(reader.GetString(ct), reader.GetString(cn)); c.SourceFragment.Constraints.Add(cns); } cns.SourceFields.Add(c); } } catch { } return(c); }
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); }
public static string GetType(SourceFieldDefinition field, TypeDefinition propType, Field2DbRelations attrs, bool unicodeStrings) { string result = field.SourceType; if (string.IsNullOrEmpty(result)) { switch (propType.ClrType.FullName) { case "System.Boolean": result = "bit"; break; case "System.Byte": result = "tinyint"; break; case "System.Int16": case "System.SByte": result = "smallint"; break; case "System.Int32": case "System.UInt16": result = "int"; break; case "System.Int64": case "System.UInt32": result = "bigint"; break; case "System.UInt64": result = "decimal"; break; case "System.Decimal": result = "money"; break; case "System.Single": result = "real"; break; case "System.Double": result = "float"; break; case "System.String": result = string.Format(unicodeStrings ? "nvarchar({0})" : "varchar({0})", field.SourceTypeSize.HasValue ? field.SourceTypeSize.Value : 50); break; case "System.Char": result = unicodeStrings ? "nchar(1)" : "char(1)"; break; case "System.Xml.XmlDocument": case "System.Xml.XmlDocumentFragment": case "System.Xml.Linq.XDocument": case "System.Xml.Linq.XElement": result = "xml"; break; case "System.DateTime": result = "datetime"; break; case "System.GUID": result = "uniqueidentifier"; break; case "System.Char[]": result = string.Format(unicodeStrings ? "nvarchar({0})" : "varchar({0})", field.SourceTypeSize.HasValue ? field.SourceTypeSize.Value : 50); break; case "System.Byte[]": if ((attrs & Field2DbRelations.RV) == Field2DbRelations.RV) { result = "rowversion"; } else { result = string.Format("varbinary({0})", field.SourceTypeSize.HasValue ? field.SourceTypeSize.Value : 50); } break; default: throw new NotSupportedException(propType.ClrType.FullName); } } else if (field.SourceTypeSize.HasValue && !result.Contains(field.SourceTypeSize.Value.ToString())) { result += string.Format("({0})", field.SourceTypeSize.Value); } return(result); }