// Copyright © 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; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.Common; using MySql.Data.MySqlClient; using System.Data; using System.Globalization; namespace MySql.Web.Security { /// /// Perform basic operations against a Database /// internal class MySqlDatabaseWrapper : IDisposable { private MySqlConnection _conn; #region Public /// /// Initialize a new instance of the class /// /// Connection String public MySqlDatabaseWrapper(string connectionString) { _conn = new MySqlConnection(connectionString); } ~MySqlDatabaseWrapper() { this.Dispose(true); } /// /// Close the current instance /// public void Close() { this.Dispose(); } /// /// Dispose the current instance /// public void Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } /// /// Execute given query on the database /// /// Query to exeute /// Parameters used in the query /// Query resultset public IEnumerable ExecuteQuery(string cmdText, params object[] parametersValues) { CheckIsConnectionOpen(); MySqlCommand cmd = _conn.CreateCommand(); cmd.CommandText = cmdText; AddParameters(cmd, parametersValues); DataTable result = new DataTable(); MySqlDataAdapter adapter = new MySqlDataAdapter(cmd); adapter.Fill(result); foreach (DataRow row in result.Rows) { yield return row; } } /// /// Execute given query on the database /// /// Query to exeute /// Parameters used in the query /// First record in the Query resultset public DataRow ExecuteQuerySingleRecord(string cmdText, params object[] parametersValues) { return ExecuteQuery(cmdText, parametersValues).FirstOrDefault(); } /// /// Execute given query on the database /// /// Query to exeute /// Parameters used in the query /// Rows affected by the query public int ExecuteNonQuery(string cmdText, params object[] parametersValues) { CheckIsConnectionOpen(); MySqlCommand cmd = _conn.CreateCommand(); cmd.CommandText = cmdText; AddParameters(cmd, parametersValues); return cmd.ExecuteNonQuery(); } /// /// Execute given query on the database /// /// Query to exeute /// Parameters used in the query /// Value of the first column in the first row in the query resulset public object ExecuteScalar(string cmdText, params object[] parametersValues) { CheckIsConnectionOpen(); MySqlCommand cmd = _conn.CreateCommand(); cmd.CommandText = cmdText; AddParameters(cmd, parametersValues); return cmd.ExecuteScalar(); } /// /// Execute all given queries on the database inside of a transaction /// /// Queries to exeute /// If queries were successfully executed public bool ExecuteInTransaction(IEnumerable> commands) { CheckIsConnectionOpen(); MySqlTransaction tran = _conn.BeginTransaction(); try { foreach (var command in commands) { MySqlCommand cmd = _conn.CreateCommand(); cmd.CommandText = command.Item1; AddParameters(cmd, command.Item2); cmd.ExecuteNonQuery(); } tran.Commit(); return true; } catch (Exception) { tran.Rollback(); return false; } } #endregion #region Protected protected virtual void Dispose(bool disposing) { if (disposing && (this._conn != null)) { if (_conn.State != ConnectionState.Closed) _conn.Close(); _conn = null; } } #endregion #region Private /// /// Verifies if the current connection is open, if not is opened /// private void CheckIsConnectionOpen() { if (this._conn.State != ConnectionState.Open) { this._conn.Open(); } } /// /// Add parameters to a command, nomenclature name used for the parameters are 'param[n]' /// /// Command that will stores the parameters /// Parameters values private void AddParameters(MySqlCommand cmd, object[] values) { int ctr = 1; foreach (object value in values) { cmd.Parameters.Add(new MySqlParameter() { ParameterName = string.Format("param{0}", ctr), Value = value ?? DBNull.Value }); ctr++; } } #endregion } }