Skip to content

Commit d018e70

Browse files
Changes from PR #49 from paulomorgado (#70)
* Changes from PR #49 from paulomorgado * Don't need the pre-pended '@' anymore. --------- Co-authored-by: Paulo Morgado <[email protected]>
1 parent 43ba8d7 commit d018e70

File tree

7 files changed

+160
-156
lines changed

7 files changed

+160
-156
lines changed

src/CosmosDBSessionStateProviderAsync/CosmosDBSessionStateProviderAsync.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,14 @@ public class CosmosDBSessionStateProviderAsync : SessionStateStoreProviderAsyncB
4646
};
4747

4848
#region CosmosDB Stored Procedures
49-
private static readonly string CreateSessionStateItemSPID = "CreateSessionStateItem";
50-
private static readonly string GetStateItemSPID = "GetStateItem2";
51-
private static readonly string GetStateItemExclusiveSPID = "GetStateItemExclusive";
52-
private static readonly string ReleaseItemExclusiveSPID = "ReleaseItemExclusive";
53-
private static readonly string RemoveStateItemSPID = "RemoveStateItem2";
54-
private static readonly string ResetItemTimeoutSPID = "ResetItemTimeout";
55-
private static readonly string UpdateSessionStateItemSPID = "UpdateSessionStateItem";
56-
57-
// Will be used in String.Format, hence needs to escape certain char
49+
private const string CreateSessionStateItemSPID = "CreateSessionStateItem";
50+
private const string GetStateItemSPID = "GetStateItem2";
51+
private const string GetStateItemExclusiveSPID = "GetStateItemExclusive";
52+
private const string ReleaseItemExclusiveSPID = "ReleaseItemExclusive";
53+
private const string RemoveStateItemSPID = "RemoveStateItem2";
54+
private const string ResetItemTimeoutSPID = "ResetItemTimeout";
55+
private const string UpdateSessionStateItemSPID = "UpdateSessionStateItem";
56+
5857
private const string CreateSessionStateItemSP = @"
5958
function CreateSessionStateItem(sessionId, timeout, lockCookie, sessionItem, uninitialized) {
6059
var collection = getContext().getCollection();
@@ -83,7 +82,7 @@ function CreateSessionStateItem(sessionId, timeout, lockCookie, sessionItem, uni
8382
}";
8483

8584

86-
private static readonly string GetStateItemSP = @"
85+
private const string GetStateItemSP = @"
8786
function GetStateItem2(sessionId) {
8887
var collection = getContext().getCollection();
8988
var collectionLink = collection.getSelfLink();
@@ -160,7 +159,7 @@ function tryGetStateItem(continuation) {
160159
}
161160
}";
162161

163-
private static readonly string GetStateItemExclusiveSP = @"
162+
private const string GetStateItemExclusiveSP = @"
164163
function GetStateItemExclusive(sessionId) {
165164
var collection = getContext().getCollection();
166165
var collectionLink = collection.getSelfLink();
@@ -241,7 +240,7 @@ function tryGetStateItemExclusive(continuation) {
241240
}
242241
}";
243242

244-
private static readonly string ReleaseItemExclusiveSP = @"
243+
private const string ReleaseItemExclusiveSP = @"
245244
function ReleaseItemExclusive(sessionId, lockCookie) {
246245
var collection = getContext().getCollection();
247246
var collectionLink = collection.getSelfLink();
@@ -298,7 +297,7 @@ function TryReleaseItemExclusive(continuation) {
298297
}
299298
}";
300299

301-
private static readonly string RemoveStateItemSP = @"
300+
private const string RemoveStateItemSP = @"
302301
function RemoveStateItem2(sessionId, lockCookie) {
303302
var collection = getContext().getCollection();
304303
var collectionLink = collection.getSelfLink();
@@ -354,7 +353,7 @@ function TryRemoveStateItem(continuation) {
354353
}
355354
}";
356355

357-
private static readonly string ResetItemTimeoutSP = @"
356+
private const string ResetItemTimeoutSP = @"
358357
function ResetItemTimeout(sessionId) {
359358
var collection = getContext().getCollection();
360359
var collectionLink = collection.getSelfLink();
@@ -408,7 +407,7 @@ function tryResetItemTimeout(continuation) {
408407
}
409408
}";
410409

411-
private static readonly string UpdateSessionStateItemSP = @"
410+
private const string UpdateSessionStateItemSP = @"
412411
function UpdateSessionStateItem(sessionId, lockCookie, timeout, sessionItem) {
413412
var collection = getContext().getCollection();
414413
var collectionLink = collection.getSelfLink();

src/SqlSessionStateProviderAsync/SqlCommandExtension.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
namespace Microsoft.AspNet.SessionState
55
{
66
using System.Data.SqlClient;
7+
using System.Diagnostics;
78

89
static class SqlCommandExtension
910
{
10-
public static SqlParameter GetOutPutParameterValue(this SqlCommand cmd, SqlParameterName parameterName)
11+
public static SqlParameter GetOutPutParameterValue(this SqlCommand cmd, string parameterName)
1112
{
12-
return cmd.Parameters[$"@{parameterName}"];
13+
// This is an internal method that only we call. We know 'parameterName' always begins
14+
// with an '@' so we don't need to check for that case. Be aware of that expectation.
15+
Debug.Assert(parameterName != null);
16+
Debug.Assert(parameterName[0] != '@');
17+
return cmd.Parameters[parameterName];
1318
}
1419
}
1520
}

src/SqlSessionStateProviderAsync/SqlInMemoryTableSessionStateRepository.cs

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ class SqlInMemoryTableSessionStateRepository : ISqlSessionStateRepository
2727
#region Sql statement
2828
// Most of the SQL statements should just work, the following statements are different
2929
#region CreateSessionTable
30-
private static readonly string CreateSessionTableSql = $@"
30+
private const string CreateSessionTableSql = @"
3131
IF NOT EXISTS (SELECT *
3232
FROM INFORMATION_SCHEMA.TABLES
33-
WHERE TABLE_NAME = '{SqlSessionStateRepositoryUtil.TableName}')
33+
WHERE TABLE_NAME = '" + SqlSessionStateRepositoryUtil.TableName + @"')
3434
BEGIN
35-
CREATE TABLE {SqlSessionStateRepositoryUtil.TableName} (
35+
CREATE TABLE " + SqlSessionStateRepositoryUtil.TableName + @" (
3636
SessionId nvarchar(88) COLLATE Latin1_General_100_BIN2 NOT NULL,
3737
Created datetime NOT NULL DEFAULT GETUTCDATE(),
3838
Expires datetime NOT NULL,
@@ -56,7 +56,7 @@ PRIMARY KEY NONCLUSTERED HASH
5656
#endregion
5757

5858
#region GetStateItemExclusive
59-
private static readonly string GetStateItemExclusiveSql = $@"
59+
private const string GetStateItemExclusiveSql = @"
6060
DECLARE @textptr AS varbinary(max)
6161
DECLARE @length AS int
6262
DECLARE @now AS datetime
@@ -69,41 +69,41 @@ DECLARE @LockedCheck bit
6969
DECLARE @Flags int
7070
7171
SELECT @LockedCheck = Locked, @Flags = Flags
72-
FROM {SqlSessionStateRepositoryUtil.TableName}
73-
WHERE SessionID = @{SqlParameterName.SessionId}
72+
FROM " + SqlSessionStateRepositoryUtil.TableName + @"
73+
WHERE SessionID = " + SqlParameterName.SessionId + @"
7474
IF @Flags&1 <> 0
7575
BEGIN
76-
SET @actionFlags = 1
77-
UPDATE {SqlSessionStateRepositoryUtil.TableName}
78-
SET Flags = Flags & ~1 WHERE SessionID = @{SqlParameterName.SessionId}
76+
SET " + SqlParameterName.ActionFlags + @" = 1
77+
UPDATE " + SqlSessionStateRepositoryUtil.TableName + @"
78+
SET Flags = Flags & ~1 WHERE SessionID = " + SqlParameterName.SessionId + @"
7979
END
8080
ELSE
81-
SET @{SqlParameterName.ActionFlags} = 0
81+
SET " + SqlParameterName.ActionFlags + @" = 0
8282
8383
IF @LockedCheck = 1
8484
BEGIN
85-
UPDATE {SqlSessionStateRepositoryUtil.TableName}
85+
UPDATE " + SqlSessionStateRepositoryUtil.TableName + @"
8686
SET Expires = DATEADD(n, Timeout, @now),
87-
@{SqlParameterName.LockAge} = DATEDIFF(second, LockDate, @now),
88-
@{SqlParameterName.LockCookie} = LockCookie,
87+
" + SqlParameterName.LockAge + @" = DATEDIFF(second, LockDate, @now),
88+
" + SqlParameterName.LockCookie + @" = LockCookie,
8989
--@textptr = NULL,
9090
@length = NULL,
91-
@{SqlParameterName.Locked} = 1
92-
WHERE SessionId = @{SqlParameterName.SessionId}
91+
" + SqlParameterName.Locked + @" = 1
92+
WHERE SessionId = " + SqlParameterName.SessionId + @"
9393
END
9494
ELSE
9595
BEGIN
96-
UPDATE {SqlSessionStateRepositoryUtil.TableName}
96+
UPDATE " + SqlSessionStateRepositoryUtil.TableName + @"
9797
SET Expires = DATEADD(n, Timeout, @now),
9898
LockDate = @now,
9999
LockDateLocal = @nowlocal,
100-
@{SqlParameterName.LockAge} = 0,
101-
@{SqlParameterName.LockCookie} = LockCookie = LockCookie + 1,
100+
" + SqlParameterName.LockAge + @" = 0,
101+
" + SqlParameterName.LockCookie + @" = LockCookie = LockCookie + 1,
102102
@textptr = SessionItemLong,
103103
@length = 1,
104-
@{SqlParameterName.Locked} = 0,
104+
" + SqlParameterName.Locked + @" = 0,
105105
Locked = 1
106-
WHERE SessionId = @{SqlParameterName.SessionId}
106+
WHERE SessionId = " + SqlParameterName.SessionId + @"
107107
108108
IF @TextPtr IS NOT NULL
109109
SELECT @TextPtr
@@ -112,22 +112,22 @@ SELECT @TextPtr
112112
#endregion
113113

114114
#region GetStateItem
115-
private static readonly string GetStateItemSql = $@"
115+
private const string GetStateItemSql = @"
116116
DECLARE @textptr AS varbinary(max)
117117
DECLARE @length AS int
118118
DECLARE @now AS datetime
119119
SET @now = GETUTCDATE()
120120
121-
UPDATE {SqlSessionStateRepositoryUtil.TableName}
121+
UPDATE " + SqlSessionStateRepositoryUtil.TableName + @"
122122
SET Expires = DATEADD(n, Timeout, @now),
123-
@{SqlParameterName.Locked} = Locked,
124-
@{SqlParameterName.LockAge} = DATEDIFF(second, LockDate, @now),
125-
@{SqlParameterName.LockCookie} = LockCookie,
126-
@textptr = CASE @{SqlParameterName.Locked}
123+
" + SqlParameterName.Locked + @" = Locked,
124+
" + SqlParameterName.LockAge + @" = DATEDIFF(second, LockDate, @now),
125+
" + SqlParameterName.LockCookie + @" = LockCookie,
126+
@textptr = CASE " + SqlParameterName.Locked + @"
127127
WHEN 0 THEN SessionItemLong
128128
ELSE NULL
129129
END,
130-
@length = CASE @{SqlParameterName.Locked}
130+
@length = CASE " + SqlParameterName.Locked + @"
131131
WHEN 0 THEN DATALENGTH(SessionItemLong)
132132
ELSE NULL
133133
END,
@@ -137,19 +137,19 @@ ELSE NULL
137137
WHEN (Flags & 1) <> 0 THEN (Flags & ~1)
138138
ELSE Flags
139139
END,
140-
@{SqlParameterName.ActionFlags} = CASE
140+
" + SqlParameterName.ActionFlags + @" = CASE
141141
WHEN (Flags & 1) <> 0 THEN 1
142142
ELSE 0
143143
END
144-
WHERE SessionId = @{SqlParameterName.SessionId}
144+
WHERE SessionId = " + SqlParameterName.SessionId + @"
145145
IF @length IS NOT NULL BEGIN
146146
SELECT @textptr
147147
END
148148
";
149149
#endregion
150150

151151
#region DeleteExpiredSessions
152-
private static readonly string DeleteExpiredSessionsSql = $@"
152+
private const string DeleteExpiredSessionsSql = @"
153153
SET NOCOUNT ON
154154
SET DEADLOCK_PRIORITY LOW
155155
@@ -163,7 +163,7 @@ SessionId nvarchar({SqlSessionStateRepositoryUtil.IdLength}) NOT NULL PRIMARY KE
163163
164164
INSERT #tblExpiredSessions (SessionId)
165165
SELECT SessionId
166-
FROM {SqlSessionStateRepositoryUtil.TableName} WITH (SNAPSHOT)
166+
FROM " + SqlSessionStateRepositoryUtil.TableName + @" WITH (SNAPSHOT)
167167
WHERE Expires < @now
168168
169169
IF @@ROWCOUNT <> 0
@@ -179,7 +179,7 @@ FETCH NEXT FROM ExpiredSessionCursor INTO @SessionId
179179
180180
WHILE @@FETCH_STATUS = 0
181181
BEGIN
182-
DELETE FROM {SqlSessionStateRepositoryUtil.TableName} WHERE SessionId = @SessionId AND Expires < @now
182+
DELETE FROM " + SqlSessionStateRepositoryUtil.TableName + @" WHERE SessionId = @SessionId AND Expires < @now
183183
FETCH NEXT FROM ExpiredSessionCursor INTO @SessionId
184184
END
185185
@@ -193,13 +193,13 @@ DEALLOCATE ExpiredSessionCursor
193193
#endregion
194194

195195
#region TempInsertUninitializedItem
196-
private static readonly string TempInsertUninitializedItemSql = $@"
196+
private const string TempInsertUninitializedItemSql = @"
197197
DECLARE @now AS datetime
198198
DECLARE @nowLocal AS datetime
199199
SET @now = GETUTCDATE()
200200
SET @nowLocal = GETDATE()
201201
202-
INSERT {SqlSessionStateRepositoryUtil.TableName} (SessionId,
202+
INSERT " + SqlSessionStateRepositoryUtil.TableName + @" (SessionId,
203203
SessionItemLong,
204204
Timeout,
205205
Expires,
@@ -209,10 +209,10 @@ DECLARE @nowLocal AS datetime
209209
LockCookie,
210210
Flags)
211211
VALUES
212-
(@{SqlParameterName.SessionId},
213-
@{SqlParameterName.SessionItemLong},
214-
@{SqlParameterName.Timeout},
215-
DATEADD(n, @{SqlParameterName.Timeout}, @now),
212+
(" + SqlParameterName.SessionId + @",
213+
" + SqlParameterName.SessionItemLong + @",
214+
" + SqlParameterName.Timeout + @",
215+
DATEADD(n, " + SqlParameterName.Timeout + @", @now),
216216
0,
217217
@now,
218218
@nowLocal,
@@ -221,45 +221,45 @@ DECLARE @nowLocal AS datetime
221221
#endregion
222222

223223
#region ReleaseItemExclusive
224-
private static readonly string ReleaseItemExclusiveSql = $@"
225-
UPDATE {SqlSessionStateRepositoryUtil.TableName}
224+
private const string ReleaseItemExclusiveSql = @"
225+
UPDATE " + SqlSessionStateRepositoryUtil.TableName + @"
226226
SET Expires = DATEADD(n, Timeout, GETUTCDATE()),
227227
Locked = 0
228-
WHERE SessionId = @{SqlParameterName.SessionId} AND LockCookie = @{SqlParameterName.LockCookie}";
228+
WHERE SessionId = " + SqlParameterName.SessionId + @" AND LockCookie = " + SqlParameterName.LockCookie;
229229
#endregion
230230

231231
#region RemoveStateItem
232-
private static readonly string RemoveStateItemSql = $@"
233-
DELETE {SqlSessionStateRepositoryUtil.TableName}
234-
WHERE SessionId = @{SqlParameterName.SessionId} AND LockCookie = @{SqlParameterName.LockCookie}";
232+
private const string RemoveStateItemSql = @"
233+
DELETE " + SqlSessionStateRepositoryUtil.TableName + @"
234+
WHERE SessionId = " + SqlParameterName.SessionId + @" AND LockCookie = " + SqlParameterName.LockCookie;
235235
#endregion
236236

237237
#region ResetItemTimeout
238-
private static readonly string ResetItemTimeoutSql = $@"
239-
UPDATE {SqlSessionStateRepositoryUtil.TableName}
238+
private const string ResetItemTimeoutSql = @"
239+
UPDATE " + SqlSessionStateRepositoryUtil.TableName + @"
240240
SET Expires = DATEADD(n, Timeout, GETUTCDATE())
241-
WHERE SessionId = @{SqlParameterName.SessionId}";
241+
WHERE SessionId = " + SqlParameterName.SessionId;
242242
#endregion
243243

244244
#region UpdateStateItemLong
245-
private static readonly string UpdateStateItemLongSql = $@"
246-
UPDATE {SqlSessionStateRepositoryUtil.TableName}
247-
SET Expires = DATEADD(n, @{SqlParameterName.Timeout}, GETUTCDATE()),
248-
SessionItemLong = @{SqlParameterName.SessionItemLong},
249-
Timeout = @{SqlParameterName.Timeout},
245+
private const string UpdateStateItemLongSql = @"
246+
UPDATE " + SqlSessionStateRepositoryUtil.TableName + @"
247+
SET Expires = DATEADD(n, " + SqlParameterName.Timeout + @", GETUTCDATE()),
248+
SessionItemLong = " + SqlParameterName.SessionItemLong + @",
249+
Timeout = " + SqlParameterName.Timeout + @",
250250
Locked = 0
251-
WHERE SessionId = @{SqlParameterName.SessionId} AND LockCookie = @{SqlParameterName.LockCookie}";
251+
WHERE SessionId = " + SqlParameterName.SessionId + @" AND LockCookie = " + SqlParameterName.LockCookie;
252252
#endregion
253253

254254
#region InsertStateItemLong
255-
private static readonly string InsertStateItemLongSql = $@"
255+
private const string InsertStateItemLongSql = @"
256256
DECLARE @now AS datetime
257257
DECLARE @nowLocal AS datetime
258258
259259
SET @now = GETUTCDATE()
260260
SET @nowLocal = GETDATE()
261261
262-
INSERT {SqlSessionStateRepositoryUtil.TableName}
262+
INSERT " + SqlSessionStateRepositoryUtil.TableName + @"
263263
(SessionId,
264264
SessionItemLong,
265265
Timeout,
@@ -269,10 +269,10 @@ DECLARE @nowLocal AS datetime
269269
LockDateLocal,
270270
LockCookie)
271271
VALUES
272-
(@{SqlParameterName.SessionId},
273-
@{SqlParameterName.SessionItemLong},
274-
@{SqlParameterName.Timeout},
275-
DATEADD(n, @{SqlParameterName.Timeout}, @now),
272+
(" + SqlParameterName.SessionId + @",
273+
" + SqlParameterName.SessionItemLong + @",
274+
" + SqlParameterName.Timeout + @",
275+
DATEADD(n, " + SqlParameterName.Timeout + @", @now),
276276
0,
277277
@now,
278278
@nowLocal,

0 commit comments

Comments
 (0)