Skip to content

Commit

Permalink
orm,增加datafield主表支持
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvin Huang committed Sep 7, 2023
1 parent 12be589 commit d2a41ab
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 25 deletions.
16 changes: 11 additions & 5 deletions H.Framework.Data.ORM/DataConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,27 @@ public static class DataConvert
}
else
{
if (dt.Columns.Contains(pi.Name))
var columnName = pi.Name;
if (pi.IsDefined(typeof(DataFieldAttribute)))
{
var dataFieldAttribute = pi.GetCustomAttribute<DataFieldAttribute>();
columnName = dataFieldAttribute.ColumnName;
}
if (dt.Columns.Contains(columnName))
{
try
{
if (!pi.CanWrite) continue;
if (dr.IsNull(pi?.Name)) continue;
if (dr.IsNull(columnName)) continue;
if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
pi.SetValue(t, Activator.CreateInstance(pi.PropertyType, dr[pi?.Name]), null);
pi.SetValue(t, Activator.CreateInstance(pi.PropertyType, dr[columnName]), null);
else
pi.SetValue(t, Convert.ChangeType(dr[pi?.Name], pi.PropertyType), null);
pi.SetValue(t, Convert.ChangeType(dr[columnName], pi.PropertyType), null);
}
catch (Exception e)
{
Trace.WriteLine(e.Message);
throw new InvalidCastException("字段名:" + pi?.Name + "-数据库值:" + dr[pi?.Name].ToString() + "-属性类型:" + pi.PropertyType.ToString());
throw new InvalidCastException("字段名:" + columnName + "-数据库值:" + dr[columnName].ToString() + "-属性类型:" + pi.PropertyType.ToString());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion H.Framework.Data.ORM/H.Framework.Data.ORM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>1.0.7.0</Version>
<Version>1.0.8.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyVersion>1.0.7.0</AssemblyVersion>
<FileVersion>1.0.7.0</FileVersion>
Expand Down
45 changes: 32 additions & 13 deletions H.Framework.Data.ORM/MySQLUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static MySQLUtility()
{
var properties = typeof(TModel).GetProperties();
var parms = new List<MySqlParameter>();
string columnName = "", columnParm = "", tableName = typeof(TModel).Name;
string columnNames = "", columnParm = "", tableName = typeof(TModel).Name;
foreach (var prop in properties)
{
if (prop.IsDefined(typeof(DataFieldIgnoreAttribute)) || prop.IsDefined(typeof(DynamicSQLFieldAttribute))) continue;
Expand All @@ -58,11 +58,18 @@ static MySQLUtility()
if (propValue == null) continue;
if (prop.PropertyType == typeof(DateTime) && Convert.ToDateTime(propValue) == DateTime.MinValue)
continue;
var columnName = prop.Name;
if (prop.IsDefined(typeof(DataFieldAttribute)))
{
var dataFieldAttribute = prop.GetCustomAttribute<DataFieldAttribute>();
columnName = dataFieldAttribute.ColumnName;
}

if (type == "add")
{
if (prop.IsDefined(typeof(PrimaryKeyIDAttribute))) continue;
if (prop.Name.ToUpper() == "ID") continue;
columnName += prop.Name + ",";
columnNames += columnName + ",";

#region 直接拼sql

Expand All @@ -77,7 +84,7 @@ static MySQLUtility()

#region 参数化

columnParm += $"@{prop.Name + index.ToString()},";
columnParm += $"@{columnName + index.ToString()},";

#endregion 参数化
}
Expand All @@ -98,18 +105,18 @@ static MySQLUtility()
#region 参数化

if (prop.Name.ToUpper() != "ID")
columnName += $"a.{ prop.Name } = @{prop.Name + index.ToString()},";
columnNames += $"a.{columnName} = @{columnName + index.ToString()},";

#endregion 参数化

columnParm = "a.id = '" + properties.First(item => item.Name.ToUpper() == "ID").GetValue(model) + "'";
}

parms.Add(new MySqlParameter(prop.Name + index.ToString(), propValue));
parms.Add(new MySqlParameter(columnName + index.ToString(), propValue));
}
}
}
return new Tuple<string, string, List<MySqlParameter>, string>(columnName, columnParm, parms, tableName);
return new Tuple<string, string, List<MySqlParameter>, string>(columnNames, columnParm, parms, tableName);
}
}

Expand All @@ -118,12 +125,18 @@ static MySQLUtility()
lock (_locker)
{
var properties = typeof(TModel).GetProperties();
string columnName = "", simpleColumnName = "", joinColumnName = "", mainTableName = $"`{typeof(TModel).GetCustomAttribute<DataTableAttribute>()?.TableName ?? typeof(TModel).Name.ToLower()}` a ", tableName = "";
string columnNames = "", simpleColumnName = "", joinColumnName = "", mainTableName = $"`{typeof(TModel).GetCustomAttribute<DataTableAttribute>()?.TableName ?? typeof(TModel).Name.ToLower()}` a ", tableName = "";
var hasPrimaryProp = properties.Any(x => x.IsDefined(typeof(PrimaryKeyIDAttribute)));
var includeArray = include.Split(",");
var dynamicSQLProps = properties.Where(x => x.IsDefined(typeof(DynamicSQLFieldAttribute)));
foreach (var prop in properties)
{
var columnName = prop.Name;
if (prop.IsDefined(typeof(DataFieldAttribute)))
{
var dataFieldAttribute = prop.GetCustomAttribute<DataFieldAttribute>();
columnName = dataFieldAttribute.ColumnName;
}
if (prop.IsDefined(typeof(DataFieldIgnoreAttribute))) continue;
if (prop.IsDefined(typeof(DynamicSQLFieldAttribute))) continue;
if (prop.IsDefined(typeof(ForeignAttribute)))
Expand Down Expand Up @@ -217,19 +230,19 @@ static MySQLUtility()
{
if (hasPrimaryProp && prop.Name.ToUpper() == "ID")
continue;
columnName += "a." + prop.Name + ",";
mapList.Add(new TableMap { Alias = "a", TableName = typeof(TModel).Name, ColumnName = prop.Name });
columnNames += "a." + columnName + ",";
mapList.Add(new TableMap { Alias = "a", TableName = typeof(TModel).Name, ColumnName = columnName });
}
}
var listDynamicSQLField = new List<string>();
foreach (var item in dynamicSQLProps)
{
var sql = item.GetCustomAttribute<DynamicSQLFieldAttribute>().SQLString;
listDynamicSQLField.Add(sql);
columnName += sql + ",";
columnNames += sql + ",";
}
columnName += joinColumnName;
return new SqlParamModel(mainTableName, tableName.ToLower(), columnName, simpleColumnName, joinColumnName, listDynamicSQLField);
columnNames += joinColumnName;
return new SqlParamModel(mainTableName, tableName.ToLower(), columnNames, simpleColumnName, joinColumnName, listDynamicSQLField);
}
}

Expand All @@ -241,9 +254,15 @@ public static void GetMap(List<TableMap> list, Type model, int i, TableType type
var props = model.GetProperties();
foreach (var prop in props)
{
var columnName = prop.Name;
if (prop.IsDefined(typeof(DataFieldAttribute)))
{
var dataFieldAttribute = prop.GetCustomAttribute<DataFieldAttribute>();
columnName = dataFieldAttribute.ColumnName;
}
if (prop.IsDefined(typeof(DataFieldIgnoreAttribute))) continue;
if (!prop.IsDefined(typeof(DetailListAttribute)) && !prop.IsDefined(typeof(ForeignAttribute)))
list.Add(new TableMap { Alias = "a" + i.ToString(), TableName = model.Name, ColumnName = prop.Name, Type = type, ForeignPropName = foreignPropName });
list.Add(new TableMap { Alias = "a" + i.ToString(), TableName = model.Name, ColumnName = columnName, Type = type, ForeignPropName = foreignPropName });
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions H.Framework.WPF.UI.Test/DiqiuModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -956,4 +957,21 @@ public void MapFrom(Notification source)
public class NotificationDAL : BaseDAL<Notification, NotificationMark>
{
}

public class IdMappingDAL : BaseDAL<IdMapping>
{
}

public class IdMappingDTO : IFoundationViewModel
{
public string ID { get; set; }
}

public class IdMapping : IFoundationModel
{
public string ID { get; set; }

[DataField("created_at")]
public string CreatedAt { get; set; }
}
}
14 changes: 8 additions & 6 deletions H.Framework.WPF.UI.Test/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public MainWindow()

private async void tt()
{
FoundationDAL.ConnectedString = "Server=10.68.99.108;Database=diqiu_crm;User ID=root;Password=Dasong@;Port=3306;TreatTinyAsBoolean=false;SslMode=none;Allow User Variables=True;charset=utf8mb4;Pooling=true;Max Pool Size=200;";
FoundationDAL.ConnectedString = "Server=rm-m5e5e52v0behudu9jko.mysql.rds.aliyuncs.com;Database=go-poseidon;User ID=posenet_dev;Password=OptBpQEt1sq$rGoV;Port=33306;TreatTinyAsBoolean=false;SslMode=none;Allow User Variables=True;charset=utf8;Pooling=true;Max Pool Size=200;";
//var query = new WhereQueryable<UserDTO, Department, Role>((x, y, z) => true);

//var asd = await new LiveRoomDstributionBLL().GetLiveRoomDstributionsAsync(new LiveRoomDstributionReq { CustomerID = "12" });
Expand All @@ -43,13 +43,15 @@ private async void tt()
//new UserBLL().GetAsync();
//new CallRecordBLL().GetAsync();
//var a = "YHZxySCyG5iI0bnRNnlURwxVsqar3rdN07B8kNcEh7/Snfu5j3V44fWMJa/YcNmZ".AnalyseToken();
//var id = (await new IdMappingDAL().AddAsync(new List<IdMapping> { new IdMapping { CreatedAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") } }));
var a = new IdMappingDAL().GetListAsync(x => true, "", null);
var t = "【超越自己】13475011820".ValidateMobile(out List<string> aa) ? "【超越自己】13475011820".Replace(aa[0], "") : "【超越自己】13475011820";
string n = null;
var nickname = n.ValidateMobile(out var matchs) ? n.Replace(matchs[0], "") : n;
var c = ToPrivacy("15229310723", "shanwaiqingshanlouwailou");
//string n = null;
//var nickname = n.ValidateMobile(out var matchs) ? n.Replace(matchs[0], "") : n;
//var c = ToPrivacy("15229310723", "shanwaiqingshanlouwailou");
//var a = ToCipher("15229310723", "vGNsTj$Gfml%4YT0trifYQK6y%dJ#UIG", "SrpPMQ8lUy^CSWSF");
var a = ToCipher("15229310723", "26HQv^fI8mNWRkw8P*0P0ht2tKSk5ASf", "aPWPxeK&Z4D!#!jH");
var cc = CipherDecrypt("RDltZ4lpXJt97nNwBSjIjA==", "vGNsTj$Gfml%4YT0trifYQK6y%dJ#UIG", "SrpPMQ8lUy^CSWSF");
//var a = ToCipher("15229310723", "26HQv^fI8mNWRkw8P*0P0ht2tKSk5ASf", "aPWPxeK&Z4D!#!jH");
//var cc = CipherDecrypt("RDltZ4lpXJt97nNwBSjIjA==", "vGNsTj$Gfml%4YT0trifYQK6y%dJ#UIG", "SrpPMQ8lUy^CSWSF");
//var b = ToCipher("openpilot is an open source driver assistance system. openpilot performs the functions of Automated Lane Centering and Adaptive Cruise Control for over 150 supported car makes and models.", "26HQv^fI8mNWRkw8P*0P0ht2tKSk5PSf", "aPWPxeK&Z4D!#!gH");
}

Expand Down

0 comments on commit d2a41ab

Please sign in to comment.