Пример #1
0
        public IEnumerable <TableQuerySummary> RolledBackExecute(SqlReplicationScriptResult scriptResult)
        {
            var identifiers = scriptResult.Data.SelectMany(x => x.Value).Select(x => x.DocumentKey).Distinct().ToList();

            // first, delete all the rows that might already exist there
            foreach (var sqlReplicationTable in _sqlReplication.Configuration.SqlReplicationTables)
            {
                var commands = new List <DbCommand>();
                DeleteItems(sqlReplicationTable.TableName, sqlReplicationTable.DocumentKeyColumn, _sqlReplication.Configuration.ParameterizeDeletesDisabled,
                            identifiers, commands.Add);
                yield return(TableQuerySummary.GenerateSummaryFromCommands(sqlReplicationTable.TableName, commands));
            }

            foreach (var sqlReplicationTable in _sqlReplication.Configuration.SqlReplicationTables)
            {
                List <ItemToReplicate> dataForTable;
                if (scriptResult.Data.TryGetValue(sqlReplicationTable.TableName, out dataForTable) == false)
                {
                    continue;
                }
                var commands = new List <DbCommand>();
                InsertItems(sqlReplicationTable.TableName, sqlReplicationTable.DocumentKeyColumn, dataForTable, commands.Add);

                yield return(TableQuerySummary.GenerateSummaryFromCommands(sqlReplicationTable.TableName, commands));
            }

            _tx.Rollback();
        }
Пример #2
0
        public IEnumerable <string> SimulateExecuteCommandText(SqlReplicationScriptResult scriptResult)
        {
            foreach (var sqlReplicationTable in _sqlReplication.Configuration.SqlReplicationTables)
            {
                if (sqlReplicationTable.InsertOnlyMode)
                {
                    continue;
                }

                // first, delete all the rows that might already exist there
                foreach (string deleteQuery in GenerateDeleteItemsCommandText(sqlReplicationTable.TableName, sqlReplicationTable.DocumentKeyColumn, _sqlReplication.Configuration.ParameterizeDeletesDisabled,
                                                                              scriptResult.Keys))
                {
                    yield return(deleteQuery);
                }
            }

            foreach (var sqlReplicationTable in _sqlReplication.Configuration.SqlReplicationTables)
            {
                List <ItemToReplicate> dataForTable;
                if (scriptResult.Data.TryGetValue(sqlReplicationTable.TableName, out dataForTable) == false)
                {
                    continue;
                }

                foreach (string insertQuery in GenerteInsertItemCommandText(sqlReplicationTable.TableName, sqlReplicationTable.DocumentKeyColumn, dataForTable))
                {
                    yield return(insertQuery);
                }
            }
        }
 public SqlReplicationPatchDocument(DocumentDatabase database, DocumentsOperationContext context, SqlReplicationScriptResult scriptResult, SqlReplicationConfiguration config, string documentKey)
     : base(database)
 {
     _context          = context;
     this.scriptResult = scriptResult;
     this.config       = config;
     this._documentKey = documentKey;
 }
Пример #4
0
        public SqlReplicationScriptResult ApplyConversionScript(List <Document> documents, DocumentsOperationContext context)
        {
            var result = new SqlReplicationScriptResult();

            foreach (var replicatedDoc in documents)
            {
                _cancellationTokenSource.Token.ThrowIfCancellationRequested();
                var patcher = new SqlReplicationPatchDocument(_database, context, result, Configuration, replicatedDoc.Key);
                try
                {
                    var scope = patcher.Apply(context, replicatedDoc, new PatchRequest {
                        Script = Configuration.Script
                    });

                    if (_logger.IsInfoEnabled && scope.DebugInfo.Count > 0)
                    {
                        _logger.Info(string.Format("Debug output for doc: {0} for script {1}:\r\n.{2}", replicatedDoc.Key, Configuration.Name, string.Join("\r\n", scope.DebugInfo.Items)));
                    }

                    Statistics.ScriptSuccess();
                }
                catch (ParseException e)
                {
                    Statistics.MarkScriptAsInvalid(_database, Configuration.Script);

                    if (_logger.IsInfoEnabled)
                    {
                        _logger.Info("Could not parse SQL Replication script for " + Configuration.Name, e);
                    }

                    return(result);
                }
                catch (Exception diffExceptionName)
                {
                    Statistics.RecordScriptError(_database, diffExceptionName);
                    if (_logger.IsInfoEnabled)
                    {
                        _logger.Info("Could not process SQL Replication script for " + Configuration.Name + ", skipping document: " + replicatedDoc.Key, diffExceptionName);
                    }
                }
            }
            return(result);
        }
Пример #5
0
        public bool ExecuteScript(SqlReplicationScriptResult scriptResult)
        {
            foreach (var sqlReplicationTable in _sqlReplication.Configuration.SqlReplicationTables)
            {
                // first, delete all the rows that might already exist there
                if (sqlReplicationTable.InsertOnlyMode == false)
                {
                    DeleteItems(sqlReplicationTable.TableName, sqlReplicationTable.DocumentKeyColumn, _sqlReplication.Configuration.ParameterizeDeletesDisabled, scriptResult.Keys);
                }

                List <ItemToReplicate> dataForTable;
                if (scriptResult.Data.TryGetValue(sqlReplicationTable.TableName, out dataForTable))
                {
                    InsertItems(sqlReplicationTable.TableName, sqlReplicationTable.DocumentKeyColumn, dataForTable);
                }
            }

            Commit();

            return(hadErrors == false);
        }
Пример #6
0
        public DynamicJsonValue Simulate(SimulateSqlReplication simulateSqlReplication, DocumentsOperationContext context, SqlReplicationScriptResult result)
        {
            if (simulateSqlReplication.PerformRolledBackTransaction)
            {
                using (var writer = new RelationalDatabaseWriter(_database, context, _predefinedSqlConnection, this))
                {
                    return(new DynamicJsonValue
                    {
                        ["Results"] = new DynamicJsonArray(writer.RolledBackExecute(result).ToArray()),
                        ["LastAlert"] = Statistics.LastAlert,
                    });
                }
            }

            var simulatedwriter     = new RelationalDatabaseWriterSimulator(_predefinedSqlConnection, this);
            var tableQuerySummaries = new List <RelationalDatabaseWriter.TableQuerySummary>
            {
                new RelationalDatabaseWriter.TableQuerySummary
                {
                    Commands = simulatedwriter.SimulateExecuteCommandText(result)
                               .Select(x => new RelationalDatabaseWriter.TableQuerySummary.CommandData
                    {
                        CommandText = x
                    }).ToArray()
                }
            }.ToArray();

            return(new DynamicJsonValue
            {
                ["Results"] = new DynamicJsonArray(tableQuerySummaries),
                ["LastAlert"] = Statistics.LastAlert,
            });
        }