public static string ParseSql(string sql, bool expectErrors, Version version) { var mySqlConnection = CreateMySqlConnection(); var mySqlParser = new MySqlWbParser(mySqlConnection, version); var noErrors = mySqlParser.CheckSyntax(sql); Assert.True((expectErrors && !noErrors) || (!expectErrors && noErrors)); return(mySqlParser.ErrorMessagesInSingleText); }
/// <summary> /// Derives the schema returned from a specified MySQL command, indicating the layout of items and blocks in a given data reader. /// </summary> /// <param name="command">Data-source-specific MySQL command for which to derive the schema</param> /// <param name="commandType">Type of the indicated MySQL command, specifying how to interpret the contents of the <paramref name="command" /> parameter.</param> /// <param name="parameters">Array of <see cref="T:Microsoft.VisualStudio.Data.AdoDotNet.AdoDotNetParameter" /> objects for the specified command type.</param> /// <param name="commandTimeout">Length of time, in seconds, to block the client before canceling the schema derivation and returning to the caller. A value of zero indicates infinite timeout; value of -1 indicates a provider default.</param> /// <returns> /// Returns a <see cref="T:Microsoft.VisualStudio.Data.DataReader" /> object instance representing the command schema. /// </returns> /// <exception cref="System.Exception"> /// There is no active MySql connection. /// </exception> public override DataReader DeriveSchema(string command, int commandType, DataParameter[] parameters, int commandTimeout) { if (string.IsNullOrEmpty(Connection.ConnectionString)) { throw new Exception(Resources.ErrorNoActiveMySqlConnection); } using (var mySqlConnection = new MySqlConnection(Connection.ConnectionString)) { using (var mySqlParser = new MySqlWbParser(mySqlConnection)) { if (!mySqlParser.CheckSyntax(command)) { // Show error dialog with syntax check error message. throw new Exception(Resources.SyntaxErrorsFoundMessage + mySqlParser.ErrorMessagesInSingleText); } } } // Return OK with the query window return(base.Execute(command, commandType, parameters, commandTimeout)); }
/// <summary> /// Event delegate method fired when the <see cref="RunScriptToolStripButton"/> is clicked. /// </summary> /// <param name="sender">Sender object.</param> /// <param name="e">Event arguments.</param> private void RunScriptToolStripButton_Click(object sender, EventArgs e) { string sql = CodeEditor.Text.Trim(); ClearResults(); string[] sqlStmt = sql.BreakIntoSqlStatements().ToArray(); int ctr = 1; using (var mySqlConnection = new MySqlConnection(Connection.ConnectionString)) { using (var mySqlParser = new MySqlWbParser(mySqlConnection)) { for (int sqlIdx = 0; sqlIdx <= sqlStmt.Length - 1; sqlIdx++) { // Check syntax if (!mySqlParser.CheckSyntax(sqlStmt[sqlIdx])) { WriteToMySqlOutput(sqlStmt[sqlIdx], Resources.SyntaxErrorsFoundMessage + mySqlParser.ErrorMessagesInSingleText, null, MessageType.Error); return; } // Check if statement returns a result set. bool isResultSet = LanguageServiceUtil.DoesStmtReturnResults(sqlStmt[sqlIdx], (MySqlConnection)Connection); if (isResultSet) { ExecuteSelect(sqlStmt[sqlIdx], ctr); ctr++; } else { ExecuteScript(sqlStmt[sqlIdx]); } } } } StoreCurrentDatabase(); }