protected override int DoUpdateRows(object id, IPersistentCollection collection, ISessionImplementor session) { // we finish all the "removes" first to take care of possible unique // constraints and so that we can take better advantage of batching try { int count = 0; if (RowDeleteEnabled) { bool useBatch = true; IDbCommand st = null; // update removed rows fks to null try { int i = 0; IEnumerable entries = collection.Entries(this); int offset = 0; IExpectation expectation = Expectations.None; foreach (object entry in entries) { if (collection.NeedsUpdating(entry, i, ElementType)) { // will still be issued when it used to be null if (st == null) { SqlCommandInfo sql = SqlDeleteRowString; if (DeleteCallable) { expectation = Expectations.AppropriateExpectation(DeleteCheckStyle); useBatch = expectation.CanBeBatched; st = useBatch ? session.Batcher.PrepareBatchCommand(SqlDeleteRowString.CommandType, sql.Text, SqlDeleteRowString.ParameterTypes) : session.Batcher.PrepareCommand(SqlDeleteRowString.CommandType, sql.Text, SqlDeleteRowString.ParameterTypes); //offset += expectation.prepare(st); } else { st = session.Batcher.PrepareBatchCommand(SqlDeleteRowString.CommandType, sql.Text, SqlDeleteRowString.ParameterTypes); } } int loc = WriteKey(st, id, offset, session); WriteElementToWhere(st, collection.GetSnapshotElement(entry, i), loc, session); if (useBatch) { session.Batcher.AddToBatch(expectation); } else { expectation.VerifyOutcomeNonBatched(session.Batcher.ExecuteNonQuery(st), st); } count++; } i++; } } catch (Exception e) { if (useBatch) { session.Batcher.AbortBatch(e); } throw; } finally { if (!useBatch && st != null) { session.Batcher.CloseCommand(st, null); } } } if (RowInsertEnabled) { IExpectation expectation = Expectations.AppropriateExpectation(InsertCheckStyle); //bool callable = InsertCallable; bool useBatch = expectation.CanBeBatched; SqlCommandInfo sql = SqlInsertRowString; IDbCommand st = null; // now update all changed or added rows fks try { int i = 0; IEnumerable entries = collection.Entries(this); foreach (object entry in entries) { int offset = 0; if (collection.NeedsUpdating(entry, i, ElementType)) { if (useBatch) { if (st == null) { st = session.Batcher.PrepareBatchCommand(SqlInsertRowString.CommandType, sql.Text, SqlInsertRowString.ParameterTypes); } } else { st = session.Batcher.PrepareCommand(SqlInsertRowString.CommandType, sql.Text, SqlInsertRowString.ParameterTypes); } //offset += expectation.Prepare(st, Factory.ConnectionProvider.Driver); int loc = WriteKey(st, id, offset, session); if (HasIndex && !indexContainsFormula) { loc = WriteIndexToWhere(st, collection.GetIndex(entry, i, this), loc, session); } WriteElementToWhere(st, collection.GetElement(entry), loc, session); if (useBatch) { session.Batcher.AddToBatch(expectation); } else { expectation.VerifyOutcomeNonBatched(session.Batcher.ExecuteNonQuery(st), st); } count++; } i++; } } catch (Exception e) { if (useBatch) { session.Batcher.AbortBatch(e); } throw; } finally { if (st != null) { session.Batcher.CloseCommand(st, null); } } } return(count); } catch (DbException sqle) { throw ADOExceptionHelper.Convert(SQLExceptionConverter, sqle, "could not update collection rows: " + MessageHelper.InfoString(this, id)); } }
protected override int DoUpdateRows(object id, IPersistentCollection collection, ISessionImplementor session) { // 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; IDbCommand st = null; // update removed rows fks to null try { int i = 0; IEnumerable entries = collection.Entries(this); foreach (object entry in entries) { if (collection.NeedsUpdating(entry, i, ElementType)) { // will still be issued when it used to be null if (useBatch) { st = session.Batcher.PrepareBatchCommand(SqlDeleteRowString.CommandType, sql.Text, SqlDeleteRowString.ParameterTypes); } else { st = session.Batcher.PrepareCommand(SqlDeleteRowString.CommandType, sql.Text, SqlDeleteRowString.ParameterTypes); } int loc = WriteKey(st, id, offset, session); WriteElementToWhere(st, collection.GetSnapshotElement(entry, i), loc, session); if (useBatch) { session.Batcher.AddToBatch(deleteExpectation); } else { deleteExpectation.VerifyOutcomeNonBatched(session.Batcher.ExecuteNonQuery(st), st); } count++; } i++; } } catch (Exception e) { if (useBatch) { session.Batcher.AbortBatch(e); } throw; } finally { if (!useBatch && st != null) { session.Batcher.CloseCommand(st, null); } } } if (RowInsertEnabled) { IExpectation insertExpectation = Expectations.AppropriateExpectation(InsertCheckStyle); //bool callable = InsertCallable; bool useBatch = insertExpectation.CanBeBatched; SqlCommandInfo sql = SqlInsertRowString; IDbCommand st = null; // now update all changed or added rows fks try { int i = 0; IEnumerable entries = collection.Entries(this); foreach (object entry in entries) { if (collection.NeedsUpdating(entry, i, ElementType)) { if (useBatch) { st = session.Batcher.PrepareBatchCommand(SqlInsertRowString.CommandType, sql.Text, SqlInsertRowString.ParameterTypes); } else { st = session.Batcher.PrepareCommand(SqlInsertRowString.CommandType, sql.Text, SqlInsertRowString.ParameterTypes); } //offset += insertExpectation.Prepare(st, Factory.ConnectionProvider.Driver); int loc = WriteKey(st, id, offset, session); if (HasIndex && !indexContainsFormula) { loc = WriteIndexToWhere(st, collection.GetIndex(entry, i, this), loc, session); } WriteElementToWhere(st, collection.GetElement(entry), loc, session); if (useBatch) { session.Batcher.AddToBatch(insertExpectation); } else { insertExpectation.VerifyOutcomeNonBatched(session.Batcher.ExecuteNonQuery(st), st); } count++; } i++; } } catch (Exception e) { if (useBatch) { session.Batcher.AbortBatch(e); } throw; } finally { if (!useBatch && st != null) { session.Batcher.CloseCommand(st, null); } } } return count; } catch (DbException sqle) { throw ADOExceptionHelper.Convert(SQLExceptionConverter, sqle, "could not update collection rows: " + MessageHelper.CollectionInfoString(this, collection, id, session)); } }
protected override int DoUpdateRows(object id, IPersistentCollection collection, ISessionImplementor session) { if (ArrayHelper.IsAllFalse(elementColumnIsSettable)) return 0; try { IDbCommand 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 (collection.NeedsUpdating(entry, i, ElementType)) { int offset = 0; if (useBatch) { if (st == null) { st = session.Batcher.PrepareBatchCommand(SqlUpdateRowString.CommandType, SqlUpdateRowString.Text, SqlUpdateRowString.ParameterTypes); } } else { st = session.Batcher.PrepareCommand(SqlUpdateRowString.CommandType, SqlUpdateRowString.Text, SqlUpdateRowString.ParameterTypes); } try { //offset += expectation.Prepare(st, Factory.ConnectionProvider.Driver); int loc = WriteElement(st, collection.GetElement(entry), offset, session); if (hasIdentifier) { WriteIdentifier(st, collection.GetIdentifier(entry, i), loc, session); } else { loc = WriteKey(st, id, loc, session); if (HasIndex && !indexContainsFormula) { WriteIndexToWhere(st, collection.GetIndex(entry, i, this), loc, session); } else { WriteElementToWhere(st, collection.GetSnapshotElement(entry, i), loc, session); } } if (useBatch) { session.Batcher.AddToBatch(expectation); } else { expectation.VerifyOutcomeNonBatched(session.Batcher.ExecuteNonQuery(st), st); } } 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.InfoString(this, id), SqlUpdateRowString.Text); } }
protected override int DoUpdateRows(object id, IPersistentCollection collection, ISessionImplementor session) { 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 (collection.NeedsUpdating(entry, i, ElementType)) { int offset = 0; if (useBatch) { if (st == null) { st = session.Batcher.PrepareBatchCommand(SqlUpdateRowString.CommandType, SqlUpdateRowString.Text, SqlUpdateRowString.ParameterTypes); } } else { st = session.Batcher.PrepareCommand(SqlUpdateRowString.CommandType, SqlUpdateRowString.Text, SqlUpdateRowString.ParameterTypes); } try { //offset += expectation.Prepare(st, Factory.ConnectionProvider.Driver); int loc = WriteElement(st, collection.GetElement(entry), offset, session); if (hasIdentifier) { WriteIdentifier(st, collection.GetIdentifier(entry, i), loc, session); } else { loc = WriteKey(st, id, loc, session); if (HasIndex && !indexContainsFormula) { WriteIndexToWhere(st, collection.GetIndex(entry, i, this), loc, session); } else { WriteElementToWhere(st, collection.GetSnapshotElement(entry, i), loc, session); } } if (useBatch) { session.Batcher.AddToBatch(expectation); } else { expectation.VerifyOutcomeNonBatched(session.Batcher.ExecuteNonQuery(st), st); } } 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 int DoUpdateRows(object id, IPersistentCollection collection, ISessionImplementor session) { // we finish all the "removes" first to take care of possible unique // constraints and so that we can take better advantage of batching IDbCommand st = null; IEnumerable entries; int i; try { // update removed rows fks to null IExpectation expectation = Expectations.AppropriateExpectation(DeleteCheckStyle); bool useBatch = expectation.CanBeBatched; int count = 0; try { i = 0; entries = collection.Entries(); int offset = 0; foreach (object entry in entries) { if (collection.NeedsUpdating(entry, i, ElementType)) // will still be issued when it used to be null { 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 = WriteKey(st, id, offset, session); WriteElementToWhere(st, collection.GetSnapshotElement(entry, i), loc, session); if (useBatch) { session.Batcher.AddToBatch(expectation); } else { expectation.VerifyOutcomeNonBatched(session.Batcher.ExecuteNonQuery(st), st); } count++; } i++; } } catch (Exception e) { if (useBatch) { session.Batcher.AbortBatch(e); } throw; } finally { if (!useBatch && st != null) { session.Batcher.CloseCommand(st, null); } } // now update all changed or added rows fks count = 0; st = null; try { expectation = Expectations.AppropriateExpectation(DeleteCheckStyle); useBatch = expectation.CanBeBatched; i = 0; entries = collection.Entries(); foreach (object entry in entries) { int offset = 0; if (collection.NeedsUpdating(entry, i, ElementType)) // will still be issued when it used to be null { 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 (HasIndex /* TODO H3: && !indexContainsFormula*/) { loc = WriteIndexToWhere(st, collection.GetIndex(entry, i), loc, session); } WriteElementToWhere(st, collection.GetElement(entry), loc, session); if (useBatch) { session.Batcher.AddToBatch(expectation); } else { expectation.VerifyOutcomeNonBatched(session.Batcher.ExecuteNonQuery(st), st); } count++; } i++; } } catch (Exception e) { if (useBatch) { session.Batcher.AbortBatch(e); } throw; } finally { if (st != null) { session.Batcher.CloseCommand(st, null); } } return count; } catch (HibernateException) { // Do not call Convert on HibernateExceptions throw; } catch (Exception sqle) { throw Convert(sqle, "could not update collection rows: " + MessageHelper.InfoString(this, id)); } }