Esempio n. 1
0
        private async Task CopyTablesToDestinationAsync(CopyToArguments args)
        {
            try {
                int counter = 1;

                foreach (TableModel table in Tables)
                {
                    if (!string.IsNullOrEmpty(Error))
                    {
                        break;
                    }

                    Message = $"Copying Table {counter} of {Tables.Count} ({table.Name})";
                    try {
                        await table.CopyToAsync(args, (rows) => {
                            Message = $"Copying Table {counter} of {Tables.Count} ({table.Name}) - ({rows} Rows Copied) ";
                        }).ConfigureAwait(false);

                        counter++;
                    } catch (Exception ex) {
                        Error = $"Faled to copy table [{table.Name}] - {ex.Message}";
                    }
                }

                CommitTransactionIfNoErrors(args);
            } catch (Exception ex) {
                Error = ex.Message;
            }
        }
Esempio n. 2
0
        private async Task CreateTableAsync(CopyToArguments args, Action <long> callback)
        {
            string sql = await ScriptTableAsync(args.SourceModel, args.DatabaseName);

            CurrentReportCallback = callback;
            await ExecuteSQL(sql, args.Connection, args.Transaction);
        }
Esempio n. 3
0
 private async Task CopyDatabaseObjectToAsync(ScriptedModel Model, CopyToArguments args)
 {
     try {
         await Model.CopyToAsync(args).ConfigureAwait(false);
     } catch (Exception ex) {
         Error = $"Faled to copy {Model} - {ex.Message}";
     }
 }
Esempio n. 4
0
        private async Task CopyDatabaseObjectsAsync(CopyToArguments args)
        {
            await CopyTablesToDestinationAsync(args).ConfigureAwait(false);

            await CopyRoutinesToDestinationAsync(args).ConfigureAwait(false);

            await CopyViewsToDestinationAsync(args).ConfigureAwait(false);
        }
Esempio n. 5
0
 private void CommitTransactionIfNoErrors(CopyToArguments args)
 {
     if (string.IsNullOrEmpty(Error))
     {
         args.Transaction.Commit();
         args.Transaction = args.Connection.BeginTransaction();
     }
 }
Esempio n. 6
0
        internal override async Task CopyToAsync(CopyToArguments args, Action <long> callback = null)
        {
            await CreateTableAsync(args, callback);

            if (args.CopyData)
            {
                await Task.Factory.StartNew(() => {
                    using (SqlConnection sourceConnection = new SqlConnection(args.SourceModel.BuildConnection(args.DatabaseName))) {
                        sourceConnection.Open();
                        using (SqlCommand cmd = new SqlCommand($"SELECT * FROM [{Schema}].[{Name}]", sourceConnection)) {
                            using (SqlDataReader reader = cmd.ExecuteReader()) {
                                CopyTableData(args.Connection, args.Transaction, reader);
                            }
                        }
                    }
                }).ConfigureAwait(false);
            }
        }
Esempio n. 7
0
        private async Task CopyRoutinesToDestinationAsync(CopyToArguments args)
        {
            try {
                foreach (RoutineModel routine in Routines)
                {
                    if (!string.IsNullOrEmpty(Error))
                    {
                        break;
                    }

                    Message = $"Copying ({routine.Type}) {routine.Name}";
                    await CopyDatabaseObjectToAsync(routine, args);
                }

                CommitTransactionIfNoErrors(args);
            } catch (Exception ex) {
                Error = ex.Message;
            }
        }
Esempio n. 8
0
        private async Task CopyViewsToDestinationAsync(CopyToArguments args)
        {
            try {
                foreach (ScriptedModel view in Views)
                {
                    if (!string.IsNullOrEmpty(Error))
                    {
                        break;
                    }

                    Message = $"Copying View {view.Name}";
                    await CopyDatabaseObjectToAsync(view, args);
                }

                CommitTransactionIfNoErrors(args);
            } catch (Exception ex) {
                Error = ex.Message;
            }
        }
Esempio n. 9
0
        internal async Task CopyToAsync(ConnectionModel destinationConnection, IEnumerable <DataBaseModel> destinationDatabases, bool copyTables, bool copyData, CancellationToken token)
        {
            StartOperation();
            try {
                await RecreateDatabase(destinationConnection, destinationDatabases, token);

                using (SqlConnection connection = new SqlConnection(destinationConnection.BuildConnection(name))) {
                    await connection.OpenAsync().ConfigureAwait(false);

                    CopyToArguments copyArguments = CreateArguments(destinationConnection, copyData, connection, token);
                    await CopyTables(copyArguments, copyTables);
                }
            } catch (Exception ex) {
                Error = ex.Message;
                throw;
            } finally {
                IsBusy  = false;
                Message = string.Empty;
            }
        }
Esempio n. 10
0
        private async Task CopyTables(CopyToArguments copyArguments, bool copyTables)
        {
            if (!copyTables)
            {
                return;
            }

            await CopyDatabaseObjectsAsync(copyArguments);

            if (string.IsNullOrEmpty(Error))
            {
                copyArguments.Transaction.Commit();
            }
            else
            {
                copyArguments.Transaction.Rollback();
            }

            if (!string.IsNullOrEmpty(Error))
            {
                throw new Exception(Error);
            }
        }
Esempio n. 11
0
 internal virtual async Task CopyToAsync(CopyToArguments args, Action <long> callback = null)
 {
     await ExecuteSQL(Script, args.Connection, args.Transaction);
 }