using System.Data;
using MySql.Data.MySqlClient;
using Milimoe.FunGame.Core.Api.Transmittal;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Library.Server;
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 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 readonly ServerModel? ServerModel;
private readonly bool _IsOneTime = false;
///
/// 执行一个命令
///
/// 执行结果
/// 影响的行数
public override int Execute(out SQLResult Result)
{
// _IsOneTime = true需要手动创建连接和关闭
if (_IsOneTime) _Connection = new MySQLConnection(out _ServerInfo);
ServerHelper.WriteLine("SQLQuery -> " + Script);
_DataSet = new DataSet();
_UpdateRows = MySQLManager.Execute(this, out Result);
_Result = Result;
if (_IsOneTime) Close();
return _UpdateRows;
}
///
/// 查询DataSet
///
/// 执行结果
/// 结果集
public override DataSet ExecuteDataSet(out SQLResult Result)
{
// _IsOneTime = true需要手动创建连接和关闭
if (_IsOneTime) _Connection = new MySQLConnection(out _ServerInfo);
ServerHelper.WriteLine("SQLQuery -> " + Script);
_DataSet = MySQLManager.ExecuteDataSet(this, out Result, out _UpdateRows);
_Result = Result;
if (_IsOneTime) Close();
return DataSet;
}
///
/// 关闭连接
///
public override void Close()
{
// _IsOneTime = false需要手动调用此方法
_Connection?.Close();
ServerHelper.WriteLine($"{GetClientName()}已释放MySQL连接");
}
///
/// 创建单次使用的SQLHelper(执行完毕会自动Close连接)
///
/// 存储过程名称或者script语句
/// 存储过程, 文本, 等等
/// 执行命令所用参数的集合
public MySQLHelper(string script = "", CommandType type = CommandType.Text, params MySqlParameter[] parameters)
{
_IsOneTime = true;
Script = script;
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);
}
private string GetClientName()
{
if (ServerModel is null) return "";
return SocketHelper.MakeClientName(ServerModel.ClientName, ServerModel.User) + " ";
}
}
}