Exemplo n.º 1
0
        public BulkCopyResponse BulkCopy(BulkCopyRequest request)
        {
            var sourceParam = new Dictionary<string, object>();
            var destParam = new Dictionary<string, object>();
            try
            {
                if (request.BatchSize <= 0)
                {
                    request.BatchSize = 100;
                }
                _eventArgs = new BulkCopyProgressEventArgs {TotalWork = 0, CompletedWork = 0, Result = string.Empty};

                sourceParam.Add("ScriptType", new[] {ScriptType.Select});
                sourceParam.Add("SkipTables", request.Skip);
                _sourceDb = new DataSource(request, Position.Source, sourceParam);

                destParam.Add("ScriptType",
                    new[] {ScriptType.Recovery, ScriptType.Truncate, ScriptType.DisableConstraint});
                destParam.Add("SkipTables", request.Skip);
                _destinationDb = new DataSource(request, Position.Destination, destParam);
                ReportStatus("Initializing process...");
                if (request.CreateObjects)
                {
                    Parallel.Invoke(
                        () => _sourceDb.Initialize()
                        , () => _sourceDb.BuildObjects()
                        );
                    _destinationDb.Initialize();
                    ReportStatus("Executing schema.sql");
                    _destinationDb.ExecScript(Runtime.Path + "schema.sql");
                    ReportStatus("Executing tables.sql");
                    _destinationDb.ExecScript(Runtime.Path + "tables.sql");
                    _destinationDb.SetSourceScript(_sourceDb.GetSourceScript());
                }
                else
                {
                    Parallel.Invoke(
                        () => _sourceDb.Initialize()
                        , () => _destinationDb.Initialize()
                        );
                }
                if (request.CreateObjects)
                {
                }
                ReportStatus("Analyzing data...");

                var ds = _sourceDb.GetRowCount();
                var query = ds.Tables[0].AsEnumerable();
                var tables = query.Select(dr => dr.Field<string>("tablename")).ToArray();
                _eventArgs.TotalWork = (uint) tables.Length;
                var currentIndex = 1;
                var skip = _sourceDb.GetSkipList();
                if (!request.ResumeOnError && skip.Count > 0)
                {
                    foreach (var kvp in skip)
                    {
                        ReportStatus($"Error occured while processing  table [{kvp.Key}] --{kvp.Value} ...");
                    }
                    return null;
                }
                foreach (var table in tables)
                {
                    long rowsToBeStaged;
                    try
                    {
                        rowsToBeStaged =
                            query.Where(dr => dr.Field<string>("tablename").ToLower() == table.ToLower())
                                .Select(dr => dr.Field<long>("rowcount"))
                                .First();
                    }
                    catch
                    {
                        rowsToBeStaged = 0;
                    }
                    ReportStatus("Sending: [" + table + "]...");
                    if (rowsToBeStaged == 0 || skip.ContainsKey(table) ||
                        (request.Skip != null && request.Skip.Contains(table)))
                    {
                        if (skip.ContainsKey(table) || (request.Skip != null && request.Skip.Contains(table)))
                        {
                            ReportStatus("Skipping table [" + table + "] ...");
                        }
                        else
                        {
                            ReportStatus($"Transfering: [{table}] rows [{0}/{0}]...");
                        }
                        _eventArgs.CompletedWork += 1;
                        continue;
                    }
                    if (rowsToBeStaged > 0)
                    {
                        currentIndex = (int) Math.Floor(rowsToBeStaged/(decimal) request.BatchSize);
                        currentIndex = currentIndex > 0 ? currentIndex : 1;
                        if (rowsToBeStaged != currentIndex*request.BatchSize && rowsToBeStaged > request.BatchSize)
                            currentIndex++;
                    }

                    for (var i = 1; i <= currentIndex; i++)
                    {
                        try
                        {
                            var biLocalProvider = _sourceDb.GetProvider(table, i);
                            var biRemoteProvider = _destinationDb.GetProvider(table, i);

                            var agent = new BulkCopyAgent();
                            agent.ChangesSelected += agent_ChangesSelected;
                            agent.ChangesApplied += agent_ChangesApplied;
                            agent.LocalProvider = biLocalProvider;
                            agent.RemoteProvider = biRemoteProvider;

                            var writerows = agent.WriteToServer();

                            _changeTotal += Convert.ToUInt32(writerows);
                            _changeFailed += 0;
                            _changeApplied += Convert.ToUInt32(writerows);
                        }
                        catch (Exception ex)
                        {
                            ReportStatus(
                                $"Failed Transfering: [{table}] rows [{(rowsToBeStaged < i*request.BatchSize ? rowsToBeStaged : i*request.BatchSize)}/{rowsToBeStaged}]...\nError:{ex}");
                            if (request.ResumeOnError)
                            {
                                break;
                            }
                            {
                                throw;
                            }
                        }
                        ReportStatus(
                            $"Transfering: [{table}] rows [{(rowsToBeStaged < i*request.BatchSize ? rowsToBeStaged : i*request.BatchSize)}/{rowsToBeStaged}]...");
                    }
                    _eventArgs.CompletedWork += 1;
                }

                _eventArgs.Result = "";
                RaiseSessionProgress(_eventArgs);
                var status = new Dictionary<string, Dictionary<string, string>> {{"Skipped", skip}};
                _response = new BulkCopyResponse(_startTime, DateTime.Now, _changeTotal, _changeApplied, _changeFailed,
                    status);
            }
            catch (Exception ex)
            {
                WriteLine(ex.ToString());
                throw;
            }
            finally
            {
                _sourceDb.Finalize(null);
                destParam.Add("EnableConstraintCheck", true);
                if (request.CreateObjects)
                {
                    ReportStatus("Executing indexes.sql");
                    _destinationDb.ExecScript(Runtime.Path + "indexes.sql");
                    ReportStatus("Executing views.sql");
                    _destinationDb.ExecScript(Runtime.Path + "views.sql");
                    ReportStatus("Executing procs.sql");
                    _destinationDb.ExecScript(Runtime.Path + "procs.sql");
                    ReportStatus("Executing fks.sql");
                    _destinationDb.ExecScript(Runtime.Path + "fks.sql");
                }
                _destinationDb.Finalize(destParam);
            }

            return _response;
        }
Exemplo n.º 2
0
        private void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _eventArgs = null;
                }

                _disposed = true;
            }
        }
Exemplo n.º 3
0
 private void RaiseSessionProgress(BulkCopyProgressEventArgs e)
 {
     BulkCopyProgress?.Invoke(this, e);
 }
Exemplo n.º 4
0
 private static void bcm_BulkCopyProgress(object sender, BulkCopyProgressEventArgs e)
 {
     Log(e.Result);
 }