/// <summary> /// Event delegate method fired when the <see cref="QueryChangedTimer"/> text changes. /// </summary> /// <param name="sender">Sender object.</param> /// <param name="e">Event arguments.</param> private void QueryChangedTimer_Tick(object sender, EventArgs e) { if (QueryTextBox.Focused) { QueryTextBox_Validated(QueryTextBox, EventArgs.Empty); } else { // The code should never hit this block in which case there is something wrong. MySqlSourceTrace.WriteToLog("QueryChangedTimer's Tick event fired but no valid control had focus."); QueryChangedTimer.Stop(); } }
/// <summary> /// Sends an error message to the application log and optionally shows it to the users. /// </summary> /// <param name="errorTitle">The title displayed on the error dialog.</param> /// <param name="errorMessage">A custom error message.</param> /// <param name="showErrorDialog">Flag indicating whether the error is shown to users.</param> /// <param name="exception">An <see cref="Exception"/> object.</param> /// <param name="errorLevel">The <see cref="SourceLevels"/> to describe the severity of the error.</param> public static void MySqlNotifierErrorHandler(string errorTitle, string errorMessage, bool showErrorDialog, Exception exception, SourceLevels errorLevel = SourceLevels.Error) { bool emptyErrorMessage = string.IsNullOrEmpty(errorMessage); if (string.IsNullOrEmpty(errorTitle)) { errorTitle = errorLevel == SourceLevels.Critical || emptyErrorMessage ? Resources.HighSeverityError : Resources.ErrorTitle; } if (emptyErrorMessage) { errorMessage = Resources.UnhandledExceptionText; } string exceptionMessage = null; string exceptionMoreInfo = null; var errorBuilder = new StringBuilder(errorMessage); if (exception != null) { if (exception.Message.Length > 0) { exceptionMessage = exception.Message; errorBuilder.AppendLine(exception.Message); } if (exception.InnerException != null) { errorBuilder.AppendLine(exception.InnerException.Message); exceptionMoreInfo = exception.InnerException != null?string.Format("{0}{1}{1}", exception.InnerException.Message, Environment.NewLine) : string.Empty; } exceptionMoreInfo += exception.StackTrace; } string completeErrorMessage = errorBuilder.ToString(); if (showErrorDialog) { var infoProperties = InfoDialogProperties.GetErrorDialogProperties(errorTitle, errorMessage, exceptionMessage, exceptionMoreInfo); infoProperties.FitTextStrategy = InfoDialog.FitTextsAction.IncreaseDialogWidth; infoProperties.WordWrapMoreInfo = false; infoProperties.CommandAreaProperties.DefaultButton = InfoDialog.DefaultButtonType.Button1; infoProperties.CommandAreaProperties.DefaultButtonTimeout = 60; InfoDialog.ShowDialog(infoProperties); } MySqlSourceTrace.WriteToLog(completeErrorMessage, errorLevel); }
/// <summary> /// Callback method specified within the onAction attribute of a ribbon control declared in the Ribbon.xml. /// </summary> /// <param name="control">A ribbon control.</param> /// <param name="buttonPressed">Flag indicating whether the toggle button is depressed.</param> public void OnClickMySqlForExcel(OfficeCore.IRibbonControl control, bool buttonPressed) { ShowMySqlForExcelPaneTogglePressed = buttonPressed; Microsoft.Office.Tools.CustomTaskPane taskPane = Globals.ThisAddIn.GetOrCreateActiveCustomPane(); if (taskPane == null) { MySqlSourceTrace.WriteToLog(string.Format("Could not get or create a custom task pane for the active Excel window. Using Excel version {0}.", Globals.ThisAddIn.ExcelVersionNumber)); return; } taskPane.Visible = buttonPressed; if (!buttonPressed) { Globals.ThisAddIn.CloseExcelPane(taskPane.Control as ExcelAddInPane); } }
/// <summary> /// Creates the import my SQL table. /// </summary> /// <param name="wbConnection">The wb connection.</param> /// <param name="operationType">The <see cref="MySqlDataTable.DataOperationType"/> intended for the new <see cref="MySqlDataTable"/>.</param> /// <param name="tableOrViewName">The name of the MySQL table or view to import data from..</param> /// <param name="importColumnNames">Flag indicating if column names will be imported as the first row of imported data.</param> /// <param name="selectQuery">A SELECT query against a database object to fill the [MySqlDataTable] return object with.</param> /// <param name="procedureResultSetIndex">The index of the result set of a stored procedure this table contains data for.</param> /// <returns>MySql Table created from the selectQuery.</returns> public static MySqlDataTable CreateImportMySqlTable(this MySqlWorkbenchConnection wbConnection, MySqlDataTable.DataOperationType operationType, string tableOrViewName, bool importColumnNames, string selectQuery, int procedureResultSetIndex = 0) { DataTable dt = GetDataFromSelectQuery(wbConnection, selectQuery); if (dt == null) { MySqlSourceTrace.WriteToLog(string.Format(Resources.SelectQueryReturnedNothing, selectQuery)); return(null); } var importMySqlDataTable = new MySqlDataTable(wbConnection, tableOrViewName, dt, operationType, selectQuery) { ImportColumnNames = importColumnNames, ProcedureResultSetIndex = procedureResultSetIndex }; return(importMySqlDataTable); }
/// <summary> /// Unbinds the <see cref="ToolsExcelTable"/>, refreshes the data on the <see cref="MySqlTable"/> and binds it again to the <see cref="ToolsExcelTable"/>. /// </summary> public void Refresh() { if (MySqlTable == null || ToolsExcelTable == null) { return; } // Test the connection before attempting the data refresh. if (!TestConnection()) { if (ConnectionInfoError != ConnectionInfoErrorType.WorkbenchConnectionDoesNotExist) { return; } // If the Workbench connection does not exist anymore, log a message to the log, remove this object from the global connections collection and exit. MySqlSourceTrace.WriteToLog(string.Format(Resources.ImportConnectionInfoRemovedConnectionText, WorkbookName, WorksheetName, ExcelTableName), SourceLevels.Warning); Globals.ThisAddIn.StoredImportConnectionInfos.Remove(this); return; } try { // In case the table is bound (it should not be) then disconnect it. if (ToolsExcelTable.IsBinding) { ToolsExcelTable.Disconnect(); } // Refresh the data on the MySqlDataTable and bind it so the Excel table is refreshed. MySqlTable.RefreshData(); // Bind the table again after it was refreshed. BindMySqlDataTable(); } catch (Exception ex) { MiscUtilities.ShowCustomizedErrorDialog(string.Format(Resources.ImportDataRefreshError, _excelTableName), ex.GetFormattedMessage(), true); MySqlSourceTrace.WriteAppErrorToLog(ex); } }
/// <summary> /// Event delegate method fired before the <see cref="MySqlConnectionsManagerDialog"/> dialog is closed. /// </summary> /// <param name="sender">Sender object.</param> /// <param name="e">Event arguments.</param> private void MySqlConnectionsManagerDialog_FormClosing(object sender, FormClosingEventArgs e) { SelectedWorkbenchConnection = null; if (DialogResult != DialogResult.OK || WorkbenchConnectionsListView.SelectedItems.Count == 0) { ResetChangeCursorDelegate(false); return; } var selectedListViewItem = WorkbenchConnectionsListView.SelectedItems[0]; SelectedWorkbenchConnection = selectedListViewItem.Tag as MySqlWorkbenchConnection; if (SelectedWorkbenchConnection == null) { ResetChangeCursorDelegate(false); return; } // Validate if the selected connection has a default schema if (string.IsNullOrEmpty(SelectedWorkbenchConnection.Schema)) { using (var yesNoDialog = new InfoDialog(InfoDialogProperties.GetYesNoDialogProperties( InfoDialog.InfoType.Warning, Resources.MySqlConnectionsManagerDialog_EmptySchemaTitle, Resources.MySqlConnectionsManagerDialog_EmptySchemaDetail, Resources.MySqlConnectionsManagerDialog_EmptySchemaSubDetail))) { yesNoDialog.DefaultButton = InfoDialog.DefaultButtonType.Button2; yesNoDialog.DefaultButtonTimeout = 30; SelectedWorkbenchConnection = null; e.Cancel = true; if (yesNoDialog.ShowDialog() != DialogResult.No) { EditConnectionToolStripMenuItem_Click(null, EventArgs.Empty); } } } // Test the connection if it has not been tested before adding it to the Server Explorer if (SelectedWorkbenchConnection != null) { // Test the connection to change its connection status SelectedWorkbenchConnection.TestConnectionAndRetryOnWrongPassword(); switch (SelectedWorkbenchConnection.ConnectionStatus) { case MySqlWorkbenchConnection.ConnectionStatusType.Unknown: // Should not be in this status, so log the error MySqlSourceTrace.WriteToLog(Resources.MySqlConnectionsManagerDialog_UnkownStatusError, false); break; case MySqlWorkbenchConnection.ConnectionStatusType.AcceptingConnections: // Do nothing since the connection was already tested and is working. break; case MySqlWorkbenchConnection.ConnectionStatusType.RefusingConnections: // Ask the user if he wants to add the connection regardless of its failing status var infoProps = InfoDialogProperties.GetYesNoDialogProperties( InfoDialog.InfoType.Warning, Resources.MySqlConnectionsManagerDialog_BadConnectionTitle, Resources.MySqlConnectionsManagerDialog_BadConnectionDetail, Resources.MySqlConnectionsManagerDialog_BadConnectionSubDetail); infoProps.FitTextStrategy = InfoDialog.FitTextsAction.IncreaseDialogWidth; using (var yesNoDialog = new InfoDialog(infoProps)) { yesNoDialog.DefaultButton = InfoDialog.DefaultButtonType.Button2; yesNoDialog.DefaultButtonTimeout = 30; if (yesNoDialog.ShowDialog() == DialogResult.No) { SelectedWorkbenchConnection = null; e.Cancel = true; } } break; } } // If the selected connection does not exist already in the Server Explorer just exit. if (SelectedWorkbenchConnection == null || !SelectedWorkbenchConnection.Existing) { ResetChangeCursorDelegate(false); return; } // Ask the user if an existing connection in the Server Explorer should be replaced with the selected one. using (var yesNoDialog = new InfoDialog(InfoDialogProperties.GetYesNoDialogProperties( InfoDialog.InfoType.Warning, Resources.MySqlConnectionsManagerDialog_ExistingConnectionTitle, string.Format(Resources.MySqlConnectionsManagerDialog_ExistingConnectionDetail, SelectedWorkbenchConnection.HostIdentifier), Resources.MySqlConnectionsManagerDialog_ExistingConnectionSubDetail))) { yesNoDialog.DefaultButton = InfoDialog.DefaultButtonType.Button2; yesNoDialog.DefaultButtonTimeout = 10; if (yesNoDialog.ShowDialog() != DialogResult.No) { SelectedWorkbenchConnection = null; e.Cancel = true; } } if (SelectedWorkbenchConnection != null && SelectedWorkbenchConnection.Existing) { RelatedServerExplorerConnection = _serverExplorerConnections.FirstOrDefault(seConn => seConn.Connection.DisplayConnectionString.Equals(SelectedWorkbenchConnection.ConnectionString)); } if (!e.Cancel) { ResetChangeCursorDelegate(false); } }
/// <summary> /// Returns a SQL query meant to push changes in this row to the database server. /// </summary> /// <param name="setVariablesSql">An optional SET statement to initialize variables used in the returned SQL query.</param> /// <returns>A SQL query containing the data changes.</returns> public string GetSql(out string setVariablesSql) { setVariablesSql = _setVariablesSql; if (_sqlQuery != null) { return(_sqlQuery); } _setVariablesSql = null; if (RowState == DataRowState.Unchanged) { _sqlQuery = string.Empty; return(_sqlQuery); } if (MySqlTable == null) { MySqlSourceTrace.WriteToLog(Resources.MySqlDataTableExpectedError, SourceLevels.Critical); _sqlQuery = null; return(_sqlQuery); } _sqlQuery = string.Empty; switch (RowState) { case DataRowState.Added: _sqlQuery = GetSqlForAddedRow(); MySqlTable.SqlBuilderForInsert.Clear(); break; case DataRowState.Deleted: _sqlQuery = GetSqlForDeletedRow(); MySqlTable.SqlBuilderForDelete.Clear(); break; case DataRowState.Modified: _sqlQuery = MySqlTable.UseOptimisticUpdate ? GetSqlForModifiedRowUsingOptimisticConcurrency() : GetSqlForModifiedRow(); setVariablesSql = _setVariablesSql; MySqlTable.SqlBuilderForUpdate.Clear(); break; case DataRowState.Unchanged: _sqlQuery = string.Empty; break; } // Verify we have not exceeded the maximum packet size allowed by the server, otherwise throw an Exception. if (MySqlTable.MySqlMaxAllowedPacket <= 0) { return(_sqlQuery); } var queryStringByteCount = Encoding.ASCII.GetByteCount(_sqlQuery); if (queryStringByteCount > MySqlTable.MySqlMaxAllowedPacket) { throw new QueryExceedsMaxAllowedPacketException(); } return(_sqlQuery); }