Skip to content

Commit d840e36

Browse files
committed
add TransactionMiddleware
add UseTransactionAttribute
1 parent 47c2865 commit d840e36

File tree

14 files changed

+184
-6
lines changed

14 files changed

+184
-6
lines changed

build/version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<VersionMajor>4</VersionMajor>
44
<VersionMinor>0</VersionMinor>
5-
<VersionPatch>28</VersionPatch>
5+
<VersionPatch>29</VersionPatch>
66
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
77
</PropertyGroup>
88
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Text;
5+
6+
namespace SmartSql.DyRepository.Annotations
7+
{
8+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
9+
public class UseTransactionAttribute : Attribute
10+
{
11+
public IsolationLevel Level { get; set; } = IsolationLevel.Unspecified;
12+
}
13+
}

src/SmartSql.DyRepository/EmitRepositoryBuilder.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private void BuildMethod(Type interfaceType, TypeBuilder typeBuilder, MethodInfo
120120
EmitNewRequestContext(ilGen, isOnlyOneClassParam, firstParamType);
121121
EmitSetCommandType(ilGen, statementAttr);
122122
EmitSetDataSourceChoice(ilGen, statementAttr);
123-
123+
EmitSetTransaction(ilGen, methodInfo);
124124
if (String.IsNullOrEmpty(statementAttr.Sql))
125125
{
126126
EmitSetScope(ilGen, statementAttr.Scope);
@@ -425,6 +425,16 @@ private static void EmitSetDataSourceChoice(ILGenerator ilGen, StatementAttribut
425425
ilGen.Callvirt(RequestContextType.Method.SetDataSourceChoice);
426426
}
427427
}
428+
private static void EmitSetTransaction(ILGenerator ilGen, MethodInfo methodInfo)
429+
{
430+
var useTransactionAttribute = methodInfo.GetCustomAttribute<UseTransactionAttribute>();
431+
if (useTransactionAttribute != null)
432+
{
433+
ilGen.LoadLocalVar(0);
434+
ilGen.LoadInt32(useTransactionAttribute.Level.GetHashCode());
435+
ilGen.Callvirt(RequestContextType.Method.SetTransaction);
436+
}
437+
}
428438
private void EmitSetRealSql(ILGenerator ilGen, StatementAttribute statementAttr)
429439
{
430440
ilGen.LoadLocalVar(0);

src/SmartSql.Test.Unit/DbSessions/DbSessionTest.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using SmartSql.Reflection;
33
using SmartSql.Test.Entities;
44
using System;
5+
using System.Data;
56
using System.Threading.Tasks;
67
using SmartSql.Data;
78
using Xunit;
@@ -98,6 +99,35 @@ public void Insert()
9899
});
99100
}
100101
[Fact]
102+
public void InsertByRequestTransaction()
103+
{
104+
var id = DbSession.ExecuteScalar<long>(new RequestContext
105+
{
106+
Scope = nameof(AllPrimitive),
107+
SqlId = "Insert",
108+
Transaction = IsolationLevel.Unspecified,
109+
Request = new AllPrimitive
110+
{
111+
DateTime = DateTime.Now,
112+
String = "SmartSql",
113+
}
114+
});
115+
}
116+
[Fact]
117+
public void InsertByStatementTransaction()
118+
{
119+
var id = DbSession.ExecuteScalar<long>(new RequestContext
120+
{
121+
Scope = nameof(AllPrimitive),
122+
SqlId = "InsertByStatementTransaction",
123+
Request = new AllPrimitive
124+
{
125+
DateTime = DateTime.Now,
126+
String = "SmartSql",
127+
}
128+
});
129+
}
130+
[Fact]
101131
public void InsertByIdGen()
102132
{
103133
var id = DbSession.ExecuteScalar<long>(new RequestContext

src/SmartSql.Test.Unit/DyRepository/RepositoryBuilder_Test.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,19 @@ public void CreateInstance()
3333
String = "",
3434
DateTime = DateTime.Now
3535
});
36-
3736
}
37+
[Fact]
38+
public void InsertByAnnotationTransaction()
39+
{
40+
var sqlMapper = SmartSqlBuilder.GetSqlMapper();
41+
var repository = _repositoryFactory.CreateInstance(typeof(IAllPrimitiveRepository), sqlMapper) as IAllPrimitiveRepository;
42+
var id = repository.InsertByAnnotationTransaction(new Entities.AllPrimitive
43+
{
44+
String = "",
45+
DateTime = DateTime.Now
46+
});
47+
}
48+
3849

3950
[Fact]
4051
public void NoMapperRepository_GetGuidFromDb()

src/SmartSql.Test.Unit/Maps/AllPrimitive.xml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,69 @@
199199
)
200200
;Select Scope_Identity();
201201
</Statement>
202+
<Statement Id="InsertByStatementTransaction" Transaction="Unspecified">
203+
INSERT INTO T_AllPrimitive
204+
(
205+
Boolean,
206+
Char,
207+
Byte,
208+
Int16,
209+
Int32,
210+
Int64,
211+
Single,
212+
Decimal,
213+
DateTime,
214+
String,
215+
Guid,
216+
TimeSpan,
217+
NumericalEnum,
218+
NullableBoolean,
219+
NullableChar,
220+
NullableByte,
221+
NullableInt16,
222+
NullableInt32,
223+
NullableInt64,
224+
NullableSingle,
225+
NullableDouble,
226+
NullableDecimal,
227+
NullableDateTime,
228+
NullableGuid,
229+
NullableTimeSpan,
230+
NullableNumericalEnum,
231+
NullableString
232+
)
233+
VALUES
234+
(
235+
@Boolean,
236+
@Char,
237+
@Byte,
238+
@Int16,
239+
@Int32,
240+
@Int64,
241+
@Single,
242+
@Decimal,
243+
@DateTime,
244+
@String,
245+
@Guid,
246+
@TimeSpan,
247+
@NumericalEnum,
248+
@NullableBoolean,
249+
@NullableChar,
250+
@NullableByte,
251+
@NullableInt16,
252+
@NullableInt32,
253+
@NullableInt64,
254+
@NullableSingle,
255+
@NullableDouble,
256+
@NullableDecimal,
257+
@NullableDateTime,
258+
@NullableGuid,
259+
@NullableTimeSpan,
260+
@NullableNumericalEnum,
261+
@NullableString
262+
)
263+
;Select Scope_Identity();
264+
</Statement>
202265
<Statement Id="InsertByIdGen">
203266
<IdGenerator Id="Int64"/>
204267
INSERT INTO T_AllPrimitive

src/SmartSql.Test.Unit/Maps/User.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
</Statement>
4242

4343
<!--删除-->
44-
<Statement Id="Delete">
44+
<Statement Id="Delete" Transaction="Unspecified">
4545
Delete From T_User
4646
Where Id=@Id
4747
</Statement>

src/SmartSql.Test/Repositories/IAllPrimitiveRepository.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data;
34
using System.Text;
45
using SmartSql.DyRepository.Annotations;
56
using SmartSql.Test.Entities;
@@ -13,5 +14,8 @@ public interface IAllPrimitiveRepository
1314
long Insert(AllPrimitive entity);
1415

1516
(IList<AllPrimitive>, int) GetByPage_ValueTuple(int PageSize = 10, int PageIndex = 1);
17+
[UseTransaction]
18+
[Statement(Id = "Insert")]
19+
long InsertByAnnotationTransaction(AllPrimitive entity);
1620
}
1721
}

src/SmartSql/Configuration/Statement.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class Statement
1313
public StatementType StatementType { get; set; } = StatementType.Unknown;
1414
public CommandType? CommandType { get; set; }
1515
public DataSourceChoice? SourceChoice { get; set; }
16-
[Obsolete("弃用")]
1716
public IsolationLevel? Transaction { get; set; }
1817
public String ReadDb { get; set; }
1918
public String FullSqlId => $"{SqlMap.Scope}.{Id}";

src/SmartSql/Middlewares/InitializerMiddleware.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ private void InitByStatement(AbstractRequestContext requestContext, SqlMap sqlMa
6868
{
6969
requestContext.CommandType = requestContext.Statement.CommandType.Value;
7070
}
71+
requestContext.Transaction = requestContext.Transaction ?? requestContext.Statement.Transaction;
7172
requestContext.ReadDb = requestContext.Statement.ReadDb;
7273
requestContext.CacheId = requestContext.Statement.CacheId;
7374
requestContext.Cache = requestContext.Statement.Cache;

0 commit comments

Comments
 (0)