// Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License, version 2.0, as // published by the Free Software Foundation. // // This program is also distributed with certain software (including // but not limited to OpenSSL) that is licensed under separate terms, // as designated in a particular file or component or in included license // documentation. The authors of MySQL hereby grant you an // additional permission to link the program and your derivative works // with the separately licensed software that they have included with // MySQL. // // Without limiting anything contained in the foregoing, this file, // which is part of MySQL Connector/NET, is also subject to the // Universal FOSS Exception, version 1.0, a copy of which can be found at // http://oss.oracle.com/licenses/universal-foss-exception. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. // See the GNU General Public License, version 2.0, for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA using System; using System.Collections.Generic; using System.Data; using System.Globalization; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Threading; using static System.String; using MySql.Data.Common; using Sog.Properties; namespace MySql.Data.MySqlClient { /// /// Provides a class capable of executing a SQL script containing /// multiple SQL statements including CREATE PROCEDURE statements /// that require changing the delimiter /// public class MySqlScript { /// /// Handles the event raised whenever a statement is executed. /// public event MySqlStatementExecutedEventHandler StatementExecuted; /// /// Handles the event raised whenever an error is raised by the execution of a script. /// public event MySqlScriptErrorEventHandler Error; /// /// Handles the event raised whenever a script execution is finished. /// public event EventHandler ScriptCompleted; #region Constructors /// /// Initializes a new instance of the /// class. /// public MySqlScript() { this.Delimiter = ";"; } /// /// Initializes a new instance of the /// class. /// /// The connection. public MySqlScript(MySqlConnection connection) : this() { this.Connection = connection; } /// /// Initializes a new instance of the /// class. /// /// The query. public MySqlScript(string query) : this() { this.Query = query; } /// /// Initializes a new instance of the /// class. /// /// The connection. /// The query. public MySqlScript(MySqlConnection connection, string query) : this() { this.Connection = connection; this.Query = query; } #endregion #region Properties /// /// Gets or sets the connection. /// /// The connection. public MySqlConnection Connection { get; set; } /// /// Gets or sets the query. /// /// The query. public string Query { get; set; } /// /// Gets or sets the delimiter. /// /// The delimiter. public string Delimiter { get; set; } #endregion #region Public Methods /// /// Executes this instance. /// /// The number of statements executed as part of the script. public int Execute() { bool openedConnection = false; if (this.Connection == null) { throw new InvalidOperationException(Resources.ConnectionNotSet); } if (IsNullOrEmpty(this.Query)) { return 0; } // next we open up the connetion if it is not already open if (this.Connection.State != ConnectionState.Open) { openedConnection = true; this.Connection.Open(); } // since we don't allow setting of parameters on a script we can // therefore safely allow the use of user variables. no one should be using // this connection while we are using it so we can temporarily tell it // to allow the use of user variables bool allowUserVars = this.Connection.Settings.AllowUserVariables; this.Connection.Settings.AllowUserVariables = true; try { string mode = this.Connection.driver.Property("sql_mode"); mode = StringUtility.ToUpperInvariant(mode); bool ansiQuotes = mode.IndexOf("ANSI_QUOTES") != -1; bool noBackslashEscapes = mode.IndexOf("NO_BACKSLASH_ESCAPES") != -1; // first we break the query up into smaller queries List statements = this.BreakIntoStatements(ansiQuotes, noBackslashEscapes); int count = 0; MySqlCommand cmd = new MySqlCommand(null, this.Connection); foreach (ScriptStatement statement in statements.Where(statement => !IsNullOrEmpty(statement.text))) { cmd.CommandText = statement.text; try { cmd.ExecuteNonQuery(); count++; this.OnQueryExecuted(statement); } catch (Exception ex) { if (this.Error == null) { throw; } if (!this.OnScriptError(ex)) { break; } } } this.OnScriptCompleted(); return count; } finally { this.Connection.Settings.AllowUserVariables = allowUserVars; if (openedConnection) { this.Connection.Close(); } } } #endregion private void OnQueryExecuted(ScriptStatement statement) { if (this.StatementExecuted == null) { return; } MySqlScriptEventArgs args = new MySqlScriptEventArgs { Statement = statement }; this.StatementExecuted(this, args); } private void OnScriptCompleted() { this.ScriptCompleted?.Invoke(this, EventArgs.Empty); } private bool OnScriptError(Exception ex) { if (this.Error == null) { return false; } MySqlScriptErrorEventArgs args = new MySqlScriptErrorEventArgs(ex); this.Error(this, args); return args.Ignore; } private List BreakScriptIntoLines() { List lineNumbers = new List(); StringReader sr = new StringReader(this.Query); string line = sr.ReadLine(); int pos = 0; while (line != null) { lineNumbers.Add(pos); pos += line.Length; line = sr.ReadLine(); } return lineNumbers; } private static int FindLineNumber(int position, List lineNumbers) { int i = 0; while (i < lineNumbers.Count && position < lineNumbers[i]) { i++; } return i; } private List BreakIntoStatements(bool ansiQuotes, bool noBackslashEscapes) { string currentDelimiter = this.Delimiter; int startPos = 0; List statements = new List(); List lineNumbers = this.BreakScriptIntoLines(); MySqlTokenizer tokenizer = new MySqlTokenizer(this.Query); tokenizer.AnsiQuotes = ansiQuotes; tokenizer.BackslashEscapes = !noBackslashEscapes; string token = tokenizer.NextToken(); while (token != null) { if (!tokenizer.Quoted) { if (token.ToLower(CultureInfo.InvariantCulture) == "delimiter") { tokenizer.NextToken(); this.AdjustDelimiterEnd(tokenizer); currentDelimiter = this.Query.Substring( tokenizer.StartIndex, tokenizer.StopIndex - tokenizer.StartIndex).Trim(); startPos = tokenizer.StopIndex; } else { // this handles the case where our tokenizer reads part of the // delimiter if (currentDelimiter.StartsWith(token, StringComparison.OrdinalIgnoreCase)) { if ((tokenizer.StartIndex + currentDelimiter.Length) <= this.Query.Length) { if (this.Query.Substring(tokenizer.StartIndex, currentDelimiter.Length) == currentDelimiter) { token = currentDelimiter; tokenizer.Position = tokenizer.StartIndex + currentDelimiter.Length; tokenizer.StopIndex = tokenizer.Position; } } } int delimiterPos = token.IndexOf(currentDelimiter, StringComparison.OrdinalIgnoreCase); if (delimiterPos != -1) { int endPos = tokenizer.StopIndex - token.Length + delimiterPos; if (tokenizer.StopIndex == this.Query.Length - 1) { endPos++; } string currentQuery = this.Query.Substring(startPos, endPos - startPos); ScriptStatement statement = new ScriptStatement(); statement.text = currentQuery.Trim(); statement.line = FindLineNumber(startPos, lineNumbers); statement.position = startPos - lineNumbers[statement.line]; statements.Add(statement); startPos = endPos + currentDelimiter.Length; } } } token = tokenizer.NextToken(); } // now clean up the last statement if (startPos < this.Query.Length - 1) { string sqlLeftOver = this.Query.Substring(startPos).Trim(); if (IsNullOrEmpty(sqlLeftOver)) { return statements; } ScriptStatement statement = new ScriptStatement { text = sqlLeftOver, line = FindLineNumber(startPos, lineNumbers) }; statement.position = startPos - lineNumbers[statement.line]; statements.Add(statement); } return statements; } private void AdjustDelimiterEnd(MySqlTokenizer tokenizer) { if (tokenizer.StopIndex >= this.Query.Length) { return; } int pos = tokenizer.StopIndex; char c = this.Query[pos]; while (!Char.IsWhiteSpace(c) && pos < (this.Query.Length - 1)) { c = this.Query[++pos]; } tokenizer.StopIndex = pos; tokenizer.Position = pos; } #region Async /// /// Initiates the asynchronous execution of SQL statements. /// /// The number of statements executed as part of the script inside. public Task ExecuteAsync() { return this.ExecuteAsync(CancellationToken.None); } /// /// Initiates the asynchronous execution of SQL statements. /// /// The cancellation token. /// The number of statements executed as part of the script inside. public Task ExecuteAsync(CancellationToken cancellationToken) { var result = new TaskCompletionSource(); if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested) { try { var executeResult = this.Execute(); result.SetResult(executeResult); } catch (Exception ex) { result.SetException(ex); } } else { result.SetCanceled(); } return result.Task; } #endregion } /// /// Represents the method that will handle errors when executing MySQL statements. /// public delegate void MySqlStatementExecutedEventHandler(object sender, MySqlScriptEventArgs args); /// /// Represents the method that will handle errors when executing MySQL scripts. /// public delegate void MySqlScriptErrorEventHandler(object sender, MySqlScriptErrorEventArgs args); /// /// Sets the arguments associated to MySQL scripts. /// public class MySqlScriptEventArgs : EventArgs { internal ScriptStatement Statement { get; set; } /// /// Gets the statement text. /// /// The statement text. public string StatementText => this.Statement.text; /// /// Gets the line. /// /// The line. public int Line => this.Statement.line; /// /// Gets the position. /// /// The position. public int Position => this.Statement.position; } /// /// Sets the arguments associated to MySQL script errors. /// public class MySqlScriptErrorEventArgs : MySqlScriptEventArgs { /// /// Initializes a new instance of the class. /// /// The exception. public MySqlScriptErrorEventArgs(Exception exception) { this.Exception = exception; } /// /// Gets the exception. /// /// The exception. public Exception Exception { get; } /// /// Gets or sets a value indicating whether this is ignored. /// /// true if ignore; otherwise, false. public bool Ignore { get; set; } } struct ScriptStatement { public string text; public int line; public int position; } }