protected override async Task <int> DoUpdateRowsAsync(object id, IPersistentCollection collection, ISessionImplementor session, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (ArrayHelper.IsAllFalse(elementColumnIsSettable)) { return(0); } try { DbCommand st = null; IExpectation expectation = Expectations.AppropriateExpectation(UpdateCheckStyle); //bool callable = UpdateCallable; bool useBatch = expectation.CanBeBatched; IEnumerable entries = collection.Entries(this); int i = 0; int count = 0; foreach (object entry in entries) { if (await(collection.NeedsUpdatingAsync(entry, i, ElementType, cancellationToken)).ConfigureAwait(false)) { int offset = 0; if (useBatch) { if (st == null) { st = await(session.Batcher.PrepareBatchCommandAsync(SqlUpdateRowString.CommandType, SqlUpdateRowString.Text, SqlUpdateRowString.ParameterTypes, cancellationToken)).ConfigureAwait(false); } } else { st = await(session.Batcher.PrepareCommandAsync(SqlUpdateRowString.CommandType, SqlUpdateRowString.Text, SqlUpdateRowString.ParameterTypes, cancellationToken)).ConfigureAwait(false); } try { //offset += expectation.Prepare(st, Factory.ConnectionProvider.Driver); int loc = await(WriteElementAsync(st, collection.GetElement(entry), offset, session, cancellationToken)).ConfigureAwait(false); if (hasIdentifier) { await(WriteIdentifierAsync(st, collection.GetIdentifier(entry, i), loc, session, cancellationToken)).ConfigureAwait(false); } else { loc = await(WriteKeyAsync(st, id, loc, session, cancellationToken)).ConfigureAwait(false); if (HasIndex && !indexContainsFormula) { await(WriteIndexToWhereAsync(st, collection.GetIndex(entry, i, this), loc, session, cancellationToken)).ConfigureAwait(false); } else { await(WriteElementToWhereAsync(st, collection.GetSnapshotElement(entry, i), 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); } } catch (OperationCanceledException) { throw; } catch (Exception e) { if (useBatch) { session.Batcher.AbortBatch(e); } throw; } finally { if (!useBatch) { session.Batcher.CloseCommand(st, null); } } count++; } i++; } return(count); } catch (DbException sqle) { throw ADOExceptionHelper.Convert(SQLExceptionConverter, sqle, "could not update collection rows: " + MessageHelper.CollectionInfoString(this, collection, id, session), SqlUpdateRowString.Text); } }
protected override async Task <int> DoUpdateRowsAsync(object id, IPersistentCollection collection, ISessionImplementor session, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); // we finish all the "removes" first to take care of possible unique // constraints and so that we can take better advantage of batching try { const int offset = 0; int count = 0; if (RowDeleteEnabled) { IExpectation deleteExpectation = Expectations.AppropriateExpectation(DeleteCheckStyle); bool useBatch = deleteExpectation.CanBeBatched; SqlCommandInfo sql = SqlDeleteRowString; // update removed rows fks to null int i = 0; IEnumerable entries = collection.Entries(this); foreach (object entry in entries) { if (await(collection.NeedsUpdatingAsync(entry, i, ElementType, cancellationToken)).ConfigureAwait(false)) { DbCommand st; // will still be issued when it used to be null if (useBatch) { st = await(session.Batcher.PrepareBatchCommandAsync(SqlDeleteRowString.CommandType, sql.Text, SqlDeleteRowString.ParameterTypes, cancellationToken)).ConfigureAwait(false); } else { st = await(session.Batcher.PrepareCommandAsync(SqlDeleteRowString.CommandType, sql.Text, SqlDeleteRowString.ParameterTypes, cancellationToken)).ConfigureAwait(false); } try { int loc = await(WriteKeyAsync(st, id, offset, session, cancellationToken)).ConfigureAwait(false); // No columnNullness handling: the element is the entity key and should not contain null // values. await(WriteElementToWhereAsync(st, collection.GetSnapshotElement(entry, i), null, loc, session, cancellationToken)).ConfigureAwait(false); if (useBatch) { await(session.Batcher.AddToBatchAsync(deleteExpectation, cancellationToken)).ConfigureAwait(false); } else { deleteExpectation.VerifyOutcomeNonBatched(await(session.Batcher.ExecuteNonQueryAsync(st, cancellationToken)).ConfigureAwait(false), st); } } catch (OperationCanceledException) { throw; } catch (Exception e) { if (useBatch) { session.Batcher.AbortBatch(e); } throw; } finally { if (!useBatch && st != null) { session.Batcher.CloseCommand(st, null); } } count++; } i++; } } if (RowInsertEnabled) { IExpectation insertExpectation = Expectations.AppropriateExpectation(InsertCheckStyle); //bool callable = InsertCallable; bool useBatch = insertExpectation.CanBeBatched; SqlCommandInfo sql = SqlInsertRowString; // now update all changed or added rows fks int i = 0; IEnumerable entries = collection.Entries(this); foreach (object entry in entries) { if (await(collection.NeedsUpdatingAsync(entry, i, ElementType, cancellationToken)).ConfigureAwait(false)) { DbCommand st; if (useBatch) { st = await(session.Batcher.PrepareBatchCommandAsync(SqlInsertRowString.CommandType, sql.Text, SqlInsertRowString.ParameterTypes, cancellationToken)).ConfigureAwait(false); } else { st = await(session.Batcher.PrepareCommandAsync(SqlInsertRowString.CommandType, sql.Text, SqlInsertRowString.ParameterTypes, cancellationToken)).ConfigureAwait(false); } try { //offset += insertExpectation.Prepare(st, Factory.ConnectionProvider.Driver); int loc = await(WriteKeyAsync(st, id, offset, session, cancellationToken)).ConfigureAwait(false); if (HasIndex && !indexContainsFormula) { loc = await(WriteIndexToWhereAsync(st, collection.GetIndex(entry, i, this), loc, session, cancellationToken)).ConfigureAwait(false); } // No columnNullness handling: the element is the entity key and should not contain null // values. await(WriteElementToWhereAsync(st, collection.GetElement(entry), null, loc, session, cancellationToken)).ConfigureAwait(false); if (useBatch) { await(session.Batcher.AddToBatchAsync(insertExpectation, cancellationToken)).ConfigureAwait(false); } else { insertExpectation.VerifyOutcomeNonBatched(await(session.Batcher.ExecuteNonQueryAsync(st, cancellationToken)).ConfigureAwait(false), st); } } catch (OperationCanceledException) { throw; } catch (Exception e) { if (useBatch) { session.Batcher.AbortBatch(e); } throw; } finally { if (!useBatch && st != null) { session.Batcher.CloseCommand(st, null); } } count++; } i++; } } return(count); } catch (DbException sqle) { throw ADOExceptionHelper.Convert(SQLExceptionConverter, sqle, "could not update collection rows: " + MessageHelper.CollectionInfoString(this, collection, id, session)); } }