⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 collectionbinder.cs

📁 NHibernate NET开发者所需的
💻 CS
📖 第 1 页 / 共 2 页
字号:
		}

		private void AddSetSecondPass(XmlNode node, Set model)
		{
			mappings.AddSecondPass(delegate(IDictionary<string, PersistentClass> persistentClasses)
				{
					PreCollectionSecondPass(model);
					BindSetSecondPass(node, model, persistentClasses);
					PostCollectionSecondPass(model);
				});
		}

		private void AddIdentifierCollectionSecondPass(XmlNode node, IdentifierCollection model)
		{
			mappings.AddSecondPass(delegate(IDictionary<string, PersistentClass> persistentClasses)
				{
					PreCollectionSecondPass(model);
					BindIdentifierCollectionSecondPass(node, model, persistentClasses);
					PostCollectionSecondPass(model);
				});
		}

		private void AddCollectionSecondPass(XmlNode node, Mapping.Collection model)
		{
			mappings.AddSecondPass(delegate(IDictionary<string, PersistentClass> persistentClasses)
				{
					PreCollectionSecondPass(model);
					BindCollectionSecondPass(node, model, persistentClasses);
					PostCollectionSecondPass(model);
				});
		}

		private void HandleCustomSQL(XmlNode node, Mapping.Collection model)
		{
			XmlNode element = node.SelectSingleNode(HbmConstants.nsSqlInsert, namespaceManager);

			if (element != null)
			{
				bool callable = IsCallable(element);
				model.SetCustomSQLInsert(element.InnerText.Trim(), callable, GetResultCheckStyle(element, callable));
			}

			element = node.SelectSingleNode(HbmConstants.nsSqlDelete, namespaceManager);
			if (element != null)
			{
				bool callable = IsCallable(element);
				model.SetCustomSQLDelete(element.InnerText.Trim(), callable, GetResultCheckStyle(element, callable));
			}

			element = node.SelectSingleNode(HbmConstants.nsSqlUpdate, namespaceManager);
			if (element != null)
			{
				bool callable = IsCallable(element);
				model.SetCustomSQLUpdate(element.InnerText.Trim(), callable, GetResultCheckStyle(element, callable));
			}

			element = node.SelectSingleNode(HbmConstants.nsSqlDeleteAll, namespaceManager);
			if (element != null)
			{
				bool callable = IsCallable(element);
				model.SetCustomSQLDeleteAll(element.InnerText.Trim(), callable, GetResultCheckStyle(element, callable));
			}
		}

		private static void PreCollectionSecondPass(Mapping.Collection collection)
		{
			if (log.IsDebugEnabled)
				log.Debug("Second pass for collection: " + collection.Role);
		}

		private static void PostCollectionSecondPass(Mapping.Collection collection)
		{
			collection.CreateAllKeys();

			if (log.IsDebugEnabled)
			{
				string msg = "Mapped collection key: " + Columns(collection.Key);
				if (collection.IsIndexed)
					msg += ", index: " + Columns(((IndexedCollection)collection).Index);
				if (collection.IsOneToMany)
					msg += ", one-to-many: " + collection.Element.Type.Name;
				else
				{
					msg += ", element: " + Columns(collection.Element);
					msg += ", type: " + collection.Element.Type.Name;
				}
				log.Debug(msg);
			}
		}

		private void BindListSecondPass(XmlNode node, List model,
			IDictionary<string, PersistentClass> persistentClasses)
		{
			BindCollectionSecondPass(node, model, persistentClasses);

			XmlNode subnode = node.SelectSingleNode(HbmConstants.nsListIndex, namespaceManager);
			if (subnode == null) { subnode = node.SelectSingleNode(HbmConstants.nsIndex, namespaceManager); }
			SimpleValue iv = new SimpleValue(model.CollectionTable);
			BindIntegerValue(subnode, iv, IndexedCollection.DefaultIndexColumnName, model.IsOneToMany);
			model.Index = iv;

			string baseIndex = XmlHelper.GetAttributeValue(subnode, "base");
			if (baseIndex != null) { model.BaseIndex = Convert.ToInt32(baseIndex); }
		}

		private void BindOneToMany(XmlNode node, OneToMany model)
		{
			model.ReferencedEntityName = ClassForNameChecked(node.Attributes["class"].Value, mappings,
				"associated class not found: {0}").FullName;

			string notFound = XmlHelper.GetAttributeValue(node, "not-found");
			model.IsIgnoreNotFound = "ignore".Equals(notFound);
		}

		private void BindIdentifierCollectionSecondPass(XmlNode node, IdentifierCollection model,
			IDictionary<string, PersistentClass> persitentClasses)
		{
			BindCollectionSecondPass(node, model, persitentClasses);

			XmlNode subnode = node.SelectSingleNode(HbmConstants.nsCollectionId, namespaceManager);
			SimpleValue id = new SimpleValue(model.CollectionTable);
			BindSimpleValue(subnode, id, false, IdentifierCollection.DefaultIdentifierColumnName);
			model.Identifier = id;
			MakeIdentifier(subnode, id);
		}

		private void BindIntegerValue(XmlNode node, SimpleValue model, string defaultColumnName,
			bool isNullable)
		{
			BindSimpleValue(node, model, isNullable, defaultColumnName);

			if (model.ColumnSpan > 1)
				log.Error("This shouldn't happen, check BindIntegerValue");
			model.TypeName = NHibernateUtil.Int32.Name;
		}

		private void BindSetSecondPass(XmlNode node, Set model,
			IDictionary<string, PersistentClass> persistentClasses)
		{
			BindCollectionSecondPass(node, model, persistentClasses);

			if (!model.IsOneToMany)
				model.CreatePrimaryKey();
		}

		/// <summary>
		/// Called for Maps
		/// </summary>
		/// <param name="node"></param>
		/// <param name="model"></param>
		/// <param name="persistentClasses"></param>
		private void BindMapSecondPass(XmlNode node, Map model,
			IDictionary<string, PersistentClass> persistentClasses)
		{
			BindCollectionSecondPass(node, model, persistentClasses);

			foreach (XmlNode subnode in node.ChildNodes)
			{
				//I am only concerned with elements that are from the nhibernate namespace
				if (subnode.NamespaceURI != Configuration.MappingSchemaXMLNS)
					continue;

				string name = subnode.LocalName; //.Name;

				if ("index".Equals(name))
				{
					SimpleValue value = new SimpleValue(model.CollectionTable);
					BindSimpleValue(subnode, value, model.IsOneToMany, IndexedCollection.DefaultIndexColumnName);
					model.Index = value;
					if (model.Index.Type == null)
						throw new MappingException("map index element must specify a type: " + model.Role);
				}
				else if ("index-many-to-many".Equals(name))
				{
					ManyToOne mto = new ManyToOne(model.CollectionTable);
					BindManyToOne(subnode, mto, IndexedCollection.DefaultIndexColumnName, model.IsOneToMany);
					model.Index = mto;
				}
				else if ("composite-index".Equals(name))
				{
					Component component = new Component(model);
					BindComponent(subnode, component, null, model.Role, "index", model.IsOneToMany);
					model.Index = component;
				}
				else if ("index-many-to-any".Equals(name))
				{
					Any any = new Any(model.CollectionTable);
					BindAny(subnode, any, model.IsOneToMany);
					model.Index = any;
				}
			}
		}

		/// <remarks>
		/// Called for all collections
		/// </remarks>
		private void BindCollectionSecondPass(XmlNode node, Mapping.Collection model,
			IDictionary<string, PersistentClass> persistentClasses)
		{
			if (model.IsOneToMany)
			{
				OneToMany oneToMany = (OneToMany)model.Element;
				string associatedEntityName = oneToMany.ReferencedEntityName;
				PersistentClass persistentClass;
				if (persistentClasses.TryGetValue(associatedEntityName, out persistentClass) == false)
					throw new MappingException("Association references unmapped class: " + associatedEntityName);
				oneToMany.AssociatedClass = persistentClass;
				model.CollectionTable = persistentClass.Table;

				if (log.IsInfoEnabled)
					log.Info("mapping collection: " + model.Role + " -> " + model.CollectionTable.Name);
			}

			//CHECK
			XmlAttribute chNode = node.Attributes["check"];
			if (chNode != null)
				model.CollectionTable.AddCheckConstraint(chNode.Value);

			//contained elements:
			foreach (XmlNode subnode in node.ChildNodes)
			{
				//I am only concerned with elements that are from the nhibernate namespace
				if (subnode.NamespaceURI != Configuration.MappingSchemaXMLNS)
					continue;

				string name = subnode.LocalName; //.Name;

				if ("key".Equals(name) || "generated-key".Equals(name))
				{
					string propRef = model.ReferencedPropertyName;
					IKeyValue keyValue;
					if (propRef == null)
					{
						keyValue = model.Owner.Identifier;
					}
					else
					{
						keyValue = (IKeyValue)model.Owner.GetProperty(propRef).Value;
					}
					DependantValue key = new DependantValue(model.CollectionTable, keyValue);
					if (subnode.Attributes["on-delete"] != null)
						key.IsCascadeDeleteEnabled = "cascade".Equals(subnode.Attributes["on-delete"].Value);
					BindSimpleValue(subnode, key, model.IsOneToMany, Mapping.Collection.DefaultKeyColumnName);
					if (key.Type.ReturnedClass.IsArray)
						throw new MappingException("illegal use of an array as an identifier (arrays don't reimplement Equals)");
					model.Key = key;


					XmlAttribute notNull = subnode.Attributes["not-null"];
					key.SetNullable(notNull == null || IsFalse(notNull.Value));
					XmlAttribute updateable = subnode.Attributes["update"];
					key.SetUpdateable(updateable == null || IsTrue(updateable.Value));
				}
				else if ("element".Equals(name))
				{
					SimpleValue elt = new SimpleValue(model.CollectionTable);
					model.Element = elt;
					BindSimpleValue(subnode, elt, true, Mapping.Collection.DefaultElementColumnName);
				}
				else if ("many-to-many".Equals(name))
				{
					ManyToOne element = new ManyToOne(model.CollectionTable);
					model.Element = element;
					BindManyToOne(subnode, element, Mapping.Collection.DefaultElementColumnName, false);
					BindManyToManySubelements(model, subnode);
				}
				else if ("composite-element".Equals(name))
				{
					Component element = new Component(model);
					model.Element = element;
					BindComponent(subnode, element, null, model.Role, "element", true);
				}
				else if ("many-to-any".Equals(name))
				{
					Any element = new Any(model.CollectionTable);
					model.Element = element;
					BindAny(subnode, element, true);
				}
				else if ("jcs-cache".Equals(name) || "cache".Equals(name))
				{
					XmlAttribute usageNode = subnode.Attributes["usage"];
					model.CacheConcurrencyStrategy = (usageNode != null) ? usageNode.Value : null;
					XmlAttribute regionNode = subnode.Attributes["region"];
					model.CacheRegionName = (regionNode != null) ? regionNode.Value : null;
				}
			}
		}

		private void BindManyToManySubelements(Mapping.Collection collection, XmlNode manyToManyNode)
		{
			// Bind the where
			XmlAttribute where = manyToManyNode.Attributes["where"];
			string whereCondition = where == null ? null : where.Value;
			collection.ManyToManyWhere = whereCondition;

			// Bind the order-by
			XmlAttribute order = manyToManyNode.Attributes["order-by"];
			string orderFragment = order == null ? null : order.Value;
			collection.ManyToManyOrdering = orderFragment;

			// Bind the filters
			if ((manyToManyNode.SelectSingleNode(HbmConstants.nsFilter, namespaceManager) != null ||
				whereCondition != null) &&
					collection.FetchMode == FetchMode.Join &&
						collection.Element.FetchMode != FetchMode.Join)
				throw new MappingException(
					"many-to-many defining filter or where without join fetching " +
						"not valid within collection using join fetching [" + collection.Role + "]"
					);
			foreach (XmlNode filterElement in manyToManyNode.SelectNodes(HbmConstants.nsFilter, namespaceManager))
			{
				string name = XmlHelper.GetAttributeValue(filterElement, "name");
				string condition = filterElement.InnerText.Trim();
				if (StringHelper.IsEmpty(condition))
					condition = XmlHelper.GetAttributeValue(filterElement, "condition");
				if (StringHelper.IsEmpty(condition))
					condition = mappings.GetFilterDefinition(name).DefaultFilterCondition;
				if (condition == null)
					throw new MappingException("no filter condition found for filter: " + name);
				log.Debug(
					"Applying many-to-many filter [" + name +
						"] as [" + condition +
							"] to role [" + collection.Role + "]"
					);
				collection.AddManyToManyFilter(name, condition);
			}
		}

		private delegate Mapping.Collection CreateCollectionCommand(XmlNode node, string className,
			string path, PersistentClass owner, System.Type containingType);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -