You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

339 lines
15 KiB

4 years ago
  1. #if MYSQL_6_10
  2. // Copyright © 2004, 2018, Oracle and/or its affiliates. All rights reserved.
  3. //
  4. // MySQL Connector/NET is licensed under the terms of the GPLv2
  5. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  6. // MySQL Connectors. There are special exceptions to the terms and
  7. // conditions of the GPLv2 as it is applied to this software, see the
  8. // FLOSS License Exception
  9. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  10. //
  11. // This program is free software; you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published
  13. // by the Free Software Foundation; version 2 of the License.
  14. //
  15. // This program is distributed in the hope that it will be useful, but
  16. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. // for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License along
  21. // with this program; if not, write to the Free Software Foundation, Inc.,
  22. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. using System;
  24. using System.Data;
  25. using System.Threading;
  26. using System.Threading.Tasks;
  27. namespace Externals.MySql.Data.MySqlClient
  28. {
  29. internal partial class MySqlHelper
  30. {
  31. #region DataRow
  32. /// <summary>
  33. /// Asynchronous version of ExecuteDataRow.
  34. /// </summary>
  35. /// <param name="connectionString">The settings to be used for the connection.</param>
  36. /// <param name="commandText">The command to execute.</param>
  37. /// <param name="parms">The parameters to use for the command.</param>
  38. /// <returns>The DataRow containing the first row of the resultset.</returns>
  39. public static Task<DataRow> ExecuteDataRowAsync(string connectionString, string commandText, params MySqlParameter[] parms)
  40. {
  41. return ExecuteDataRowAsync(connectionString, commandText, CancellationToken.None, parms);
  42. }
  43. /// <summary>
  44. /// Asynchronous version of ExecuteDataRow.
  45. /// </summary>
  46. /// <param name="connectionString">The settings to be used for the connection.</param>
  47. /// <param name="commandText">The command to execute.</param>
  48. /// <param name="cancellationToken">The cancellation token.</param>
  49. /// <param name="parms">The parameters to use for the command.</param>
  50. /// <returns>The DataRow containing the first row of the resultset.</returns>
  51. public static Task<DataRow> ExecuteDataRowAsync(string connectionString, string commandText, CancellationToken cancellationToken, params MySqlParameter[] parms)
  52. {
  53. var result = new TaskCompletionSource<DataRow>();
  54. if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested)
  55. {
  56. try
  57. {
  58. var row = ExecuteDataRow(connectionString, commandText, parms);
  59. result.SetResult(row);
  60. }
  61. catch (Exception ex)
  62. {
  63. result.SetException(ex);
  64. }
  65. }
  66. else
  67. {
  68. result.SetCanceled();
  69. }
  70. return result.Task;
  71. }
  72. #endregion
  73. #region DataSet
  74. /// <summary>
  75. /// Executes a single SQL command and returns the first row of the resultset. A new MySqlConnection object
  76. /// is created, opened, and closed during this method.
  77. /// </summary>
  78. /// <param name="connectionString">Settings to be used for the connection</param>
  79. /// <param name="commandText">Command to execute</param>
  80. /// <param name="parms">Parameters to use for the command</param>
  81. /// <returns>DataRow containing the first row of the resultset</returns>
  82. public static DataRow ExecuteDataRow(string connectionString, string commandText, params MySqlParameter[] parms)
  83. {
  84. DataSet ds = ExecuteDataset(connectionString, commandText, parms);
  85. if (ds == null) return null;
  86. if (ds.Tables.Count == 0) return null;
  87. if (ds.Tables[0].Rows.Count == 0) return null;
  88. return ds.Tables[0].Rows[0];
  89. }
  90. /// <summary>
  91. /// Executes a single SQL command and returns the resultset in a <see cref="DataSet"/>.
  92. /// A new MySqlConnection object is created, opened, and closed during this method.
  93. /// </summary>
  94. /// <param name="connectionString">Settings to be used for the connection</param>
  95. /// <param name="commandText">Command to execute</param>
  96. /// <returns><see cref="DataSet"/> containing the resultset</returns>
  97. public static DataSet ExecuteDataset(string connectionString, string commandText)
  98. {
  99. //pass through the call providing null for the set of SqlParameters
  100. return ExecuteDataset(connectionString, commandText, (MySqlParameter[])null);
  101. }
  102. /// <summary>
  103. /// Executes a single SQL command and returns the resultset in a <see cref="DataSet"/>.
  104. /// A new MySqlConnection object is created, opened, and closed during this method.
  105. /// </summary>
  106. /// <param name="connectionString">Settings to be used for the connection</param>
  107. /// <param name="commandText">Command to execute</param>
  108. /// <param name="commandParameters">Parameters to use for the command</param>
  109. /// <returns><see cref="DataSet"/> containing the resultset</returns>
  110. public static DataSet ExecuteDataset(string connectionString, string commandText, params MySqlParameter[] commandParameters)
  111. {
  112. //create & open a SqlConnection, and dispose of it after we are done.
  113. using (MySqlConnection cn = new MySqlConnection(connectionString))
  114. {
  115. cn.Open();
  116. //call the overload that takes a connection in place of the connection string
  117. return ExecuteDataset(cn, commandText, commandParameters);
  118. }
  119. }
  120. /// <summary>
  121. /// Executes a single SQL command and returns the resultset in a <see cref="DataSet"/>.
  122. /// The state of the <see cref="MySqlConnection"/> object remains unchanged after execution
  123. /// of this method.
  124. /// </summary>
  125. /// <param name="connection"><see cref="MySqlConnection"/> object to use</param>
  126. /// <param name="commandText">Command to execute</param>
  127. /// <returns><see cref="DataSet"/> containing the resultset</returns>
  128. public static DataSet ExecuteDataset(MySqlConnection connection, string commandText)
  129. {
  130. //pass through the call providing null for the set of SqlParameters
  131. return ExecuteDataset(connection, commandText, (MySqlParameter[])null);
  132. }
  133. /// <summary>
  134. /// Executes a single SQL command and returns the resultset in a <see cref="DataSet"/>.
  135. /// The state of the <see cref="MySqlConnection"/> object remains unchanged after execution
  136. /// of this method.
  137. /// </summary>
  138. /// <param name="connection"><see cref="MySqlConnection"/> object to use</param>
  139. /// <param name="commandText">Command to execute</param>
  140. /// <param name="commandParameters">Parameters to use for the command</param>
  141. /// <returns><see cref="DataSet"/> containing the resultset</returns>
  142. public static DataSet ExecuteDataset(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters)
  143. {
  144. //create a command and prepare it for execution
  145. MySqlCommand cmd = new MySqlCommand();
  146. cmd.Connection = connection;
  147. cmd.CommandText = commandText;
  148. cmd.CommandType = CommandType.Text;
  149. if (commandParameters != null)
  150. foreach (MySqlParameter p in commandParameters)
  151. cmd.Parameters.Add(p);
  152. //create the DataAdapter & DataSet
  153. MySqlDataAdapter da = new MySqlDataAdapter(cmd);
  154. DataSet ds = new DataSet();
  155. //fill the DataSet using default values for DataTable names, etc.
  156. da.Fill(ds);
  157. // detach the MySqlParameters from the command object, so they can be used again.
  158. cmd.Parameters.Clear();
  159. //return the dataset
  160. return ds;
  161. }
  162. /// <summary>
  163. /// Updates the given table with data from the given <see cref="DataSet"/>
  164. /// </summary>
  165. /// <param name="connectionString">Settings to use for the update</param>
  166. /// <param name="commandText">Command text to use for the update</param>
  167. /// <param name="ds"><see cref="DataSet"/> containing the new data to use in the update</param>
  168. /// <param name="tablename">Tablename in the dataset to update</param>
  169. public static void UpdateDataSet(string connectionString, string commandText, DataSet ds, string tablename)
  170. {
  171. MySqlConnection cn = new MySqlConnection(connectionString);
  172. cn.Open();
  173. MySqlDataAdapter da = new MySqlDataAdapter(commandText, cn);
  174. MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
  175. cb.ToString();
  176. da.Update(ds, tablename);
  177. cn.Close();
  178. }
  179. /// <summary>
  180. /// Async version of ExecuteDataset
  181. /// </summary>
  182. /// <param name="connectionString">Settings to be used for the connection</param>
  183. /// <param name="commandText">Command to execute</param>
  184. /// <returns><see cref="DataSet"/> containing the resultset</returns>
  185. public static Task<DataSet> ExecuteDatasetAsync(string connectionString, string commandText)
  186. {
  187. return ExecuteDatasetAsync(connectionString, commandText, CancellationToken.None, (MySqlParameter[])null);
  188. }
  189. public static Task<DataSet> ExecuteDatasetAsync(string connectionString, string commandText, CancellationToken cancellationToken)
  190. {
  191. return ExecuteDatasetAsync(connectionString, commandText, cancellationToken, (MySqlParameter[])null);
  192. }
  193. /// <summary>
  194. /// Async version of ExecuteDataset
  195. /// </summary>
  196. /// <param name="connectionString">Settings to be used for the connection</param>
  197. /// <param name="commandText">Command to execute</param>
  198. /// <param name="commandParameters">Parameters to use for the command</param>
  199. /// <returns><see cref="DataSet"/> containing the resultset</returns>
  200. public static Task<DataSet> ExecuteDatasetAsync(string connectionString, string commandText, params MySqlParameter[] commandParameters)
  201. {
  202. return ExecuteDatasetAsync(connectionString, commandText, CancellationToken.None, commandParameters);
  203. }
  204. public static Task<DataSet> ExecuteDatasetAsync(string connectionString, string commandText, CancellationToken cancellationToken, params MySqlParameter[] commandParameters)
  205. {
  206. var result = new TaskCompletionSource<DataSet>();
  207. if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested)
  208. {
  209. try
  210. {
  211. var dataset = ExecuteDataset(connectionString, commandText, commandParameters);
  212. result.SetResult(dataset);
  213. }
  214. catch (Exception ex)
  215. {
  216. result.SetException(ex);
  217. }
  218. }
  219. else
  220. {
  221. result.SetCanceled();
  222. }
  223. return result.Task;
  224. }
  225. /// <summary>
  226. /// Async version of ExecuteDataset
  227. /// </summary>
  228. /// <param name="connection"><see cref="MySqlConnection"/> object to use</param>
  229. /// <param name="commandText">Command to execute</param>
  230. /// <returns><see cref="DataSet"/> containing the resultset</returns>
  231. public static Task<DataSet> ExecuteDatasetAsync(MySqlConnection connection, string commandText)
  232. {
  233. return ExecuteDatasetAsync(connection, commandText, CancellationToken.None, (MySqlParameter[])null);
  234. }
  235. public static Task<DataSet> ExecuteDatasetAsync(MySqlConnection connection, string commandText, CancellationToken cancellationToken)
  236. {
  237. return ExecuteDatasetAsync(connection, commandText, cancellationToken, (MySqlParameter[])null);
  238. }
  239. /// <summary>
  240. /// Async version of ExecuteDataset
  241. /// </summary>
  242. /// <param name="connection"><see cref="MySqlConnection"/> object to use</param>
  243. /// <param name="commandText">Command to execute</param>
  244. /// <param name="commandParameters">Parameters to use for the command</param>
  245. /// <returns><see cref="DataSet"/> containing the resultset</returns>
  246. public static Task<DataSet> ExecuteDatasetAsync(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters)
  247. {
  248. return ExecuteDatasetAsync(connection, commandText, CancellationToken.None, commandParameters);
  249. }
  250. public static Task<DataSet> ExecuteDatasetAsync(MySqlConnection connection, string commandText, CancellationToken cancellationToken, params MySqlParameter[] commandParameters)
  251. {
  252. var result = new TaskCompletionSource<DataSet>();
  253. if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested)
  254. {
  255. try
  256. {
  257. var dataset = ExecuteDataset(connection, commandText, commandParameters);
  258. result.SetResult(dataset);
  259. }
  260. catch (Exception ex)
  261. {
  262. result.SetException(ex);
  263. }
  264. }
  265. else
  266. {
  267. result.SetCanceled();
  268. }
  269. return result.Task;
  270. }
  271. /// <summary>
  272. /// Async version of UpdateDataset
  273. /// </summary>
  274. /// <param name="connectionString">Settings to use for the update</param>
  275. /// <param name="commandText">Command text to use for the update</param>
  276. /// <param name="ds"><see cref="DataSet"/> containing the new data to use in the update</param>
  277. /// <param name="tablename">Tablename in the dataset to update</param>
  278. public static Task UpdateDataSetAsync(string connectionString, string commandText, DataSet ds, string tablename)
  279. {
  280. return UpdateDataSetAsync(connectionString, commandText, ds, tablename, CancellationToken.None);
  281. }
  282. public static Task UpdateDataSetAsync(string connectionString, string commandText, DataSet ds, string tablename, CancellationToken cancellationToken)
  283. {
  284. var result = new TaskCompletionSource<bool>();
  285. if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested)
  286. {
  287. try
  288. {
  289. UpdateDataSet(connectionString, commandText, ds, tablename);
  290. result.SetResult(true);
  291. }
  292. catch (Exception ex)
  293. {
  294. result.SetException(ex);
  295. }
  296. }
  297. else
  298. {
  299. result.SetCanceled();
  300. }
  301. return result.Task;
  302. }
  303. #endregion
  304. }
  305. }
  306. #endif