Skip to content

Commit a664bc4

Browse files
2881028810
authored andcommitted
- 完善 ExpressionCall 方法;
1 parent ea7a860 commit a664bc4

File tree

7 files changed

+215
-19
lines changed

7 files changed

+215
-19
lines changed

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql.Tests/FreeSql.Tests/UnitTest2.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Threading;
1414
using System.Data.SqlClient;
1515
using kwlib;
16+
using System.Text;
1617

1718
namespace FreeSql.Tests
1819
{

FreeSql/DataAnnotations/ExpressionCallAttribute.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using System;
1+
using FreeSql.Internal;
2+
using FreeSql.Internal.Model;
3+
using System;
24
using System.Collections.Generic;
35
using System.Data.Common;
6+
using System.Linq.Expressions;
47
using System.Text;
58

69
namespace FreeSql.DataAnnotations
@@ -23,6 +26,15 @@ public class RawValueAttribute : Attribute
2326

2427
public class ExpressionCallContext
2528
{
29+
internal ExpressionCallContext()
30+
{
31+
Utility = new DefaultUtility { _context = this };
32+
}
33+
34+
public IUtility Utility { get; }
35+
36+
internal CommonExpression _commonExp;
37+
internal CommonExpression.ExpTSC _tsc;
2638
/// <summary>
2739
/// 数据库类型,可用于适配多种数据库环境
2840
/// </summary>
@@ -32,6 +44,10 @@ public class ExpressionCallContext
3244
/// 已解析的表达式中参数内容
3345
/// </summary>
3446
public Dictionary<string, string> ParsedContent { get; } = new Dictionary<string, string>();
47+
/// <summary>
48+
/// 表达式原始值
49+
/// </summary>
50+
public Dictionary<string, Expression> RawExpression { get; } = new Dictionary<string, Expression>();
3551

3652
/// <summary>
3753
/// 主对象的参数化对象,可重塑其属性
@@ -53,5 +69,31 @@ public class ExpressionCallContext
5369
/// 返回表达式函数表示的 SQL 字符串
5470
/// </summary>
5571
public string Result { get; set; }
72+
73+
public interface IUtility
74+
{
75+
/// <summary>
76+
/// 获取实体元数据
77+
/// </summary>
78+
/// <param name="entityType"></param>
79+
/// <returns></returns>
80+
TableInfo GetTableByEntity(Type entityType);
81+
82+
/// <summary>
83+
/// 解析表达式
84+
/// </summary>
85+
/// <param name="exp"></param>
86+
/// <returns></returns>
87+
string ParseExpression(Expression exp);
88+
}
89+
90+
class DefaultUtility : IUtility
91+
{
92+
internal ExpressionCallContext _context;
93+
94+
public TableInfo GetTableByEntity(Type entityType) => _context?._commonExp._common.GetTableByEntity(entityType);
95+
96+
public string ParseExpression(Expression exp) => _context?._commonExp.ExpressionLambdaToSql(exp, _context._tsc.CloneDisableDiyParse());
97+
}
5698
}
5799
}

FreeSql/FreeSql.xml

Lines changed: 150 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/Internal/CommonExpression.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,15 +602,25 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc)
602602
exp3.Method.GetCustomAttributes(typeof(ExpressionCallAttribute), true).Any()
603603
))
604604
{
605-
var ecc = new ExpressionCallContext { DataType = _ado.DataType, UserParameters = tsc.dbParams == null ? null : new List<DbParameter>(), FormatSql = obj => formatSql(obj, null, null, null) };
605+
var ecc = new ExpressionCallContext {
606+
_commonExp = this,
607+
_tsc = tsc,
608+
DataType = _ado.DataType,
609+
UserParameters = tsc.dbParams == null ? null : new List<DbParameter>(),
610+
FormatSql = obj => formatSql(obj, null, null, null)
611+
};
606612
var exp3MethodParams = exp3.Method.GetParameters();
607613
var dbParamsIndex = tsc.dbParams?.Count;
608-
ecc.ParsedContent.Add(exp3MethodParams[0].Name, exp3MethodParams[0].GetCustomAttributes(typeof(RawValueAttribute), true).Any() ? null: ExpressionLambdaToSql(exp3.Arguments[0], tsc));
614+
ecc.RawExpression.Add(exp3MethodParams[0].Name, exp3.Arguments[0]);
615+
ecc.ParsedContent.Add(exp3MethodParams[0].Name, exp3MethodParams[0].GetCustomAttributes(typeof(RawValueAttribute), true).Any() ? null : ExpressionLambdaToSql(exp3.Arguments[0], tsc));
609616
if (tsc.dbParams?.Count > dbParamsIndex) ecc.DbParameter = tsc.dbParams.Last();
610617
List<DbParameter> oldDbParams = tsc.SetDbParamsReturnOld(null);
611618
for (var a = 1; a < exp3.Arguments.Count; a++)
612619
if (exp3.Arguments[a].Type != typeof(ExpressionCallContext))
620+
{
621+
ecc.RawExpression.Add(exp3MethodParams[a].Name, exp3.Arguments[a]);
613622
ecc.ParsedContent.Add(exp3MethodParams[a].Name, exp3MethodParams[a].GetCustomAttributes(typeof(RawValueAttribute), true).Any() ? null : ExpressionLambdaToSql(exp3.Arguments[a], tsc));
623+
}
614624
tsc.SetDbParamsReturnOld(oldDbParams);
615625

616626
var exp3InvokeParams = new object[exp3.Arguments.Count];

FreeSql/Internal/CommonProvider/AdoProvider/AdoProvider.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void LoggerException(IObjectPool<DbConnection> pool, (DbCommand cmd, bool isclos
7878
cmd.Parameters.Clear();
7979
if (isThrowException)
8080
{
81-
cmd.Dispose();
81+
if (DataType == DataType.Sqlite) cmd.Dispose();
8282
throw e;
8383
}
8484
}
@@ -575,7 +575,7 @@ void ExecuteReaderMultiple(int multipleResult, DbConnection connection, DbTransa
575575
}
576576
LoggerException(pool, pc, new Exception($"连接失败,准备切换其他可用服务器"), dt, logtxt, false);
577577
pc.cmd.Parameters.Clear();
578-
pc.cmd.Dispose();
578+
if (DataType == DataType.Sqlite) pc.cmd.Dispose();
579579
ExecuteReaderMultiple(multipleResult, connection, transaction, readerHander, cmdType, cmdText, cmdParms);
580580
return;
581581
}
@@ -638,7 +638,7 @@ void ExecuteReaderMultiple(int multipleResult, DbConnection connection, DbTransa
638638
}
639639
LoggerException(pool, pc, ex, dt, logtxt);
640640
pc.cmd.Parameters.Clear();
641-
pc.cmd.Dispose();
641+
if (DataType == DataType.Sqlite) pc.cmd.Dispose();
642642
}
643643
public object[][] ExecuteArray(string cmdText, object parms = null) => ExecuteArray(null, null, CommandType.Text, cmdText, GetDbParamtersByObject(cmdText, parms));
644644
public object[][] ExecuteArray(DbTransaction transaction, string cmdText, object parms = null) => ExecuteArray(null, transaction, CommandType.Text, cmdText, GetDbParamtersByObject(cmdText, parms));
@@ -739,7 +739,7 @@ public int ExecuteNonQuery(DbConnection connection, DbTransaction transaction, C
739739
}
740740
LoggerException(this.MasterPool, pc, ex, dt, logtxt);
741741
pc.cmd.Parameters.Clear();
742-
pc.cmd.Dispose();
742+
if (DataType == DataType.Sqlite) pc.cmd.Dispose();
743743
return val;
744744
}
745745
public object ExecuteScalar(string cmdText, object parms = null) => ExecuteScalar(null, null, CommandType.Text, cmdText, GetDbParamtersByObject(cmdText, parms));
@@ -775,7 +775,7 @@ public object ExecuteScalar(DbConnection connection, DbTransaction transaction,
775775
}
776776
LoggerException(this.MasterPool, pc, ex, dt, logtxt);
777777
pc.cmd.Parameters.Clear();
778-
pc.cmd.Dispose();
778+
if (DataType == DataType.Sqlite) pc.cmd.Dispose();
779779
return val;
780780
}
781781

FreeSql/Internal/CommonProvider/AdoProvider/AdoProviderAsync.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ async Task ExecuteReaderMultipleAsync(int multipleResult, DbConnection connectio
504504
}
505505
LoggerException(pool, pc, new Exception($"连接失败,准备切换其他可用服务器"), dt, logtxt, false);
506506
pc.cmd.Parameters.Clear();
507-
pc.cmd.Dispose();
507+
if (DataType == DataType.Sqlite) pc.cmd.Dispose();
508508
await ExecuteReaderMultipleAsync(multipleResult, connection, transaction, readerHander, cmdType, cmdText, cmdParms);
509509
return;
510510
}
@@ -567,7 +567,7 @@ async Task ExecuteReaderMultipleAsync(int multipleResult, DbConnection connectio
567567
}
568568
LoggerException(pool, pc, ex, dt, logtxt);
569569
pc.cmd.Parameters.Clear();
570-
pc.cmd.Dispose();
570+
if (DataType == DataType.Sqlite) pc.cmd.Dispose();
571571
}
572572
public Task<object[][]> ExecuteArrayAsync(string cmdText, object parms = null) => ExecuteArrayAsync(null, null, CommandType.Text, cmdText, GetDbParamtersByObject(cmdText, parms));
573573
public Task<object[][]> ExecuteArrayAsync(DbTransaction transaction, string cmdText, object parms = null) => ExecuteArrayAsync(null, transaction, CommandType.Text, cmdText, GetDbParamtersByObject(cmdText, parms));
@@ -669,7 +669,7 @@ async public Task<int> ExecuteNonQueryAsync(DbConnection connection, DbTransacti
669669
}
670670
LoggerException(this.MasterPool, pc, ex, dt, logtxt);
671671
pc.cmd.Parameters.Clear();
672-
pc.cmd.Dispose();
672+
if (DataType == DataType.Sqlite) pc.cmd.Dispose();
673673
return val;
674674
}
675675
public Task<object> ExecuteScalarAsync(string cmdText, object parms = null) => ExecuteScalarAsync(null, null, CommandType.Text, cmdText, GetDbParamtersByObject(cmdText, parms));
@@ -705,7 +705,7 @@ async public Task<object> ExecuteScalarAsync(DbConnection connection, DbTransact
705705
}
706706
LoggerException(this.MasterPool, pc, ex, dt, logtxt);
707707
pc.cmd.Parameters.Clear();
708-
pc.cmd.Dispose();
708+
if (DataType == DataType.Sqlite) pc.cmd.Dispose();
709709
return val;
710710
}
711711

0 commit comments

Comments
 (0)