- 修复 ToList表达式查询赋值string.Empty后产生错误的SQL

This commit is contained in:
28810 2019-07-25 10:11:50 +08:00
parent 96c7ef3845
commit 762bd0df2b
2 changed files with 44 additions and 8 deletions

View File

@ -13,6 +13,19 @@ using System.Collections;
namespace FreeSql.Tests
{
public static class SqlFunc
{
public static T TryTo<T>(this string that)
{
return default(T);
}
public static string FormatDateTime()
{
return "";
}
}
public class UnitTest1
{
@ -287,17 +300,32 @@ namespace FreeSql.Tests
public TaskBuild Parent { get; set; }
}
public class SqlFunc
{
public static string FormatDateTime()
{
return "";
}
}
void parseExp(object sender, Aop.ParseExpressionEventArgs e)
{
if (e.Expression.NodeType == ExpressionType.Call)
{
var callExp = e.Expression as MethodCallExpression;
if (callExp.Object == null && callExp.Arguments.Any() && callExp.Arguments[0].Type == typeof(string))
{
if (callExp.Method.Name == "TryTo")
{
e.Result = Expression.Lambda(
typeof(Func<>).MakeGenericType(callExp.Method.GetGenericArguments().FirstOrDefault()),
e.Expression).Compile().DynamicInvoke()?.ToString();
return;
}
}
}
}
[Fact]
public void Test1()
{
g.sqlite.Aop.ParseExpression += parseExp;
var sqddddl = g.sqlite.Select<TaskBuild>().ToSql(t => t.OptionsEntity04 == "1".TryTo<int>());
var sqksdkfjl = g.sqlite.Select<TaskBuild>()
.LeftJoin(a => a.Templates.Id2 == a.TemplatesId)
.LeftJoin(a => a.Parent.Id == a.Id)
@ -809,6 +837,8 @@ namespace FreeSql.Tests
{
a.Key.tt2,
cou1 = a.Count(),
empty = "",
nil = (string)null,
arg1 = a.Avg(a.Key.mod4),
ccc2 = a.Key.tt2 ?? "now()",
//ccc = Convert.ToDateTime("now()"), partby = Convert.ToDecimal("sum(num) over(PARTITION BY server_id,os,rid,chn order by id desc)")

View File

@ -44,7 +44,13 @@ namespace FreeSql.Internal
// ccc = "now()",
// partby = "sum(num) over(PARTITION BY server_id,os,rid,chn order by id desc)"
//}),有缺点即 ccc partby 接受类型都是 string可配合 Convert.ToXxx 类型转换,请看下面的兼容
parent.DbField = constExp.Type.FullName == "System.String" ? (constExp.Value?.ToString() ?? "NULL") : _common.FormatSql("{0}", constExp?.Value);
if (constExp.Type.FullName == "System.String")
{
var constExpValue = constExp.Value?.ToString() ?? "NULL";
if (constExpValue == string.Empty) constExpValue = _common.FormatSql("{0}", "");
parent.DbField = constExpValue;
} else
parent.DbField = _common.FormatSql("{0}", constExp?.Value);
field.Append(", ").Append(parent.DbField);
if (index >= 0) field.Append(" as").Append(++index);
return false;