using System.Data; using MySql.Data.MySqlClient; using Milimoe.FunGame.Core.Api.Transmittal; using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Common.Architecture; using Milimoe.FunGame.Server.Utility.DataUtility; using Milimoe.FunGame.Server.Others; using Milimoe.FunGame.Server.Model; namespace Milimoe.FunGame.Server.Utility { public class MySQLHelper : SQLHelper { public override FunGameInfo.FunGame FunGameType => Config.FunGameType; public override string Script { get; set; } = ""; public override CommandType CommandType { get; set; } = CommandType.Text; public override SQLResult Result => _Result; public override bool Success => Result == SQLResult.Success; public override SQLServerInfo ServerInfo => _ServerInfo ?? SQLServerInfo.Create(); public override int UpdateRows => _UpdateRows; public override DataSet DataSet => _DataSet; public MySqlParameter[] Parameters { get; set; } public MySQLConnection? Connection => _Connection; private SQLResult _Result = SQLResult.Success; private SQLServerInfo? _ServerInfo; private int _UpdateRows = 0; private DataSet _DataSet = new(); private MySQLConnection? _Connection; private MySqlTransaction? _Transaction; private readonly ServerModel? ServerModel; private readonly bool _IsOneTime = false; /// /// 执行一个命令 /// /// 执行结果 /// 影响的行数 public override int Execute() { // _IsOneTime = true需要手动创建连接和关闭 if (_IsOneTime) _Connection = new MySQLConnection(out _ServerInfo); ServerHelper.WriteLine("SQLQuery -> " + Script); _DataSet = new DataSet(); _UpdateRows = MySQLManager.Execute(this, out _Result); if (_IsOneTime) Close(); return _UpdateRows; } /// /// 执行一个指定的命令 /// /// 命令 /// 执行结果 /// 影响的行数 public override int Execute(string Script) { // _IsOneTime = true需要手动创建连接和关闭 if (_IsOneTime) _Connection = new MySQLConnection(out _ServerInfo); ServerHelper.WriteLine("SQLQuery -> " + Script); this.Script = Script; _DataSet = new DataSet(); _UpdateRows = MySQLManager.Execute(this, out _Result); if (_IsOneTime) Close(); return _UpdateRows; } /// /// 查询DataSet /// /// 执行结果 /// 结果集 public override DataSet ExecuteDataSet() { // _IsOneTime = true需要手动创建连接和关闭 if (_IsOneTime) _Connection = new MySQLConnection(out _ServerInfo); ServerHelper.WriteLine("SQLQuery -> " + Script); _DataSet = MySQLManager.ExecuteDataSet(this, out _Result, out _UpdateRows); if (_IsOneTime) Close(); return DataSet; } /// /// 执行指定的命令查询DataSet /// /// 命令 /// 执行结果 /// 结果集 public override DataSet ExecuteDataSet(string Script) { // _IsOneTime = true需要手动创建连接和关闭 if (_IsOneTime) _Connection = new MySQLConnection(out _ServerInfo); ServerHelper.WriteLine("SQLQuery -> " + Script); this.Script = Script; _DataSet = MySQLManager.ExecuteDataSet(this, out _Result, out _UpdateRows); if (_IsOneTime) Close(); return DataSet; } /// /// 关闭连接 /// public override void Close() { // _IsOneTime = false需要手动调用此方法 _Connection?.Close(); ServerHelper.WriteLine($"{GetClientName()}已释放MySQL连接"); } /// /// 创建SQLHelper /// /// 是否是单次使用(执行完毕会自动Close连接) /// 存储过程名称或者script语句 /// 存储过程, 文本, 等等 /// 执行命令所用参数的集合 public MySQLHelper(string script = "", bool IsOneTime = true, CommandType type = CommandType.Text, params MySqlParameter[] parameters) { Script = script; _IsOneTime = IsOneTime; CommandType = type; Parameters = parameters; } /// /// 创建为SocketModel服务的SQLHelper /// /// SocketModel public MySQLHelper(ServerModel ServerModel) { this.ServerModel = ServerModel; Script = ""; CommandType = CommandType.Text; Parameters = Array.Empty(); _Connection = new MySQLConnection(out _ServerInfo); } /// /// 创建一个SQL事务 /// public void NewTransaction() { _Transaction ??= _Connection?.Connection?.BeginTransaction(); } /// /// 提交事务 /// public void Commit() { _Transaction?.Commit(); _Transaction = null; } /// /// 回滚事务 /// public void Rollback() { _Transaction?.Rollback(); _Transaction = null; } /// /// 获取客户端名称 /// /// private string GetClientName() { if (ServerModel is null) return ""; return ServerHelper.MakeClientName(ServerModel.ClientName, ServerModel.User) + " "; } } }