forked from MiniProfiler/dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlServerStorage.cs
More file actions
362 lines (330 loc) · 14.2 KB
/
SqlServerStorage.cs
File metadata and controls
362 lines (330 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Dapper;
using StackExchange.Profiling.Helpers;
namespace StackExchange.Profiling.Storage
{
/// <summary>
/// Understands how to store a <see cref="MiniProfiler"/> to a MSSQL database.
/// </summary>
public class SqlServerStorage : DatabaseStorageBase
{
/// <summary>
/// Load the SQL statements (using Dapper Multiple Results)
/// </summary>
protected string SqlStatements = @"
SELECT * FROM MiniProfilers WHERE Id = @id;
SELECT * FROM MiniProfilerTimings WHERE MiniProfilerId = @id ORDER BY StartMilliseconds;
SELECT * FROM MiniProfilerClientTimings WHERE MiniProfilerId = @id ORDER BY Start;";
/// <summary>
/// Initializes a new instance of the <see cref="SqlServerStorage"/> class with the specified connection string.
/// </summary>
/// <param name="connectionString">
/// The connection string to use.
/// </param>
public SqlServerStorage(string connectionString) : base(connectionString) { }
/// <summary>
/// Stores to <c>dbo.MiniProfilers</c> under its <see cref="MiniProfiler.Id"/>;
/// </summary>
/// <param name="profiler">The Mini Profiler</param>
public override void Save(MiniProfiler profiler)
{
const string sql =
@"insert into MiniProfilers
(Id,
RootTimingId,
Name,
Started,
DurationMilliseconds,
[User],
HasUserViewed,
MachineName,
CustomLinksJson,
ClientTimingsRedirectCount)
select @Id,
@RootTimingId,
@Name,
@Started,
@DurationMilliseconds,
@User,
@HasUserViewed,
@MachineName,
@CustomLinksJson,
@ClientTimingsRedirectCount
where not exists (select 1 from MiniProfilers where Id = @Id)"; // this syntax works on both mssql and sqlite
using (var conn = GetConnection())
{
conn.Execute(sql, new
{
profiler.Id,
profiler.Started,
Name = profiler.Name.Truncate(200),
User = profiler.User.Truncate(100),
RootTimingId = profiler.Root != null ? profiler.Root.Id : (Guid?)null,
profiler.DurationMilliseconds,
profiler.HasUserViewed,
MachineName = profiler.MachineName.Truncate(100),
profiler.CustomLinksJson,
ClientTimingsRedirectCount = profiler.ClientTimings != null ? profiler.ClientTimings.RedirectCount : (int?)null
});
var timings = new List<Timing>();
if (profiler.Root != null)
{
profiler.Root.MiniProfilerId = profiler.Id;
FlattenTimings(profiler.Root, timings);
}
SaveTimings(timings, conn);
var clientTimings = profiler.ClientTimings?.Timings;
if (clientTimings != null && clientTimings.Any())
{
// set the profilerId (isn't needed unless we are storing it)
foreach(var x in clientTimings)
{
x.MiniProfilerId = profiler.Id;
x.Id = Guid.NewGuid();
}
SaveClientTimings(clientTimings, conn);
}
}
}
private void SaveTimings(List<Timing> timings, DbConnection conn)
{
const string sql = @"
INSERT INTO MiniProfilerTimings
(Id,
MiniProfilerId,
ParentTimingId,
Name,
DurationMilliseconds,
StartMilliseconds,
IsRoot,
Depth,
CustomTimingsJson)
SELECT @Id,
@MiniProfilerId,
@ParentTimingId,
@Name,
@DurationMilliseconds,
@StartMilliseconds,
@IsRoot,
@Depth,
@CustomTimingsJson
WHERE NOT EXISTS (SELECT 1 FROM MiniProfilerTimings WHERE Id = @Id)";
foreach (var timing in timings)
{
conn.Execute(sql, new
{
timing.Id,
timing.MiniProfilerId,
timing.ParentTimingId,
Name = timing.Name.Truncate(200),
timing.DurationMilliseconds,
timing.StartMilliseconds,
timing.IsRoot,
timing.Depth,
timing.CustomTimingsJson
});
}
}
private void SaveClientTimings(IReadOnlyList<ClientTiming> timings, DbConnection conn)
{
const string sql = @"
INSERT INTO MiniProfilerClientTimings
(Id,
MiniProfilerId,
Name,
Start,
Duration)
SELECT @Id,
@MiniProfilerId,
@Name,
@Start,
@Duration
WHERE NOT EXISTS (SELECT 1 FROM MiniProfilerClientTimings WHERE Id = @Id)";
foreach (var timing in timings)
{
conn.Execute(sql, new
{
timing.Id,
timing.MiniProfilerId,
Name = timing.Name.Truncate(200),
timing.Start,
timing.Duration
});
}
}
/// <summary>
/// Loads the <c>MiniProfiler</c> identified by 'id' from the database.
/// </summary>
/// <param name="id">The id.</param>
/// <returns>the mini profiler.</returns>
public override MiniProfiler Load(Guid id)
{
MiniProfiler result;
using (var conn = GetConnection())
{
using (var multi = conn.QueryMultiple(SqlStatements, new { id }))
{
result = multi.Read<MiniProfiler>().SingleOrDefault();
var timings = multi.Read<Timing>().ToList();
var clientTimings = multi.Read<ClientTiming>().ToList();
ConnectTimings(result, timings, clientTimings);
}
}
if (result != null)
{
// HACK: stored dates are utc, but are pulled out as local time
result.Started = new DateTime(result.Started.Ticks, DateTimeKind.Utc);
}
return result;
}
/// <summary>
/// Sets the session to un-viewed
/// </summary>
/// <param name="user">The user.</param>
/// <param name="id">The id.</param>
public override void SetUnviewed(string user, Guid id) => ToggleViewed(user, id, false);
/// <summary>
/// sets the session to viewed
/// </summary>
/// <param name="user">The user.</param>
/// <param name="id">The id.</param>
public override void SetViewed(string user, Guid id) => ToggleViewed(user, id, true);
private void ToggleViewed(string user, Guid id, bool hasUserVeiwed)
{
using (var conn = GetConnection())
{
conn.Execute(@"
Update MiniProfilers
Set HasUserViewed = @hasUserVeiwed
Where Id = @id
And [User] = @user", new { id, user, hasUserVeiwed });
}
}
/// <summary>
/// Returns a list of <see cref="MiniProfiler.Id"/>s that haven't been seen by <paramref name="user"/>.
/// </summary>
/// <param name="user">User identified by the current UserProvider"/>.</param>
/// <returns>the list of keys.</returns>
public override List<Guid> GetUnviewedIds(string user)
{
using (var conn = GetConnection())
{
return conn.Query<Guid>(@"
Select Id
From MiniProfilers
Where [User] = @user
And HasUserViewed = 0
Order By Started", new { user }).ToList();
}
}
/// <summary>
/// List the Results.
/// </summary>
/// <param name="maxResults">The max results.</param>
/// <param name="start">The start.</param>
/// <param name="finish">The finish.</param>
/// <param name="orderBy">order by.</param>
/// <returns>the list of key values.</returns>
public override IEnumerable<Guid> List(int maxResults, DateTime? start = null, DateTime? finish = null, ListResultsOrder orderBy = ListResultsOrder.Descending)
{
var sb = new StringBuilder(@"
Select Top {=maxResults} Id
From MiniProfilers
");
if (finish != null)
{
sb.AppendLine("Where Started < @finish");
}
if (start != null)
{
sb.AppendLine(finish != null
? " And Started > @start"
: "Where Started > @start");
}
sb.Append("Order By ").Append(orderBy == ListResultsOrder.Descending ? "Started Desc" : "Started Asc");
using (var conn = GetConnection())
{
return conn.Query<Guid>(sb.ToString(), new { start, finish }).ToList();
}
}
/// <summary>
/// Load individual MiniProfiler
/// </summary>
/// <param name="connection">The connection.</param>
/// <param name="keyParameter">The id Parameter.</param>
/// <returns>Related MiniProfiler object</returns>
private MiniProfiler LoadProfilerRecord(DbConnection connection, object keyParameter)
{
using (var multi = connection.QueryMultiple(SqlStatements, keyParameter))
{
var profiler = multi.Read<MiniProfiler>().SingleOrDefault();
var timings = multi.Read<Timing>().ToList();
var clientTimings = multi.Read<ClientTiming>().ToList();
ConnectTimings(profiler, timings, clientTimings);
return profiler;
}
}
/// <summary>
/// Returns a connection to Sql Server.
/// </summary>
protected override DbConnection GetConnection() => new SqlConnection(ConnectionString);
/// <summary>
/// Creates needed tables. Run this once on your database.
/// </summary>
/// <remarks>
/// Works in SQL server and <c>sqlite</c> (with documented removals).
/// </remarks>
public static readonly string TableCreationScript =
@"
create table MiniProfilers
(
RowId integer not null identity constraint PK_MiniProfilers primary key clustered, -- Need a clustered primary key for SQL Azure
Id uniqueidentifier not null, -- don't cluster on a guid
RootTimingId uniqueidentifier null,
Name nvarchar(200) not null,
Started datetime not null,
DurationMilliseconds decimal(7, 1) not null,
[User] nvarchar(100) null,
HasUserViewed bit not null,
MachineName nvarchar(100) null,
CustomLinksJson nvarchar(max),
ClientTimingsRedirectCount int null
);
-- displaying results selects everything based on the main MiniProfilers.Id column
create unique nonclustered index IX_MiniProfilers_Id on MiniProfilers (Id);
-- speeds up a query that is called on every .Stop()
create nonclustered index IX_MiniProfilers_User_HasUserViewed_Includes on MiniProfilers ([User], HasUserViewed) include (Id, [Started]);
create table MiniProfilerTimings
(
RowId integer not null identity constraint PK_MiniProfilerTimings primary key clustered,
Id uniqueidentifier not null,
MiniProfilerId uniqueidentifier not null,
ParentTimingId uniqueidentifier null,
Name nvarchar(200) not null,
DurationMilliseconds decimal(9, 3) not null,
StartMilliseconds decimal(9, 3) not null,
IsRoot bit not null,
Depth smallint not null,
CustomTimingsJson nvarchar(max) null
);
create unique nonclustered index IX_MiniProfilerTimings_Id on MiniProfilerTimings (Id);
create nonclustered index IX_MiniProfilerTimings_MiniProfilerId on MiniProfilerTimings (MiniProfilerId);
create table MiniProfilerClientTimings
(
RowId integer not null identity constraint PK_MiniProfilerClientTimings primary key clustered,
Id uniqueidentifier not null,
MiniProfilerId uniqueidentifier not null,
Name nvarchar(200) not null,
Start decimal(9, 3) not null,
Duration decimal(9, 3) not null
);
create unique nonclustered index IX_MiniProfilerClientTimings_Id on MiniProfilerClientTimings (Id);
create nonclustered index IX_MiniProfilerClientTimings_MiniProfilerId on MiniProfilerClientTimings (MiniProfilerId);
";
}
}