mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-22 03:59:36 +08:00
开始搭SQL框架
This commit is contained in:
parent
163ad53e48
commit
bf6b50ba9f
@ -33,15 +33,19 @@
|
||||
<ItemGroup>
|
||||
<Reference Include="FunGame.Core">
|
||||
<HintPath>..\..\FunGame\bin\Server\Debug\net7.0\FunGame.Core.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="MySql.Data">
|
||||
<HintPath>..\..\FunGame\bin\Server\Debug\net6.0\MySql.Data.dll</HintPath>
|
||||
<EmbedInteropTypes>False</EmbedInteropTypes>
|
||||
</Reference>
|
||||
<Reference Include="System.Configuration.ConfigurationManager">
|
||||
<HintPath>..\..\FunGame\bin\Server\Debug\net6.0\System.Configuration.ConfigurationManager.dll</HintPath>
|
||||
<EmbedInteropTypes>False</EmbedInteropTypes>
|
||||
</Reference>
|
||||
<Reference Include="System.Security.Permissions">
|
||||
<HintPath>..\..\FunGame\bin\Server\Debug\net6.0\System.Security.Permissions.dll</HintPath>
|
||||
<EmbedInteropTypes>False</EmbedInteropTypes>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -5,6 +5,7 @@ using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.Server.Model;
|
||||
using Milimoe.FunGame.Server.Others;
|
||||
using Milimoe.FunGame.Server.Utility;
|
||||
using Milimoe.FunGame.Server.Utility.DataUtility;
|
||||
|
||||
Console.Title = Config.SERVER_NAME;
|
||||
Console.WriteLine(FunGameInfo.GetInfo((FunGameInfo.FunGame)Config.FunGameType));
|
||||
@ -72,10 +73,10 @@ void StartServer()
|
||||
Console.Title = Config.SERVER_NAME + " - FunGame Server Port: " + Config.SERVER_PORT;
|
||||
}
|
||||
|
||||
DataHelper.Close();
|
||||
MySQLConnection.Close();
|
||||
|
||||
// 连接MySQL服务器
|
||||
if (!DataHelper.Connect())
|
||||
if (!MySQLConnection.Connect(out _))
|
||||
{
|
||||
Running = false;
|
||||
throw new ServerErrorException();
|
||||
|
76
FunGame.Server/Utility/DataUtility/MySQLConnection.cs
Normal file
76
FunGame.Server/Utility/DataUtility/MySQLConnection.cs
Normal file
@ -0,0 +1,76 @@
|
||||
using MySql.Data.MySqlClient;
|
||||
using Milimoe.FunGame.Core.Api.Utility;
|
||||
|
||||
namespace Milimoe.FunGame.Server.Utility.DataUtility
|
||||
{
|
||||
public class MySQLConnection
|
||||
{
|
||||
public static MySqlConnection? Connection = null;
|
||||
|
||||
public static string Name { get; set; } = "";
|
||||
public static string DataSource { get; set; } = "";
|
||||
public static string Port { get; set; } = "";
|
||||
public static string DataBase { get; set; } = "";
|
||||
public static string User { get; set; } = "";
|
||||
public static string Password { get; set; } = "";
|
||||
public static string GetConnection { get; set; } = "";
|
||||
|
||||
public static bool Connect(out MySqlConnection? conn)
|
||||
{
|
||||
try
|
||||
{
|
||||
GetConnection = GetConnectProperties();
|
||||
if (GetConnection != null)
|
||||
{
|
||||
string[] DataSetting = GetConnection.Split(";");
|
||||
if (DataSetting.Length > 1 && DataSetting[0].Length > 14 && DataSetting[1].Length > 8)
|
||||
{
|
||||
ServerHelper.WriteLine("Connect -> MySQL://" + DataSetting[0][14..] + ":" + DataSetting[1][8..]);
|
||||
}
|
||||
Connection = new MySqlConnection(GetConnection);
|
||||
Connection.Open();
|
||||
if (Connection.State == System.Data.ConnectionState.Open)
|
||||
{
|
||||
ServerHelper.WriteLine("Connected: MySQL服务器连接成功");
|
||||
conn = Connection;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new MySQLConfigException();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ServerHelper.Error(e);
|
||||
}
|
||||
conn = Connection;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void Close()
|
||||
{
|
||||
if (Connection != null && Connection.State == System.Data.ConnectionState.Open)
|
||||
{
|
||||
Connection.Close();
|
||||
}
|
||||
Connection = null;
|
||||
}
|
||||
|
||||
private static string GetConnectProperties()
|
||||
{
|
||||
if (INIHelper.ExistINIFile())
|
||||
{
|
||||
DataSource = INIHelper.ReadINI("MySQL", "DBServer");
|
||||
Port = INIHelper.ReadINI("MySQL", "DBPort");
|
||||
DataBase = INIHelper.ReadINI("MySQL", "DBName");
|
||||
User = INIHelper.ReadINI("MySQL", "DBUser");
|
||||
Password = INIHelper.ReadINI("MySQL", "DBPassword");
|
||||
return "data source = " + DataSource + "; port = " + Port + "; database = " + DataBase + "; user = " + User + "; password = " + Password + "; charset = utf8mb4;";
|
||||
}
|
||||
else ServerHelper.Error(new Exception("找不到配置文件。"));
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
165
FunGame.Server/Utility/DataUtility/MySQLHelper.cs
Normal file
165
FunGame.Server/Utility/DataUtility/MySQLHelper.cs
Normal file
@ -0,0 +1,165 @@
|
||||
using System.Data;
|
||||
using MySql.Data.MySqlClient;
|
||||
using Milimoe.FunGame.Core.Api.Data;
|
||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.Server.Utility.DataUtility;
|
||||
|
||||
namespace Milimoe.FunGame.Server.Utility
|
||||
{
|
||||
internal class MySQLHelper : SQLHelper
|
||||
{
|
||||
public new string Script { get; set; } = "";
|
||||
public new EntityType EntityType => _EntityType;
|
||||
public new object Entity => _Entity;
|
||||
public new SQLResult Result => _Result;
|
||||
public override SQLServerInfo ServerInfo
|
||||
{
|
||||
get
|
||||
{
|
||||
return _ServerInfo ?? SQLServerInfo.Create();
|
||||
}
|
||||
}
|
||||
public new int UpdateRows => _UpdateRows;
|
||||
public DataSet DataSet => _DataSet;
|
||||
|
||||
private EntityType _EntityType;
|
||||
private object _Entity = General.EntityInstance;
|
||||
private SQLResult _Result;
|
||||
private SQLServerInfo? _ServerInfo;
|
||||
private int _UpdateRows = 0;
|
||||
private DataSet _DataSet = new DataSet();
|
||||
|
||||
public static SQLHelper GetHelper()
|
||||
{
|
||||
return new MySQLHelper();
|
||||
}
|
||||
|
||||
public override SQLResult Execute()
|
||||
{
|
||||
return SQLResult.NotFound;
|
||||
}
|
||||
|
||||
public DataSet ExecuteDataSet()
|
||||
{
|
||||
return _DataSet;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行一个sql命令
|
||||
/// </summary>
|
||||
/// <param name="type">存储过程, 文本, 等等</param>
|
||||
/// <param name="sql">存储过程名称或者sql语句</param>
|
||||
/// <param name="parameters">执行命令的参数</param>
|
||||
/// <returns>执行命令所影响的行数</returns>
|
||||
private int Execute(CommandType type, string sql, params MySqlParameter[] parameters)
|
||||
{
|
||||
MySqlCommand cmd = new();
|
||||
|
||||
PrepareCommand(cmd, null, type, sql, parameters);
|
||||
|
||||
int updaterow = cmd.ExecuteNonQuery();
|
||||
return updaterow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回DataSet
|
||||
/// </summary>
|
||||
/// <param name="type">存储过程, 文本, 等等</param>
|
||||
/// <param name="sql">存储过程名称或者sql语句</param>
|
||||
/// <param name="parameters">执行命令所用参数的集合</param>
|
||||
/// <returns></returns>
|
||||
private DataSet ExecuteDataSet(CommandType type, string sql, params MySqlParameter[] parameters)
|
||||
{
|
||||
MySqlCommand cmd = new();
|
||||
DataSet ds = new();
|
||||
|
||||
try
|
||||
{
|
||||
PrepareCommand(cmd, null, type, sql, parameters);
|
||||
|
||||
MySqlDataAdapter adapter = new()
|
||||
{
|
||||
SelectCommand = cmd
|
||||
};
|
||||
adapter.Fill(ds);
|
||||
|
||||
//清 除参数
|
||||
cmd.Parameters.Clear();
|
||||
return ds;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ServerHelper.Error(e);
|
||||
}
|
||||
|
||||
return ds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回插入值ID
|
||||
/// </summary>
|
||||
/// <param name="type">存储过程, 文本, 等等</param>
|
||||
/// <param name="sql">存储过程名称或者sql语句</param>
|
||||
/// <param name="parameters">执行命令所用参数的集合</param>
|
||||
/// <returns></returns>
|
||||
private object ExecuteNonExist(CommandType type, string sql, params MySqlParameter[] parameters)
|
||||
{
|
||||
MySqlCommand cmd = new();
|
||||
|
||||
PrepareCommand(cmd, null, type, sql, parameters);
|
||||
cmd.ExecuteNonQuery();
|
||||
|
||||
return cmd.LastInsertedId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 准备执行一个命令
|
||||
/// </summary>
|
||||
/// <param name="cmd">命令</param>
|
||||
/// <param name="trans">事务</param>
|
||||
/// <param name="type">存储过程或者文本</param>
|
||||
/// <param name="sql">sql语句</param>
|
||||
/// <param name="parameters">执行命令的参数</param>
|
||||
private void PrepareCommand(MySqlCommand cmd, MySqlTransaction? trans, CommandType type, string sql, MySqlParameter[] parameters)
|
||||
{
|
||||
MySqlConnection? conn = MySQLConnection.Connection;
|
||||
if (conn != null && conn.State != ConnectionState.Open)
|
||||
conn.Open();
|
||||
|
||||
cmd.Connection = conn;
|
||||
cmd.CommandText = sql;
|
||||
|
||||
if (trans != null)
|
||||
{
|
||||
cmd.Transaction = trans;
|
||||
}
|
||||
|
||||
cmd.CommandType = type;
|
||||
|
||||
if (parameters != null)
|
||||
{
|
||||
foreach (MySqlParameter parm in parameters)
|
||||
{
|
||||
cmd.Parameters.Add(parm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建SQLHelper
|
||||
/// </summary>
|
||||
/// <param name="type">获取实体类的类型</param>
|
||||
/// <param name="info">SQL服务器信息</param>
|
||||
/// <param name="result">执行结果</param>
|
||||
/// <param name="rows">更新的行数</param>
|
||||
private MySQLHelper(EntityType type = EntityType.Empty, SQLServerInfo? info = null, SQLResult result = SQLResult.Success, int rows = 0)
|
||||
{
|
||||
_EntityType = type;
|
||||
if (info == null) _ServerInfo = SQLServerInfo.Create();
|
||||
else _ServerInfo = info;
|
||||
_Result = result;
|
||||
_UpdateRows = rows;
|
||||
}
|
||||
}
|
||||
}
|
83
FunGame.Server/Utility/DataUtility/MySQLManager.cs
Normal file
83
FunGame.Server/Utility/DataUtility/MySQLManager.cs
Normal file
@ -0,0 +1,83 @@
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
using Milimoe.FunGame.Core.Api.Data;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.Core.Service;
|
||||
|
||||
namespace Milimoe.FunGame.Server.Utility
|
||||
{
|
||||
public class MySQLManager : SQLManager
|
||||
{
|
||||
public new SQLHelper SQLHelper { get; }
|
||||
|
||||
public MySQLManager(SQLHelper SQLHelper)
|
||||
{
|
||||
this.SQLHelper = SQLHelper;
|
||||
}
|
||||
|
||||
public override int Add(StringBuilder sql, ref SQLResult result)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public override int Add(string sql, ref SQLResult result)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public override SQLResult Execute()
|
||||
{
|
||||
return SQLResult.NotFound;
|
||||
}
|
||||
|
||||
public override SQLResult Execute(StringBuilder sql)
|
||||
{
|
||||
return SQLResult.NotFound;
|
||||
}
|
||||
|
||||
public override SQLResult Execute(string sql)
|
||||
{
|
||||
return SQLResult.NotFound;
|
||||
}
|
||||
|
||||
public override DataSet ExecuteDataSet(StringBuilder sql)
|
||||
{
|
||||
return new DataSet();
|
||||
}
|
||||
|
||||
public override DataSet ExecuteDataSet(string sql)
|
||||
{
|
||||
return new DataSet();
|
||||
}
|
||||
|
||||
public override object Query(EntityType type, StringBuilder sql)
|
||||
{
|
||||
return General.EntityInstance;
|
||||
}
|
||||
|
||||
public override object Query(EntityType type, string sql)
|
||||
{
|
||||
return General.EntityInstance;
|
||||
}
|
||||
|
||||
public override int Remove(StringBuilder sql, ref SQLResult result)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public override int Remove(string sql, ref SQLResult result)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public override int Update(StringBuilder sql, ref SQLResult result)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public override int Update(string sql, ref SQLResult result)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -6,89 +6,6 @@ using Milimoe.FunGame.Server.Others;
|
||||
|
||||
namespace Milimoe.FunGame.Server.Utility
|
||||
{
|
||||
public class DataHelper
|
||||
{
|
||||
private static MySqlConnection? msc = null;
|
||||
|
||||
public static string Name { get; set; } = "";
|
||||
public static string DataSource { get; set; } = "";
|
||||
public static string Port { get; set; } = "";
|
||||
public static string DataBase { get; set; } = "";
|
||||
public static string User { get; set; } = "";
|
||||
public static string Password { get; set; } = "";
|
||||
public static string GetConnection { get; set; } = "";
|
||||
|
||||
private static string GetConnectProperties()
|
||||
{
|
||||
if (INIHelper.ExistINIFile())
|
||||
{
|
||||
DataSource = INIHelper.ReadINI("MySQL", "DBServer");
|
||||
Port = INIHelper.ReadINI("MySQL", "DBPort");
|
||||
DataBase = INIHelper.ReadINI("MySQL", "DBName");
|
||||
User = INIHelper.ReadINI("MySQL", "DBUser");
|
||||
Password = INIHelper.ReadINI("MySQL", "DBPassword");
|
||||
return "data source = " + DataSource + "; port = " + Port + "; database = " + DataBase + "; user = " + User + "; password = " + Password + "; charset = utf8mb4;";
|
||||
}
|
||||
else ServerHelper.Error(new Exception("找不到配置文件。"));
|
||||
return "";
|
||||
}
|
||||
|
||||
private static string GetConnectProperties(string datasource, string port, string database, string user, string password)
|
||||
{
|
||||
DataSource = datasource;
|
||||
Port = port;
|
||||
DataBase = database;
|
||||
User = user;
|
||||
Password = password;
|
||||
return "data source = " + DataSource + "; port = " + Port + "; database = " + DataBase + "; user = " + User + "; password = " + Password + "; charset = utf8mb4;";
|
||||
}
|
||||
|
||||
public static bool Connect(object[]? objs = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (objs != null && objs.Length == 5)
|
||||
{
|
||||
GetConnection = GetConnectProperties((string)objs[0], (string)objs[1], (string)objs[2], (string)objs[3], (string)objs[4]);
|
||||
}
|
||||
else GetConnection = GetConnectProperties();
|
||||
if (GetConnection != null)
|
||||
{
|
||||
string[] DataSetting = GetConnection.Split(";");
|
||||
if (DataSetting.Length > 1 && DataSetting[0].Length > 14 && DataSetting[1].Length > 8)
|
||||
{
|
||||
ServerHelper.WriteLine("Connect -> MySQL://" + DataSetting[0][14..] + ":" + DataSetting[1][8..]);
|
||||
}
|
||||
msc = new MySqlConnection(GetConnection);
|
||||
msc.Open();
|
||||
if (msc.State == System.Data.ConnectionState.Open)
|
||||
{
|
||||
ServerHelper.WriteLine("Connected: MySQL服务器连接成功");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new MySQLConfigException();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ServerHelper.Error(e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void Close()
|
||||
{
|
||||
if (msc != null && msc.State == System.Data.ConnectionState.Open)
|
||||
{
|
||||
msc.Close();
|
||||
}
|
||||
msc = null;
|
||||
}
|
||||
}
|
||||
|
||||
public class ServerHelper
|
||||
{
|
||||
public static string GetPrefix()
|
||||
|
Loading…
x
Reference in New Issue
Block a user