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

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

@ -1,7 +1,8 @@
using System.Reflection;
using MySql.Data.MySqlClient;
using OMS.NET.Common;
namespace OMS.NET.DbClass{
namespace OMS.NET.DbClass
{
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class TableAttribute : Attribute
{
@ -27,13 +28,20 @@ namespace OMS.NET.DbClass{
}
}
public class SqlHelper{
public class SqlHelper
{
public static string GetTableName(Type type)
{
var attr = type.GetCustomAttribute<TableAttribute>();
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)
{
return type.GetProperties()

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

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

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

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

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

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

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

@ -1,4 +1,7 @@
using System.Text.Json;
using OMS.NET.Common;
using OMS.NET.DbClass;
using ZstdSharp.Unsafe;
namespace OMS.NET.Instructs
{
@ -9,7 +12,7 @@ namespace OMS.NET.Instructs
{
public TestInstruct()
{
this.Type = "test";
Type = "test";
}
public override Task Handler(string wsid)
@ -17,8 +20,47 @@ namespace OMS.NET.Instructs
return Task.Run(() =>
{
//GlobalArea.UserConnects.Where(x => x.ID == wsid).First().UserEmail = "xxx@xx.com";//login
Log.Info("当前客户端连接数:" + GlobalArea.ConnectClientsCount);
Log.Info("当前登录用户数:" + GlobalArea.LoginUserCount);
// Log.Info("当前客户端连接数:" + GlobalArea.ConnectClientsCount);
// 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);
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);
if (instruct != null)
{
await instruct.HandlerAndMeasure(this.ID);
await instruct.HandlerAndMeasure(ID);
if (instruct.ResponseOrBroadcastInstructs.Count > 0)
{
instruct.ResponseOrBroadcastInstructs.ForEach(res =>
@ -28,7 +28,7 @@ namespace OMS.NET
if (res.IsBroadcast)
{
string str = res.ToJsonString();
foreach (IWebSocketSession session in this.Sessions.Sessions)
foreach (IWebSocketSession session in Sessions.Sessions)
{
//看起来只有登录后的连接才能收到广播,这里添加下过滤
if (GlobalArea.LoginCheckByID(session.ID))
@ -44,23 +44,23 @@ namespace OMS.NET
protected override void OnOpen()
{
this.iPEndPoint = this.Context.UserEndPoint;
GlobalArea.AddUserConnect(this.ID, this.iPEndPoint);
// Console.WriteLine(this.ID + " " + this.iPEndPoint.ToString() + " Conection Open");
iPEndPoint = Context.UserEndPoint;
GlobalArea.AddUserConnect(ID, iPEndPoint);
Log.Info(ID + " " + iPEndPoint.ToString() + " Conection Open");
// Console.WriteLine($"当前连接客户端数量: {GlobalArea.ConnectClientsCount}");
}
protected override void OnClose(CloseEventArgs e)
{
GlobalArea.RemoveUserConnectByID(this.ID);
// Console.WriteLine(this.ID + " " + this.iPEndPoint.ToString() + " Conection Close" + e.Reason);
GlobalArea.RemoveUserConnectByID(ID);
Log.Info(this.ID + " " + this.iPEndPoint.ToString() + " Conection Close" + e.Reason);
// Console.WriteLine($"当前连接客户端数量: {GlobalArea.ConnectClientsCount}");
}
protected override void OnError(ErrorEventArgs e)
{
GlobalArea.RemoveUserConnectByID(this.ID);
// Console.WriteLine(this.ID + " " + this.iPEndPoint.ToString() + " Conection Error Close" + e.Message);
GlobalArea.RemoveUserConnectByID(ID);
Log.Info(this.ID + " " + this.iPEndPoint.ToString() + " Conection Error Close" + e.Message);
// Console.WriteLine($"当前连接客户端数量: {GlobalArea.ConnectClientsCount}");
}
}

Loading…
Cancel
Save