Skip to content

Commit

Permalink
chore: Fix DatabaseStorageBase and SqlServerStorage after review
Browse files Browse the repository at this point in the history
  • Loading branch information
unchase committed Sep 3, 2021
1 parent 8758fd7 commit 93e7691
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
66 changes: 33 additions & 33 deletions src/MiniProfiler.Providers.SqlServer/SqlServerStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ public SqlServerStorage(string connectionString, string profilersTable = null, s
private string _saveSql, _saveTimingsSql, _saveClientTimingsSql;

private string SaveSql => _saveSql ??= $@"
INSERT INTO {SchemaName}.{MiniProfilersTable}
INSERT INTO {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilersTable}
(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 {SchemaName}.{MiniProfilersTable} WHERE Id = @Id)";
WHERE NOT EXISTS (SELECT 1 FROM {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilersTable} WHERE Id = @Id)";

private string SaveTimingsSql => _saveTimingsSql ??= $@"
INSERT INTO {SchemaName}.{MiniProfilerTimingsTable}
INSERT INTO {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilerTimingsTable}
(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 {SchemaName}.{MiniProfilerTimingsTable} WHERE Id = @Id)";
WHERE NOT EXISTS (SELECT 1 FROM {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilerTimingsTable} WHERE Id = @Id)";

private string SaveClientTimingsSql => _saveClientTimingsSql ??= $@"
INSERT INTO {SchemaName}.{MiniProfilerClientTimingsTable}
INSERT INTO {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilerClientTimingsTable}
(Id, MiniProfilerId, Name, Start, Duration)
SELECT @Id, @MiniProfilerId, @Name, @Start, @Duration
WHERE NOT EXISTS (SELECT 1 FROM {SchemaName}.{MiniProfilerClientTimingsTable} WHERE Id = @Id)";
WHERE NOT EXISTS (SELECT 1 FROM {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilerClientTimingsTable} WHERE Id = @Id)";

/// <summary>
/// Stores to <c>dbo.MiniProfilers</c> under its <see cref="MiniProfiler.Id"/>;
Expand Down Expand Up @@ -182,9 +182,9 @@ public override async Task SaveAsync(MiniProfiler profiler)
private string _loadSql;

private string LoadSql => _loadSql ??= $@"
SELECT * FROM {SchemaName}.{MiniProfilersTable} WHERE Id = @id;
SELECT * FROM {SchemaName}.{MiniProfilerTimingsTable} WHERE MiniProfilerId = @id ORDER BY StartMilliseconds;
SELECT * FROM {SchemaName}.{MiniProfilerClientTimingsTable} WHERE MiniProfilerId = @id ORDER BY Start;";
SELECT * FROM {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilersTable} WHERE Id = @id;
SELECT * FROM {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilerTimingsTable} WHERE MiniProfilerId = @id ORDER BY StartMilliseconds;
SELECT * FROM {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilerClientTimingsTable} WHERE MiniProfilerId = @id ORDER BY Start;";

/// <summary>
/// Loads the <c>MiniProfiler</c> identified by 'id' from the database.
Expand Down Expand Up @@ -243,14 +243,14 @@ public override async Task<MiniProfiler> LoadAsync(Guid id)
}

/// <summary>
/// Sets a particular profiler session so it is considered "unviewed"
/// Sets a particular profiler session so it is considered "unviewed"
/// </summary>
/// <param name="user">The user to set this profiler ID as unviewed for.</param>
/// <param name="id">The profiler ID to set unviewed.</param>
public override void SetUnviewed(string user, Guid id) => ToggleViewed(user, id, false);

/// <summary>
/// Asynchronously sets a particular profiler session so it is considered "unviewed"
/// Asynchronously sets a particular profiler session so it is considered "unviewed"
/// </summary>
/// <param name="user">The user to set this profiler ID as unviewed for.</param>
/// <param name="id">The profiler ID to set unviewed.</param>
Expand All @@ -273,9 +273,9 @@ public override async Task<MiniProfiler> LoadAsync(Guid id)
private string _toggleViewedSql;

private string ToggleViewedSql => _toggleViewedSql ??= $@"
Update {SchemaName}.{MiniProfilersTable}
Set HasUserViewed = @hasUserVeiwed
Where Id = @id
Update {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilersTable}
Set HasUserViewed = @hasUserVeiwed
Where Id = @id
And [User] = @user";

private void ToggleViewed(string user, Guid id, bool hasUserVeiwed)
Expand All @@ -298,7 +298,7 @@ private async Task ToggleViewedAsync(string user, Guid id, bool hasUserVeiwed)

private string GetUnviewedIdsSql => _getUnviewedIdsSql ??= $@"
Select Id
From {SchemaName}.{MiniProfilersTable}
From {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilersTable}
Where [User] = @user
And HasUserViewed = 0
Order By Started";
Expand Down Expand Up @@ -368,7 +368,7 @@ private string BuildListQuery(DateTime? start = null, DateTime? finish = null, L
var sb = StringBuilderCache.Get();
sb.Append(@"
Select Top {=maxResults} Id
From ").Append(SchemaName).Append(".").Append(MiniProfilersTable).Append(@"
From ").Append(SchemaName == null ? string.Empty : $"{SchemaName}.").Append(MiniProfilersTable).Append(@"
");
if (finish != null)
{
Expand Down Expand Up @@ -399,11 +399,11 @@ protected override IEnumerable<string> GetTableCreationScripts()
-- creating schema name if not exists
IF NOT EXISTS ( SELECT * FROM sys.schemas WHERE name = N'{SchemaName}') EXEC('CREATE SCHEMA [{SchemaName}]');
IF OBJECT_ID(N'{SchemaName}.{MiniProfilersTable}', N'U') IS NULL
IF OBJECT_ID(N'{(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilersTable}', N'U') IS NULL
BEGIN
CREATE TABLE {SchemaName}.{MiniProfilersTable}
CREATE TABLE {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilersTable}
(
RowId integer not null identity constraint PK_{SchemaName}_{MiniProfilersTable} primary key clustered, -- Need a clustered primary key for SQL Azure
RowId integer not null identity constraint PK_{(SchemaName == null ? string.Empty : $"{SchemaName}_")}{MiniProfilersTable} 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) null,
Expand All @@ -416,17 +416,17 @@ CustomLinksJson nvarchar(max),
ClientTimingsRedirectCount int null
);
-- displaying results selects everything based on the main MiniProfilers.Id column
CREATE UNIQUE NONCLUSTERED INDEX IX_{SchemaName}_{MiniProfilersTable}_Id ON {SchemaName}.{MiniProfilersTable} (Id);
CREATE UNIQUE NONCLUSTERED INDEX IX_{(SchemaName == null ? string.Empty : $"{SchemaName}_")}{MiniProfilersTable}_Id ON {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilersTable} (Id);
-- speeds up a query that is called on every .Stop()
CREATE NONCLUSTERED INDEX IX_{SchemaName}_{MiniProfilersTable}_User_HasUserViewed_Includes ON {SchemaName}.{MiniProfilersTable} ([User], HasUserViewed) INCLUDE (Id, [Started]);
CREATE NONCLUSTERED INDEX IX_{(SchemaName == null ? string.Empty : $"{SchemaName}_")}{MiniProfilersTable}_User_HasUserViewed_Includes ON {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilersTable} ([User], HasUserViewed) INCLUDE (Id, [Started]);
END;
IF OBJECT_ID(N'{SchemaName}.{MiniProfilerTimingsTable}', N'U') IS NULL
IF OBJECT_ID(N'{(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilerTimingsTable}', N'U') IS NULL
BEGIN
CREATE TABLE {SchemaName}.{MiniProfilerTimingsTable}
CREATE TABLE {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilerTimingsTable}
(
RowId integer not null identity constraint PK_{SchemaName}_{MiniProfilerTimingsTable} primary key clustered,
RowId integer not null identity constraint PK_{(SchemaName == null ? string.Empty : $"{SchemaName}_")}{MiniProfilerTimingsTable} primary key clustered,
Id uniqueidentifier not null,
MiniProfilerId uniqueidentifier not null,
ParentTimingId uniqueidentifier null,
Expand All @@ -438,25 +438,25 @@ StartMilliseconds decimal(15,3) not null,
CustomTimingsJson nvarchar(max) null
);
CREATE UNIQUE NONCLUSTERED INDEX IX_{SchemaName}_{MiniProfilerTimingsTable}_Id ON {SchemaName}.{MiniProfilerTimingsTable} (Id);
CREATE NONCLUSTERED INDEX IX_{SchemaName}_{MiniProfilerTimingsTable}_MiniProfilerId ON {SchemaName}.{MiniProfilerTimingsTable} (MiniProfilerId);
CREATE UNIQUE NONCLUSTERED INDEX IX_{(SchemaName == null ? string.Empty : $"{SchemaName}_")}{MiniProfilerTimingsTable}_Id ON {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilerTimingsTable} (Id);
CREATE NONCLUSTERED INDEX IX_{(SchemaName == null ? string.Empty : $"{SchemaName}_")}{MiniProfilerTimingsTable}_MiniProfilerId ON {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilerTimingsTable} (MiniProfilerId);
END;
IF OBJECT_ID(N'{SchemaName}.{MiniProfilerClientTimingsTable}', N'U') IS NULL
IF OBJECT_ID(N'{(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilerClientTimingsTable}', N'U') IS NULL
BEGIN
CREATE TABLE {SchemaName}.{MiniProfilerClientTimingsTable}
CREATE TABLE {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilerClientTimingsTable}
(
RowId integer not null identity constraint PK_{SchemaName}_{MiniProfilerClientTimingsTable} primary key clustered,
RowId integer not null identity constraint PK_{(SchemaName == null ? string.Empty : $"{SchemaName}_")}{MiniProfilerClientTimingsTable} 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_{SchemaName}_{MiniProfilerClientTimingsTable}_Id on {SchemaName}.{MiniProfilerClientTimingsTable} (Id);
CREATE NONCLUSTERED INDEX IX_{SchemaName}_{MiniProfilerClientTimingsTable}_MiniProfilerId on {SchemaName}.{MiniProfilerClientTimingsTable} (MiniProfilerId);
END;
CREATE UNIQUE NONCLUSTERED INDEX IX_{(SchemaName == null ? string.Empty : $"{SchemaName}_")}{MiniProfilerClientTimingsTable}_Id on {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilerClientTimingsTable} (Id);
CREATE NONCLUSTERED INDEX IX_{(SchemaName == null ? string.Empty : $"{SchemaName}_")}{MiniProfilerClientTimingsTable}_MiniProfilerId on {(SchemaName == null ? string.Empty : $"{SchemaName}.")}{MiniProfilerClientTimingsTable} (MiniProfilerId);
END;
";
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/MiniProfiler.Shared/Storage/DatabaseStorageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public abstract class DatabaseStorageBase : IAsyncStorage, IDatabaseStorageConne
/// <summary>
/// The database schema to use for MiniProfiler tables.
/// </summary>
public readonly string SchemaName = "dbo";
public readonly string SchemaName = null;

/// <summary>
/// Gets or sets how we connect to the database used to save/load MiniProfiler results.
Expand Down Expand Up @@ -61,7 +61,7 @@ protected DatabaseStorageBase(string connectionString, string profilersTable = n
MiniProfilersTable = profilersTable ?? MiniProfilersTable;
MiniProfilerTimingsTable = timingsTable ?? MiniProfilerTimingsTable;
MiniProfilerClientTimingsTable = clientTimingsTable ?? MiniProfilerClientTimingsTable;
SchemaName = schemaName ?? SchemaName;
SchemaName = schemaName;
}

/// <summary>
Expand Down

0 comments on commit 93e7691

Please sign in to comment.