Exemplo n.º 1
0
        public async Task InsertRowsAsync(IPersistentCollection collection, object id, ISessionImplementor session, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (!isInverse && RowInsertEnabled)
            {
                if (log.IsDebugEnabled())
                {
                    log.Debug("Inserting rows of collection: {0}", MessageHelper.CollectionInfoString(this, collection, id, session));
                }

                try
                {
                    // insert all the new entries
                    await(collection.PreInsertAsync(this, cancellationToken)).ConfigureAwait(false);
                    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 (await(collection.NeedsInsertingAsync(entry, i, elementType, cancellationToken)).ConfigureAwait(false))
                        {
                            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())
                    {
                        log.Debug("done inserting rows: {0} inserted", count);
                    }
                }
                catch (DbException sqle)
                {
                    throw ADOExceptionHelper.Convert(sqlExceptionConverter, sqle,
                                                     "could not insert collection rows: " + MessageHelper.CollectionInfoString(this, collection, id, session));
                }
            }
        }