在test指令中测试sqltable的一些方法,发现其对主键属性变化时的一些支持不太好

调整this.的格式
simpledesign
nxiaoxiao 1 year ago
parent 192dbff619
commit dc17af2ca7

@ -1,7 +1,8 @@
using System.Reflection; using System.Reflection;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using OMS.NET.Common; using OMS.NET.Common;
namespace OMS.NET.DbClass{ namespace OMS.NET.DbClass
{
[AttributeUsage(AttributeTargets.Class, Inherited = false)] [AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class TableAttribute : Attribute public class TableAttribute : Attribute
{ {
@ -27,13 +28,20 @@ namespace OMS.NET.DbClass{
} }
} }
public class SqlHelper{ public class SqlHelper
{
public static string GetTableName(Type type) public static string GetTableName(Type type)
{ {
var attr = type.GetCustomAttribute<TableAttribute>(); var attr = type.GetCustomAttribute<TableAttribute>();
return attr?.TableName ?? throw new Exception($"{type.Name} 没有指定数据表"); return attr?.TableName ?? throw new Exception($"{type.Name} 没有指定数据表");
} }
public static bool IsIdentity(Type type)
{
var primaryKey = GetPrimaryKeyProperties(type);
return primaryKey.GetCustomAttribute<ColumnAttribute>()!.IsIdentity;
}
public static IEnumerable<PropertyInfo> GetProperties(Type type) public static IEnumerable<PropertyInfo> GetProperties(Type type)
{ {
return type.GetProperties() return type.GetProperties()

@ -16,6 +16,9 @@ namespace OMS.NET.DbClass
Console.WriteLine("===========this is a test area================="); Console.WriteLine("===========this is a test area=================");
} }
/// <summary>
/// 将对象本身插入到数据库中
/// </summary>
public virtual void Insert() public virtual void Insert()
{ {
var type = GetType(); var type = GetType();
@ -38,7 +41,7 @@ namespace OMS.NET.DbClass
var type = GetType(); var type = GetType();
var updateSql = SqlHelper.GetUpdateSql(GetType()); var updateSql = SqlHelper.GetUpdateSql(GetType());
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName); using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
connection.Open(); // 确保在执行命令之前打开连接 connection.Open();
using var command = new MySqlCommand(updateSql, connection); using var command = new MySqlCommand(updateSql, connection);
AddParameters(command); AddParameters(command);
command.ExecuteNonQuery(); command.ExecuteNonQuery();
@ -47,25 +50,29 @@ namespace OMS.NET.DbClass
public virtual void Delete() public virtual void Delete()
{ {
var type = GetType();
var deleteSql = SqlHelper.GetDeleteSql(GetType()); var deleteSql = SqlHelper.GetDeleteSql(GetType());
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName); using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
connection.Open(); // 确保在执行命令之前打开连接 connection.Open();
using var command = new MySqlCommand(deleteSql, connection); using var command = new MySqlCommand(deleteSql, connection);
AddParameters(command); AddParameters(command);
command.ExecuteNonQuery(); command.ExecuteNonQuery();
Log.Info($"{SqlHelper.GetTableName(type)}数据表删除({string.Join(',', ShowDetail())})"); Log.Info($"{SqlHelper.GetTableName(GetType())}数据表删除({string.Join(',', ShowDetail())})");
} }
private void AddParameters(MySqlCommand command) private void AddParameters(MySqlCommand command)
{ {
//bool IsIdentity = SqlHelper.IsIdentity(GetType());
foreach (var property in SqlHelper.GetProperties(GetType())) foreach (var property in SqlHelper.GetProperties(GetType()))
{ {
if (property.GetCustomAttribute<ColumnAttribute>()!.IsIdentity)
{
continue;
}
command.Parameters.AddWithValue("@" + property.Name, property.GetValue(this)); command.Parameters.AddWithValue("@" + property.Name, property.GetValue(this));
} }
} }
private IEnumerable<string> ShowDetail() public IEnumerable<string> ShowDetail()
{ {
return SqlHelper.GetProperties(GetType()).Select(p => $"{p.Name}={p.GetValue(this)}"); return SqlHelper.GetProperties(GetType()).Select(p => $"{p.Name}={p.GetValue(this)}");
} }

@ -6,7 +6,7 @@ namespace OMS.NET.Instructs
{ {
public GetMapDataInstruct() public GetMapDataInstruct()
{ {
this.Type = "get_mapData"; Type = "get_mapData";
} }
public override Task Handler(string wsid) public override Task Handler(string wsid)
@ -25,7 +25,7 @@ namespace OMS.NET.Instructs
{ {
public SendMapDataInstruct() public SendMapDataInstruct()
{ {
this.Type = "send_mapData"; Type = "send_mapData";
} }
} }
} }

@ -6,7 +6,7 @@ namespace OMS.NET.Instructs
{ {
public GetMapLayerInstruct() public GetMapLayerInstruct()
{ {
this.Type = "get_mapLayer"; Type = "get_mapLayer";
} }
public override Task Handler(string wsid) public override Task Handler(string wsid)
@ -25,7 +25,7 @@ namespace OMS.NET.Instructs
{ {
public SendMapLayerInstruct() public SendMapLayerInstruct()
{ {
this.Type = "send_mapLayer"; Type = "send_mapLayer";
} }
} }
} }

@ -6,14 +6,14 @@ namespace OMS.NET.Instructs
{ {
public GetPublickeyInstruct() public GetPublickeyInstruct()
{ {
this.Type = "get_publickey"; Type = "get_publickey";
} }
public override Task Handler(string wsid) public override Task Handler(string wsid)
{ {
return Task.Run(() => return Task.Run(() =>
{ {
this.ResponseOrBroadcastInstructs.Add(new PublickeyInstruct() ResponseOrBroadcastInstructs.Add(new PublickeyInstruct()
{ {
Data = GlobalArea.GetRsaPublickKey(), Data = GlobalArea.GetRsaPublickKey(),
IsResponse = true IsResponse = true
@ -27,7 +27,7 @@ namespace OMS.NET.Instructs
{ {
public PublickeyInstruct() public PublickeyInstruct()
{ {
this.Type = "publickey"; Type = "publickey";
} }
} }

@ -6,7 +6,7 @@ namespace OMS.NET.Instructs
{ {
public GetUserDataInstruct() public GetUserDataInstruct()
{ {
this.Type = "get_userData"; Type = "get_userData";
} }
public override Task Handler(string wsid) public override Task Handler(string wsid)
@ -25,7 +25,7 @@ namespace OMS.NET.Instructs
{ {
public SendUserDataInstruct() public SendUserDataInstruct()
{ {
this.Type = "send_userData"; Type = "send_userData";
} }
} }
} }

@ -112,7 +112,7 @@ namespace OMS.NET.Instructs
public Instruct() public Instruct()
{ {
this.Type = ""; Type = "";
} }
/// <summary> /// <summary>
@ -130,7 +130,7 @@ namespace OMS.NET.Instructs
stopWatch.Start(); stopWatch.Start();
await Handler(wsid); await Handler(wsid);
stopWatch.Stop(); stopWatch.Stop();
Log.Debug($"处理{this.GetType()}耗时:{stopWatch.ElapsedMilliseconds}ms"); Log.Debug($"处理{GetType()}耗时:{stopWatch.ElapsedMilliseconds}ms");
} }
public string ToJsonString() public string ToJsonString()

@ -11,17 +11,17 @@ namespace OMS.NET.Instructs
{ {
public LoginInstruct() public LoginInstruct()
{ {
this.Type = "login"; Type = "login";
} }
public override Task Handler(string wsid) public override Task Handler(string wsid)
{ {
return Task.Run(() => return Task.Run(() =>
{ {
if (this.Data?.GetType() == typeof(JsonElement)) if (Data?.GetType() == typeof(JsonElement))
{ {
string email = this.Data.GetProperty("email").GetString(); string email = Data.GetProperty("email").GetString();
string password = this.Data.GetProperty("password").GetString(); string password = Data.GetProperty("password").GetString();
if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
{ {
Log.Warn("登录信息不能为空!"); Log.Warn("登录信息不能为空!");
@ -36,7 +36,7 @@ namespace OMS.NET.Instructs
{ {
public LoginStatusInstuct() public LoginStatusInstuct()
{ {
this.Type = "loginStatus"; Type = "loginStatus";
} }
} }
} }

@ -1,4 +1,7 @@
using System.Text.Json;
using OMS.NET.Common; using OMS.NET.Common;
using OMS.NET.DbClass;
using ZstdSharp.Unsafe;
namespace OMS.NET.Instructs namespace OMS.NET.Instructs
{ {
@ -9,7 +12,7 @@ namespace OMS.NET.Instructs
{ {
public TestInstruct() public TestInstruct()
{ {
this.Type = "test"; Type = "test";
} }
public override Task Handler(string wsid) public override Task Handler(string wsid)
@ -17,8 +20,47 @@ namespace OMS.NET.Instructs
return Task.Run(() => return Task.Run(() =>
{ {
//GlobalArea.UserConnects.Where(x => x.ID == wsid).First().UserEmail = "xxx@xx.com";//login //GlobalArea.UserConnects.Where(x => x.ID == wsid).First().UserEmail = "xxx@xx.com";//login
Log.Info("当前客户端连接数:" + GlobalArea.ConnectClientsCount); // Log.Info("当前客户端连接数:" + GlobalArea.ConnectClientsCount);
Log.Info("当前登录用户数:" + GlobalArea.LoginUserCount); // Log.Info("当前登录用户数:" + GlobalArea.LoginUserCount);
//测试数据表连接
if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查
try
{
int op = Data.GetInt32();
Map map = new()
{
Id = "map1",
Name = "test",
Description = "haksjhdkasd"
};
switch (op)
{
case 0:
map.Test();
break;
case 1:
map.Insert();
Log.Debug($"插入测试成功,新的id为{map.Id}");
break;
case 2:
Log.Debug(string.Join(',', map.ShowDetail()));
break;
case 3:
map.Description = "随便写一个有意义的字符串";
map.Update();
break;
case 4:
map.Delete();
Log.Debug($"删除测试成功");
break;
default:
throw new Exception("do nothing");
}
}
catch (Exception ex)
{
Log.Warn($"处理{Type}指令出错:" + ex.Message);
}
}); });
} }
} }

@ -11,11 +11,11 @@ namespace OMS.NET
private IPEndPoint iPEndPoint = new(IPAddress.Any, 0); private IPEndPoint iPEndPoint = new(IPAddress.Any, 0);
protected override async void OnMessage(MessageEventArgs e) protected override async void OnMessage(MessageEventArgs e)
{ {
Log.Debug(this.ID + " " + this.Context.UserEndPoint.ToString() + ":" + e.Data); Log.Debug(ID + " " + Context.UserEndPoint.ToString() + ":" + e.Data);
Instruct? instruct = Instruct.JsonStringParse(e.Data); Instruct? instruct = Instruct.JsonStringParse(e.Data);
if (instruct != null) if (instruct != null)
{ {
await instruct.HandlerAndMeasure(this.ID); await instruct.HandlerAndMeasure(ID);
if (instruct.ResponseOrBroadcastInstructs.Count > 0) if (instruct.ResponseOrBroadcastInstructs.Count > 0)
{ {
instruct.ResponseOrBroadcastInstructs.ForEach(res => instruct.ResponseOrBroadcastInstructs.ForEach(res =>
@ -28,7 +28,7 @@ namespace OMS.NET
if (res.IsBroadcast) if (res.IsBroadcast)
{ {
string str = res.ToJsonString(); string str = res.ToJsonString();
foreach (IWebSocketSession session in this.Sessions.Sessions) foreach (IWebSocketSession session in Sessions.Sessions)
{ {
//看起来只有登录后的连接才能收到广播,这里添加下过滤 //看起来只有登录后的连接才能收到广播,这里添加下过滤
if (GlobalArea.LoginCheckByID(session.ID)) if (GlobalArea.LoginCheckByID(session.ID))
@ -44,23 +44,23 @@ namespace OMS.NET
protected override void OnOpen() protected override void OnOpen()
{ {
this.iPEndPoint = this.Context.UserEndPoint; iPEndPoint = Context.UserEndPoint;
GlobalArea.AddUserConnect(this.ID, this.iPEndPoint); GlobalArea.AddUserConnect(ID, iPEndPoint);
// Console.WriteLine(this.ID + " " + this.iPEndPoint.ToString() + " Conection Open"); Log.Info(ID + " " + iPEndPoint.ToString() + " Conection Open");
// Console.WriteLine($"当前连接客户端数量: {GlobalArea.ConnectClientsCount}"); // Console.WriteLine($"当前连接客户端数量: {GlobalArea.ConnectClientsCount}");
} }
protected override void OnClose(CloseEventArgs e) protected override void OnClose(CloseEventArgs e)
{ {
GlobalArea.RemoveUserConnectByID(this.ID); GlobalArea.RemoveUserConnectByID(ID);
// Console.WriteLine(this.ID + " " + this.iPEndPoint.ToString() + " Conection Close" + e.Reason); Log.Info(this.ID + " " + this.iPEndPoint.ToString() + " Conection Close" + e.Reason);
// Console.WriteLine($"当前连接客户端数量: {GlobalArea.ConnectClientsCount}"); // Console.WriteLine($"当前连接客户端数量: {GlobalArea.ConnectClientsCount}");
} }
protected override void OnError(ErrorEventArgs e) protected override void OnError(ErrorEventArgs e)
{ {
GlobalArea.RemoveUserConnectByID(this.ID); GlobalArea.RemoveUserConnectByID(ID);
// Console.WriteLine(this.ID + " " + this.iPEndPoint.ToString() + " Conection Error Close" + e.Message); Log.Info(this.ID + " " + this.iPEndPoint.ToString() + " Conection Error Close" + e.Message);
// Console.WriteLine($"当前连接客户端数量: {GlobalArea.ConnectClientsCount}"); // Console.WriteLine($"当前连接客户端数量: {GlobalArea.ConnectClientsCount}");
} }
} }

Loading…
Cancel
Save