- 修复 GroupBy(..).Count() 开启参数化无效的 bug #390; UseGenerateCommandParameterWithLambda

This commit is contained in:
28810 2020-07-27 19:57:53 +08:00
parent 072a8b7cfa
commit b9a0251ef1
2 changed files with 70 additions and 11 deletions

View File

@ -0,0 +1,60 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using Xunit;
namespace FreeSql.Tests.Issues
{
public class _390
{
[Fact]
public void SelectTest()
{
IFreeSql db = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Oracle, "user id=1user;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=1")
.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
.UseGenerateCommandParameterWithLambda(true)
.UseAutoSyncStructure(true)
.UseMonitorCommand(cmd => Trace.WriteLine("\r\n线程" + Thread.CurrentThread.ManagedThreadId + ": " + cmd.CommandText))
.Build();
var startTime = DateTime.Now;
var endTime = DateTime.Now;
var cou = db.Select<V_HospitalReport>()
.Where(a => a.ScheduledDttm.Date >= startTime.Date && a.ScheduledDttm.Date <= (endTime.AddDays(1)).Date)
.GroupBy(a =>
new
{
a.HospitalName,
a.Dep,
a.Instrna,
a.ConfirmDoctorName,
a.ScheduledDttm.Date
})
.Count();
}
[Table(Name = "V_HospitalReport")]
public class V_HospitalReport
{
[Column(Name = "hospital_name")]
public string HospitalName { get; set; }
[Column(Name = "dep")]
public string Dep { get; set; }
[Column(Name = "instrna")]
public string Instrna { get; set; }
[Column(Name = "confirm_doctor_name")]
public string ConfirmDoctorName { get; set; }
[Column(Name = "Scheduled_Dttm")]
public DateTime ScheduledDttm { get; set; }
}
}
}

View File

@ -2,6 +2,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
@ -13,13 +14,13 @@ namespace FreeSql.Internal.CommonProvider
public class SelectGroupingProvider public class SelectGroupingProvider
{ {
public IFreeSql _orm; public IFreeSql _orm;
public object _select; public Select0Provider _select;
public ReadAnonymousTypeInfo _map; public ReadAnonymousTypeInfo _map;
public string _field; public string _field;
public CommonExpression _comonExp; public CommonExpression _comonExp;
public List<SelectTableInfo> _tables; public List<SelectTableInfo> _tables;
public SelectGroupingProvider(IFreeSql orm, object select, ReadAnonymousTypeInfo map, string field, CommonExpression comonExp, List<SelectTableInfo> tables) public SelectGroupingProvider(IFreeSql orm, Select0Provider select, ReadAnonymousTypeInfo map, string field, CommonExpression comonExp, List<SelectTableInfo> tables)
{ {
_orm = orm; _orm = orm;
_select = select; _select = select;
@ -139,7 +140,7 @@ namespace FreeSql.Internal.CommonProvider
public class SelectGroupingProvider<TKey, TValue> : SelectGroupingProvider, ISelectGrouping<TKey, TValue> public class SelectGroupingProvider<TKey, TValue> : SelectGroupingProvider, ISelectGrouping<TKey, TValue>
{ {
public SelectGroupingProvider(IFreeSql orm, object select, ReadAnonymousTypeInfo map, string field, CommonExpression comonExp, List<SelectTableInfo> tables) public SelectGroupingProvider(IFreeSql orm, Select0Provider select, ReadAnonymousTypeInfo map, string field, CommonExpression comonExp, List<SelectTableInfo> tables)
:base(orm, select, map, field, comonExp, tables) { } :base(orm, select, map, field, comonExp, tables) { }
public string ToSql<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select, FieldAliasOptions fieldAlias = FieldAliasOptions.AsIndex) => InternalToSql(select, fieldAlias); public string ToSql<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select, FieldAliasOptions fieldAlias = FieldAliasOptions.AsIndex) => InternalToSql(select, fieldAlias);
@ -154,26 +155,24 @@ namespace FreeSql.Internal.CommonProvider
public ISelectGrouping<TKey, TValue> Skip(int offset) public ISelectGrouping<TKey, TValue> Skip(int offset)
{ {
var method = _select.GetType().GetMethod("Skip", new[] { typeof(int) }); _select._skip = offset;
method.Invoke(_select, new object[] { offset });
return this; return this;
} }
public ISelectGrouping<TKey, TValue> Offset(int offset) => this.Skip(offset); public ISelectGrouping<TKey, TValue> Offset(int offset) => this.Skip(offset);
public ISelectGrouping<TKey, TValue> Limit(int limit) public ISelectGrouping<TKey, TValue> Limit(int limit)
{ {
var method = _select.GetType().GetMethod("Limit", new[] { typeof(int) }); _select._limit = limit;
method.Invoke(_select, new object[] { limit });
return this; return this;
} }
public ISelectGrouping<TKey, TValue> Take(int limit) => this.Limit(limit); public ISelectGrouping<TKey, TValue> Take(int limit) => this.Limit(limit);
public ISelectGrouping<TKey, TValue> Page(int pageNumber, int pageSize) public ISelectGrouping<TKey, TValue> Page(int pageNumber, int pageSize)
{ {
var method = _select.GetType().GetMethod("Page", new[] { typeof(int), typeof(int) }); _select._skip = Math.Max(0, pageNumber - 1) * pageSize;
method.Invoke(_select, new object[] { pageNumber, pageSize }); _select._limit = pageSize;
return this; return this;
} }
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); 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);
public ISelectGrouping<TKey, TValue> Count(out long count) public ISelectGrouping<TKey, TValue> Count(out long count)
{ {
count = this.Count(); count = this.Count();
@ -202,7 +201,7 @@ namespace FreeSql.Internal.CommonProvider
#if net40 #if net40
#else #else
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); 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);
public Task<List<TReturn>> ToListAsync<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select) => InternalToList(select, typeof(TReturn), true) as Task<List<TReturn>>; public Task<List<TReturn>> ToListAsync<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select) => InternalToList(select, typeof(TReturn), true) as Task<List<TReturn>>;
async public Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TElement>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TElement>> elementSelector) async public Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TElement>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TElement>> elementSelector)