Beispiel #1
0
 public override void Check(ICheckNotifier notifier)
 {
     if (!RemoteSource.Discover(DataAccessContext.DataLoad).Exists())
     {
         throw new Exception("Database " + RemoteSource.Database + " did not exist on the remote server");
     }
 }
Beispiel #2
0
        public override ExitCodeType Attach(IDataLoadJob job, GracefulCancellationToken cancellationToken)
        {
            if (job == null)
            {
                throw new Exception("Job is Null, we require to know the job to build a DataFlowPipeline");
            }

            string sql;

            var dbFrom = RemoteSource.Discover(DataAccessContext.DataLoad);

            var remoteTables = new HashSet <string>(dbFrom.DiscoverTables(true).Select(t => t.GetRuntimeName()), StringComparer.CurrentCultureIgnoreCase);
            var loadables    = job.RegularTablesToLoad.Union(job.LookupTablesToLoad).ToArray();

            var syntaxFrom = dbFrom.Server.GetQuerySyntaxHelper();

            foreach (var tableInfo in loadables)
            {
                var table = tableInfo.GetRuntimeName();
                if (!remoteTables.Contains(table))
                {
                    throw new Exception("Loadable table " + table + " was NOT found on the remote DB!");
                }

                if (LoadRawColumnsOnly)
                {
                    var rawColumns = LoadRawColumnsOnly ? tableInfo.GetColumnsAtStage(LoadStage.AdjustRaw) : tableInfo.ColumnInfos;
                    sql = "SELECT " + String.Join(",", rawColumns.Select(c =>
                                                                         syntaxFrom.EnsureWrapped(c.GetRuntimeName(LoadStage.AdjustRaw)))) + " FROM " + syntaxFrom.EnsureWrapped(table);
                }
                else
                {
                    sql = "SELECT * FROM " + syntaxFrom.EnsureWrapped(table);
                }

                job.OnNotify(this, new NotifyEventArgs(ProgressEventType.Information, "About to execute SQL:" + Environment.NewLine + sql));

                var source = new DbDataCommandDataFlowSource(sql, "Fetch data from " + dbFrom + " to populate RAW table " + table, dbFrom.Server.Builder, Timeout == 0 ? 50000 : Timeout);

                var destination = new SqlBulkInsertDestination(_dbInfo, table, Enumerable.Empty <string>());

                var contextFactory = new DataFlowPipelineContextFactory <DataTable>();
                var context        = contextFactory.Create(PipelineUsage.LogsToTableLoadInfo | PipelineUsage.FixedDestination);

                var engine = new DataFlowPipelineEngine <DataTable>(context, source, destination, job);

                ITableLoadInfo loadInfo = job.DataLoadInfo.CreateTableLoadInfo("Truncate RAW table " + table,
                                                                               _dbInfo.Server.Name + "." + _dbInfo.GetRuntimeName(),
                                                                               new[]
                {
                    new DataSource(
                        "Remote SqlServer Servername=" + dbFrom.Server + ";Database=" + _dbInfo.GetRuntimeName() +

                        //Either list the table or the query depending on what is populated
                        (table != null ? " Table=" + table : " Query = " + sql), DateTime.Now)
                }, -1);

                engine.Initialize(loadInfo);
                engine.ExecutePipeline(new GracefulCancellationToken());

                if (source.TotalRowsRead == 0)
                {
                    job.OnNotify(this, new NotifyEventArgs(ProgressEventType.Information, "No rows were read from the remote table " + table + "."));
                }

                job.OnNotify(this, new NotifyEventArgs(source.TotalRowsRead > 0 ? ProgressEventType.Information : ProgressEventType.Warning, "Finished after reading " + source.TotalRowsRead + " rows"));
            }

            return(ExitCodeType.Success);
        }