Skip to content

Commit

Permalink
- 修复 WhereDynamicFilter 在 System.Text.Json 下的问题 #371
Browse files Browse the repository at this point in the history
  • Loading branch information
28810 authored and 28810 committed Jul 13, 2020
1 parent 4447500 commit f93e0f1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 17 deletions.
68 changes: 66 additions & 2 deletions Examples/base_entity/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
using System;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Data.SQLite;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -40,8 +44,23 @@ public class Products : BaseEntity<Products, int>

static AsyncLocal<IUnitOfWork> _asyncUow = new AsyncLocal<IUnitOfWork>();

public class TestEnumCls
{
public CollationTypeEnum val { get; set; } = CollationTypeEnum.Binary;
}

static void Main(string[] args)
{
// var result2 = Newtonsoft.Json.JsonConvert.DeserializeObject<TestEnumCls>(@"
//{
// ""val"": ""Binary""
//}");
// var result1 = System.Text.Json.JsonSerializer.Deserialize<TestEnumCls>(@"
//{
// ""val"": ""Binary""
//}");


#region 初始化 IFreeSql
var fsql = new FreeSql.FreeSqlBuilder()
.UseAutoSyncStructure(true)
Expand Down Expand Up @@ -90,7 +109,7 @@ static void Main(string[] args)
new Products { title = "product-4" }.Save();
new Products { title = "product-5" }.Save();

Products.Select.WhereDynamicFilter(JsonConvert.DeserializeObject<DynamicFilterInfo>(@"
var wdy1 = JsonConvert.DeserializeObject<DynamicFilterInfo>(@"
{
""Logic"" : ""Or"",
""Filters"" :
Expand Down Expand Up @@ -125,7 +144,52 @@ static void Main(string[] args)
},
]
}
")).ToList();
");
var config = new JsonSerializerOptions()
{
PropertyNamingPolicy = null,
AllowTrailingCommas = true,
IgnoreNullValues = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
Converters = { new JsonStringEnumConverter() }
};
var wdy2 = System.Text.Json.JsonSerializer.Deserialize<DynamicFilterInfo>(@"
{
""Logic"" : 1,
""Filters"" :
[
{
""Field"" : ""title"",
""Operator"" : 8,
""Value"" : ""product-1"",
""Filters"" :
[
{
""Field"" : ""title"",
""Operator"" : 0,
""Value"" : ""product-1111""
}
]
},
{
""Field"" : ""title"",
""Operator"" : 8,
""Value"" : ""product-2""
},
{
""Field"" : ""title"",
""Operator"" : 8,
""Value"" : ""product-3""
},
{
""Field"" : ""title"",
""Operator"" : 8,
""Value"" : ""product-4""
}
]
}
", config);
Products.Select.WhereDynamicFilter(wdy2).ToList();

var items1 = Products.Select.Limit(10).OrderByDescending(a => a.CreateTime).ToList();
var items2 = fsql.Select<Products>().Limit(10).OrderByDescending(a => a.CreateTime).ToList();
Expand Down
9 changes: 0 additions & 9 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions FreeSql.Tests/FreeSql.Tests/UnitTest3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ public void Test03()

.Page(1, 10).ToSql("Id");

var sqlextMax1 = g.mysql.Select<EdiItem>()
.GroupBy(a => a.Id)
.ToSql(a => new
{
Id = a.Key, EdiId = SqlExt.Max(a.Key).Over().ToValue()
});

var sqlextGroupConcat = g.mysql.Select<Edi, EdiItem>()
.InnerJoin((a, b) => b.Id == a.Id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1086,12 +1086,12 @@ void ParseFilter(DynamicFilterLogic logic, DynamicFilterInfo fi, bool isend)

switch (fi.Operator)
{
case DynamicFilterOperator.Contains: exp = Expression.Call(exp, MethodStringContains, Expression.Constant(fi.Value)); break;
case DynamicFilterOperator.StartsWith: exp = Expression.Call(exp, MethodStringStartsWith, Expression.Constant(fi.Value)); break;
case DynamicFilterOperator.EndsWith: exp = Expression.Call(exp, MethodStringEndsWith, Expression.Constant(fi.Value)); break;
case DynamicFilterOperator.NotContains: exp = Expression.Not(Expression.Call(exp, MethodStringContains, Expression.Constant(fi.Value))); break;
case DynamicFilterOperator.NotStartsWith: exp = Expression.Not(Expression.Call(exp, MethodStringStartsWith, Expression.Constant(fi.Value))); break;
case DynamicFilterOperator.NotEndsWith: exp = Expression.Not(Expression.Call(exp, MethodStringEndsWith, Expression.Constant(fi.Value))); break;
case DynamicFilterOperator.Contains: exp = Expression.Call(exp, MethodStringContains, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fi.Value), exp.Type)); break;
case DynamicFilterOperator.StartsWith: exp = Expression.Call(exp, MethodStringStartsWith, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fi.Value), exp.Type)); break;
case DynamicFilterOperator.EndsWith: exp = Expression.Call(exp, MethodStringEndsWith, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fi.Value), exp.Type)); break;
case DynamicFilterOperator.NotContains: exp = Expression.Not(Expression.Call(exp, MethodStringContains, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fi.Value), exp.Type))); break;
case DynamicFilterOperator.NotStartsWith: exp = Expression.Not(Expression.Call(exp, MethodStringStartsWith, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fi.Value), exp.Type))); break;
case DynamicFilterOperator.NotEndsWith: exp = Expression.Not(Expression.Call(exp, MethodStringEndsWith, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fi.Value), exp.Type))); break;

case DynamicFilterOperator.Eq:
case DynamicFilterOperator.Equals:
Expand Down

0 comments on commit f93e0f1

Please sign in to comment.