/// <summary>
 /// Toggles the editors between batch mode and console mode.
 /// </summary>
 /// <param name="executionMode">The execution mode.</param>
 private void ToggleEditors(ExecutionModeOption executionMode)
 {
     try
     {
         panel1.SuspendLayout();
         if (executionMode == ExecutionModeOption.BatchMode)
         {
             panel1.Controls.Remove(BaseShellConsoleEditor);
             panel1.Controls.Add(ResultsTabControl);
             panel1.Controls.Add(splitter1);
             // Register the code editor, to add back its handles and events
             CodeEditor.RegisterEditor();
             panel1.Controls.Add(CodeEditor);
             RunScriptToolStripButton.Enabled = true;
             CodeEditor.Focus();
         }
         else
         {
             panel1.Controls.Remove(ResultsTabControl);
             panel1.Controls.Remove(splitter1);
             // Unregister the code editor, to remove its handles and events
             CodeEditor.UnregisterEditor();
             panel1.Controls.Remove(CodeEditor);
             BaseShellConsoleEditor.Dock = DockStyle.Fill;
             panel1.Controls.Add(BaseShellConsoleEditor);
             RunScriptToolStripButton.Enabled = false;
             BaseShellConsoleEditor.Focus();
         }
     }
     finally
     {
         panel1.ResumeLayout();
     }
 }
 /// <summary>
 /// Writes to the My SQL Output Tool Window.
 /// </summary>
 /// <param name="action">The action.</param>
 /// <param name="message">The message.</param>
 /// <param name="duration">The duration.</param>
 /// <param name="messageType">Type of the message.</param>
 protected override void WriteToMySqlOutput(string action, string message, string duration, MessageType messageType)
 {
     base.WriteToMySqlOutput(action, message, duration, messageType);
     if (_executionModeOption == ExecutionModeOption.ConsoleMode)
     {
         BaseShellConsoleEditor.AddMessage(message);
     }
 }
        /// <summary>
        /// Executes the script in console mode.
        /// </summary>
        /// <param name="script">The script.</param>
        private void ExecuteConsoleScript(string script)
        {
            var jsonString  = _mySqlXProxy.ExecuteQuery(script, ScriptLanguageType);
            var boxedResult = ExtensionMethods.ToBaseShellResultObject(jsonString);

            PrintResult(script, boxedResult);
            BaseShellConsoleEditor.Focus();
        }
        /// <summary>
        /// Handles the Command event of the BaseShellConsoleEditor1 control, and execute the command received.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="BaseShellConsoleCommandEventArgs"/> instance containing the event data.</param>
        private void BaseShellConsoleEditor_Command(object sender, BaseShellConsoleCommandEventArgs e)
        {
            if (e.Command == "cls")
            {
                BaseShellConsoleEditor.ClearMessages();
                e.Cancel = true;
                return;
            }

            ExecuteScript(e.Command);
        }
        /// <summary>
        /// Prints a <see cref="DocResult"/> in the output window showing extended information about the execution result (documents returned, execution time, etc.).
        /// </summary>
        /// <param name="statement">The executed statement.</param>
        /// <param name="dictionariesList">A list of dictionaries of results and information about them.</param>
        /// <param name="executionTime">Execution time formatted to seconds.</param>
        private void PrintGenericResult(string statement, List <Dictionary <string, object> > dictionariesList, string executionTime)
        {
            var count = dictionariesList != null ? dictionariesList.Count : 0;

            switch (_executionModeOption)
            {
            case ExecutionModeOption.BatchMode:
                if (dictionariesList != null && count > 0)
                {
                    CreateResultPane(dictionariesList, _tabCounter);
                    _tabCounter++;
                }

                break;

            case ExecutionModeOption.ConsoleMode:
                if (dictionariesList == null)
                {
                    break;
                }

                foreach (var rowData in dictionariesList)
                {
                    var sb = new StringBuilder();
                    sb.AppendLine("{");
                    int i            = 0;
                    var rowDataCount = rowData.Count;
                    foreach (var kvp in rowData)
                    {
                        sb.AppendFormat("\t\"{0}\": \"{1}\"{2}{3}", kvp.Key, kvp.Value, i == rowDataCount - 1 ? "" : ",", Environment.NewLine);
                        i++;
                    }

                    sb.AppendLine("},");
                    BaseShellConsoleEditor.AddMessage(sb.ToString());
                }

                break;
            }

            WriteToMySqlOutput(statement, string.Format("{0} documents in set.", count), executionTime, MessageType.Information);
        }
        /// <summary>
        /// Prints a <see cref="RowResult"/> in the output window showing extended information about the execution result (rows returned, execution time, etc.).
        /// </summary>
        /// <param name="statement">The executed statement.</param>
        /// <param name="rowResult">A <see cref="RowResult"/> instance.</param>
        private void PrintRowResult(string statement, RowResult rowResult)
        {
            string executionTime    = rowResult.ExecutionTime;
            var    dictionariesList = rowResult.Data;
            var    count            = dictionariesList != null ? dictionariesList.Count : 0;

            switch (_executionModeOption)
            {
            case ExecutionModeOption.BatchMode:
                if (dictionariesList != null && count > 0)
                {
                    CreateResultPane(dictionariesList, _tabCounter);
                    _tabCounter++;
                }

                break;

            case ExecutionModeOption.ConsoleMode:
                if (dictionariesList == null)
                {
                    break;
                }

                // Get columns names
                var columns = rowResult.GetColumnNames();

                // Create console table object for output format
                var table = new ConsoleTable(columns.ToArray());
                foreach (var rowData in dictionariesList)
                {
                    object[] columnValues          = rowData.Select(o => o.Value == null ? (object)"null" : o.Value.ToString()).ToArray();
                    var      formattedColumnValues = new List <Object>();

                    //Format values
                    foreach (var value in rowData.Values)
                    {
                        if (value is Dictionary <string, object> )
                        {
                            formattedColumnValues.Add(Utils.GetFormattedValue(value));
                            continue;
                        }

                        formattedColumnValues.Add(value);
                    }

                    table.AddRow(formattedColumnValues.ToArray());
                }

                if (table.Rows.Count > 0)
                {
                    BaseShellConsoleEditor.AddMessage(table.ToStringAlternative());
                }
                break;
            }

            var resultMessage = new StringBuilder();

            // If no items are returned, it is a DDL statement (Drop, Create, etc.)
            if (count == 0)
            {
                var sqlResult = rowResult as SqlResult;
                if (sqlResult != null)
                {
                    resultMessage.AppendFormat("Query OK, {0} row(s) affected, {1} warning(s)", sqlResult.AffectedRowCount, rowResult.WarningCount);
                }
                else
                {
                    resultMessage.AppendFormat("Query OK, {0} warning(s)", rowResult.WarningCount);
                }
            }
            else
            {
                resultMessage.AppendFormat("{0} row(s) in set.", count);
            }

            WriteToMySqlOutput(statement, resultMessage.ToString(), executionTime, MessageType.Information);
            PrintWarnings(statement, rowResult, executionTime);
        }