参数化增加 ExpandoObject支持

This commit is contained in:
hyzx86 2023-01-17 20:20:28 +08:00
parent a212feb750
commit 97ad7a34d8
2 changed files with 30 additions and 5 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using System.Dynamic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -45,6 +46,30 @@ namespace FreeSql.Tests.Internal
Assert.Equal("test", ps2[0].Value); Assert.Equal("test", ps2[0].Value);
Assert.Equal("p", ps2[0].ParameterName); Assert.Equal("p", ps2[0].ParameterName);
Assert.Equal(typeof(SqlParameter), ps2[0].GetType()); Assert.Equal(typeof(SqlParameter), ps2[0].GetType());
//测试匿名对象支持
dynamic expObj = new ExpandoObject();
expObj.p = "test";
Func<string, Type, object, DbParameter> constructorParamter = (name, type, value) =>
{
if (value?.Equals(DateTime.MinValue) == true) value = new DateTime(1970, 1, 1);
var ret = new SqlParameter { ParameterName = $"@{name}", Value = value };
return ret;
};
var ps3 = FreeSql.Internal.Utils.
GetDbParamtersByObject<DbParameter>("select @p",
expObj,
"@",
constructorParamter);
Assert.Single(ps3);
Assert.Equal("test", ps3[0].Value);
Assert.Equal("p", ps3[0].ParameterName);
Assert.Equal(typeof(SqlParameter), ps3[0].GetType());
} }
[Fact] [Fact]
@ -52,7 +77,7 @@ namespace FreeSql.Tests.Internal
{ {
var dict = new Dictionary<string, string>(); var dict = new Dictionary<string, string>();
string sql1 = "", sql2 = "", sql3 = ""; string sql1 = "", sql2 = "", sql3 = "";
sql2 = FreeSql.Internal.Utils.ReplaceSqlConstString(sql1 = @"UPDATE ""as_table_log_202201"" SET ""msg"" = 'msg01', ""createtime"" = '2022-01-01 13:00:11' sql2 = FreeSql.Internal.Utils.ReplaceSqlConstString(sql1 = @"UPDATE ""as_table_log_202201"" SET ""msg"" = 'msg01', ""createtime"" = '2022-01-01 13:00:11'
WHERE (""id"" = '6252a2e6-5df3-bb10-00c1-bda60c4053fe')", dict); WHERE (""id"" = '6252a2e6-5df3-bb10-00c1-bda60c4053fe')", dict);
Assert.Equal(3, dict.Count); Assert.Equal(3, dict.Count);

View File

@ -91,7 +91,7 @@ namespace FreeSql.Internal
else colattr.IsIgnore = true; else colattr.IsIgnore = true;
//Navigate 错误提示 //Navigate 错误提示
var pnvAttr = common.GetEntityNavigateAttribute(trytb.Type, p); var pnvAttr = common.GetEntityNavigateAttribute(trytb.Type, p);
if (pnvAttr != null) throw new Exception(CoreStrings.Navigation_Missing_SetProperty(trytb.Type.DisplayCsharp(),p.Name)); if (pnvAttr != null) throw new Exception(CoreStrings.Navigation_Missing_SetProperty(trytb.Type.DisplayCsharp(), p.Name));
} }
if (tp == null && colattr?.IsIgnore != true) if (tp == null && colattr?.IsIgnore != true)
{ {
@ -396,7 +396,7 @@ namespace FreeSql.Internal
trytb.VersionColumn = trytb.Columns.Values.Where(a => a.Attribute.IsVersion == true).LastOrDefault(); trytb.VersionColumn = trytb.Columns.Values.Where(a => a.Attribute.IsVersion == true).LastOrDefault();
if (trytb.VersionColumn != null) if (trytb.VersionColumn != null)
{ {
if (trytb.VersionColumn.Attribute.MapType.IsNullableType() || if (trytb.VersionColumn.Attribute.MapType.IsNullableType() ||
trytb.VersionColumn.Attribute.MapType.IsNumberType() == false && !new[] { typeof(byte[]), typeof(string) }.Contains(trytb.VersionColumn.Attribute.MapType)) trytb.VersionColumn.Attribute.MapType.IsNumberType() == false && !new[] { typeof(byte[]), typeof(string) }.Contains(trytb.VersionColumn.Attribute.MapType))
throw new Exception(CoreStrings.Properties_AsRowLock_Must_Numeric_Byte(trytb.VersionColumn.CsName)); throw new Exception(CoreStrings.Properties_AsRowLock_Must_Numeric_Byte(trytb.VersionColumn.CsName));
} }
@ -1061,7 +1061,7 @@ namespace FreeSql.Internal
trytb.AddOrUpdateTableRef(pnv.Name, nvref); trytb.AddOrUpdateTableRef(pnv.Name, nvref);
} }
} }
if (nvref.Exception == null && trytb.Primarys.Length == 1 && isArrayToMany == false) if (nvref.Exception == null && trytb.Primarys.Length == 1 && isArrayToMany == false)
{ {
if (pnvBind?.Length == 1) if (pnvBind?.Length == 1)
@ -1431,7 +1431,7 @@ namespace FreeSql.Internal
var type = obj.GetType(); var type = obj.GetType();
if (ttype.IsAssignableFrom(type)) return new[] { (T)obj }; if (ttype.IsAssignableFrom(type)) return new[] { (T)obj };
var ret = new List<T>(); var ret = new List<T>();
var dic = obj as IDictionary; var dic = obj as IDictionary<string, object>;
if (dic != null) if (dic != null)
{ {
foreach (var key in dic.Keys) foreach (var key in dic.Keys)