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

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

                try
                {
                    // delete all the deleted entries
                    var offset = 0;
                    var count  = 0;

                    foreach (var entry in await(collection.GetDeletesAsync(this, !deleteByIndex, cancellationToken)).ConfigureAwait(false))
                    {
                        DbCommand st;
                        var       expectation = Expectations.AppropriateExpectation(deleteCheckStyle);
                        //var callable = DeleteCallable;
                        var commandInfo = GetDeleteCommand(deleteByIndex, entry, out var columnNullness);

                        var useBatch = expectation.CanBeBatched;
                        if (useBatch)
                        {
                            st = await(session.Batcher.PrepareBatchCommandAsync(
                                           commandInfo.CommandType, commandInfo.Text, commandInfo.ParameterTypes, cancellationToken)).ConfigureAwait(false);
                        }
                        else
                        {
                            st = await(session.Batcher.PrepareCommandAsync(
                                           commandInfo.CommandType, commandInfo.Text, commandInfo.ParameterTypes, cancellationToken)).ConfigureAwait(false);
                        }
                        try
                        {
                            var loc = offset;
                            if (hasIdentifier)
                            {
                                await(WriteIdentifierAsync(st, entry, loc, session, cancellationToken)).ConfigureAwait(false);
                            }
                            else
                            {
                                loc = await(WriteKeyAsync(st, id, loc, session, cancellationToken)).ConfigureAwait(false);

                                if (deleteByIndex)
                                {
                                    await(WriteIndexToWhereAsync(st, entry, loc, session, cancellationToken)).ConfigureAwait(false);
                                }
                                else
                                {
                                    await(WriteElementToWhereAsync(st, entry, columnNullness, loc, session, cancellationToken)).ConfigureAwait(false);
                                }
                            }
                            if (useBatch)
                            {
                                await(session.Batcher.AddToBatchAsync(expectation, cancellationToken)).ConfigureAwait(false);
                            }
                            else
                            {
                                expectation.VerifyOutcomeNonBatched(await(session.Batcher.ExecuteNonQueryAsync(st, cancellationToken)).ConfigureAwait(false), st);
                            }
                            count++;
                        }
                        catch (OperationCanceledException) { throw; }
                        catch (Exception e)
                        {
                            if (useBatch)
                            {
                                session.Batcher.AbortBatch(e);
                            }
                            throw;
                        }
                        finally
                        {
                            if (!useBatch)
                            {
                                session.Batcher.CloseCommand(st, null);
                            }
                        }
                    }

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