private void OutputSpecial()
        {
            if (credentialsPhase != CredentialsPhase.None)
            {
                OutputCredentialsPrompt();
                return; //early exit, prompt rendered
            }

            if (currentPrompt != null)
            {
                if ((DateTime.Now - promptTime).TotalSeconds > 30)
                {
                    currentPrompt = null;
                }
                else
                {
                    OutputCurrentPrompt();
                    return; //early exit, prompt rendered
                }
            }

            OutputCurrentProgress();
        }
Ejemplo n.º 2
0
        public bool Synchronize(BackgroundWorker worker, List <WorkspaceBase> workspaces, DbTransaction trans)
        {
            Dictionary <WorkspaceBase, List <string> > scriptStrings = new Dictionary <WorkspaceBase, List <string> >();

            int  totalProgress = 0;
            bool ignorePrompt  = false;

            // for drops remove fks first
            var dropTables = from ws in workspaces.OfType <DropWorkspace>()
                             where ws.TargetObject is Table && (ws.TargetObject as Table).ForeignKeys.Any()
                             select ws.TargetObject as Table;

            if (dropTables.Any())
            {
                using (var cmd = trans.Connection.CreateCommand())
                {
                    cmd.Transaction = trans;
                    foreach (var t in dropTables)
                    {
                        t.RemoveForeignKeys(cmd);
                    }
                }
            }

            // create tables first
            List <SynchronizationItem> foreignKeys = new List <SynchronizationItem>();

            foreach (var ws in workspaces)
            {
                if (!ws.SynchronizationItems.Any())
                {
                    continue;
                }

                scriptStrings.Add(ws, new List <string>());
                var allScripts = new Dictionary <int, StringBuilder>();
                foreach (var item in ws.SynchronizationItems)
                {
                    if (!item.Omit && item.DatabaseObject is ForeignKey)
                    {
                        foreignKeys.Add(item);
                        totalProgress++;
                        item.Omit = true;
                    }

                    if (item.Omit)
                    {
                        continue;
                    }

                    // TODO: need others?
                    if (ws is DropWorkspace)
                    {
                        if (item.DatabaseObject.Synchronized)
                        {
                            continue;
                        }

                        item.DatabaseObject.Synchronized = true;
                    }

                    foreach (var kvp in item.Scripts)
                    {
                        if (kvp.Value.Length <= 0)
                        {
                            continue;
                        }
                        if (!allScripts.ContainsKey(kvp.Key))
                        {
                            allScripts.Add(kvp.Key, new StringBuilder());
                        }
                        allScripts[kvp.Key].AppendLine(kvp.Value.ToString());
                    }
                }
                foreach (var kvp in allScripts.OrderBy(s => s.Key))
                {
                    if (kvp.Value.Length <= 0)
                    {
                        continue;
                    }
                    totalProgress++;
                    scriptStrings[ws].Add(kvp.Value.ToString());
                }
            }

            using (var cmd = trans.Connection.CreateCommand())
            {
                cmd.Transaction = trans;

                int i = 0;
                foreach (var kvp in scriptStrings)
                {
                    foreach (var script in kvp.Value)
                    {
                        i++;
                        worker.ReportProgress(100 * i / totalProgress, string.Format("Synchronizing {0} {1} of {2}", kvp.Key.ToString(), i.ToString(), totalProgress.ToString()));
                        cmd.CommandText    = script;
                        cmd.CommandTimeout = 600;
                        try
                        {
                            cmd.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {
                            if (!ignorePrompt)
                            {
                                var args = new PromptEventArgs("Failed to synchronize. Continue? \"" + (kvp.Key is WorkspaceWithSourceBase && kvp.Key.TargetObject == null ? (kvp.Key as WorkspaceWithSourceBase).SourceObject.ObjectName : kvp.Key.TargetObject.ObjectName)
                                                               + "\": " + ex.Message + "\r\n\r\n" + script + ".");
                                Prompt(this, args);
                                switch (args.Result)
                                {
                                case PromptResult.No:
                                    return(false);

                                case PromptResult.Yes:
                                    break;

                                case PromptResult.YesToAll:
                                    ignorePrompt = true;
                                    break;
                                }
                            }
                        }
                    }
                }

                foreach (var key in foreignKeys)
                {
                    i++;
                    worker.ReportProgress(100 * i / totalProgress, string.Format("Synchronizing {0} {1} of {2}", key.ToString(), i.ToString(), totalProgress.ToString()));
                    foreach (var script in key.Scripts.OrderBy(s => s.Key))
                    {
                        cmd.CommandText    = script.Value.ToString();
                        cmd.CommandTimeout = 600;
                        try
                        {
                            cmd.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {
                            if (!ignorePrompt)
                            {
                                var args = new PromptEventArgs("Failed to synchronize \"" + key.ToString()
                                                               + "\": " + ex.Message + ".");
                                Prompt(this, args);
                                switch (args.Result)
                                {
                                case PromptResult.No:
                                    return(false);

                                case PromptResult.YesToAll:
                                    ignorePrompt = true;
                                    break;

                                case PromptResult.Yes:
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return(true);
        }
        protected override void SetupApplication()
        {
            RenderSplashScreen();

            app.SetupMiningEngineEvents();
            app.LoadPreviousHistory();

            app.DataModified += (object sender, EventArgs e) =>
            {
                ScreenDirty = true;
            };

            app.ConfigurationModified += (object sender, EventArgs e) =>
            {
                ScreenDirty = true;
            };

            app.ProgressStarted += (object sender, ProgressEventArgs e) =>
            {
                currentProgress = e.Text;
                RenderScreen();
            };

            app.ProgressCompleted += (object sender, EventArgs e) =>
            {
                currentProgress = String.Empty;
                RenderScreen();
            };

            app.NotificationReceived += (object sender, NotificationEventArgs e) =>
            {
                notifications.Add(e);
                ScreenDirty = true;
            };

            app.NotificationDismissed += (object sender, NotificationEventArgs e) =>
            {
                notifications.RemoveAll(n => !String.IsNullOrEmpty(n.Id) && n.Id.Equals(e.Id));
                ScreenDirty = true;
            };

            app.PromptReceived += (object sender, PromptEventArgs e) =>
            {
                currentPrompt = e;
                promptTime    = DateTime.Now;
                RenderScreen();
            };

            app.CredentialsRequested += (object sender, CredentialsEventArgs e) =>
            {
                e.CredentialsProvided = false;
                credentialsPhase      = CredentialsPhase.Username;
                credentialsTarget     = app.LocalViewModel.GetNetworkDeviceByFriendlyName(e.ProtectedResource);
                RenderScreen();
            };

            app.Context = threadContext;

            mineOnStartTimer.Elapsed += (object sender, ElapsedEventArgs e) =>
            {
                if (app.StartupMiningCountdownSeconds > 0)
                {
                    currentProgress = string.Format("Mining will start automatically in {0} seconds...", app.StartupMiningCountdownSeconds);
                }
                else
                {
                    currentProgress          = String.Empty;
                    mineOnStartTimer.Enabled = false;
                }
                RenderScreen();
            };

            RegisterCommands();
            RegisterScreens();
            RegisterSettings();
        }
Ejemplo n.º 4
0
        public bool Generate(BackgroundWorker worker, List <TableWorkspace> workspaces)
        {
            using (var conn = new SqlConnection(DataSource.ConnectionString))
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    int totalCount = workspaces.Select(w => w.AddRowCount).Sum();
                    int curr       = 0;

                    var keys        = workspaces.Where(t => t.RemoveAddKeys).ToList();
                    var truncDelete = workspaces.Where(t => t.Truncate || t.Delete).ToList();

                    using (var cmd = conn.CreateCommand())
                    {
                        cmd.Transaction = trans;
                        foreach (var table in keys)
                        {
                            worker.ReportProgress(100 * curr / keys.Count, "Removing foreign keys for " + table.Table.TableName);
                            table.Table.RemoveForeignKeys(cmd);
                            curr++;
                        }

                        curr = 0;
                        foreach (var table in truncDelete)
                        {
                            worker.ReportProgress(100 * curr / truncDelete.Count, "Truncating/Deleting " + table.Table.TableName);
                            table.Table.TruncateDelete(table.Table.Database, cmd, table.Truncate);
                            curr++;
                        }
                    }

                    curr = 0;
                    var ds = new DataSet();
                    foreach (var ws in workspaces.Where(x => x.AddRowCount > 0))
                    {
                        try
                        {
                            List <ColumnWorkspace> addedCols = new List <ColumnWorkspace>();
                            foreach (var col in ws.ColumnWorkspaces)
                            {
                                if (col.Content != null && !(col.Content is NoContent))
                                {
                                    addedCols.Add(col);
                                }
                            }


                            if (worker.CancellationPending)
                            {
                                trans.Rollback();
                                return(false);
                            }

                            var da = new SqlDataAdapter(string.Format("select {2} from [{0}].[{1}] where 1 = 2", ws.Table.Schema.SchemaName, ws.Table.TableName,
                                                                      string.Join(", ", addedCols.Select(c => string.Format("[{0}]", c.Column.ColumnName)).ToArray())),
                                                        conn);
                            var dt = new DataTable();
                            da.SelectCommand.Transaction = trans;
                            da.Fill(dt);

                            string lbl = "Inserting into " + ws.Table.ToString();

                            for (int i = 0; i < ws.AddRowCount; i++)
                            {
                                worker.ReportProgress(100 * curr++ / totalCount, lbl);
                                var dr = dt.NewRow();
                                foreach (var col in addedCols)
                                {
                                    var colObj = col.Content.GetContent(trans);
                                    dr[col.Column.ColumnName] = colObj == null ? DBNull.Value : colObj;
                                }
                                dt.Rows.Add(dr);

                                if (worker.CancellationPending)
                                {
                                    trans.Rollback();
                                    return(false);
                                }
                            }

                            da.InsertCommand = new SqlCommand(string.Format("insert into [{0}].[{1}] ({2}) values ({3})",
                                                                            ws.Table.Schema.SchemaName, ws.Table.TableName,
                                                                            string.Join(", ", addedCols.Select(c => string.Format("[{0}]", c.Column.ColumnName)).ToArray()),
                                                                            string.Join(", ", addedCols.Select(c => string.Format("@{0}", c.Column.ColumnName)).ToArray())), conn);
                            da.InsertCommand.Transaction = trans;

                            foreach (var col in addedCols)
                            {
                                da.InsertCommand.Parameters.Add("@" + col.Column.ColumnName, col.Content.DbType, col.Column.CharacterMaximumLength.GetValueOrDefault(), col.Column.ColumnName);
                            }

                            da.Update(dt);
                        }
                        catch (Exception ex)
                        {
                            var args = new PromptEventArgs("Failed to generate data for " + ws.Table.TableName + ": " + ex.Message + ". Continue?");
                            Prompt(this, args);
                            if (args.Result == PromptResult.No)
                            {
                                trans.Rollback();
                                return(false);
                            }
                        }
                    }

                    curr = 0;
                    using (var cmd = conn.CreateCommand())
                    {
                        cmd.Transaction = trans;
                        foreach (var table in keys)
                        {
                            curr++;
                            worker.ReportProgress(100 * curr / keys.Count, "Adding foreign keys for " + table.Table.TableName);
                            table.Table.AddForeignKeys(cmd);
                        }
                    }

                    trans.Commit();
                }
                conn.Close();
                SqlConnection.ClearPool(conn);
            }
            return(true);
        }
 private void SetPromptParameter2(object sender, PromptEventArgs args)
 {
     args.Value = "Argentina";
 }
 private void SetPromptParameter1(object sender, PromptEventArgs args)
 {
     args.Value = 28;
 }
Ejemplo n.º 7
0
 private static void PromptClosed(object sender, PromptEventArgs args)
 {
     Debug.Print("<Event=TestManager.PromptClosed,\tPrompt=" + args.Name + ",\tPrompt Name='" + args.Text + "'>");
 }
Ejemplo n.º 8
0
 void Prompt_Continue(object sender, PromptEventArgs args)
 {
     qf4net.QF.Instance.Publish(new PromptEvent(QFSignal.PromptContinue, Name));
 }