示例#1
0
        private void Audit(IPersistentCollection col, bool recreate)
        {
            var ce       = _sessionImpl.PersistenceContext.GetCollectionEntry(col);
            var entities = col.Entries(ce.CurrentPersister).Cast <object>().ToArray();

            // we don't support non-entity collections... (Jury?)
            if (!ce.CurrentPersister.ElementType.IsEntityType)
            {
                return;
            }

            for (var i = 0; i < entities.Length; i++)
            {
                var entity = entities[i];
                if (recreate || col.NeedsInserting(entity, i, ce.CurrentPersister.ElementType))
                {
                    _audit.Collections.Add(AuditCollection(col, ce, entity, true));
                }
            }

            if (!recreate)
            {
                foreach (var entity in col.GetDeletes(ce.CurrentPersister, true))
                {
                    _audit.Collections.Add(AuditCollection(col, ce, entity, false));
                }
            }
        }
示例#2
0
        private void Audit(IPersistentCollection col)
        {
            var ce       = _sessionImpl.PersistenceContext.GetCollectionEntry(col);
            var entities = col.Entries(ce.CurrentPersister).Cast <object>().ToArray();

            for (var i = 0; i < entities.Length; i++)
            {
                var entity = entities[i];
                if (col.NeedsInserting(entity, i, ce.CurrentPersister.ElementType))
                {
                    _audit.Collections.Add(AuditCollection(col, entity, true));
                }
            }

            foreach (var entity in col.GetDeletes(ce.CurrentPersister, true))
            {
                _audit.Collections.Add(AuditCollection(col, entity, false));
            }
        }
		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)
					{
						if (collection.NeedsInserting(entry, i, elementType))
						{
							object entryId;
							if (!IsIdentifierAssignedByInsert)
							{
								// NH Different implementation: write once
								entryId = PerformInsert(id, collection, expectation, entry, i, useBatch, false, session);
							}
							else
							{
								entryId = PerformInsert(id, collection, entry, i, session);
							}
							collection.AfterRowInsert(this, entry, i, entryId);
							count++;
						}
						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 void InsertRows(IPersistentCollection collection, object id, ISessionImplementor session)
		{
			if (!isInverse)
			{
				if (log.IsDebugEnabled)
				{
					log.Debug("Inserting rows of collection: " + role + "#" + id);
				}

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

					IEnumerable entries = collection.Entries();
					foreach (object entry in entries)
					{
						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);
								int loc = WriteKey(st, id, offset, session);
								if (hasIdentifier)
								{
									loc = WriteIdentifier(st, collection.GetIdentifier(entry, i), loc, session);
								}
								if (hasIndex /*&& !indexIsFormula*/)
								{
									loc = WriteIndex(st, collection.GetIndex(entry, i), loc, session);
								}
								loc = 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 rows: {0} inserted", count));
					}
				}
				catch (HibernateException)
				{
					// Do not call Convert on HibernateExceptions
					throw;
				}
				catch (Exception sqle)
				{
					throw Convert(sqle, "could not insert collection rows: " + MessageHelper.InfoString(this, id));
				}
			}
		}