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

📄 abstractcollectionpersister.cs

📁 NHibernate NET开发者所需的
💻 CS
📖 第 1 页 / 共 4 页
字号:
					IDbCommand st = useBatch
														? session.Batcher.PrepareBatchCommand(SqlDeleteString.CommandType, SqlDeleteString.Text,
																																	SqlDeleteString.ParameterTypes)
														: session.Batcher.PrepareCommand(SqlDeleteString.CommandType, SqlDeleteString.Text,
																														 SqlDeleteString.ParameterTypes);

					try
					{
						//offset += expectation.Prepare(st, factory.ConnectionProvider.Driver);
						WriteKey(st, id, offset, session);
						if (useBatch)
						{
							session.Batcher.AddToBatch(expectation);
						}
						else
						{
							expectation.VerifyOutcomeNonBatched(session.Batcher.ExecuteNonQuery(st), st);
						}
					}
					catch (Exception e)
					{
						if (useBatch)
						{
							session.Batcher.AbortBatch(e);
						}
						throw;
					}
					finally
					{
						if (!useBatch)
						{
							session.Batcher.CloseCommand(st, null);
						}
					}

					if (log.IsDebugEnabled)
					{
						log.Debug("done deleting collection");
					}
				}
				catch (DbException sqle)
				{
					throw ADOExceptionHelper.Convert(sqlExceptionConverter, sqle, "could not delete collection: " + MessageHelper.InfoString(this, id));
				}
			}
		}

		public void Recreate(IPersistentCollection collection, object id, ISessionImplementor session)
		{
			if (!isInverse && RowInsertEnabled)
			{
				if (log.IsDebugEnabled)
				{
					log.Debug("Inserting collection: " + MessageHelper.InfoString(this, id));
				}

				try
				{
					// create all the new entries
					IEnumerator entries = collection.Entries(this).GetEnumerator();
					if (entries.MoveNext())
					{
						entries.Reset();
						collection.PreInsert(this);
						int i = 0;
						int count = 0;

						while (entries.MoveNext())
						{
							object entry = entries.Current;
							if (collection.EntryExists(entry, i))
							{
								int offset = 0;
								IDbCommand st;
								IExpectation expectation = Expectations.AppropriateExpectation(insertCheckStyle);
								//bool callable = InsertCallable;

								bool useBatch = expectation.CanBeBatched;
								if (useBatch)
								{
									st =
										session.Batcher.PrepareBatchCommand(SqlInsertRowString.CommandType, SqlInsertRowString.Text,
										                                    SqlInsertRowString.ParameterTypes);
								}
								else
								{
									st =
										session.Batcher.PrepareCommand(SqlInsertRowString.CommandType, SqlInsertRowString.Text,
										                               SqlInsertRowString.ParameterTypes);
								}
								try
								{
									//offset += expectation.Prepare(st, factory.ConnectionProvider.Driver);
									int loc = WriteKey(st, id, offset, session);
									if (hasIdentifier && !isPostInsertIdentifier)
										loc = WriteIdentifier(st, collection.GetIdentifier(entry, i), loc, session);

									if (hasIndex)
										loc = WriteIndex(st, collection.GetIndex(entry, i), loc, session);

									WriteElement(st, collection.GetElement(entry), loc, session);
									if (useBatch)
									{
										session.Batcher.AddToBatch(expectation);
									}
									else
									{
										expectation.VerifyOutcomeNonBatched(session.Batcher.ExecuteNonQuery(st), st);
									}
									collection.AfterRowInsert(this, entry, i);
									count++;
								}
								catch (Exception e)
								{
									if (useBatch)
									{
										session.Batcher.AbortBatch(e);
									}
									throw;
								}
								finally
								{
									if (!useBatch)
									{
										session.Batcher.CloseCommand(st, null);
									}
								}
							}
							i++;
						}

						if (log.IsDebugEnabled)
						{
							log.Debug(string.Format("done inserting collection: {0} rows inserted", count));
						}
					}
					else
					{
						if (log.IsDebugEnabled)
							log.Debug("collection was empty");
					}
				}
				catch (DbException sqle)
				{
					throw ADOExceptionHelper.Convert(sqlExceptionConverter, sqle,
					                                 "could not insert collection: " + MessageHelper.InfoString(this, id));
				}
			}
		}

		public void DeleteRows(IPersistentCollection collection, object id, ISessionImplementor session)
		{
			if (!isInverse && RowDeleteEnabled)
			{
				if (log.IsDebugEnabled)
				{
					log.Debug("Deleting rows of collection: " + MessageHelper.InfoString(this, id));
				}

				bool deleteByIndex = !IsOneToMany && hasIndex && !indexContainsFormula;

				try
				{
					// delete all the deleted entries
					IEnumerator deletes = collection.GetDeletes(elementType, !deleteByIndex).GetEnumerator();
					if (deletes.MoveNext())
					{
						deletes.Reset();
						int offset = 0;
						int count = 0;

						while (deletes.MoveNext())
						{
							IDbCommand st;
							IExpectation expectation = Expectations.AppropriateExpectation(deleteCheckStyle);
							//bool callable = DeleteCallable;

							bool useBatch = expectation.CanBeBatched;
							if (useBatch)
							{
								st =
									session.Batcher.PrepareBatchCommand(SqlDeleteRowString.CommandType, SqlDeleteRowString.Text,
									                                    SqlDeleteRowString.ParameterTypes);
							}
							else
							{
								st =
									session.Batcher.PrepareCommand(SqlDeleteRowString.CommandType, SqlDeleteRowString.Text,
									                               SqlDeleteRowString.ParameterTypes);
							}
							try
							{
								object entry = deletes.Current;
								int loc = offset;
								if (hasIdentifier)
								{
									WriteIdentifier(st, entry, loc, session);
								}
								else
								{
									loc = WriteKey(st, id, loc, session);

									if (deleteByIndex)
										WriteIndexToWhere(st, entry, loc, session);
									else
										WriteElementToWhere(st, entry, loc, session);
								}
								if (useBatch)
								{
									session.Batcher.AddToBatch(expectation);
								}
								else
								{
									expectation.VerifyOutcomeNonBatched(session.Batcher.ExecuteNonQuery(st), st);
								}
								count++;
							}
							catch (Exception e)
							{
								if (useBatch)
								{
									session.Batcher.AbortBatch(e);
								}
								throw;
							}
							finally
							{
								if (!useBatch)
								{
									session.Batcher.CloseCommand(st, null);
								}
							}
						}

						if (log.IsDebugEnabled)
							log.Debug("done deleting collection rows: " + count + " deleted");
					}
					else
					{
						if (log.IsDebugEnabled)
						{
							log.Debug("no rows to delete");
						}
					}
				}
				catch (DbException sqle)
				{
					throw ADOExceptionHelper.Convert(sqlExceptionConverter, sqle,
					                                 "could not delete collection rows: " + MessageHelper.InfoString(this, id));
				}
			}
		}

		public void InsertRows(IPersistentCollection collection, object id, ISessionImplementor session)
		{
			if (!isInverse && RowInsertEnabled)
			{
				if (log.IsDebugEnabled)
					log.Debug("Inserting rows of collection: " + MessageHelper.InfoString(this, id, Factory));

				try
				{
					// insert all the new entries
					collection.PreInsert(this);
					IExpectation expectation = Expectations.AppropriateExpectation(insertCheckStyle);
					//bool callable = InsertCallable;
					bool useBatch = expectation.CanBeBatched;
					int i = 0;
					int count = 0;

					IEnumerable entries = collection.Entries(this);
					foreach (object entry in entries)
					{
						int offset = 0;
						if (collection.NeedsInserting(entry, i, elementType))
						{
							IDbCommand st = useBatch
							                	? session.Batcher.PrepareBatchCommand(SqlInsertRowString.CommandType, SqlInsertRowString.Text,
							                	                                      SqlInsertRowString.ParameterTypes)
							                	: session.Batcher.PrepareCommand(SqlInsertRowString.CommandType, SqlInsertRowString.Text,
							                	                                 SqlInsertRowString.ParameterTypes);
							try
							{
								//offset += expectation.Prepare(st, factory.ConnectionProvider.Driver);
								offset = WriteKey(st, id, offset, session);
								if (hasIdentifier && !isPostInsertIdentifier)
								{
									offset = WriteIdentifier(st, collection.GetIdentifier(entry, i), offset, session);
								}
								if (hasIndex)
								{
									offset = WriteIndex(st, collection.GetIndex(entry, i), offset, session);
								}
								WriteElement(st, collection.GetElement(entry), offset, session);
								if (useBatch)
								{
									session.Batcher.AddToBatch(expectation);
								}
								else
								{
									expectation.VerifyOutcomeNonBatched(session.Batcher.ExecuteNonQuery(st), st);
								}
								collection.AfterRowInsert(this, entry, i);
								count++;
							}
							catch (Exception e)
							{
								if (useBatch)
								{
									session.Batcher.AbortBatch(e);
								}
								throw;
							}
							finally
							{
								if (!useBatch)
								{
									session.Batcher.CloseCommand(st, null);
								}
							}
						}
						i++;
					}

					if (log.IsDebugEnabled)
					{
						log.Debug(string.Format("done inserting rows: {0} inserted", count));
					}
				}
				catch (DbException sqle)
				{
					throw ADOExceptionHelper.Convert(sqlExceptionConverter, sqle, "could not insert collection rows: " + MessageHelper.InfoString(this, id));
				}
			}
		}

		public bool HasOrphanDelete
		{
			get { return hasOrphanDelete; }
		}

		public IType ToType(string propertyName)
		{
			if ("index".Equals(propertyName))
				return indexType;

			return elementPropertyMapping.ToType(propertyName);
		}

		public string GetManyToManyFilterFragment(string alias, IDictionary<string, IFilter> enabledFilters)
		{
			StringBuilder buffer = new StringBuilder();
			manyToManyFilterHelper.Render(buffer, alias, enabledFilters);

			if (manyToManyWhereString != null)
			{
				buffer.Append(" and ").Append(StringHelper.Replace(manyToManyWhereTemplate, Template.Placeholder, alias));
			}

			return buffer.ToString();
		}

		public string[] ToColumns(string alias, string propertyName)
		{
			if ("index".Equals(propertyName))
			{
				if (IsManyToMany)
					throw new QueryException("index() function not supported for many-to-many association");

				return StringHelper.Qualify(alias, indexColumnNames);
			}
			return elementPropertyMapping.ToColumns(alias, propertyName);
		}

		public string[] ToColumns(string propertyName)
		{
			if ("index".Equals(propertyName))
			{
				if (IsManyToMany)
					throw new QueryException("index() function not supported for many-to-many association");

				return indexColumnNames;
			}

			return elementPropertyMapping.ToColumns(propertyName);
		}

		protected abstract SqlCommandInfo GenerateDeleteString();
		protected abstract SqlCommandInfo GenerateDeleteRowString();
		protected abstract SqlCommandInfo GenerateUpdateRowString();
		protected abstract SqlCommandInfo GenerateInsertRowString();

		public void UpdateRows(IPersistentCollection collection, object id, ISessionImplementor session)
		{
			if (!isInverse && collection.RowUpdatePossible)
			{
				if (log.IsDebugEnabled)
					log.Debug(string.Format("Updating rows of collection: {0}#{1}", role, id));

				// update all the modified entries
				int count = DoUpdateRows(id, collection, session);

				if (log.IsDebugEnabled)
					log.Debug(string.Format("done updating rows: {0} updated", count));
			}
		}

		protected abstract int DoUpdateRows(object key, IPersistentCollection collection, ISessionImplementor session);

		protected virtual string FilterFragment(string alias)
		{
			return HasWhere ? " and " + GetSQLWhereString(alias) : "";
		}

		public virtual string FilterFragment(string alias, IDictionary<string, IFilter> enabledFilters)
		{
			StringBuilder sessionFilterFragment = new StringBuilder();
			filterHelper.Render(sessionFilterFragment, alias, enabledFilters);

			return sessionFilterFragment.Append(FilterFragment(alias)).ToString();
		}

		public string OneToManyFilterFragment(string alias)
		{
			return string.Empty;
		}

		public override string ToString()
		{
			// Java has StringHelper.root( getClass().getName() ) instead of GetType().Name,
			// but it doesn't make sense to me.
			return string.Format("{0}({1})", GetType().Name, role);
		}

		public bool IsAffectedByEnabledFilters(ISessionImplementor session)
		{
			return
				filterHelper.IsAffectedBy(session.EnabledFilters)
				|| (IsManyToMany && manyToManyFilterHelper.IsAffectedBy(session.EnabledFilters));
		}

		public string[] GetCollectionPropertyColumnAliases(string propertyName, string suffix)
		{
			string[] rawAliases = (string[])collectionPropertyColumnAliases[propertyName];

			if (rawAliases == null)
			{
				return null;
			}

			string[] result = new string[rawAliases.Length];
			for (int i = 0; i < rawAliases.Length; i++)
			{
				result[i] = new Alias(suffix).ToUnquotedAliasString(rawAliases[i], dialect);
			}
			return result;
		}

⌨️ 快捷键说明

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