protected void CheckCurrentCommand() { if (_commandBuilder.ParameterCount >= _driver.SqlDialect.MaxParamCount) { FinalizeCurrentCommand(); _commandBuilder = new DataCommandBuilder(_driver, batchMode: true, mode: SqlGenMode.PreferLiteral); } }
private object ExecuteLinqNonQuery(EntitySession session, LinqCommand command, DataConnection conn) { var sql = SqlFactory.GetLinqSql(command); var fmtOptions = command.Options.IsSet(QueryOptions.NoParameters) ? SqlGenMode.NoParameters : SqlGenMode.PreferParam; var cmdBuilder = new DataCommandBuilder(this._driver); cmdBuilder.AddLinqStatement(sql, command.ParamValues); var dataCmd = cmdBuilder.CreateCommand(conn, DbExecutionType.NonQuery, sql.ResultProcessor); ExecuteDataCommand(dataCmd); return(dataCmd.ProcessedResult ?? dataCmd.Result); }
public object ExecuteLinqSelect(EntitySession session, LinqCommand command, DataConnection conn) { var sql = SqlFactory.GetLinqSql(command); var genMode = command.Options.IsSet(QueryOptions.NoParameters) ? SqlGenMode.NoParameters : SqlGenMode.PreferParam; var cmdBuilder = new DataCommandBuilder(this._driver, batchMode: false, mode: genMode); cmdBuilder.AddLinqStatement(sql, command.ParamValues); var dataCmd = cmdBuilder.CreateCommand(conn, DbExecutionType.Reader, sql.ResultProcessor); ExecuteDataCommand(dataCmd); return(dataCmd.ProcessedResult); }
private void SaveTableGroupRecordsOneByOne(DbUpdateTableGroup tableGrp, DataConnection conn, DbUpdateSet updateSet) { foreach (var rec in tableGrp.Records) { if (updateSet.InsertsIdentity && rec.EntityInfo.Flags.IsSet(EntityFlags.ReferencesIdentity)) { rec.RefreshIdentityReferences(); } var cmdBuilder = new DataCommandBuilder(this._driver); var sql = SqlFactory.GetCrudSqlForSingleRecord(tableGrp.Table, rec); cmdBuilder.AddRecordUpdate(sql, rec); var cmd = cmdBuilder.CreateCommand(conn, sql.ExecutionType, sql.ResultProcessor); ExecuteDataCommand(cmd); } }
public DbBatch Build(DbUpdateSet updateSet) { _batch = new DbBatch() { UpdateSet = updateSet }; _commandBuilder = new DataCommandBuilder(_driver, batchMode: true, mode: SqlGenMode.PreferLiteral); AddScheduledCommands(updateSet.Session.ScheduledCommandsAtStart); foreach (var group in updateSet.UpdateGroups) { foreach (var tableGroup in group.TableGroups) { AddUpdateGroup(tableGroup); }// foreach group } AddScheduledCommands(updateSet.Session.ScheduledCommandsAtEnd); FinalizeCurrentCommand(completed: true); return(_batch); }
private void SaveChangesNoBatch(DbUpdateSet updateSet) { var session = updateSet.Session; var conn = updateSet.Connection; var withTrans = conn.DbTransaction == null && updateSet.UseTransaction; try { LogComment(session, "-- SaveChanges starting, {0} records ------------", updateSet.Records.Count); var start = _timeService.ElapsedMilliseconds; if (withTrans) { conn.BeginTransaction(commitOnSave: true); } //execute commands ExecuteScheduledCommands(conn, session, session.ScheduledCommandsAtStart); //Apply record updates foreach (var grp in updateSet.UpdateGroups) { foreach (var tableGrp in grp.TableGroups) { switch (tableGrp.Operation) { case LinqOperation.Insert: if (CanProcessMany(tableGrp)) { var cmdBuilder = new DataCommandBuilder(this._driver, mode: SqlGenMode.PreferLiteral); var sql = SqlFactory.GetCrudInsertMany(tableGrp.Table, tableGrp.Records, cmdBuilder); cmdBuilder.AddInsertMany(sql, tableGrp.Records); var cmd = cmdBuilder.CreateCommand(conn, sql.ExecutionType, sql.ResultProcessor); ExecuteDataCommand(cmd); } else { SaveTableGroupRecordsOneByOne(tableGrp, conn, updateSet); } break; case LinqOperation.Update: SaveTableGroupRecordsOneByOne(tableGrp, conn, updateSet); break; case LinqOperation.Delete: if (CanProcessMany(tableGrp)) { var cmdBuilder = new DataCommandBuilder(this._driver); var sql = SqlFactory.GetCrudDeleteMany(tableGrp.Table); cmdBuilder.AddDeleteMany(sql, tableGrp.Records, new object[] { tableGrp.Records }); var cmd = cmdBuilder.CreateCommand(conn, DbExecutionType.NonQuery, sql.ResultProcessor); ExecuteDataCommand(cmd); } else { SaveTableGroupRecordsOneByOne(tableGrp, conn, updateSet); } break; } } //foreach tableGrp } //Execute scheduled commands ExecuteScheduledCommands(conn, session, session.ScheduledCommandsAtEnd); if (conn.DbTransaction != null && conn.Flags.IsSet(DbConnectionFlags.CommitOnSave)) { conn.Commit(); } var end = _timeService.ElapsedMilliseconds; LogComment(session, "-- SaveChanges completed. Records: {0}, Time: {1} ms. ------------", updateSet.Records.Count, end - start); ReleaseConnection(conn); } catch { ReleaseConnection(conn, inError: true); throw; } }