示例#1
0
        public async Task RecreateAsync(IPersistentCollection collection, object id, ISessionImplementor session, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (!isInverse && RowInsertEnabled)
            {
                if (log.IsDebugEnabled())
                {
                    log.Debug("Inserting collection: {0}", MessageHelper.CollectionInfoString(this, collection, id, session));
                }

                try
                {
                    IExpectation expectation = null;
                    bool         useBatch    = false;
                    int          i           = 0;
                    int          count       = 0;

                    // create all the new entries
                    foreach (var entry in collection.Entries(this))
                    {
                        // Init, if we're on the first element.
                        if (count == 0)
                        {
                            expectation = Expectations.AppropriateExpectation(insertCheckStyle);
                            await(collection.PreInsertAsync(this, cancellationToken)).ConfigureAwait(false);
                            //bool callable = InsertCallable;
                            useBatch = expectation.CanBeBatched;
                        }

                        if (collection.EntryExists(entry, i))
                        {
                            object entryId;
                            if (!IsIdentifierAssignedByInsert)
                            {
                                // NH Different implementation: write once
                                entryId = await(PerformInsertAsync(id, collection, expectation, entry, i, useBatch, false, session, cancellationToken)).ConfigureAwait(false);
                            }
                            else
                            {
                                entryId = await(PerformInsertAsync(id, collection, entry, i, session, cancellationToken)).ConfigureAwait(false);
                            }
                            collection.AfterRowInsert(this, entry, i, entryId);
                            count++;
                        }
                        i++;
                    }

                    if (log.IsDebugEnabled())
                    {
                        if (count > 0)
                        {
                            log.Debug("done inserting collection: {0} rows inserted", count);
                        }
                        else
                        {
                            log.Debug("collection was empty");
                        }
                    }
                }
                catch (DbException sqle)
                {
                    throw ADOExceptionHelper.Convert(sqlExceptionConverter, sqle,
                                                     "could not insert collection: " + MessageHelper.CollectionInfoString(this, collection, id, session));
                }
            }
        }
		public void Recreate(IPersistentCollection collection, object id, ISessionImplementor session)
		{
			if (!isInverse)
			{
				if (log.IsDebugEnabled)
				{
					log.Debug("Inserting collection: " + MessageHelper.InfoString(this, id));
				}

				try
				{
					// create all the new entries
					IEnumerable entries = collection.Entries();
					IExpectation expectation = Expectations.AppropriateExpectation(insertCheckStyle);
					bool useBatch = expectation.CanBeBatched;

					int i = 0;
					int count = 0;
					collection.PreInsert(this);

					IDbCommand st = null;
					foreach (object entry in entries)
					{
						if (collection.EntryExists(entry, i))
						{
							try
							{
								int offset = 0;
								if (useBatch)
								{
									if (st == null)
									{
										st = session.Batcher.PrepareBatchCommand(
											SqlInsertRowString.CommandType,
											SqlInsertRowString.Text,
											SqlInsertRowString.ParameterTypes);
									}
								}
								else
								{
									st = session.Batcher.PrepareCommand(
										SqlInsertRowString.CommandType,
										SqlInsertRowString.Text,
										SqlInsertRowString.ParameterTypes);
								}
								//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 collection: {0} rows inserted", count));
					}

					if (count == 0 && log.IsDebugEnabled)
					{
						log.Debug("collection was empty");
					}
				}
				catch (HibernateException)
				{
					// Do not call Convert on HibernateExceptions
					throw;
				}
				catch (Exception sqle)
				{
					throw Convert(sqle, "could not insert 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();
						IExpectation expectation = Expectations.AppropriateExpectation(insertCheckStyle);
						collection.PreInsert(this);
						//bool callable = InsertCallable;
						bool useBatch = expectation.CanBeBatched;
						int i = 0;
						int count = 0;

						while (entries.MoveNext())
						{
							object entry = entries.Current;
							if (collection.EntryExists(entry, i))
							{
								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 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 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));
                }
            }
        }