private void ProcessOneToOneReference(Entity entity, DirectedReference directedReference, Action <object> addItem, string cascade, string lazy) { if (directedReference.FromEndEnabled == false) { return; } DirectedRelationship directedRelationship = GetDirectedMappedRelationship(entity, directedReference.Reference); fetchMode fetchMode; if (directedReference.Entity1IsFromEnd) { fetchMode = (fetchMode)Enum.Parse(typeof(fetchMode), directedReference.Reference.GetReferenceEnd1FetchMode().ToString(), true); } else { fetchMode = (fetchMode)Enum.Parse(typeof(fetchMode), directedReference.Reference.GetReferenceEnd2FetchMode().ToString(), true); } // if this side has the foreign key, it gets the many to one node if (directedRelationship.FromKey.Keytype == DatabaseKeyType.Foreign) { manytoone manyToOneNode = CreateManyToOneNode(directedReference, directedRelationship, cascade); manyToOneNode.unique = true; manyToOneNode.fetch = fetchMode; manyToOneNode.fetchSpecified = true; addItem(manyToOneNode); } else { onetoone oneToOneNode = new onetoone(); oneToOneNode.@class = directedReference.ToEntity.Name; oneToOneNode.name = directedReference.FromName; oneToOneNode.propertyref = directedReference.ToName; oneToOneNode.fetch = fetchMode; oneToOneNode.fetchSpecified = true; addItem(oneToOneNode); } }
private manytoone CreateManyToOneNode(DirectedReference directedReference, DirectedRelationship directedRelationship, string cascade) { fetchMode fetchMode; bool insert; bool update; if (directedReference.Entity1IsFromEnd) { fetchMode = (fetchMode)Enum.Parse(typeof(fetchMode), directedReference.Reference.GetReferenceEnd1FetchMode().ToString(), true); insert = directedReference.Reference.GetReferenceEnd1Insert(); update = directedReference.Reference.GetReferenceEnd1Update(); } else { fetchMode = (fetchMode)Enum.Parse(typeof(fetchMode), directedReference.Reference.GetReferenceEnd2FetchMode().ToString(), true); insert = directedReference.Reference.GetReferenceEnd2Insert(); update = directedReference.Reference.GetReferenceEnd2Update(); } manytoone manyToOneNode = new manytoone(); manyToOneNode.@class = directedReference.ToEntity.Name; manyToOneNode.name = directedReference.FromName; bool notNullableColumnsExist = directedRelationship.MappedColumns.Any(mc => !mc.Source.IsNullable); manyToOneNode.notnull = notNullableColumnsExist; manyToOneNode.notnullSpecified = true; manyToOneNode.fetch = fetchMode; manyToOneNode.fetchSpecified = true; manyToOneNode.insert = insert; manyToOneNode.update = update; manyToOneNode.cascade = cascade == "none" ? null : cascade; if (directedRelationship.ToTable == directedRelationship.FromTable) { if (directedRelationship.ToKey.Keytype == Providers.EntityModel.Helper.DatabaseKeyType.Primary) if (directedRelationship.FromKey.Columns.Count > 1) foreach (var column in GetColumnNodes(directedRelationship.FromKey.Columns)) manyToOneNode.AddColumn(column); else manyToOneNode.column = directedRelationship.FromKey.Columns[0].Name.BackTick(); else if (directedRelationship.ToKey.Columns.Count > 1) foreach (var column in GetColumnNodes(directedRelationship.ToKey.Columns)) manyToOneNode.AddColumn(column); else manyToOneNode.column = directedRelationship.ToKey.Columns[0].Name.BackTick(); } else { if (directedRelationship.FromKey.Columns.Count > 1) foreach (var column in GetColumnNodes(directedRelationship.FromKey.Columns)) manyToOneNode.AddColumn(column); else manyToOneNode.column = directedRelationship.FromKey.Columns[0].Name.BackTick(); } return manyToOneNode; }
private manytoone CreateManyToOneNode(DirectedReference directedReference, DirectedRelationship directedRelationship, string cascade) { fetchMode fetchMode; bool insert; bool update; if (directedReference.Entity1IsFromEnd) { fetchMode = (fetchMode)Enum.Parse(typeof(fetchMode), directedReference.Reference.GetReferenceEnd1FetchMode().ToString(), true); insert = directedReference.Reference.GetReferenceEnd1Insert(); update = directedReference.Reference.GetReferenceEnd1Update(); } else { fetchMode = (fetchMode)Enum.Parse(typeof(fetchMode), directedReference.Reference.GetReferenceEnd2FetchMode().ToString(), true); insert = directedReference.Reference.GetReferenceEnd2Insert(); update = directedReference.Reference.GetReferenceEnd2Update(); } manytoone manyToOneNode = new manytoone(); manyToOneNode.@class = directedReference.ToEntity.Name; manyToOneNode.name = directedReference.FromName; bool notNullableColumnsExist = directedRelationship.MappedColumns.Any(mc => !mc.Source.IsNullable); manyToOneNode.notnull = notNullableColumnsExist; manyToOneNode.notnullSpecified = true; manyToOneNode.fetch = fetchMode; manyToOneNode.fetchSpecified = true; manyToOneNode.insert = insert; manyToOneNode.update = update; manyToOneNode.cascade = cascade == "none" ? null : cascade; if (directedRelationship.ToTable == directedRelationship.FromTable) { if (directedRelationship.ToKey.Keytype == Providers.EntityModel.Helper.DatabaseKeyType.Primary) { if (directedRelationship.FromKey.Columns.Count > 1) { foreach (var column in GetColumnNodes(directedRelationship.FromKey.Columns)) { manyToOneNode.AddColumn(column); } } else { manyToOneNode.column = directedRelationship.FromKey.Columns[0].Name.BackTick(); } } else if (directedRelationship.ToKey.Columns.Count > 1) { foreach (var column in GetColumnNodes(directedRelationship.ToKey.Columns)) { manyToOneNode.AddColumn(column); } } else { manyToOneNode.column = directedRelationship.ToKey.Columns[0].Name.BackTick(); } } else { if (directedRelationship.FromKey.Columns.Count > 1) { foreach (var column in GetColumnNodes(directedRelationship.FromKey.Columns)) { manyToOneNode.AddColumn(column); } } else { manyToOneNode.column = directedRelationship.FromKey.Columns[0].Name.BackTick(); } } return(manyToOneNode); }
private void ProcessManyToOneReference( Entity entity, DirectedReference directedReference, Action <object> addItem, string cascade, string collectionCascade, string lazy, string orderByClause, bool inverse) { if (directedReference.FromEndEnabled == false) { return; } ITable referenceMappedTable = directedReference.Reference.MappedTable(); DirectedRelationship directedRelationship = null; if (referenceMappedTable == null) { directedRelationship = GetDirectedMappedRelationship(entity, directedReference.Reference); } if (directedReference.FromEndCardinality == Cardinality.One) { fetchMode fetchMode; bool insert; bool update; if (directedReference.Entity1IsFromEnd) { fetchMode = (fetchMode)Enum.Parse(typeof(fetchMode), directedReference.Reference.GetReferenceEnd1FetchMode().ToString(), true); insert = directedReference.Reference.GetReferenceEnd1Insert(); update = directedReference.Reference.GetReferenceEnd1Update(); } else { fetchMode = (fetchMode)Enum.Parse(typeof(fetchMode), directedReference.Reference.GetReferenceEnd2FetchMode().ToString(), true); insert = directedReference.Reference.GetReferenceEnd2Insert(); update = directedReference.Reference.GetReferenceEnd2Update(); } manytoone manyToOneNode; if (referenceMappedTable == null) { manyToOneNode = CreateManyToOneNode(directedReference, directedRelationship, cascade); } else { manyToOneNode = CreateManyToOneNode(directedReference, referenceMappedTable); } manyToOneNode.fetch = fetchMode; manyToOneNode.fetchSpecified = true; manyToOneNode.insert = insert; manyToOneNode.update = update; addItem(manyToOneNode); } else { key keyNode = new key(); if (referenceMappedTable == null && directedRelationship.ToKey.Columns.Count > 1) { foreach (var columnNode in GetColumnNodes(directedRelationship.ToKey.Columns)) { keyNode.AddColumn(columnNode); } } else if (referenceMappedTable != null) { ITable toPrimaryMappedTable = EntityMapper.GetPrimaryTable(directedReference.ToEntity); var toColumnsInPrimaryKey = referenceMappedTable.Relationships.First(t => t.PrimaryTable == toPrimaryMappedTable || t.ForeignTable == toPrimaryMappedTable).ForeignKey.Columns; foreach (var columnNode in GetColumnNodes(toColumnsInPrimaryKey)) { keyNode.AddColumn(columnNode); } } else { keyNode.column1 = directedRelationship.ToKey.Columns[0].Name.BackTick(); } onetomany oneToManyNode = new onetomany(); oneToManyNode.@class = directedReference.ToEntity.Name; collectionFetchMode collFetchMode; if (directedReference.Entity1IsFromEnd) { collFetchMode = (collectionFetchMode)Enum.Parse(typeof(collectionFetchMode), directedReference.Reference.GetReferenceEnd1CollectionFetchMode().ToString(), true); } else { collFetchMode = (collectionFetchMode)Enum.Parse(typeof(collectionFetchMode), directedReference.Reference.GetReferenceEnd2CollectionFetchMode().ToString(), true); } AssociationType type = NHCollections.GetAssociationType(directedReference); switch (type) { case AssociationType.None: Log.WarnFormat("No association type was set on reference {0} for the end {1}. This is usually an error.", directedReference.Reference.Name, directedReference.Entity1IsFromEnd ? "1" : "2"); return; case AssociationType.Set: var set = CreateSetNode(directedReference, keyNode, collectionCascade, collFetchMode, lazy, inverse); set.Item = oneToManyNode; if (orderByClause.Length > 0) { set.orderby = orderByClause; } addItem(set); break; case AssociationType.Map: var mapNode = CreateMapNode(directedReference, keyNode, collectionCascade, collFetchMode, lazy, inverse); mapNode.Item = new index { column1 = NHCollections.GetIndexColumnName(directedReference), type = NHCollections.GetIndexColumnTypeName(directedReference, EntityMapper.GetPrimaryTable(directedReference.ToEntity)) }; mapNode.Item1 = oneToManyNode; if (orderByClause.Length > 0) { mapNode.orderby = orderByClause; } addItem(mapNode); break; case AssociationType.Bag: var bag = CreateBagNode(directedReference, keyNode, collectionCascade, collFetchMode, lazy, inverse); bag.Item = oneToManyNode; if (orderByClause.Length > 0) { bag.orderby = orderByClause; } addItem(bag); break; case AssociationType.List: list listNode = CreateListNode(directedReference, keyNode, collectionCascade, collFetchMode, lazy, inverse); listNode.Item = new index { column1 = NHCollections.GetIndexColumnName(directedReference) }; listNode.Item1 = oneToManyNode; if (orderByClause.Length > 0) { listNode.orderby = orderByClause; } addItem(listNode); break; case AssociationType.IDBag: idbag idbagNode = CreateIdBagNode(directedReference, keyNode, collectionCascade, collFetchMode, lazy, inverse); idbagNode.collectionid = new collectionid { column1 = NHCollections.GetIndexColumnName(directedReference), generator = new generator { @class = "sequence" }, type = NHCollections.GetIndexColumnTypeName(directedReference, EntityMapper.GetPrimaryTable(directedReference.ToEntity)) }; addItem(idbagNode); break; default: throw new ArgumentOutOfRangeException("AssociationType not handled yet: " + type.ToString()); } } }