public MainCalcTables(DbTableLoader dbTableLoader, Dictionary <string, string> userInput)
 {
     this.dbTableLoader = dbTableLoader;
     this.userInput     = userInput;
     LoadDbTables();
     CreateCalcsTables();
 }
        private void SaveChanges()
        {
            JobEditting.ConnectionStringDestination = txtStringDestination.Text;
            JobEditting.ConnectionStringSource      = txtStringSource.Text;
            JobEditting.DialectDestination          = (BaseDbType.DbTypeSupported)cmdDest.SelectedIndex;
            JobEditting.DialectSource = (BaseDbType.DbTypeSupported)cmdSource.SelectedIndex;

            JobEditting.TablesAvailable = lstTables.Items.Cast <ListViewItem>().Select(s => s.Tag as TableInfo).ToList();


            //TODO: Fill schema info at this point

            try
            {
                using (DbCon db = new DbConnectionInfo(JobEditting.ConnectionStringSource, JobEditting.DialectSource).CreateConnection())
                {
                    DbTableLoader dbt = new DbTableLoader(db, null);
                    dbt.LoadTableSchemaBatch(JobEditting.TablesAvailable.Where(w => w.Checked).ToList());
                }
            }
            catch (Exception)
            {
            }
            JobEditting.IgnoreFields.Clear();

            foreach (string item in lstExFields.Items)
            {
                JobEditting.IgnoreFields.Add(item);
            }

            //JobEditting.Errors.Clear();

            //foreach (string erro in lstErros.Items)
            //{
            //    JobEditting.Errors.Add(erro);
            //}


            JobEditting.JobName               = txtJobName.Text;
            JobEditting.PostUpdateSQL         = txtPostUpdate.Text;
            JobEditting.RetrieveDataCondition = txtIgnoreCondition.Text;



            if (!Preferences.Settings.jobs.Contains(JobEditting))
            {
                Preferences.Settings.jobs.Add(JobEditting);
            }

            Preferences.Save();
        }
 private void LoadSchema()
 {
     try
     {
         lnkLoadSchema.Text = "Load Schema (...)";
         using (DbCon db = new DbConnectionInfo(replicationTaskInfo.ConnectionStringSource, replicationTaskInfo.DialectSource).CreateConnection())
         {
             DbTableLoader dbt = new DbTableLoader(db, null);
             dbt.LoadTableSchema(tableSyncInfo);
         }
         lnkLoadSchema.Text = "Load Schema (ok)";
     }
     catch (Exception)
     {
         lnkLoadSchema.Text = "Load Schema (failed)";
     }
 }
        public void Replicate(ReplicationTaskInfo job)
        {
            try
            {
                job.IsRunning = true;

                DbConnectionInfo s = new DbConnectionInfo(job.ConnectionStringSource, job.DialectSource);
                DbConnectionInfo d = new DbConnectionInfo(job.ConnectionStringDestination, job.DialectDestination);

                /*
                 * d.status += new DlgStatusMsg(r_status);
                 * d.statuspgb += new DlgStatusPgb(r_statuspgb);
                 * d.everros += new DlgStatusMsg(r_erros);
                 */

                string status = "Table {0} of {1} - {2}";

                int totalTables = job.TablesAvailable.Count(w => w.Checked);
                int curTable    = 1;

                using (DbCon dbSource = s.CreateConnection())
                    using (DbCon dbDest = d.CreateConnection())
                    {
                        foreach (TableInfo table in job.TablesAvailable.Where(w => w.Checked))
                        {
                            bool workDoneForThisTable = table.CompareEntireTableAtOnce;

                            long   curMinId      = 0;
                            long   curMaxId      = 0;
                            string columnKeyName = null;
                            long   maxId         = 0;

                            if (!table.CompareEntireTableAtOnce)
                            {
                                columnKeyName = table.ColumnKeyName;
                                if (!string.IsNullOrWhiteSpace(columnKeyName))
                                {
                                    maxId = dbSource.GetMax(table.TableName, columnKeyName);
                                }
                            }


                            do
                            {
                                curMinId = curMaxId;
                                curMaxId = curMinId + table.IdRangeSize;

                                messageHandler.SendStatus(string.Format(status, curTable, totalTables, "Loading " + table + " [" + curMinId + "-" + curMaxId + "]"));

                                //Load Source Table (Async)
                                DbTableLoader dblo1 = new DbTableLoader(dbSource, messageHandler);

                                Task <Table> taskTb1 = Task.Run(() =>
                                {
                                    return(dblo1.LoadTableData(table, null, columnKeyName, curMinId, curMaxId, job.RetrieveDataCondition));
                                });

                                //Load Destination Table (Sync)
                                DbTableLoader dblo2 = new DbTableLoader(dbDest, messageHandler);

                                Table tb2 = dblo2.LoadTableData(table, table, columnKeyName, curMinId, curMaxId, job.RetrieveDataCondition);

                                //Wait finish loading Source Table
                                taskTb1.Wait();
                                Table tb1 = taskTb1.Result;


                                if (tb1.Data.Count == 0)
                                {
                                    workDoneForThisTable = curMaxId > maxId;
                                }

                                messageHandler.SendStatus(string.Format(status, curTable, totalTables, "Syncing " + table));

                                //Execute Synchronization
                                DbSyncRunner dbSyncRunner = new DbSyncRunner(dbDest, messageHandler);
                                dbSyncRunner.OnProgress = delegate(double v, double max)
                                {
                                    job.OnProgress(CalcProgress(curMinId, curMaxId, maxId, v, max), 100);
                                };

                                dbSyncRunner.ExecuteInstructions(tb1, tb2);


                                job.OnProgress(CalcProgress(curMinId, curMaxId, maxId, 100, 100), 100);

                                if (Replicator.AbortReplication)
                                {
                                    throw new ApplicationException("Aborted");
                                }
                            } while (!workDoneForThisTable);

                            curTable++;
                        }
                    }

                messageHandler.SendStatus("Finished");
            }
            catch (Exception e)
            {
                messageHandler.SendError(e.Message);
                messageHandler.SendStatus("Error " + e.Message);
            }
            finally
            {
                job.LastTimeReplicated = DateTime.Now.Ticks;
                job.IsRunning          = false;
            }
        }