Skip to content

Commit 2782f32

Browse files
author
Mohsen Esmailpour
committed
Add SQL query builder base class.
1 parent fb6ff0f commit 2782f32

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace Serilog.Ui.Core.QueryBuilder.Sql;
2+
3+
/// <summary>
4+
/// Represents the column names used in the SQL-based sink for logging.
5+
/// </summary>
6+
public abstract class SinkColumnNames
7+
{
8+
/// <summary>
9+
/// Gets or sets the message of the log entry.
10+
/// </summary>
11+
public string Message { get; set; } = string.Empty;
12+
13+
/// <summary>
14+
/// Gets or sets the message template of the log entry.
15+
/// </summary>
16+
public string MessageTemplate { get; set; } = string.Empty;
17+
18+
/// <summary>
19+
/// Gets or sets the level of the log entry.
20+
/// </summary>
21+
public string Level { get; set; } = string.Empty;
22+
23+
/// <summary>
24+
/// Gets or sets the timestamp of the log entry.
25+
/// </summary>
26+
public string Timestamp { get; set; } = string.Empty;
27+
28+
/// <summary>
29+
/// Gets or sets the exception of the log entry.
30+
/// </summary>
31+
public string Exception { get; set; } = string.Empty;
32+
33+
/// <summary>
34+
/// Gets or sets the serialized log event like properties.
35+
/// </summary>
36+
public string LogEventSerialized { get; set; } = string.Empty;
37+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Serilog.Ui.Core.Models;
2+
using static Serilog.Ui.Core.Models.SearchOptions;
3+
4+
namespace Serilog.Ui.Core.QueryBuilder.Sql;
5+
6+
/// <summary>
7+
/// Abstract class that provides methods to build SQL queries for fetching and counting logs.
8+
/// </summary>
9+
public abstract class SqlQueryBuilder
10+
{
11+
/// <summary>
12+
/// Builds a SQL query to fetch logs from the specified table.
13+
/// </summary>
14+
/// <typeparam name="T">The type of the log model.</typeparam>
15+
/// <param name="columns">The column names used in the sink for logging.</param>
16+
/// <param name="schema">The schema of the table.</param>
17+
/// <param name="tableName">The name of the table.</param>
18+
/// <param name="query">The query parameters for fetching logs.</param>
19+
/// <returns>A SQL query string to fetch logs.</returns>
20+
public abstract string BuildFetchLogsQuery<T>(SinkColumnNames columns, string schema, string tableName, FetchLogsQuery query) where T : LogModel;
21+
22+
/// <summary>
23+
/// Builds a SQL query to count logs in the specified table.
24+
/// </summary>
25+
/// <typeparam name="T">The type of the log model.</typeparam>
26+
/// <param name="columns">The column names used in the sink for logging.</param>
27+
/// <param name="schema">The schema of the table.</param>
28+
/// <param name="tableName">The name of the table.</param>
29+
/// <param name="query">The query parameters for counting logs.</param>
30+
/// <returns>A SQL query string to count logs.</returns>
31+
public abstract string BuildCountLogsQuery<T>(SinkColumnNames columns, string schema, string tableName, FetchLogsQuery query) where T : LogModel;
32+
33+
/// <summary>
34+
/// Generates a SQL sort clause based on the specified sort property and direction.
35+
/// </summary>
36+
/// <param name="columns">The column names used in the sink for logging.</param>
37+
/// <param name="sortOn">The property to sort on.</param>
38+
/// <param name="sortBy">The direction to sort by.</param>
39+
/// <returns>A SQL sort clause string.</returns>
40+
protected static string GenerateSortClause(SinkColumnNames columns, SortProperty sortOn, SortDirection sortBy)
41+
{
42+
var sortPropertyName = sortOn switch
43+
{
44+
SortProperty.Timestamp => columns.Timestamp,
45+
SortProperty.Level => columns.Level,
46+
SortProperty.Message => columns.Message,
47+
_ => columns.Timestamp
48+
};
49+
50+
return $"\"{sortPropertyName}\" {sortBy.ToString().ToUpper()}";
51+
}
52+
}

src/Serilog.Ui.Core/Serilog.Ui.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<ItemGroup>
1010
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
1111
<PackageReference Include="Microsoft.Extensions.Primitives" Version="8.0.0" />
12-
<PackageReference Include="System.Text.Json" Version="8.0.4"/>
12+
<PackageReference Include="System.Text.Json" Version="8.0.4" />
1313
</ItemGroup>
1414
</Project>

0 commit comments

Comments
 (0)