Skip to content

Commit 5bab422

Browse files
committed
add support automatically assigns values ​​to entities using the IdGenerator
1 parent e060c7d commit 5bab422

File tree

8 files changed

+145
-29
lines changed

8 files changed

+145
-29
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>1</VersionMinor>
5-
<VersionPatch>32</VersionPatch>
5+
<VersionPatch>33</VersionPatch>
66
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
77
</PropertyGroup>
88
</Project>

doc/Schema/SmartSqlMap.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,7 @@
965965
<xs:complexType>
966966
<xs:attribute name="Id" type="xs:string" use="required"/>
967967
<xs:attribute name="Name" type="xs:string" use="optional"/>
968+
<xs:attribute name="Assign" type="xs:boolean" use="optional"/>
968969
</xs:complexType>
969970
</xs:element>
970971
<xs:element name="Statement">

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public DbSessionTest(SmartSqlFixture smartSqlFixture)
1818
{
1919
SqlMapper = smartSqlFixture.SqlMapper;
2020
}
21+
2122
#region Insert_From_RealSql
23+
2224
private const string INSERT_SQL = @"INSERT INTO T_AllPrimitive
2325
(Boolean
2426
,[Char]
@@ -76,6 +78,7 @@ public DbSessionTest(SmartSqlFixture smartSqlFixture)
7678
,@NullableNumericalEnum
7779
,@NullableString);
7880
Select SCOPE_IDENTITY();";
81+
7982
[Fact]
8083
public void Insert_From_RealSql()
8184
{
@@ -89,6 +92,7 @@ public void Insert_From_RealSql()
8992
}
9093
});
9194
}
95+
9296
#endregion
9397

9498
[Fact]
@@ -105,6 +109,7 @@ public void Insert()
105109
}
106110
});
107111
}
112+
108113
[Fact]
109114
public void InsertByRequestTransaction()
110115
{
@@ -120,6 +125,7 @@ public void InsertByRequestTransaction()
120125
}
121126
});
122127
}
128+
123129
[Fact]
124130
public void InsertByStatementTransaction()
125131
{
@@ -134,21 +140,40 @@ public void InsertByStatementTransaction()
134140
}
135141
});
136142
}
143+
137144
[Fact]
138145
public void InsertByIdGen()
139146
{
147+
var entity = new AllPrimitive
148+
{
149+
DateTime = DateTime.Now,
150+
String = "SmartSql",
151+
};
140152
var id = SqlMapper.ExecuteScalar<long>(new RequestContext
141153
{
142154
Scope = nameof(AllPrimitive),
143155
SqlId = "InsertByIdGen",
144-
Request = new AllPrimitive
145-
{
146-
DateTime = DateTime.Now,
147-
String = "SmartSql",
148-
}
156+
Request = entity
149157
});
150158
}
151159

160+
[Fact]
161+
public void InsertByIdGenAssignId()
162+
{
163+
var entity = new AllPrimitive
164+
{
165+
DateTime = DateTime.Now,
166+
String = "SmartSql",
167+
};
168+
var affected = SqlMapper.Execute(new RequestContext
169+
{
170+
Scope = nameof(AllPrimitive),
171+
SqlId = "InsertByIdGenAssignId",
172+
Request = entity
173+
});
174+
Assert.True(entity.Int64 > 0);
175+
}
176+
152177
[Fact]
153178
public async Task QueryAsync()
154179
{
@@ -207,6 +232,7 @@ public void Update()
207232
}
208233
});
209234
}
235+
210236
//[Fact]
211237
public void Delete()
212238
{
@@ -221,6 +247,7 @@ public void Delete()
221247
}
222248
});
223249
}
250+
224251
[Fact]
225252
public void DeleteCheckIncludeRequired()
226253
{
@@ -238,6 +265,7 @@ public void DeleteCheckIncludeRequired()
238265
Assert.True(true);
239266
}
240267
}
268+
241269
[Fact]
242270
public void DeleteCheckIsNotEmptyRequired()
243271
{
@@ -257,8 +285,6 @@ public void DeleteCheckIsNotEmptyRequired()
257285
}
258286

259287

260-
261-
262288
//Create PROCEDURE[dbo].[SP_QueryUser]
263289
//@Total int = 0 Out
264290
// AS
@@ -287,6 +313,5 @@ public void SP()
287313
var list = SqlMapper.Query<User>(context);
288314
dbParameterCollection.TryGetParameterValue("Total", out int total);
289315
}
290-
291316
}
292-
}
317+
}

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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@
270270
;Select Scope_Identity();
271271
</Statement>
272272
<Statement Id="InsertByIdGen">
273-
<IdGenerator Id="Int64"/>
273+
<IdGenerator Id="Int64" Assign="false"/>
274274
INSERT INTO T_AllPrimitive
275275
(
276276
Boolean,
@@ -333,6 +333,69 @@
333333
)
334334
;Select @Int64;
335335
</Statement>
336+
<Statement Id="InsertByIdGenAssignId">
337+
<IdGenerator Id="Int64"/>
338+
INSERT INTO T_AllPrimitive
339+
(
340+
Boolean,
341+
Char,
342+
Byte,
343+
Int16,
344+
Int32,
345+
Int64,
346+
Single,
347+
Decimal,
348+
DateTime,
349+
String,
350+
Guid,
351+
TimeSpan,
352+
NumericalEnum,
353+
NullableBoolean,
354+
NullableChar,
355+
NullableByte,
356+
NullableInt16,
357+
NullableInt32,
358+
NullableInt64,
359+
NullableSingle,
360+
NullableDouble,
361+
NullableDecimal,
362+
NullableDateTime,
363+
NullableGuid,
364+
NullableTimeSpan,
365+
NullableNumericalEnum,
366+
NullableString
367+
)
368+
VALUES
369+
(
370+
@Boolean,
371+
@Char,
372+
@Byte,
373+
@Int16,
374+
@Int32,
375+
@Int64,
376+
@Single,
377+
@Decimal,
378+
@DateTime,
379+
@String,
380+
@Guid,
381+
@TimeSpan,
382+
@NumericalEnum,
383+
@NullableBoolean,
384+
@NullableChar,
385+
@NullableByte,
386+
@NullableInt16,
387+
@NullableInt32,
388+
@NullableInt64,
389+
@NullableSingle,
390+
@NullableDouble,
391+
@NullableDecimal,
392+
@NullableDateTime,
393+
@NullableGuid,
394+
@NullableTimeSpan,
395+
@NullableNumericalEnum,
396+
@NullableString
397+
);
398+
</Statement>
336399
<!--删除-->
337400
<Statement Id="Delete">
338401
Delete From T_AllPrimitive
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using SmartSql.IdGenerator;
22
using System;
33
using System.Collections.Generic;
4+
using System.Linq.Expressions;
5+
using System.Reflection.Emit;
46
using System.Text;
7+
using SmartSql.Reflection.PropertyAccessor;
58

69
namespace SmartSql.Configuration.Tags
710
{
@@ -11,20 +14,22 @@ public class IdGenerator : ITag
1114
public string Id { get; set; }
1215
public Statement Statement { get; set; }
1316
public ITag Parent { get; set; }
17+
public bool Assign { get; set; }
1418

1519
public void BuildSql(AbstractRequestContext context)
1620
{
1721
var nextId = IdGen.NextId();
18-
if (!context.Parameters.TryAdd(Id, nextId))
19-
{
20-
context.Parameters.TryGetValue(Id, out Data.SqlParameter sqlParameter);
21-
sqlParameter.Value = nextId;
22-
}
22+
if (context.Parameters.TryAdd(Id, nextId)) return;
23+
context.Parameters.TryGetValue(Id, out Data.SqlParameter sqlParameter);
24+
sqlParameter.Value = nextId;
25+
if (!Assign) return;
26+
var sourceRequest = context.GetRequest();
27+
EmitSetAccessorFactory.Instance.Create(sourceRequest.GetType(), Id)(sourceRequest, nextId);
2328
}
2429

2530
public bool IsCondition(AbstractRequestContext context)
2631
{
2732
return true;
2833
}
2934
}
30-
}
35+
}

src/SmartSql/Configuration/Tags/TagBuilders/IdGeneratorBuilder.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@ public override ITag Build(XmlNode xmlNode, Statement statement)
2121
throw new SmartSqlException($"Can not find IdGenerator.Name:{idGenName},XmlNode:{xmlNode}.");
2222
}
2323
}
24+
25+
if (!xmlNode.Attributes.TryGetValueAsBoolean(nameof(IdGenerator.Assign), out var assign))
26+
{
27+
assign = true;
28+
}
29+
2430
return new IdGenerator
2531
{
2632
Statement = statement,
2733
Id = GetXmlAttributeValue(xmlNode, nameof(IdGenerator.Id)),
28-
IdGen = idGen
34+
IdGen = idGen,
35+
Assign = assign
2936
};
3037
}
3138
}
32-
}
39+
}

src/SmartSql/RequestContext.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public abstract class AbstractRequestContext
2727
public String RealSql { get; set; }
2828

2929
public String AutoConverterName { get; set; }
30-
30+
3131
internal IAutoConverter AutoConverter { get; set; }
32-
32+
3333
/// <summary>
3434
/// SmartSqlMap.Scope
3535
/// </summary>
@@ -102,6 +102,7 @@ public CacheKey EnsureCacheKey()
102102

103103
public abstract void SetupParameters();
104104
public abstract void SetRequest(object requestObj);
105+
public abstract Object GetRequest();
105106

106107
/// <summary>
107108
/// 获取请求实体变更的版本号
@@ -127,6 +128,11 @@ public override void SetRequest(object requestObj)
127128
Request = (TRequest) requestObj;
128129
}
129130

131+
public override object GetRequest()
132+
{
133+
return Request;
134+
}
135+
130136
public override int GetPropertyVersion(string propName)
131137
{
132138
if (Request is IEntityPropertyChangedTrackProxy trackProxy)

src/SmartSql/Utils/XmlExtensions.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,57 @@ namespace System.Xml
88
{
99
public static class XmlExtensions
1010
{
11-
public static bool TryGetValueAsString(this XmlAttributeCollection xmlAttributeCollection, string attrName, out string attrVal, Properties properties = null)
11+
public static bool TryGetValueAsString(this XmlAttributeCollection xmlAttributeCollection, string attrName,
12+
out string attrVal, Properties properties = null)
1213
{
1314
var attr = xmlAttributeCollection[attrName];
1415
if (attr == null)
1516
{
1617
attrVal = default;
1718
return false;
1819
}
20+
1921
attrVal = attr.Value;
2022
if (properties != null)
2123
{
2224
attrVal = properties.GetPropertyValue(attr.Value);
2325
}
26+
2427
return true;
2528
}
26-
public static bool TryGetValueAsBoolean(this XmlAttributeCollection xmlAttributeCollection, string attrName, out bool attrVal, Properties properties = null)
29+
30+
public static bool TryGetValueAsBoolean(this XmlAttributeCollection xmlAttributeCollection, string attrName,
31+
out bool attrVal, Properties properties = null)
2732
{
2833
if (xmlAttributeCollection.TryGetValueAsString(attrName, out string attrValStr))
2934
{
3035
if (properties != null)
3136
{
3237
attrValStr = properties.GetPropertyValue(attrValStr);
3338
}
34-
Boolean.TryParse(attrValStr, out attrVal);
35-
return true;
39+
40+
return Boolean.TryParse(attrValStr, out attrVal);
3641
}
42+
3743
attrVal = default;
3844
return false;
3945
}
40-
public static bool TryGetValueAsInt32(this XmlAttributeCollection xmlAttributeCollection, string attrName, out int attrVal, Properties properties = null)
46+
47+
public static bool TryGetValueAsInt32(this XmlAttributeCollection xmlAttributeCollection, string attrName,
48+
out int attrVal, Properties properties = null)
4149
{
4250
if (xmlAttributeCollection.TryGetValueAsString(attrName, out string attrValStr))
4351
{
4452
if (properties != null)
4553
{
4654
attrValStr = properties.GetPropertyValue(attrValStr);
4755
}
48-
Int32.TryParse(attrValStr, out attrVal);
49-
return true;
56+
57+
return Int32.TryParse(attrValStr, out attrVal);
5058
}
59+
5160
attrVal = default;
5261
return false;
5362
}
5463
}
55-
}
64+
}

0 commit comments

Comments
 (0)