Esempio n. 1
0
    /// <summary>
    /// DataCloner Hello World program
    /// </summary>
    static void Main(string[] args)
    {
        //Minimal configuration
        var project = new Project()
        {
            ConnectionStrings = new List <Connection>
            {
                new Connection {
                    Id = "UNI", ProviderName = "System.Data.SqlClient", ConnectionString = @"Data Source=(localdb)\MSSQLLocalDB;Integrated Security=True;"
                }
            }
        };

        //Creating an execution plan for reuse later
        var builder = new ExecutionPlanBuilder(project, null);

        builder.StatusChanged += Builder_StatusChanged;

        Console.WriteLine("Retreiving rows to the execution plan");

        NbRowsFetch = 0;
        var startTime = DateTime.Now.Ticks;

        builder.Append(new RowIdentifier
        {
            ServerId = "UNI",
            Database = "Chinook",
            Schema   = "dbo",
            Table    = "Customer",
            Columns  = new ColumnsWithValue {
                { "CustomerId", 1 }
            }
        });
        var endTime = DateTime.Now.Ticks;

        //Creating a mew clone of the data inside the database
        var query = builder.Compile();

        query.Commiting += Query_Commiting;
        query.Execute();

        //Results
        var msElapsed = new TimeSpan(endTime - startTime).TotalMilliseconds;

        Console.WriteLine($"Rows fetched : {NbRowsFetch}");
        Console.WriteLine($"Completed in : {msElapsed} ms");
        Console.WriteLine($"Row per second : {NbRowsFetch / (msElapsed / 1000)}");

        Console.ReadKey();
    }
Esempio n. 2
0
        private void InitClonerWorker()
        {
            _cloneWorker = new BackgroundWorker();
            _cloneWorker.WorkerReportsProgress = true;
            _cloneWorker.RunWorkerCompleted   += (s, e) =>
            {
                var sbLog     = new StringBuilder();
                var paramsOut = e.Result as ClonerWorkerOutputArgs;

                sbLog.Append("\rCloning completed in : ")
                .Append(DateTime.Now.Subtract(paramsOut.StartDate).ToString("hh':'mm':'ss'.'fff"))
                .Append("\r");

                if (chkSimulation.IsChecked.GetValueOrDefault())
                {
                    sbLog.AppendLine("Simulation mode : No clone appended to the database.");
                }
                else
                {
                    foreach (var row in paramsOut.ClonedRow)
                    {
                        sbLog.Append("New clone : ")
                        .Append(row.Database).Append(".").Append(row.Schema)
                        .Append(".").Append(row.Table).Append(" : (");

                        foreach (var col in row.Columns)
                        {
                            var sqlVar = col.Value as SqlVariable;
                            sbLog.Append(col.Key).Append("=").Append(sqlVar ?? col.Value).Append(", ");
                        }

                        sbLog.Remove(sbLog.Length - 2, 2);
                        sbLog.Append(")").Append("\r");
                    }
                }

                sbLog.Append("\r");
                rtbStatus.AppendText(sbLog.ToString());
                rtbStatus.ScrollToEnd();

                BtnAppend.IsEnabled = BtnExec.IsEnabled = BtnReset.IsEnabled = true;
            };
            _cloneWorker.ProgressChanged += (s, e) =>
            {
                var statusArgs = e.UserState as StatusChangedEventArgs;
                if (statusArgs != null)
                {
                    StatusChanged_event(s, statusArgs);
                }

                var queryArgs = e.UserState as QueryCommitingEventArgs;
                if (queryArgs != null)
                {
                    QueryCommiting_event(s, queryArgs);
                }
            };
            _cloneWorker.DoWork += (s, arg) =>
            {
                var paramsIn  = arg.Argument as ClonerWorkerInputArgs;
                var paramsOut = new ClonerWorkerOutputArgs
                {
                    StartDate = DateTime.Now,
                    ClonedRow = new List <RowIdentifier>()
                };

                var source = new RowIdentifier();
                source.Columns.Clear();
                source.ServerId = paramsIn.Server;
                source.Database = paramsIn.Database;
                source.Schema   = paramsIn.Schema;
                source.Table    = paramsIn.Table;
                source.Columns  = paramsIn.Columns;

                if (paramsIn.AppendMode)
                {
                    _executionPlanBuilder.Append(source, true);
                    _lastQuery = null;
                }
                else
                {
                    if (_lastQuery == null)
                    {
                        _lastQuery            = _executionPlanBuilder.Compile();
                        _lastQuery.Commiting += ClonerWorkerQueryCommiting_event;
                    }

                    //Clone
                    for (int i = 0; i < paramsIn.NbCopies; i++)
                    {
                        paramsOut.ClonedRow.AddRange(_lastQuery.Execute().Results);
                    }
                }

                arg.Result = paramsOut;
            };
        }