Example #1
0
    private void ModifyCurrentObject()
    {
      if (_bs.Current == null)
      {
        return;
      }

      DataRowView rw = _bs.Current as DataRowView;
      if (rw == null)
        return;


      int objId = (int)rw.Row.ItemArray[0];
      string objName = (string)rw.Row.ItemArray[2];
      string objType = (string)rw.Row.ItemArray[3];

      if (DBConstants.DoesObjectTypeHasScript(objType))
      {
        int type = DBConstants.GetDBObjectType(objType);
        string script = ScriptingHelper.GetAlterScript(_connParams.ConnectionString, _connParams.Database, objId, type);
        frmScriptEditor editor = ScriptEditorFactory.Create(objName, script, objId, type, _connParams, _connParams.Database);
        ScriptEditorFactory.ShowScriptEditor(editor);
      }

    }
Example #2
0
    private void PerformActionOnCurrentRow()
    {
      if (_bs.Current == null)
      {
        return;
      }

      DataRowView rw = _bs.Current as DataRowView;
      if (rw == null)
        return;

      int objId = (int)rw.Row.ItemArray[0];
      string objType = (string)rw.Row.ItemArray[3];
      string objName = (string)rw.Row.ItemArray[2];

      if (DBConstants.DoesObjectTypeHasScript(objType))
      {
        int type = DBConstants.GetDBObjectType(objType);
        string script = ScriptingHelper.GetAlterScript(_connParams, _connParams.Database, objId, type);
        frmScriptEditor editor = ScriptEditorFactory.Create(objName, script, objId, type, _connParams, _connParams.Database);
        ScriptEditorFactory.ShowScriptEditor(editor);
      }
      else if (DBConstants.DoesObjectTypeHoldsData(objType))
      {
        int type = DBConstants.GetDBObjectType(objType);
        string caption = objName + "{" + _connParams.InfoDbServer + "}";
        string script = " select * from [" + objName + "]";
        bool isReadOnly = (type == DBObjectType.View) ? true : false;

        frmDataViewer viewer = DataViewerFactory.CreateDataViewer(_connParams, _connParams.Database, objName, caption, script, isReadOnly, true);
        DataViewerFactory.ShowDataViewer(viewer);
      }

    }
Example #3
0
        private void SwitchToScriptEditor()
        {
            ConnectionParams cp = frmConnectionRepository.SelectSingleConnection(true, true);

            if (cp == null)
            {
                return;
            }

            string content  = this.Content;
            string caption  = this.Caption;
            string filePath = this.FileName;

            frmScriptEditor scriptEditor = ScriptEditorFactory.Create(caption, content, cp, filePath);

            scriptEditor.ContentModified    = base.ContentModified;
            scriptEditor.IsRecoveredContent = base.IsRecoveredContent;
            scriptEditor.ContentPersister   = ContentPersister;
            if (ContentPersister.ContentType == EditorContentType.SharedScript || ContentPersister.ContentType == EditorContentType.SharedSnippet)
            {
                scriptEditor.Icon = this.Icon;
            }

            if (base.ContentModified)
            {
                base.ContentModified = false;
            }

            ScriptEditorFactory.ShowScriptEditor(scriptEditor);
            this.Close();
        }
Example #4
0
        private void PerformActionOnFirstSelectedRow( )
        {
            if (grd.SelectedRows.Count == 0)
            {
                return;
            }

            int    objId   = -1;
            string objType = String.Empty;
            string objName = String.Empty;


            DataGridViewRow row = grd.SelectedRows[0];

            DataGridViewCell cellName  = row.Cells[0];
            DataGridViewCell cellType  = row.Cells[1];
            DataGridViewCell cellObjid = row.Cells[2];

            if (cellName.ValueType != typeof(string) || cellName.Value == null)
            {
                return;
            }

            if (cellType.ValueType != typeof(string) || cellType.Value == null)
            {
                return;
            }

            if (cellObjid.ValueType != typeof(int) || cellObjid.Value == null)
            {
                return;
            }

            objId   = (int)cellObjid.Value;
            objType = (string)cellType.Value;
            objName = (string)cellName.Value;

            if (DBConstants.DoesObjectTypeHasScript(objType))
            {
                int    type   = DBConstants.GetDBObjectType(objType);
                string script = String.Empty;
                using (SqlConnection conn = _connParams.CreateSqlConnection(true, false))
                {
                    script = ScriptingHelper.GetAlterScript(conn, objId, type);
                }
                frmScriptEditor editor = ScriptEditorFactory.Create(objName, script, objId, type, _connParams, _dbName);
                ScriptEditorFactory.ShowScriptEditor(editor);
            }
            else if (DBConstants.DoesObjectTypeHoldsData(objType))
            {
                int    type       = DBConstants.GetDBObjectType(objType);
                string caption    = objName + "{" + _dbName + " on " + _connParams.Server + "}";
                string script     = " select * from [" + objName + "]";
                bool   isReadOnly = (type == DBObjectType.View) ? true : false;

                frmDataViewer viewer = DataViewerFactory.CreateDataViewer(_connParams, _dbName, objName, caption, script, isReadOnly, true);
                DataViewerFactory.ShowDataViewer(viewer);
            }
        }
Example #5
0
        private void ModifySelectedObjects( )
        {
            if (grd.SelectedRows.Count == 0)
            {
                return;
            }

            int    objId   = -1;
            string objType = String.Empty;
            string objName = String.Empty;
            IList <frmScriptEditor> editors = new List <frmScriptEditor>();

            foreach (DataGridViewRow row in grd.SelectedRows)
            {
                DataGridViewCell cellName  = row.Cells[0];
                DataGridViewCell cellType  = row.Cells[1];
                DataGridViewCell cellObjid = row.Cells[2];

                if (cellName.ValueType != typeof(string) || cellName.Value == null)
                {
                    continue;
                }

                if (cellType.ValueType != typeof(string) || cellType.Value == null)
                {
                    continue;
                }

                if (cellObjid.ValueType != typeof(int) || cellObjid.Value == null)
                {
                    continue;
                }

                objId   = (int)cellObjid.Value;
                objType = (string)cellType.Value;
                objName = (string)cellName.Value;

                if (DBConstants.DoesObjectTypeHasScript(objType))
                {
                    int    type   = DBConstants.GetDBObjectType(objType);
                    string script = String.Empty;
                    using (SqlConnection conn = _connParams.CreateSqlConnection(true, false))
                    {
                        script = ScriptingHelper.GetAlterScript(conn, objId, type);
                    }
                    frmScriptEditor editor = ScriptEditorFactory.Create(objName, script, objId, type, _connParams, _dbName);
                    editors.Add(editor);
                }
            }

            foreach (frmScriptEditor editor in editors)
            {
                ScriptEditorFactory.ShowScriptEditor(editor);
            }
        }
Example #6
0
        private void OnAction_OpenFromFile_Execute(object sender, EventArgs e)
        {
            NodeData data = Program.MainForm.GetCurrentSelectedNodeDataFromObjectExplorer();

            if (data == null)
            {
                return;
            }
            frmScriptEditor editor = ScriptEditorFactory.OpenFile(String.Empty, data);

            ScriptEditorFactory.ShowScriptEditor(editor);
        }
Example #7
0
        private bool EditObjectInScriptEditor()
        {
            if (_objectInfo == null || _cp == null)
            {
                return(false);
            }

            frmScriptEditor editor = ScriptEditorFactory.Create(_objectInfo, _cp, _cp.Database);

            ScriptEditorFactory.ShowScriptEditor(editor);
            return(true);
        }
        private void OnAction_NewScript_Execute(object sender, EventArgs e)
        {
            NodeData data = Program.MainForm.GetCurrentSelectedNodeDataFromObjectExplorer();

            if (data == null)
            {
                return;
            }

            frmScriptEditor editor = ScriptEditorFactory.CreateScriptEditor(data);

            ScriptEditorFactory.ShowScriptEditor(editor);
        }
Example #9
0
        public void LoadScriptFile(string fileName, ConnectionParams cp)
        {
            frmScriptEditor frm = null;

            try
            {
                frm = ScriptEditorFactory.OpenFile(fileName, cp);
                ScriptEditorFactory.ShowScriptEditor(frm);
            }
            catch (Exception ex)
            {
                frm.Dispose();
                frm = null;
                throw ex;
            }
        }
Example #10
0
        public void CreateScriptEditor(string caption, string script, ConnectionParams cp)
        {
            frmScriptEditor frm = null;

            try
            {
                frm = ScriptEditorFactory.Create(caption, script, cp);
                ScriptEditorFactory.ShowScriptEditor(frm);
            }
            catch (Exception ex)
            {
                frm.Dispose();
                frm = null;
                throw ex;
            }
        }
Example #11
0
        private void PostAction(DbObjectScripterResult result, bool isCancelled)
        {
            string errors = String.Empty;

            if (result.errors != null && result.errors.Count > 0)
            {
                if (!isCancelled)
                {
                    lblStatus.Text = "Completed with errors!";
                }

                foreach (Exception ex in result.errors)
                {
                    errors += "- " + ex.Message.Replace("\n", " ").Replace("\r", " ") + "\r\n";
                }
            }
            else
            {
                if (!isCancelled)
                {
                    lblStatus.Text = "Completed. Press \"Start\" to script objects.";
                }
            }

            switch (_destination)
            {
            case ScriptDestination.Window:
                string          caption = "Database Objects [" + _connParams.Server + " {" + _connParams.Database + "} ]";
                frmScriptEditor frm     = ScriptEditorFactory.Create(caption, result.script, _connParams);
                ScriptEditorFactory.ShowScriptEditor(frm);
                break;

            case ScriptDestination.File:
                File.AppendAllText(_destPath, result.script);
                break;

            case ScriptDestination.Folder:
                break;
            }

            if (!String.IsNullOrEmpty(errors))
            {
                GenericErrorDialog.ShowError("Error", "Scripting completed with errors! See details below.", errors);
            }
        }
Example #12
0
        public void ModifySelectedObjects( )
        {
            string error = String.Empty;

            IList <frmScriptEditor> editors = new List <frmScriptEditor>();

            foreach (TreeNode node in tv.SelectedNodes)
            {
                ObjectGroupingItemData data = ObjectGroupingItemDataFactory.GetNodeData(node);
                if (data == null)
                {
                    continue;
                }

                ObjectInfo objInfo = ProgrammabilityHelper.GetObjectInfo(_connParams, String.Empty, data.Name);
                if (objInfo == null)
                {
                    error += " - " + data.Name + "\r\n";
                    continue;
                }

                if (!DBConstants.DoesObjectTypeHasScript(data.Type ?? -1))
                {
                    continue;
                }
                string          script = ScriptingHelper.GetAlterScript(_connParams, _connParams.Database, objInfo.ObjectID, objInfo.ObjectType);
                frmScriptEditor editor = ScriptEditorFactory.Create(objInfo.ObjectName, script, objInfo.ObjectID, objInfo.ObjectType, _connParams, cmbDatabases.Text);
                editors.Add(editor);
            }

            foreach (frmScriptEditor editor in editors)
            {
                ScriptEditorFactory.ShowScriptEditor(editor);
            }

            if (!String.IsNullOrEmpty(error))
            {
                MessageService.ShowError("Objects listed below do not exist in the database!\r\n" + error);
            }
        }
Example #13
0
        public static bool OpenSharedScripts(IPragmaEditor currentEditor, ConnectionParams cp)
        {
            bool result = false;

            if (currentEditor != null && currentEditor.ContentModified)
            {
                DialogResult dlgRes = MessageBox.Show("Save changes to \"" + currentEditor.Caption + "\" before opening another script in current script editor", "Save Script", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (dlgRes == DialogResult.Cancel)
                {
                    return(false);
                }
                else if (dlgRes == DialogResult.Yes)
                {
                    currentEditor.SaveContent();
                }
            }

            IList <IPragmaEditor>         editors = new List <IPragmaEditor>();
            IList <SharedScriptsItemData> scripts = frmSharedScriptSelectDialog.ShowOpenSharedScriptDialog_MultiSelect();
            int           i   = 0;
            IPragmaEditor frm = null;

            foreach (SharedScriptsItemData data in scripts)
            {
                if (data.Type != GenericItemType.Item)
                {
                    continue;
                }

                i++;
                if (i == 1 && currentEditor != null)
                {
                    currentEditor.Caption                    = data.Name;
                    currentEditor.Icon                       = PragmaSQL.Properties.Resources.sharedScript;
                    currentEditor.ContentPersister           = new SharedScriptContentPersister();
                    currentEditor.ContentPersister.Data      = data;
                    currentEditor.ContentPersister.Hint      = "This is a shared script: " + data.Name;
                    currentEditor.ContentInfo                = currentEditor.ContentPersister.Hint;
                    currentEditor.ContentPersister.FilePath  = data.Name;
                    currentEditor.ActiveDocument.TextContent = data.Script;
                    currentEditor.ContentModified            = false;
                    result = true;
                    continue;
                }
                if (cp != null)
                {
                    frm = ScriptEditorFactory.OpenSharedScript(data, cp);
                    editors.Add(frm);
                }
                else
                {
                    frm = TextEditorFactory.OpenSharedScript(data);
                    editors.Add(frm);
                }
            }

            foreach (IPragmaEditor editor in editors)
            {
                if (editor is frmScriptEditor)
                {
                    ScriptEditorFactory.ShowScriptEditor(editor as frmScriptEditor);
                }
                else if (editor is frmTextEditor)
                {
                    TextEditorFactory.ShowTextEditor(editor as frmTextEditor);
                }
            }

            return(result);
        }
Example #14
0
        private void PerformRestore()
        {
            ConnectionParams        cp            = null;
            IList <frmTextEditor>   textEditors   = new List <frmTextEditor>();
            IList <frmScriptEditor> scriptEditors = new List <frmScriptEditor>();
            StringBuilder           sbErrors      = new StringBuilder();
            bool   showTextEditorInfoHeader       = false;
            bool   failedConnections = false;
            string scriptWarningText = String.Empty;

            InitializeProgress(lv.CheckedItems.Count);
            try
            {
                foreach (ListViewItem lvItem in lv.Items)
                {
                    cp = null;
                    scriptWarningText = String.Empty;
                    RecoverContent item = lvItem.Tag as RecoverContent;
                    if (!lvItem.Checked || item == null)
                    {
                        continue;
                    }

                    string itemCaption = item.Title;
                    failedConnections        = false;
                    showTextEditorInfoHeader = false;
                    frmTextEditor   txtEditor    = null;
                    frmScriptEditor scriptEditor = null;

                    try
                    {
                        if (item.ItemType == RecoverContentType.Script || item.ItemType == RecoverContentType.ScriptFile)
                        {
                            if (item.HasConnectionInfo)
                            {
                                cp = PrepareWorkspaceItemConnection(item, out failedConnections);

                                if (cp == null && !failedConnections)
                                {
                                    Utils.ShowWarning(String.Format(Properties.Resources.RecoverScriptWarning, item.Database, item.Server), MessageBoxButtons.OK);
                                }
                            }

                            if (cp == null)
                            {
                                item.SyntaxMode = "SQL";
                                if (item.ItemType == RecoverContentType.Script)
                                {
                                    item.ItemType = RecoverContentType.Text;
                                }
                                else if (item.ItemType == RecoverContentType.ScriptFile)
                                {
                                    item.ItemType = RecoverContentType.TextFile;
                                }
                                showTextEditorInfoHeader = true;
                                scriptWarningText        = !item.HasConnectionInfo ? Properties.Resources.RecoverScript_NoConnWarningInfoHeader
                  : String.Format(Properties.Resources.RecoverScriptWarningInfoHeader, item.Database, item.Server);
                            }
                            else
                            {
                                if (item.ItemType == RecoverContentType.Script)
                                {
                                    scriptEditor = ScriptEditorFactory.CreateWithAsyncConnection(itemCaption, item.Content, cp, cp.Database, String.Empty);
                                }
                                else if (item.ItemType == RecoverContentType.ScriptFile)
                                {
                                    scriptEditor = ScriptEditorFactory.CreateWithAsyncConnection(itemCaption, item.Content, cp, cp.Database, item.FileName);
                                }

                                scriptEditor.Uid = item.Uid;
                                scriptEditor.IsRecoveredContent = true;
                                scriptEditor.ObjectType         = item.ObjectType;
                                if (OpMode == OperationMode.Recover)
                                {
                                    scriptEditor.ShowInfo(String.Format("Recovered content. Content automatically saved on {0}", DateTime.FromFileTime(item.Time)));
                                }
                                scriptEditors.Add(scriptEditor);
                            }
                        }

                        txtEditor = null;
                        switch (item.ItemType)
                        {
                        case RecoverContentType.Script:
                        case RecoverContentType.ScriptFile:
                            break;

                        case RecoverContentType.SharedScript:
                            txtEditor = TextEditorFactory.CreateSharedScript(itemCaption, item.Content);
                            txtEditor.SetSyntaxMode("SQL");
                            txtEditor.Uid = item.Uid;
                            txtEditor.IsRecoveredContent = true;
                            break;

                        case RecoverContentType.SharedSnippet:
                            txtEditor = TextEditorFactory.CreateSharedSnippet(itemCaption, item.Content);
                            txtEditor.SetSyntaxMode("SQL");
                            txtEditor.Uid = item.Uid;
                            txtEditor.IsRecoveredContent = true;
                            break;

                        case RecoverContentType.Text:
                            txtEditor     = TextEditorFactory.Create(itemCaption, item.Content, item.SyntaxMode);
                            txtEditor.Uid = item.Uid;
                            txtEditor.IsRecoveredContent = true;
                            break;

                        case RecoverContentType.TextFile:
                            txtEditor     = TextEditorFactory.Create(itemCaption, item.Content, item.SyntaxMode, item.FileName);
                            txtEditor.Uid = item.Uid;
                            txtEditor.IsRecoveredContent = true;
                            break;

                        default:
                            throw new Exception(String.Format("Unknowm item type: {0}", item.ItemType));
                        }

                        if (txtEditor != null)
                        {
                            if (showTextEditorInfoHeader)
                            {
                                txtEditor.ShowInfo(scriptWarningText);
                            }
                            else if (OpMode == OperationMode.Recover)
                            {
                                txtEditor.ShowInfo(String.Format("Recovered content. Automatically saved on: {0}", DateTime.FromFileTime(item.Time)));
                            }

                            textEditors.Add(txtEditor);
                        }

                        ChangeProgress(String.Format("Restoring {0}", item.Title));
                    }
                    catch (Exception ex)
                    {
                        sbErrors.AppendLine(String.Format("- Can not restore '{0} ({1})'. Error: {2}", item.Title, item.ItemType, ex.Message));
                    }
                    Application.DoEvents();
                }
            }
            finally
            {
                ResetProgress();
            }

            try
            {
                this.Visible = false;
                foreach (frmTextEditor editor in textEditors)
                {
                    TextEditorFactory.ShowTextEditor(editor);
                }
                foreach (frmScriptEditor editor in scriptEditors)
                {
                    ScriptEditorFactory.ShowScriptEditor(editor);
                }
            }
            catch (Exception ex)
            {
                this.Visible = true;
                throw ex;
            }

            if (sbErrors.Length > 0)
            {
                GenericErrorDialog.ShowError("Content Restore Error", "Some content items can not be restored.", sbErrors.ToString());
            }
        }