Skip to content

Commit a131a45

Browse files
committed
Hierarchical property name support in SqlColumn class
1 parent a55daf1 commit a131a45

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/SqlColumn.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Data;
34
using static System.FormattableString;
45

@@ -12,6 +13,7 @@ public class SqlColumn
1213
private SqlDbType _dataType = SqlDbType.VarChar; // backwards-compatibility default
1314
private string _columnName = string.Empty;
1415
private string _propertyName;
16+
private List<string> _propertyNameHierarchy = new List<string>();
1517

1618
/// <summary>
1719
/// Default constructor.
@@ -104,13 +106,30 @@ public SqlDbType DataType
104106
public string PropertyName
105107
{
106108
get => _propertyName ?? ColumnName;
107-
set => _propertyName = value;
109+
set
110+
{
111+
_propertyName = value;
112+
ParseHierarchicalPropertyName(value);
113+
}
108114
}
109115

116+
/// <summary>
117+
/// List of the hierachical parts of a property name and all sub properties (e.g. Property.Settings.EventName)
118+
/// </summary>
119+
internal IReadOnlyList<string> PropertyNameHierarchy
120+
=> _propertyNameHierarchy;
121+
122+
/// <summary>
123+
/// True if property name is hierarchical (e.g. Property.Settings.EventName)
124+
/// </summary>
125+
internal bool HasHierarchicalPropertyName
126+
=> _propertyNameHierarchy.Count > 1;
127+
110128
// Set by the constructors of the Standard Column classes that inherit from this;
111129
// allows Standard Columns and user-defined columns to coexist but remain identifiable
112130
// and allows casting back to the Standard Column without a lot of switch gymnastics.
113131
internal StandardColumn? StandardColumnIdentifier { get; set; }
132+
114133
internal Type StandardColumnType { get; set; }
115134

116135
/// <summary>
@@ -150,5 +169,10 @@ internal void SetDataTypeFromConfigString(string requestedSqlType)
150169

151170
DataType = sqlType;
152171
}
172+
173+
private void ParseHierarchicalPropertyName(string propertyName)
174+
{
175+
_propertyNameHierarchy.AddRange(propertyName.Split('.'));
176+
}
153177
}
154178
}

test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/SqlServerColumnTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Data;
22
using Serilog.Sinks.MSSqlServer.Tests.TestUtils;
33
using Xunit;
4+
using static System.FormattableString;
45

56
namespace Serilog.Sinks.MSSqlServer.Tests
67
{
@@ -32,6 +33,33 @@ public void StoresPropertyName()
3233

3334
// Assert
3435
Assert.Equal(propertyName, sut.PropertyName);
36+
Assert.Equal(1, sut.PropertyNameHierarchy.Count);
37+
Assert.Equal(propertyName, sut.PropertyNameHierarchy[0]);
38+
Assert.False(sut.HasHierarchicalPropertyName);
39+
}
40+
41+
[Fact]
42+
public void StoresHierachicalPropertyName()
43+
{
44+
// Arrange
45+
const string propertyName1 = "TestPropertyName";
46+
const string propertyName2 = "SubPropertyName";
47+
const string propertyName3 = "SubSubPropertyName";
48+
var propertyName = Invariant($"{propertyName1}.{propertyName2}.{propertyName3}");
49+
50+
// Act
51+
var sut = new SqlColumn("TestColumnName", SqlDbType.Int)
52+
{
53+
PropertyName = propertyName
54+
};
55+
56+
// Assert
57+
Assert.Equal(propertyName, sut.PropertyName);
58+
Assert.Equal(3, sut.PropertyNameHierarchy.Count);
59+
Assert.Equal(propertyName1, sut.PropertyNameHierarchy[0]);
60+
Assert.Equal(propertyName2, sut.PropertyNameHierarchy[1]);
61+
Assert.Equal(propertyName3, sut.PropertyNameHierarchy[2]);
62+
Assert.True(sut.HasHierarchicalPropertyName);
3563
}
3664
}
3765
}

0 commit comments

Comments
 (0)