diff --git a/FunGame.Server/FunGame.Server.csproj b/FunGame.Server/FunGame.Server.csproj index c6c8fe7..408dba1 100644 --- a/FunGame.Server/FunGame.Server.csproj +++ b/FunGame.Server/FunGame.Server.csproj @@ -33,15 +33,19 @@ ..\..\FunGame\bin\Server\Debug\net7.0\FunGame.Core.dll + True ..\..\FunGame\bin\Server\Debug\net6.0\MySql.Data.dll + False ..\..\FunGame\bin\Server\Debug\net6.0\System.Configuration.ConfigurationManager.dll + False ..\..\FunGame\bin\Server\Debug\net6.0\System.Security.Permissions.dll + False diff --git a/FunGame.Server/Main.cs b/FunGame.Server/Main.cs index a76aa3c..185ae37 100644 --- a/FunGame.Server/Main.cs +++ b/FunGame.Server/Main.cs @@ -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(); diff --git a/FunGame.Server/Utility/DataUtility/MySQLConnection.cs b/FunGame.Server/Utility/DataUtility/MySQLConnection.cs new file mode 100644 index 0000000..1840e98 --- /dev/null +++ b/FunGame.Server/Utility/DataUtility/MySQLConnection.cs @@ -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 ""; + } + } +} diff --git a/FunGame.Server/Utility/DataUtility/MySQLHelper.cs b/FunGame.Server/Utility/DataUtility/MySQLHelper.cs new file mode 100644 index 0000000..15c47ef --- /dev/null +++ b/FunGame.Server/Utility/DataUtility/MySQLHelper.cs @@ -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; + } + + /// + /// 执行一个sql命令 + /// + /// 存储过程, 文本, 等等 + /// 存储过程名称或者sql语句 + /// 执行命令的参数 + /// 执行命令所影响的行数 + 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; + } + + /// + /// 返回DataSet + /// + /// 存储过程, 文本, 等等 + /// 存储过程名称或者sql语句 + /// 执行命令所用参数的集合 + /// + 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; + } + + /// + /// 返回插入值ID + /// + /// 存储过程, 文本, 等等 + /// 存储过程名称或者sql语句 + /// 执行命令所用参数的集合 + /// + private object ExecuteNonExist(CommandType type, string sql, params MySqlParameter[] parameters) + { + MySqlCommand cmd = new(); + + PrepareCommand(cmd, null, type, sql, parameters); + cmd.ExecuteNonQuery(); + + return cmd.LastInsertedId; + } + + /// + /// 准备执行一个命令 + /// + /// 命令 + /// 事务 + /// 存储过程或者文本 + /// sql语句 + /// 执行命令的参数 + 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); + } + } + } + + /// + /// 创建SQLHelper + /// + /// 获取实体类的类型 + /// SQL服务器信息 + /// 执行结果 + /// 更新的行数 + 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; + } + } +} diff --git a/FunGame.Server/Utility/DataUtility/MySQLManager.cs b/FunGame.Server/Utility/DataUtility/MySQLManager.cs new file mode 100644 index 0000000..7d05a92 --- /dev/null +++ b/FunGame.Server/Utility/DataUtility/MySQLManager.cs @@ -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; + } + } +} diff --git a/FunGame.Server/Utility/Utility.cs b/FunGame.Server/Utility/Utility.cs index 1d7dd3c..8cecf00 100644 --- a/FunGame.Server/Utility/Utility.cs +++ b/FunGame.Server/Utility/Utility.cs @@ -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()