#if MYSQL_6_9 // Copyright ?2004, 2014, Oracle and/or its affiliates. All rights reserved. // // MySQL Connector/NET is licensed under the terms of the GPLv2 // , like most // MySQL Connectors. There are special exceptions to the terms and // conditions of the GPLv2 as it is applied to this software, see the // FLOSS License Exception // . // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published // by the Free Software Foundation; version 2 of the License. // // 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 // 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.Data; using Externals.MySql.Data.MySqlClient; using System.Text; using System.Threading.Tasks; using System.Threading; using System; namespace Externals.MySql.Data.MySqlClient { /// /// Helper class that makes it easier to work with the provider. /// internal sealed class MySqlHelper { enum CharClass : byte { None, Quote, Backslash } private static string stringOfBackslashChars = "\u005c\u00a5\u0160\u20a9\u2216\ufe68\uff3c"; private static string stringOfQuoteChars = "\u0022\u0027\u0060\u00b4\u02b9\u02ba\u02bb\u02bc\u02c8\u02ca\u02cb\u02d9\u0300\u0301\u2018\u2019\u201a\u2032\u2035\u275b\u275c\uff07"; private static CharClass[] charClassArray = makeCharClassArray(); // this class provides only static methods private MySqlHelper() { } #region ExecuteNonQuery /// /// Executes a single command against a MySQL database. The is assumed to be /// open when the method is called and remains open after the method completes. /// /// object to use /// SQL command to be executed /// Array of objects to use with the command. /// public static int ExecuteNonQuery(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters) { //create a command and prepare it for execution MySqlCommand cmd = new MySqlCommand(); cmd.Connection = connection; cmd.CommandText = commandText; cmd.CommandType = CommandType.Text; if (commandParameters != null) foreach (MySqlParameter p in commandParameters) cmd.Parameters.Add(p); int result = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return result; } /// /// Executes a single command against a MySQL database. A new is created /// using the given. /// /// to use /// SQL command to be executed /// Array of objects to use with the command. /// public static int ExecuteNonQuery(string connectionString, string commandText, params MySqlParameter[] parms) { //create & open a SqlConnection, and dispose of it after we are done. using (MySqlConnection cn = new MySqlConnection(connectionString)) { cn.Open(); //call the overload that takes a connection in place of the connection string return ExecuteNonQuery(cn, commandText, parms); } } #endregion #region ExecuteDataSet /// /// Executes a single SQL command and returns the first row of the resultset. A new MySqlConnection object /// is created, opened, and closed during this method. /// /// Settings to be used for the connection /// Command to execute /// Parameters to use for the command /// DataRow containing the first row of the resultset public static DataRow ExecuteDataRow(string connectionString, string commandText, params MySqlParameter[] parms) { DataSet ds = ExecuteDataset(connectionString, commandText, parms); if (ds == null) return null; if (ds.Tables.Count == 0) return null; if (ds.Tables[0].Rows.Count == 0) return null; return ds.Tables[0].Rows[0]; } /// /// Executes a single SQL command and returns the resultset in a . /// A new MySqlConnection object is created, opened, and closed during this method. /// /// Settings to be used for the connection /// Command to execute /// containing the resultset public static DataSet ExecuteDataset(string connectionString, string commandText) { //pass through the call providing null for the set of SqlParameters return ExecuteDataset(connectionString, commandText, (MySqlParameter[])null); } /// /// Executes a single SQL command and returns the resultset in a . /// A new MySqlConnection object is created, opened, and closed during this method. /// /// Settings to be used for the connection /// Command to execute /// Parameters to use for the command /// containing the resultset public static DataSet ExecuteDataset(string connectionString, string commandText, params MySqlParameter[] commandParameters) { //create & open a SqlConnection, and dispose of it after we are done. using (MySqlConnection cn = new MySqlConnection(connectionString)) { cn.Open(); //call the overload that takes a connection in place of the connection string return ExecuteDataset(cn, commandText, commandParameters); } } /// /// Executes a single SQL command and returns the resultset in a . /// The state of the object remains unchanged after execution /// of this method. /// /// object to use /// Command to execute /// containing the resultset public static DataSet ExecuteDataset(MySqlConnection connection, string commandText) { //pass through the call providing null for the set of SqlParameters return ExecuteDataset(connection, commandText, (MySqlParameter[])null); } /// /// Executes a single SQL command and returns the resultset in a . /// The state of the object remains unchanged after execution /// of this method. /// /// object to use /// Command to execute /// Parameters to use for the command /// containing the resultset public static DataSet ExecuteDataset(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters) { //create a command and prepare it for execution MySqlCommand cmd = new MySqlCommand(); cmd.Connection = connection; cmd.CommandText = commandText; cmd.CommandType = CommandType.Text; if (commandParameters != null) foreach (MySqlParameter p in commandParameters) cmd.Parameters.Add(p); //create the DataAdapter & DataSet MySqlDataAdapter da = new MySqlDataAdapter(cmd); DataSet ds = new DataSet(); //fill the DataSet using default values for DataTable names, etc. da.Fill(ds); // detach the MySqlParameters from the command object, so they can be used again. cmd.Parameters.Clear(); //return the dataset return ds; } /// /// Updates the given table with data from the given /// /// Settings to use for the update /// Command text to use for the update /// containing the new data to use in the update /// Tablename in the dataset to update public static void UpdateDataSet(string connectionString, string commandText, DataSet ds, string tablename) { MySqlConnection cn = new MySqlConnection(connectionString); cn.Open(); MySqlDataAdapter da = new MySqlDataAdapter(commandText, cn); MySqlCommandBuilder cb = new MySqlCommandBuilder(da); cb.ToString(); da.Update(ds, tablename); cn.Close(); } #endregion #region ExecuteDataReader /// /// Executes a single command against a MySQL database, possibly inside an existing transaction. /// /// object to use for the command /// object to use for the command /// Command text to use /// Array of objects to use with the command /// True if the connection should be preserved, false if not /// object ready to read the results of the command private static MySqlDataReader ExecuteReader(MySqlConnection connection, MySqlTransaction transaction, string commandText, MySqlParameter[] commandParameters, bool ExternalConn) { //create a command and prepare it for execution MySqlCommand cmd = new MySqlCommand(); cmd.Connection = connection; cmd.Transaction = transaction; cmd.CommandText = commandText; cmd.CommandType = CommandType.Text; if (commandParameters != null) foreach (MySqlParameter p in commandParameters) cmd.Parameters.Add(p); //create a reader MySqlDataReader dr; // call ExecuteReader with the appropriate CommandBehavior if (ExternalConn) { dr = cmd.ExecuteReader(); } else { dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); } // detach the SqlParameters from the command object, so they can be used again. cmd.Parameters.Clear(); return dr; } /// /// Executes a single command against a MySQL database. /// /// Settings to use for this command /// Command text to use /// object ready to read the results of the command public static MySqlDataReader ExecuteReader(string connectionString, string commandText) { //pass through the call providing null for the set of SqlParameters return ExecuteReader(connectionString, commandText, (MySqlParameter[])null); } /// /// Executes a single command against a MySQL database. /// /// object to use for the command /// Command text to use /// object ready to read the results of the command public static MySqlDataReader ExecuteReader(MySqlConnection connection, string commandText) { //pass through the call providing null for the set of SqlParameters return ExecuteReader(connection, null, commandText, (MySqlParameter[])null, true); } /// /// Executes a single command against a MySQL database. /// /// Settings to use for this command /// Command text to use /// Array of objects to use with the command /// object ready to read the results of the command public static MySqlDataReader ExecuteReader(string connectionString, string commandText, params MySqlParameter[] commandParameters) { //create & open a SqlConnection MySqlConnection cn = new MySqlConnection(connectionString); cn.Open(); //call the private overload that takes an internally owned connection in place of the connection string return ExecuteReader(cn, null, commandText, commandParameters, false); } /// /// Executes a single command against a MySQL database. /// /// Connection to use for the command /// Command text to use /// Array of objects to use with the command /// object ready to read the results of the command public static MySqlDataReader ExecuteReader(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters) { //call the private overload that takes an internally owned connection in place of the connection string return ExecuteReader(connection, null, commandText, commandParameters, true); } #endregion #region ExecuteScalar /// /// Execute a single command against a MySQL database. /// /// Settings to use for the update /// Command text to use for the update /// The first column of the first row in the result set, or a null reference if the result set is empty. public static object ExecuteScalar(string connectionString, string commandText) { //pass through the call providing null for the set of MySqlParameters return ExecuteScalar(connectionString, commandText, (MySqlParameter[])null); } /// /// Execute a single command against a MySQL database. /// /// Settings to use for the command /// Command text to use for the command /// Parameters to use for the command /// The first column of the first row in the result set, or a null reference if the result set is empty. public static object ExecuteScalar(string connectionString, string commandText, params MySqlParameter[] commandParameters) { //create & open a SqlConnection, and dispose of it after we are done. using (MySqlConnection cn = new MySqlConnection(connectionString)) { cn.Open(); //call the overload that takes a connection in place of the connection string return ExecuteScalar(cn, commandText, commandParameters); } } /// /// Execute a single command against a MySQL database. /// /// object to use /// Command text to use for the command /// The first column of the first row in the result set, or a null reference if the result set is empty. public static object ExecuteScalar(MySqlConnection connection, string commandText) { //pass through the call providing null for the set of MySqlParameters return ExecuteScalar(connection, commandText, (MySqlParameter[])null); } /// /// Execute a single command against a MySQL database. /// /// object to use /// Command text to use for the command /// Parameters to use for the command /// The first column of the first row in the result set, or a null reference if the result set is empty. public static object ExecuteScalar(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters) { //create a command and prepare it for execution MySqlCommand cmd = new MySqlCommand(); cmd.Connection = connection; cmd.CommandText = commandText; cmd.CommandType = CommandType.Text; if (commandParameters != null) foreach (MySqlParameter p in commandParameters) cmd.Parameters.Add(p); //execute the command & return the results object retval = cmd.ExecuteScalar(); // detach the SqlParameters from the command object, so they can be used again. cmd.Parameters.Clear(); return retval; } #endregion #region Utility methods private static CharClass[] makeCharClassArray() { CharClass[] a = new CharClass[65536]; foreach (char c in stringOfBackslashChars) { a[c] = CharClass.Backslash; } foreach (char c in stringOfQuoteChars) { a[c] = CharClass.Quote; } return a; } private static bool needsQuoting(string s) { foreach (char c in s) { if (charClassArray[c] != CharClass.None) { return true; } } return false; } /// /// Escapes the string. /// /// The string to escape /// The string with all quotes escaped. public static string EscapeString(string value) { if (!needsQuoting(value)) return value; StringBuilder sb = new StringBuilder(); foreach (char c in value) { CharClass charClass = charClassArray[c]; if (charClass != CharClass.None) { sb.Append("\\"); } sb.Append(c); } return sb.ToString(); } public static string DoubleQuoteString(string value) { if (!needsQuoting(value)) return value; StringBuilder sb = new StringBuilder(); foreach (char c in value) { CharClass charClass = charClassArray[c]; if (charClass == CharClass.Quote) sb.Append(c); else if (charClass == CharClass.Backslash) sb.Append("\\"); sb.Append(c); } return sb.ToString(); } #endregion #region Async #region DataRow /// /// Async version of ExecuteDataRow /// /// Settings to be used for the connection /// Command to execute /// Parameters to use for the command /// DataRow containing the first row of the resultset public static Task ExecuteDataRowAsync(string connectionString, string commandText, params MySqlParameter[] parms) { return ExecuteDataRowAsync(connectionString, commandText, CancellationToken.None, parms); } public static Task ExecuteDataRowAsync(string connectionString, string commandText, CancellationToken cancellationToken, params MySqlParameter[] parms) { var result = new TaskCompletionSource(); if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested) { try { var row = ExecuteDataRow(connectionString, commandText, parms); result.SetResult(row); } catch (Exception ex) { result.SetException(ex); } } else { result.SetCanceled(); } return result.Task; } #endregion #region NonQuery /// /// Async version of ExecuteNonQuery /// /// object to use /// SQL command to be executed /// Array of objects to use with the command. /// Rows affected public static Task ExecuteNonQueryAsync(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters) { return ExecuteNonQueryAsync(connection, commandText, CancellationToken.None, commandParameters); } public static Task ExecuteNonQueryAsync(MySqlConnection connection, string commandText, CancellationToken cancellationToken, params MySqlParameter[] commandParameters) { var result = new TaskCompletionSource(); if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested) { try { var queryResult = ExecuteNonQuery(connection, commandText, commandParameters); result.SetResult(queryResult); } catch (Exception ex) { result.SetException(ex); } } else { result.SetCanceled(); } return result.Task; } /// /// Async version of ExecuteNonQuery /// /// to use /// SQL command to be executed /// Array of objects to use with the command. /// Rows affected public static Task ExecuteNonQueryAsync(string connectionString, string commandText, params MySqlParameter[] commandParameters) { return ExecuteNonQueryAsync(connectionString, commandText, CancellationToken.None, commandParameters); } public static Task ExecuteNonQueryAsync(string connectionString, string commandText, CancellationToken cancellationToken, params MySqlParameter[] commandParameters) { var result = new TaskCompletionSource(); if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested) { try { var queryResult = ExecuteNonQuery(connectionString, commandText, commandParameters); result.SetResult(queryResult); } catch (Exception ex) { result.SetException(ex); } } else { result.SetCanceled(); } return result.Task; } #endregion #region DataSet /// /// Async version of ExecuteDataset /// /// Settings to be used for the connection /// Command to execute /// containing the resultset public static Task ExecuteDatasetAsync(string connectionString, string commandText) { return ExecuteDatasetAsync(connectionString, commandText, CancellationToken.None, (MySqlParameter[])null); } public static Task ExecuteDatasetAsync(string connectionString, string commandText, CancellationToken cancellationToken) { return ExecuteDatasetAsync(connectionString, commandText, cancellationToken, (MySqlParameter[])null); } /// /// Async version of ExecuteDataset /// /// Settings to be used for the connection /// Command to execute /// Parameters to use for the command /// containing the resultset public static Task ExecuteDatasetAsync(string connectionString, string commandText, params MySqlParameter[] commandParameters) { return ExecuteDatasetAsync(connectionString, commandText, CancellationToken.None, commandParameters); } public static Task ExecuteDatasetAsync(string connectionString, string commandText, CancellationToken cancellationToken, params MySqlParameter[] commandParameters) { var result = new TaskCompletionSource(); if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested) { try { var dataset = ExecuteDataset(connectionString, commandText, commandParameters); result.SetResult(dataset); } catch (Exception ex) { result.SetException(ex); } } else { result.SetCanceled(); } return result.Task; } /// /// Async version of ExecuteDataset /// /// object to use /// Command to execute /// containing the resultset public static Task ExecuteDatasetAsync(MySqlConnection connection, string commandText) { return ExecuteDatasetAsync(connection, commandText, CancellationToken.None, (MySqlParameter[])null); } public static Task ExecuteDatasetAsync(MySqlConnection connection, string commandText, CancellationToken cancellationToken) { return ExecuteDatasetAsync(connection, commandText, cancellationToken, (MySqlParameter[])null); } /// /// Async version of ExecuteDataset /// /// object to use /// Command to execute /// Parameters to use for the command /// containing the resultset public static Task ExecuteDatasetAsync(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters) { return ExecuteDatasetAsync(connection, commandText, CancellationToken.None, commandParameters); } public static Task ExecuteDatasetAsync(MySqlConnection connection, string commandText, CancellationToken cancellationToken, params MySqlParameter[] commandParameters) { var result = new TaskCompletionSource(); if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested) { try { var dataset = ExecuteDataset(connection, commandText, commandParameters); result.SetResult(dataset); } catch (Exception ex) { result.SetException(ex); } } else { result.SetCanceled(); } return result.Task; } /// /// Async version of UpdateDataset /// /// Settings to use for the update /// Command text to use for the update /// containing the new data to use in the update /// Tablename in the dataset to update public static Task UpdateDataSetAsync(string connectionString, string commandText, DataSet ds, string tablename) { return UpdateDataSetAsync(connectionString, commandText, ds, tablename, CancellationToken.None); } public static Task UpdateDataSetAsync(string connectionString, string commandText, DataSet ds, string tablename, CancellationToken cancellationToken) { var result = new TaskCompletionSource(); if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested) { try { UpdateDataSet(connectionString, commandText, ds, tablename); result.SetResult(true); } catch (Exception ex) { result.SetException(ex); } } else { result.SetCanceled(); } return result.Task; } #endregion #region DataReader /// /// Async version of ExecuteReader /// /// object to use for the command /// object to use for the command /// Command text to use /// Array of objects to use with the command /// True if the connection should be preserved, false if not /// object ready to read the results of the command private static Task ExecuteReaderAsync(MySqlConnection connection, MySqlTransaction transaction, string commandText, MySqlParameter[] commandParameters, bool ExternalConn) { return ExecuteReaderAsync(connection, transaction, commandText, commandParameters, ExternalConn, CancellationToken.None); } private static Task ExecuteReaderAsync(MySqlConnection connection, MySqlTransaction transaction, string commandText, MySqlParameter[] commandParameters, bool ExternalConn, CancellationToken cancellationToken) { var result = new TaskCompletionSource(); if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested) { try { var reader = ExecuteReader(connection, transaction, commandText, commandParameters, ExternalConn); result.SetResult(reader); } catch (Exception ex) { result.SetException(ex); } } else { result.SetCanceled(); } return result.Task; } /// /// Async version of ExecuteReader /// /// Settings to use for this command /// Command text to use /// object ready to read the results of the command public static Task ExecuteReaderAsync(string connectionString, string commandText) { return ExecuteReaderAsync(connectionString, commandText, CancellationToken.None, (MySqlParameter[])null); } public static Task ExecuteReaderAsync(string connectionString, string commandText, CancellationToken cancellationToken) { return ExecuteReaderAsync(connectionString, commandText, cancellationToken, (MySqlParameter[])null); } /// /// Async version of ExecuteReader /// /// object to use for the command /// Command text to use /// object ready to read the results of the command public static Task ExecuteReaderAsync(MySqlConnection connection, string commandText) { return ExecuteReaderAsync(connection, null, commandText, (MySqlParameter[])null, true, CancellationToken.None); } public static Task ExecuteReaderAsync(MySqlConnection connection, string commandText, CancellationToken cancellationToken) { return ExecuteReaderAsync(connection, null, commandText, (MySqlParameter[])null, true, cancellationToken); } /// /// Async version of ExecuteReader /// /// Settings to use for this command /// Command text to use /// Array of objects to use with the command /// object ready to read the results of the command public static Task ExecuteReaderAsync(string connectionString, string commandText, params MySqlParameter[] commandParameters) { return ExecuteReaderAsync(connectionString, commandText, CancellationToken.None, commandParameters); } public static Task ExecuteReaderAsync(string connectionString, string commandText, CancellationToken cancellationToken, params MySqlParameter[] commandParameters) { var result = new TaskCompletionSource(); if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested) { try { var reader = ExecuteReader(connectionString, commandText, commandParameters); result.SetResult(reader); } catch (Exception ex) { result.SetException(ex); } } else { result.SetCanceled(); } return result.Task; } /// /// Async version of ExecuteReader /// /// Connection to use for the command /// Command text to use /// Array of objects to use with the command /// object ready to read the results of the command public static Task ExecuteReaderAsync(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters) { return ExecuteReaderAsync(connection, null, commandText, commandParameters, true, CancellationToken.None); } public static Task ExecuteReaderAsync(MySqlConnection connection, string commandText, CancellationToken cancellationToken, params MySqlParameter[] commandParameters) { return ExecuteReaderAsync(connection, null, commandText, commandParameters, true, cancellationToken); } #endregion #region Scalar /// /// Async version of ExecuteScalar /// /// Settings to use for the update /// Command text to use for the update /// The first column of the first row in the result set, or a null reference if the result set is empty. public static Task ExecuteScalarAsync(string connectionString, string commandText) { return ExecuteScalarAsync(connectionString, commandText, CancellationToken.None, (MySqlParameter[])null); } public static Task ExecuteScalarAsync(string connectionString, string commandText, CancellationToken cancellationToken) { return ExecuteScalarAsync(connectionString, commandText, cancellationToken, (MySqlParameter[])null); } /// /// Async version of ExecuteScalar /// /// Settings to use for the command /// Command text to use for the command /// Parameters to use for the command /// The first column of the first row in the result set, or a null reference if the result set is empty. public static Task ExecuteScalarAsync(string connectionString, string commandText, params MySqlParameter[] commandParameters) { return ExecuteScalarAsync(connectionString, commandText, CancellationToken.None, commandParameters); } public static Task ExecuteScalarAsync(string connectionString, string commandText, CancellationToken cancellationToken, params MySqlParameter[] commandParameters) { var result = new TaskCompletionSource(); if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested) { try { var scalarResult = ExecuteScalar(connectionString, commandText, commandParameters); result.SetResult(scalarResult); } catch (Exception ex) { result.SetException(ex); } } else { result.SetCanceled(); } return result.Task; } /// /// Async version of ExecuteScalar /// /// object to use /// Command text to use for the command /// The first column of the first row in the result set, or a null reference if the result set is empty. public static Task ExecuteScalarAsync(MySqlConnection connection, string commandText) { return ExecuteScalarAsync(connection, commandText, CancellationToken.None, (MySqlParameter[])null); } public static Task ExecuteScalarAsync(MySqlConnection connection, string commandText, CancellationToken cancellationToken) { return ExecuteScalarAsync(connection, commandText, cancellationToken, (MySqlParameter[])null); } /// /// Async version of ExecuteScalar /// /// object to use /// Command text to use for the command /// Parameters to use for the command /// The first column of the first row in the result set, or a null reference if the result set is empty. public static Task ExecuteScalarAsync(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters) { return ExecuteScalarAsync(connection, commandText, CancellationToken.None, commandParameters); } public static Task ExecuteScalarAsync(MySqlConnection connection, string commandText, CancellationToken cancellationToken, params MySqlParameter[] commandParameters) { var result = new TaskCompletionSource(); if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested) { try { var scalarResult = ExecuteScalar(connection, commandText, commandParameters); result.SetResult(scalarResult); } catch (Exception ex) { result.SetException(ex); } } else { result.SetCanceled(); } return result.Task; } #endregion #endregion } } #endif