Exemplo n.º 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));
                }
            }
        }
Exemplo n.º 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 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(this, !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 DeleteRows(IPersistentCollection collection, object id, ISessionImplementor session)
		{
			if (!isInverse)
			{
				if (log.IsDebugEnabled)
				{
					log.Debug("Deleting rows of collection: " + MessageHelper.InfoString(this, id));
				}

				bool deleteByIndex = !IsOneToMany && hasIndex /* TODO H3: && !indexContainsFormula*/;

				try
				{
					// delete all the deleted entries
					ICollection deletes = collection.GetDeletes(elementType, !deleteByIndex);
					if (deletes.Count > 0)
					{
						int offset = 0;
						int count = 0;
						IExpectation expectation = Expectations.AppropriateExpectation(deleteCheckStyle);
						bool useBatch = expectation.CanBeBatched;
						IDbCommand st = null;

						try
						{
							foreach (object entry in deletes)
							{
								if (useBatch)
								{
									if (st != null)
									{
										st = session.Batcher.PrepareBatchCommand(
											SqlDeleteRowString.CommandType,
											SqlDeleteRowString.Text,
											SqlDeleteRowString.ParameterTypes);
									}
								}
								else
								{
									st = session.Batcher.PrepareCommand(
										SqlDeleteRowString.CommandType,
										SqlDeleteRowString.Text,
										SqlDeleteRowString.ParameterTypes);
								}
								//offset += expectation.Prepare(st, factory.ConnectionProvider.Driver);
								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 (HibernateException)
				{
					// Do not call Convert on HibernateExceptions
					throw;
				}
				catch (Exception sqle)
				{
					throw Convert(sqle, "could not delete collection rows: " + MessageHelper.InfoString(this, id));
				}
			}
		}