/// <summary> /// Creates a new schema with the given name. /// </summary> /// <returns><c>true</c> if the schema was created successfully, <c>false</c> otherwise.</returns> private bool CreateSchema() { var passwordFlags = _wbConnection.TestConnectionAndRetryOnWrongPassword(); if (!passwordFlags.ConnectionSuccess) { return(false); } Cursor = Cursors.WaitCursor; string createSql = _wbConnection.GetCreateSchemaSql(SchemaName, CharSet, Collation, false); var operationInfoText = string.Format(Resources.ScriptCreatingSchemaText, SchemaName); List <IMySqlDataRow> results; using (var sqlScriptDialog = new MySqlScriptDialog(_wbConnection, createSql, operationInfoText)) { if (Settings.Default.GlobalSqlQueriesPreviewQueries) { sqlScriptDialog.ShowDialog(); } else { sqlScriptDialog.ApplyScript(); } var erroredOutRow = sqlScriptDialog.ErroredOutDataRow; results = sqlScriptDialog.ScriptResult == MySqlStatement.StatementResultType.ErrorThrown ? erroredOutRow != null ? new List <IMySqlDataRow>(1) { erroredOutRow } : null : sqlScriptDialog.ActualStatementRowsList; } if (results == null) { Cursor = Cursors.Default; return(false); } string operationSummary; bool success = true; bool warningsFound = false; var operationDetails = new StringBuilder(); var warningStatementDetails = new StringBuilder(); foreach (var statement in results.Select(statementRow => statementRow.Statement)) { // Create details text for the schema creation. switch (statement.StatementType) { case MySqlStatement.SqlStatementType.CreateSchema: break; case MySqlStatement.SqlStatementType.GrantAll: break; } if (Settings.Default.GlobalSqlQueriesShowQueriesWithResults) { operationDetails.AppendFormat(Resources.NewSchemaExecutedQuery, SchemaName); operationDetails.AddNewLine(2); operationDetails.Append(statement.SqlQuery); operationDetails.AddNewLine(2); } switch (statement.StatementResult) { case MySqlStatement.StatementResultType.Successful: operationDetails.AppendFormat(Resources.NewSchemaCreatedSuccessfullyText, SchemaName); break; case MySqlStatement.StatementResultType.WarningsFound: warningsFound = true; operationDetails.AppendFormat(Resources.NewSchemaCreatedWithWarningsText, SchemaName, statement.WarningsQuantity); operationDetails.AddNewLine(); operationDetails.Append(statement.ResultText); break; case MySqlStatement.StatementResultType.ErrorThrown: success = false; operationDetails.AppendFormat(Resources.NewSchemaCreationErrorText, SchemaName); operationDetails.AddNewLine(); operationDetails.Append(statement.ResultText); break; } } InfoDialog.InfoType operationsType; if (success) { operationSummary = string.Format(Resources.NewSchemaOperationSuccessSummaryText, SchemaName); operationsType = warningsFound ? InfoDialog.InfoType.Warning : InfoDialog.InfoType.Success; } else { operationSummary = string.Format(Resources.NewSchemaOperationErrorSummaryText, SchemaName); operationsType = InfoDialog.InfoType.Error; } Cursor = Cursors.Default; MiscUtilities.ShowCustomizedInfoDialog(operationsType, operationSummary, operationDetails.ToString(), false); operationDetails.Clear(); warningStatementDetails.Clear(); return(success); }
/// <summary> /// Pushes the data changes currently done in the Excel worksheet to attempt to commit them to the MySQL server. /// </summary> /// <returns><c>true</c> if the transaction was committed successfully to the database, <c>false</c> otherwise.</returns> private bool PushDataChanges() { var warningsFound = false; var errorsFound = false; var autoCommitOn = AutoCommitCheckBox.Checked; var warningsCount = 0; Cursor = Cursors.WaitCursor; _mySqlTable.UseOptimisticUpdate = _useOptimisticUpdateForThisSession; var modifiedRowsList = _mySqlTable.PushData(!autoCommitOn && Settings.Default.GlobalSqlQueriesPreviewQueries); if (modifiedRowsList == null) { Cursor = Cursors.Default; return(false); } var operationSummary = new StringBuilder(); var operationDetails = new StringBuilder(); var warningDetails = new StringBuilder(); var warningStatementDetails = new StringBuilder(); operationSummary.AppendFormat(Resources.EditedDataForTable, EditingTableName); if (Settings.Default.GlobalSqlQueriesShowQueriesWithResults) { operationDetails.AppendFormat( Resources.EditDataCommittedWithQueryText, modifiedRowsList.GetResultsCount(MySqlStatement.SqlStatementType.Delete), modifiedRowsList.GetResultsCount(MySqlStatement.SqlStatementType.Insert), modifiedRowsList.GetResultsCount(MySqlStatement.SqlStatementType.Update)); operationDetails.AddNewLine(); } var warningDetailHeaderAppended = false; var statementsQuantityFormat = new string('0', modifiedRowsList.Count.StringSize()); var sqlQueriesFormat = "{0:" + statementsQuantityFormat + "}: {1}"; foreach (var statement in modifiedRowsList.Select(statementRow => statementRow.Statement)) { if (Settings.Default.GlobalSqlQueriesShowQueriesWithResults && statement.SqlQuery.Length > 0) { operationDetails.AddNewLine(); if (statement.MySqlRow is MySqlDummyErroredRow) { operationDetails.Append(statement.SqlQuery); } else { operationDetails.AppendFormat(sqlQueriesFormat, statement.ExecutionOrder, statement.SqlQuery); } } switch (statement.StatementResult) { case MySqlStatement.StatementResultType.WarningsFound: if (Settings.Default.GlobalSqlQueriesPreviewQueries) { if (!warningDetailHeaderAppended) { warningDetailHeaderAppended = true; warningStatementDetails.AddNewLine(1, true); warningStatementDetails.Append(Resources.SqlStatementsProducingWarningsText); } if (statement.SqlQuery.Length > 0) { warningStatementDetails.AddNewLine(1, true); warningStatementDetails.AppendFormat(sqlQueriesFormat, statement.ExecutionOrder, statement.SqlQuery); } } warningsFound = true; warningDetails.AddNewLine(1, true); warningDetails.Append(statement.ResultText); warningsCount += statement.WarningsQuantity; break; case MySqlStatement.StatementResultType.ErrorThrown: errorsFound = true; operationDetails.AddNewLine(2, true); if (statement.MySqlRow is MySqlDummyErroredRow) { // This is not really the SQL Query, for a MySqlDummyErroredRow it contains a header for the error message displayed in the ResultText. operationDetails.AppendLine(statement.SqlQuery); } operationDetails.Append(statement.ResultText); break; } if (!errorsFound) { continue; } break; } if (warningsFound) { operationDetails.AddNewLine(2, true); operationDetails.AppendFormat(Resources.EditDataCommittedWarningsFound, warningsCount); operationDetails.AddNewLine(); if (warningStatementDetails.Length > 0) { operationDetails.Append(warningStatementDetails); operationDetails.AddNewLine(); } operationDetails.Append(warningDetails); } if (!Settings.Default.GlobalSqlQueriesShowQueriesWithResults) { operationDetails.AddNewLine(2, true); operationDetails.AppendFormat( Resources.EditDataCommittedText, modifiedRowsList.GetResultsCount(MySqlStatement.SqlStatementType.Delete), modifiedRowsList.GetResultsCount(MySqlStatement.SqlStatementType.Insert), modifiedRowsList.GetResultsCount(MySqlStatement.SqlStatementType.Update)); } InfoDialog.InfoType operationsType; if (!errorsFound) { if (warningsFound) { operationSummary.Append(Resources.EditedDataCommittedWarning); operationsType = InfoDialog.InfoType.Warning; } else { operationSummary.Append(Resources.EditedDataCommittedSucess); operationsType = InfoDialog.InfoType.Success; } } else { operationSummary.Append(Resources.EditedDataCommittedError); operationsType = InfoDialog.InfoType.Error; } if (!autoCommitOn || warningsFound || errorsFound) { MiscUtilities.ShowCustomizedInfoDialog(operationsType, operationSummary.ToString(), operationDetails.ToString(), false); } operationSummary.Clear(); operationDetails.Clear(); warningDetails.Clear(); warningStatementDetails.Clear(); CommitChangesButton.Enabled = UncommittedDataExists && !autoCommitOn; Cursor = Cursors.Default; return(!errorsFound); }