Skip to content

Commit

Permalink
- 优化 Lambda Min/Max/First 针对 DateTime 类型不自动处理 IsNull 逻辑,因为各数据库没有统一默认值;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Nov 2, 2024
1 parent 9d16345 commit 14c2d0b
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions FreeSql/Internal/CommonExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,10 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc)

var sql4 = fsqlType.GetMethod("ToSql", new Type[] { typeof(string) })?.Invoke(fsql, new object[] { $"{exp3.Method.Name.ToLower()}({fieldSql})" })?.ToString();
asSelectBefores.Clear();
return _common.IsNull($"({sql4.Replace(" \r\n", " \r\n ")})", formatSql(exp3.Method.ReturnType.CreateInstanceGetDefaultValue(), exp3.Method.ReturnType, null, null));
sql4 = $"({sql4.Replace(" \r\n", " \r\n ")})";
if (exp3.Method.ReturnType.NullableTypeOrThis() != typeof(DateTime))
sql4 = _common.IsNull(sql4, formatSql(exp3.Method.ReturnType.CreateInstanceGetDefaultValue(), exp3.Method.ReturnType, null, null));
return sql4;
}

var sql3 = manySubSelectAggMethod.Invoke(fsql, new object[] { exp3Args0, FieldAliasOptions.AsProperty }) as string;
Expand Down Expand Up @@ -1696,9 +1699,13 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc)
tscClone1.subSelect001.GetNestSelectSql(exp3Args0, sqlSumField, tosqlField =>
fsqlType.GetMethod("ToSql", new Type[] { typeof(string) })?.Invoke(fsql, new object[] { tosqlField })?.ToString());
if (string.IsNullOrEmpty(sqlSum) == false)
return tscClone1.subSelect001._limit <= 0 && tscClone1.subSelect001._skip <= 0 ?
_common.IsNull($"({sqlSum.Replace(" \r\n", " \r\n ")})", formatSql(exp3.Method.ReturnType.CreateInstanceGetDefaultValue(), exp3.Method.ReturnType, null, null)) :
_common.IsNull($"({sqlSum})", formatSql(exp3.Method.ReturnType.CreateInstanceGetDefaultValue(), exp3.Method.ReturnType, null, null));
{
sqlSum = tscClone1.subSelect001._limit <= 0 && tscClone1.subSelect001._skip <= 0 ?
$"({sqlSum.Replace(" \r\n", " \r\n ")})" : $"({sqlSum})";
if (exp3.Method.ReturnType.NullableTypeOrThis() != typeof(DateTime))
sqlSum = _common.IsNull(sqlSum, formatSql(exp3.Method.ReturnType.CreateInstanceGetDefaultValue(), exp3.Method.ReturnType, null, null));
return sqlSum;
}
break;
case "ToList":
case "ToOne":
Expand Down Expand Up @@ -2901,7 +2908,9 @@ void LocalInitSelectProvider()
commonExp.ReadAnonymousField(select._tables, select._tableRule, field, map, ref index, callExp.Arguments[1], null, null, null, null, null, false);
var fieldSql = field.Length > 0 ? field.Remove(0, 2).ToString() : null;

e.Result = commonExp._common.IsNull($"({select.ToSql($"{aggregateMethodName}({fieldSql})").Replace(" \r\n", " \r\n ")})", commonExp.formatSql(callExp.Method.ReturnType.CreateInstanceGetDefaultValue(), callExp.Method.ReturnType, null, null));
e.Result = $"({select.ToSql($"{aggregateMethodName}({fieldSql})").Replace(" \r\n", " \r\n ")})";
if (callExp.Method.ReturnType.NullableTypeOrThis() != typeof(DateTime))
e.Result = commonExp._common.IsNull(e.Result, commonExp.formatSql(callExp.Method.ReturnType.CreateInstanceGetDefaultValue(), callExp.Method.ReturnType, null, null));
return;
}
throw throwCallExp($"不支持 {callExp.Arguments.Count}个参数的方法");
Expand Down

0 comments on commit 14c2d0b

Please sign in to comment.