public void Map(Model.Model model, params IMapperOption[] options) { Mapping.Mapping mapping = new Mapping.Mapping(); mapping.Model = model; foreach (Model.Entity e in model) { Mapping.Entity entityMapping = new Mapping.Entity(); entityMapping.TableName = "Entity"; entityMapping.Add(new Mapping.Attribute("Id") { IsPrimaryKey = true, Generator = SqlMapper.Mapping.Generator.Native, DbType = System.Data.DbType.UInt32 }); entityMapping.Constraint = "Type=='" + e.Type + "'"; foreach (Model.Attribute attribute in e.Attributes.Values) { entityMapping.Add(new Mapping.Attribute(attribute.Name) { TableName = "Attribute", ColumnName = "Name", DbType = driver.GetDbType(attribute.Type) }); } foreach (Model.Reference reference in e.References.Values) { Mapping.Reference referenceMapping = new Mapping.Reference(); referenceMapping.Rules.Add(new Rule() { ParentTableName = "Entity", ParentFieldNames = "Id", ChildTableName = "Relation", ChildFieldNames = "FK_ParentID,Name='" + reference.Name + "'", }); referenceMapping.Rules.Add(new Rule() { ParentTableName = "Relation", ParentFieldNames = "FK_ChildID,Name='" + reference.Name + "'", ChildTableName = "Entity", ChildFieldNames = "Id", }); } } }
private Mapping.Entity GetEntityMapping(Model.Entity e, params IMapperOption[] options) { if (Mapping.Entities.ContainsKey(e.Type)) return Mapping.Entities[e.Type]; Mapping.Entity em = new Mapping.Entity(); em.Type = e.Type; em.EntityModel = e; Mapping.Entities.Add(em.Type, em); //DefaultStrategy : TablePerHierarchy if (!string.IsNullOrEmpty(e.Inherit)) { Mapping.Entity parentEm = GetEntityMapping(model.Entities[e.Inherit], options); em.TableName = parentEm.TableName; em.Schema = parentEm.Schema; em.Inherit = parentEm; parentEm.Derived.Add(em); SqlMapper.Mapping.InheritanceMappings inheritanceType = SqlMapper.Mapping.InheritanceMappings.TablePerHierarchy; if (em.Inheritance != null) { inheritanceType = em.Inheritance.Type; } switch (inheritanceType) { case Evaluant.Uss.SqlMapper.Mapping.InheritanceMappings.TablePerClass: break; case Evaluant.Uss.SqlMapper.Mapping.InheritanceMappings.TablePerHierarchy: foreach (Mapping.Reference rm in em.Inherit.References.Values) em.Add(rm.Clone(em.Inherit.TableName, em)); foreach (Mapping.Attribute am in em.Inherit.Attributes.Values) em.Add(am.Clone(em.Inherit.TableName, em)); if (em.Inheritance.Discriminator == null) em.Inheritance.Discriminator = "Type=='" + em.Type + "'"; Field[] fields; if (parentEm.Inheritance.Discriminator == null) { parentEm.Inheritance.Discriminator = "Type=='" + parentEm.Type + "'"; fields = GetFields(parentEm.Inheritance.DiscriminatorExpression); foreach (Field field in fields) { parentEm.Add(field); } } fields = GetFields(em.Inheritance.DiscriminatorExpression); foreach (Field field in fields) { em.Add(field); } break; case Evaluant.Uss.SqlMapper.Mapping.InheritanceMappings.TablePerConcreteClass: break; default: break; } } else { em.Schema = e.Type.Substring(0, e.Type.LastIndexOf('.')); em.TableName = e.Type.Substring(em.Schema.Length + 1); } foreach (KeyValuePair<string, Model.Attribute> attribute in e.Attributes) { Mapping.Attribute field = new Mapping.Attribute(attribute.Key); field.Model = attribute.Value; field.ColumnName = attribute.Value.Name; field.IsId = attribute.Value.Name == "Id" || attribute.Value.Name == e.Type.Substring(e.Type.LastIndexOf('.') + 1) + "Id"; field.Name = attribute.Value.Name; field.IsNullable = true; //if (attribute.Key != "#Id") driver.GetTypeInformation(attribute.Value, field); //else //{ // field.DbType = System.Data.DbType.Guid; // field.DefaultValue = Guid.Empty.ToString(); //} em.Add(field); } foreach (KeyValuePair<string, Model.Reference> reference in e.References) { Mapping.Reference rm = new Mapping.Reference(); rm.Name = reference.Value.Name; rm.Model = reference.Value; rm.ChildType = reference.Value.ChildType; //rm.Target = GetEntityMapping(model.Entities[reference.Value.ChildType]); //rm.Cardinality = reference.Value.Cardinality; //rm.Inherited = reference.Value.Inherited; if (reference.Value.Cardinality.IsToMany) { string[] parentFields = new string[em.Ids.Keys.Count]; em.Ids.Keys.CopyTo(parentFields, 0); var target = GetEntityMapping(model.Entities[rm.ChildType]); if (target.Ids.Count == 0) throw new Mapping.MappingException("No Id could be found on the " + target.Type + " entity you need to either specify one or specify manually the way you want to map this reference " + reference.Value); string[] childFields = new string[target.Ids.Keys.Count]; target.Ids.Keys.CopyTo(childFields, 0); string indexSchema = e.Type.Substring(0, e.Type.LastIndexOf('.')); string indexTableName = e.Type.Substring(indexSchema.Length + 1) + "_" + rm.Name; rm.Rules.Add(new Mapping.Rule() { ParentFieldNames = string.Join(",", parentFields), ChildSchema = indexSchema, ChildTableName = indexTableName, ChildFieldNames = "FK_Parent" + string.Join(",FK_Parent", parentFields) }); rm.Rules.Add(new Mapping.Rule() { ParentSchema = indexSchema, ParentTableName = indexTableName, ParentFieldNames = "FK_Child" + string.Join(",FK_Child", childFields), ChildSchema = target.Schema, ChildTableName = target.TableName, ChildFieldNames = string.Join(",", childFields) }); } else { if (reference.Value.Cardinality.IsOne) { var target = GetEntityMapping(model.Entities[rm.ChildType]); string[] parentFields = new string[em.Ids.Keys.Count]; em.Ids.Keys.CopyTo(parentFields, 0); rm.Rules.Add(new Mapping.Rule() { ParentFieldNames = string.Join(",", parentFields), ChildSchema = target.Schema, ChildTableName = target.TableName, ChildFieldNames = "FK_" + em.TableName + string.Join(",FK_" + em.TableName, parentFields) }); } if (reference.Value.Cardinality.IsMany) { var target = GetEntityMapping(model.Entities[rm.ChildType]); string[] childFields = new string[target.Ids.Keys.Count]; target.Ids.Keys.CopyTo(childFields, 0); for (int i = 0; i < childFields.Length; i++) { bool fkMapped = false; foreach (var attr in em.Attributes.Keys) { if ((attr == "FK_" + rm.Name || attr == "FK_" + target.TableName) && childFields.Length == 1 || (attr == "FK_" + rm.Name + childFields[i] || attr == rm.Name + childFields[i] || attr == target.TableName + childFields[i])) { childFields[i] = attr; fkMapped = true; break; } } if (!fkMapped) childFields[i] = "FK_" + target.TableName + childFields[i]; } rm.Rules.Add(new Mapping.Rule() { ParentFieldNames = string.Join("," + target.TableName, childFields), ChildSchema = target.Schema, ChildTableName = target.TableName, ChildFieldNames = string.Join(",", target.Ids.Keys) }); } } em.Add(rm); } return em; }