Skip to content

Commit b9a0251

Browse files
2881028810
authored andcommitted
- 修复 GroupBy(..).Count() 开启参数化无效的 bug #390; UseGenerateCommandParameterWithLambda
1 parent 072a8b7 commit b9a0251

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using FreeSql.DataAnnotations;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Text;
6+
using System.Threading;
7+
using Xunit;
8+
9+
namespace FreeSql.Tests.Issues
10+
{
11+
public class _390
12+
{
13+
[Fact]
14+
public void SelectTest()
15+
{
16+
IFreeSql db = new FreeSql.FreeSqlBuilder()
17+
.UseConnectionString(FreeSql.DataType.Oracle, "user id=1user;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=1")
18+
.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
19+
.UseGenerateCommandParameterWithLambda(true)
20+
.UseAutoSyncStructure(true)
21+
.UseMonitorCommand(cmd => Trace.WriteLine("\r\n线程" + Thread.CurrentThread.ManagedThreadId + ": " + cmd.CommandText))
22+
.Build();
23+
24+
var startTime = DateTime.Now;
25+
var endTime = DateTime.Now;
26+
27+
var cou = db.Select<V_HospitalReport>()
28+
.Where(a => a.ScheduledDttm.Date >= startTime.Date && a.ScheduledDttm.Date <= (endTime.AddDays(1)).Date)
29+
.GroupBy(a =>
30+
new
31+
{
32+
a.HospitalName,
33+
a.Dep,
34+
a.Instrna,
35+
a.ConfirmDoctorName,
36+
a.ScheduledDttm.Date
37+
})
38+
.Count();
39+
}
40+
41+
[Table(Name = "V_HospitalReport")]
42+
public class V_HospitalReport
43+
{
44+
[Column(Name = "hospital_name")]
45+
public string HospitalName { get; set; }
46+
47+
[Column(Name = "dep")]
48+
public string Dep { get; set; }
49+
50+
[Column(Name = "instrna")]
51+
public string Instrna { get; set; }
52+
53+
[Column(Name = "confirm_doctor_name")]
54+
public string ConfirmDoctorName { get; set; }
55+
56+
[Column(Name = "Scheduled_Dttm")]
57+
public DateTime ScheduledDttm { get; set; }
58+
}
59+
}
60+
}

FreeSql/Internal/CommonProvider/SelectProvider/SelectGroupingProvider.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections;
44
using System.Collections.Generic;
5+
using System.Data;
56
using System.Linq;
67
using System.Linq.Expressions;
78
using System.Reflection;
@@ -13,13 +14,13 @@ namespace FreeSql.Internal.CommonProvider
1314
public class SelectGroupingProvider
1415
{
1516
public IFreeSql _orm;
16-
public object _select;
17+
public Select0Provider _select;
1718
public ReadAnonymousTypeInfo _map;
1819
public string _field;
1920
public CommonExpression _comonExp;
2021
public List<SelectTableInfo> _tables;
2122

22-
public SelectGroupingProvider(IFreeSql orm, object select, ReadAnonymousTypeInfo map, string field, CommonExpression comonExp, List<SelectTableInfo> tables)
23+
public SelectGroupingProvider(IFreeSql orm, Select0Provider select, ReadAnonymousTypeInfo map, string field, CommonExpression comonExp, List<SelectTableInfo> tables)
2324
{
2425
_orm = orm;
2526
_select = select;
@@ -139,7 +140,7 @@ public string InternalToSql(Expression select, FieldAliasOptions fieldAlias = Fi
139140

140141
public class SelectGroupingProvider<TKey, TValue> : SelectGroupingProvider, ISelectGrouping<TKey, TValue>
141142
{
142-
public SelectGroupingProvider(IFreeSql orm, object select, ReadAnonymousTypeInfo map, string field, CommonExpression comonExp, List<SelectTableInfo> tables)
143+
public SelectGroupingProvider(IFreeSql orm, Select0Provider select, ReadAnonymousTypeInfo map, string field, CommonExpression comonExp, List<SelectTableInfo> tables)
143144
:base(orm, select, map, field, comonExp, tables) { }
144145

145146
public string ToSql<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select, FieldAliasOptions fieldAlias = FieldAliasOptions.AsIndex) => InternalToSql(select, fieldAlias);
@@ -154,26 +155,24 @@ public string ToSql(string field)
154155

155156
public ISelectGrouping<TKey, TValue> Skip(int offset)
156157
{
157-
var method = _select.GetType().GetMethod("Skip", new[] { typeof(int) });
158-
method.Invoke(_select, new object[] { offset });
158+
_select._skip = offset;
159159
return this;
160160
}
161161
public ISelectGrouping<TKey, TValue> Offset(int offset) => this.Skip(offset);
162162
public ISelectGrouping<TKey, TValue> Limit(int limit)
163163
{
164-
var method = _select.GetType().GetMethod("Limit", new[] { typeof(int) });
165-
method.Invoke(_select, new object[] { limit });
164+
_select._limit = limit;
166165
return this;
167166
}
168167
public ISelectGrouping<TKey, TValue> Take(int limit) => this.Limit(limit);
169168
public ISelectGrouping<TKey, TValue> Page(int pageNumber, int pageSize)
170169
{
171-
var method = _select.GetType().GetMethod("Page", new[] { typeof(int), typeof(int) });
172-
method.Invoke(_select, new object[] { pageNumber, pageSize });
170+
_select._skip = Math.Max(0, pageNumber - 1) * pageSize;
171+
_select._limit = pageSize;
173172
return this;
174173
}
175174

176-
public long Count() => long.TryParse(string.Concat(_orm.Ado.ExecuteScalar($"select count(1) from ({this.ToSql($"1{_comonExp._common.FieldAsAlias("as1")}")}) fta")), out var trylng) ? trylng : default(long);
175+
public long Count() => long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(CommandType.Text, $"select count(1) from ({this.ToSql($"1{_comonExp._common.FieldAsAlias("as1")}")}) fta", _select._params.ToArray())), out var trylng) ? trylng : default(long);
177176
public ISelectGrouping<TKey, TValue> Count(out long count)
178177
{
179178
count = this.Count();
@@ -202,7 +201,7 @@ public ISelectGrouping<TKey, TValue> OrderByDescending<TMember>(Expression<Func<
202201

203202
#if net40
204203
#else
205-
async public Task<long> CountAsync() => long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync($"select count(1) from ({this.ToSql($"1{_comonExp._common.FieldAsAlias("as1")}")}) fta")), out var trylng) ? trylng : default(long);
204+
async public Task<long> CountAsync() => long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync(CommandType.Text, $"select count(1) from ({this.ToSql($"1{_comonExp._common.FieldAsAlias("as1")}")}) fta", _select._params.ToArray())), out var trylng) ? trylng : default(long);
206205

207206
public Task<List<TReturn>> ToListAsync<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select) => InternalToList(select, typeof(TReturn), true) as Task<List<TReturn>>;
208207
async public Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TElement>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TElement>> elementSelector)

0 commit comments

Comments
 (0)