Пример #1
0
        public void AddUpdateGroup(DbUpdateTableGroup group)
        {
            SqlStatement sql = null;

            switch (group.Operation)
            {
            case LinqOperation.Insert:
                if (_db.CanProcessMany(group))
                {
                    // TODO: handle the case when there are too many params within 1 insert command
                    var recGroups = GroupRecordsForInsertMany(group.Records, _driver.SqlDialect.MaxRecordsInInsertMany);
                    foreach (var recGroup in recGroups)
                    {
                        sql = _sqlFactory.GetCrudInsertMany(group.Table, recGroup, _commandBuilder);
                        _commandBuilder.AddInsertMany(sql, recGroup);
                    } //foreach
                }
                else
                {
                    foreach (var rec in group.Records)
                    {
                        CheckCurrentCommand();
                        sql = sql ?? _sqlFactory.GetCrudSqlForSingleRecord(group.Table, rec);
                        _commandBuilder.AddRecordUpdate(sql, rec);
                    }
                }
                break;

            case LinqOperation.Update:
                foreach (var rec in group.Records)
                {
                    CheckCurrentCommand();
                    sql = _sqlFactory.GetCrudSqlForSingleRecord(group.Table, rec);
                    _commandBuilder.AddRecordUpdate(sql, rec);
                }
                break;

            case LinqOperation.Delete:
                if (_db.CanProcessMany(group))
                {
                    sql = _sqlFactory.GetCrudDeleteMany(group.Table);
                    _commandBuilder.AddDeleteMany(sql, group.Records, new object[] { group.Records });
                }
                else
                {
                    foreach (var rec in group.Records)
                    {
                        CheckCurrentCommand();
                        sql = sql ?? _sqlFactory.GetCrudSqlForSingleRecord(group.Table, rec);
                        _commandBuilder.AddRecordUpdate(sql, rec);
                    }
                }
                break;
            }//switch
        }
Пример #2
0
        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;
            }
        }