Skip to content

Commit e75f35e

Browse files
authored
'修复当返回类型为枚举集合时无法获取到TypeHandler' (#241)
1 parent a836e04 commit e75f35e

File tree

7 files changed

+64
-23
lines changed

7 files changed

+64
-23
lines changed

src/SmartSql.Test.Unit/DB/init-mysql-db.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ create table T_AllPrimitive
1616
String varchar(100) not null,
1717
Guid char(36) not null,
1818
TimeSpan time not null,
19-
NumericalEnum tinyint(1) not null,
19+
NumericalEnum smallint not null,
2020
NullableBoolean tinyint(1) null,
2121
NullableChar char null,
2222
NullableInt16 mediumint null,
@@ -27,7 +27,7 @@ create table T_AllPrimitive
2727
NullableDateTime datetime null,
2828
NullableGuid char(36) null,
2929
NullableTimeSpan time null,
30-
NullableNumericalEnum tinyint(1) null,
30+
NullableNumericalEnum smallint null,
3131
NullableString varchar(100) null
3232
) engine = InnoDb;
3333

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using SmartSql.Test.Entities;
2+
using System.Linq;
43
using SmartSql.Test.Repositories;
54
using Xunit;
6-
using Microsoft.Extensions.Logging;
7-
using SmartSql.DyRepository;
85

96
namespace SmartSql.Test.Unit.DyRepository
107
{
118
[Collection("GlobalSmartSql")]
12-
public class AllPrimitiveRepositoryTest
9+
public class AllPrimitiveRepositoryTest
1310
{
14-
private IAllPrimitiveRepository _repository;
11+
private readonly IAllPrimitiveRepository _repository;
12+
private ISqlMapper _mapper;
1513
public AllPrimitiveRepositoryTest(SmartSqlFixture smartSqlFixture)
1614
{
1715
_repository = smartSqlFixture.AllPrimitiveRepository;
16+
_mapper = smartSqlFixture.SqlMapper;
1817
}
1918

2019
[Fact]
@@ -24,14 +23,33 @@ public void GetByPage_ValueTuple()
2423

2524
Assert.NotNull(result);
2625
}
27-
26+
2827
[Fact]
2928
public void QueryDictionary()
3029
{
3130
var result = _repository.QueryDictionary(10);
3231

3332
Assert.NotNull(result);
3433
}
35-
34+
35+
[Theory]
36+
[InlineData(1, NumericalEnum11.One)]
37+
[InlineData(2, NumericalEnum11.Two)]
38+
public void GetNumericalEnums(int id, NumericalEnum11 numericalEnum)
39+
{
40+
var list = _mapper.Query<NumericalEnum11?>(new RequestContext
41+
{
42+
RealSql = "SELECT NumericalEnum FROM T_AllPrimitive WHERE Id = ?id",
43+
Request = new { id }
44+
});
45+
46+
Assert.NotNull(list);
47+
Assert.True(list.All(t => t == numericalEnum));
48+
49+
var result = _repository.GetNumericalEnums(id);
50+
51+
Assert.NotNull(result);
52+
Assert.True(result.All(t => t == numericalEnum));
53+
}
3654
}
3755
}

src/SmartSql.Test.Unit/SmartSqlFixture.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,13 @@ public SmartSqlFixture()
6262

6363
protected void InitTestData()
6464
{
65+
AllPrimitiveRepository.Truncate();
6566
for (int i = 0; i < 10; i++)
6667
{
67-
AllPrimitiveRepository.Insert(new AllPrimitive());
68+
AllPrimitiveRepository.Insert(new AllPrimitive()
69+
{
70+
NumericalEnum = i % 2 == 0 ? NumericalEnum.One : NumericalEnum.Two
71+
});
6872
}
6973
}
7074

src/SmartSql.Test.Unit/init-db.sql

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use SmartSqlTestDB;
44

55
create table T_AllPrimitive
66
(
7-
Id bigint auto_increment
8-
primary key,
7+
Id bigint auto_increment primary key,
98
Boolean tinyint(1) not null,
109
`Char` char not null,
1110
Int16 mediumint not null,
@@ -17,7 +16,7 @@ create table T_AllPrimitive
1716
String varchar(100) not null,
1817
Guid char(36) not null,
1918
TimeSpan time not null,
20-
NumericalEnum tinyint(1) not null,
19+
NumericalEnum smallint not null,
2120
NullableBoolean tinyint(1) null,
2221
NullableChar char null,
2322
NullableInt16 mediumint null,
@@ -28,10 +27,9 @@ create table T_AllPrimitive
2827
NullableDateTime datetime null,
2928
NullableGuid char(36) null,
3029
NullableTimeSpan time null,
31-
NullableNumericalEnum tinyint(1) null,
30+
NullableNumericalEnum smallint null,
3231
NullableString varchar(100) null
33-
) engine = InnoDb
34-
;
32+
) engine = InnoDb;
3533

3634
create table t_column_annotation_entity
3735
(
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
42

53
namespace SmartSql.Test.Entities
64
{
@@ -9,4 +7,10 @@ public enum NumericalEnum : Byte
97
One = 1,
108
Two = 2
119
}
10+
11+
public enum NumericalEnum11 : Byte
12+
{
13+
One = 1,
14+
Two = 2
15+
}
1216
}

src/SmartSql.Test/Repositories/IAllPrimitiveRepository.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public interface IAllPrimitiveRepository
2626
long InsertByAnnotationAOPTransaction(AllPrimitive entity);
2727

2828
[Statement(Id = "QueryDictionary", Sql = "SELECT T.* From T_AllPrimitive T limit ?Taken")]
29-
IList<IDictionary<String, Object>> QueryDictionary([Param("Taken")] int taken);
29+
IList<IDictionary<string, object>> QueryDictionary([Param("Taken")] int taken);
30+
31+
[Statement(Sql = "SELECT NumericalEnum FROM T_AllPrimitive WHERE Id = ?id")]
32+
List<NumericalEnum11> GetNumericalEnums(int id);
33+
34+
[Statement(Sql = "truncate table T_AllPrimitive")]
35+
void Truncate();
3036
}
3137
}

src/SmartSql/Deserializer/ValueTypeDeserializer.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
4+
using SmartSql.Exceptions;
45
using SmartSql.Reflection.TypeConstants;
56
using SmartSql.TypeHandlers;
67

@@ -17,7 +18,7 @@ public bool CanDeserialize(ExecutionContext executionContext, Type resultType, b
1718
public TResult ToSingle<TResult>(ExecutionContext executionContext)
1819
{
1920
var dataReader = executionContext.DataReaderWrapper;
20-
if (!dataReader.HasRows) return default(TResult);
21+
if (!dataReader.HasRows) return default;
2122
dataReader.Read();
2223
return TypeHandlerCache<TResult, AnyFieldType>.Handler.GetValue(dataReader, VALUE_ORDINAL, executionContext.Result.ResultType); ;
2324
}
@@ -28,6 +29,16 @@ public IList<TResult> ToList<TResult>(ExecutionContext executionContext)
2829
if (!dataReader.HasRows) return list;
2930

3031
var typeHandler = TypeHandlerCache<TResult, AnyFieldType>.Handler;
32+
33+
var resultType = typeof(TResult);
34+
if (typeHandler == null)
35+
{
36+
typeHandler =
37+
(ITypeHandler<TResult, AnyFieldType>)executionContext.SmartSqlConfig.TypeHandlerFactory
38+
.GetTypeHandler(resultType) ??
39+
throw new SmartSqlException($"Not Find TypeHandler.Type:{resultType.FullName}.");
40+
}
41+
3142
while (dataReader.Read())
3243
{
3344
var val = typeHandler.GetValue(dataReader, VALUE_ORDINAL, executionContext.Result.ResultType);
@@ -39,7 +50,7 @@ public IList<TResult> ToList<TResult>(ExecutionContext executionContext)
3950
public async Task<TResult> ToSingleAsync<TResult>(ExecutionContext executionContext)
4051
{
4152
var dataReader = executionContext.DataReaderWrapper;
42-
if (!dataReader.HasRows) return default(TResult);
53+
if (!dataReader.HasRows) return default;
4354
await dataReader.ReadAsync();
4455
return TypeHandlerCache<TResult, AnyFieldType>.Handler.GetValue(dataReader, VALUE_ORDINAL, executionContext.Result.ResultType);
4556
}

0 commit comments

Comments
 (0)