Ejemplo n.º 1
0
        /// <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);
        }