-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAttributeParser.cs
More file actions
228 lines (197 loc) · 8.1 KB
/
AttributeParser.cs
File metadata and controls
228 lines (197 loc) · 8.1 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
namespace PowerSync.Common.DB.Schema.Attributes;
using System.Reflection;
using Dapper;
class AttributeParser
{
private readonly Type _type;
private readonly TableAttribute _tableAttr;
public string TableName
{
get { return _tableAttr.Name; }
}
public AttributeParser(Type type)
{
_type = type;
var tableAttr = _type.GetCustomAttribute<TableAttribute>();
if (tableAttr == null)
{
throw new InvalidOperationException("Table classes must be marked with TableAttribute.");
}
_tableAttr = tableAttr;
}
public Table ParseTable()
{
return new Table(
name: _tableAttr.Name,
columns: ParseColumns(),
options: ParseTableOptions()
);
}
public Dictionary<string, ColumnType> ParseColumns()
{
var columns = new Dictionary<string, ColumnType>();
PropertyInfo? idProperty = null;
foreach (var prop in _type.GetProperties())
{
if (prop.GetCustomAttribute<IgnoredAttribute>() != null) continue;
var columnAttr = prop.GetCustomAttribute<ColumnAttribute>();
var columnName = columnAttr?.Name ?? prop.Name;
// Handle 'id' field separately
if (columnName.ToLowerInvariant() == "id")
{
if (idProperty != null)
{
throw new InvalidOperationException($"Cannot define multiple ID columns for table '{_tableAttr.Name}'.");
}
idProperty = prop;
continue;
}
var userColumnType = columnAttr?.ColumnType ?? ColumnType.Inferred;
// Infer column type from property's type
var columnType = userColumnType == ColumnType.Inferred
? PropertyTypeToColumnType(prop.PropertyType)
: userColumnType;
columns.Add(columnName, columnType);
}
// Validate 'id' property exists and is a string
if (idProperty == null)
{
throw new InvalidOperationException($"An 'id' property is required for table '{_tableAttr.Name}'.");
}
if (idProperty.PropertyType != typeof(string))
{
throw new InvalidOperationException($"ID Property '{idProperty.Name}' must be of type string.");
}
var idAttr = idProperty.GetCustomAttribute<ColumnAttribute>();
if (idAttr != null)
{
// ID column only supports Text and Inferred as options
if (idAttr.ColumnType != ColumnType.Text && idAttr.ColumnType != ColumnType.Inferred)
{
throw new InvalidOperationException
(
$"ID Property '{idProperty.Name}' must have ColumnType set to ColumnType.Text or ColumnType.Inferred."
);
}
}
return columns;
}
public Dictionary<string, List<string>> ParseIndexes()
{
var indexes = new Dictionary<string, List<string>>();
var indexAttrs = _type.GetCustomAttributes<IndexAttribute>();
foreach (var index in indexAttrs)
{
var name = index.Name;
var columns = index.Columns.ToList();
indexes.Add(name, columns);
}
return indexes;
}
private ColumnType PropertyTypeToColumnType(Type propertyType)
{
Type underlyingType = Nullable.GetUnderlyingType(propertyType) ?? propertyType;
return underlyingType switch
{
// TEXT types
Type t when t == typeof(string) => ColumnType.Text,
Type t when t == typeof(char) => ColumnType.Text,
Type t when t == typeof(Guid) => ColumnType.Text,
Type t when t == typeof(DateTime) => ColumnType.Text,
Type t when t == typeof(DateTimeOffset) => ColumnType.Text,
Type t when t == typeof(TimeSpan) => ColumnType.Text,
// 'decimal' is 128-bit, ColumnType.Real is only 64-bit
Type t when t == typeof(decimal) => ColumnType.Text,
// INTEGER types
Type t when t.IsEnum => ColumnType.Integer,
Type t when t == typeof(bool) => ColumnType.Integer, // bool
Type t when t == typeof(sbyte) => ColumnType.Integer, // i8
Type t when t == typeof(byte) => ColumnType.Integer, // u8
Type t when t == typeof(short) => ColumnType.Integer, // i16
Type t when t == typeof(ushort) => ColumnType.Integer, // u16
Type t when t == typeof(int) => ColumnType.Integer, // i32
Type t when t == typeof(uint) => ColumnType.Integer, // u32
Type t when t == typeof(long) => ColumnType.Integer, // i64
Type t when t == typeof(ulong) => ColumnType.Integer, // u64
// .NET 5.0+ only
// Type t when t == typeof(nint) => ColumnType.Integer, // isize
// Type t when t == typeof(nuint) => ColumnType.Integer, // usize
// REAL types
Type t when t == typeof(float) => ColumnType.Real,
Type t when t == typeof(double) => ColumnType.Real,
// Fallback
_ => throw new InvalidOperationException($"Unable to automatically infer ColumnType of property type '{underlyingType.Name}'."),
};
}
public TableOptions ParseTableOptions()
{
return new TableOptions(
indexes: ParseIndexes(),
localOnly: _tableAttr.LocalOnly,
insertOnly: _tableAttr.InsertOnly,
viewName: _tableAttr.ViewName,
trackMetadata: _tableAttr.TrackMetadata,
trackPreviousValues: ParseTrackPreviousOptions(),
ignoreEmptyUpdates: _tableAttr.IgnoreEmptyUpdates
);
}
public TrackPreviousOptions? ParseTrackPreviousOptions()
{
TrackPrevious trackPrevious = _tableAttr.TrackPreviousValues;
if (trackPrevious == TrackPrevious.None)
{
return null;
}
if (trackPrevious.HasFlag(TrackPrevious.Columns) && trackPrevious.HasFlag(TrackPrevious.Table))
{
throw new InvalidOperationException("Cannot specify both TrackPrevious.Columns and TrackPrevious.Table on the same table.");
}
if (!trackPrevious.HasFlag(TrackPrevious.Columns)
&& !trackPrevious.HasFlag(TrackPrevious.Table)
&& trackPrevious.HasFlag(TrackPrevious.OnlyWhenChanged))
{
throw new InvalidOperationException("Cannot specify TrackPrevious.OnlyWhenChanged without also specifying either TrackPrevious.Columns or TrackPrevious.Table.");
}
bool trackWholeTable = _tableAttr.TrackPreviousValues.HasFlag(TrackPrevious.Table);
bool onlyWhenChanged = trackPrevious.HasFlag(TrackPrevious.OnlyWhenChanged);
return new TrackPreviousOptions
{
Columns = trackWholeTable ? null : ParseTrackedColumns(),
OnlyWhenChanged = onlyWhenChanged,
};
}
public CustomPropertyTypeMap ParseDapperTypeMap()
{
return new(
_type,
(type, columnName) => type.GetProperties()
.FirstOrDefault(prop => prop.GetCustomAttributes()
.OfType<ColumnAttribute>()
.Any(columnAttr => columnAttr.Name == columnName))!
);
}
public void RegisterDapperTypeMap()
{
// Only register type map if some Column("custom_name") is found
if (_type
.GetProperties()
.Any(prop => prop
.GetCustomAttributes()
.OfType<ColumnAttribute>()
.Any(attr => attr.Name != null)))
{
Dapper.SqlMapper.SetTypeMap(_type, ParseDapperTypeMap());
}
}
public List<string> ParseTrackedColumns()
{
var trackedColumns = new List<string>();
foreach (var prop in _type.GetProperties())
{
var columnAttr = prop.GetCustomAttribute<ColumnAttribute>();
if (columnAttr == null || !columnAttr.TrackPrevious) continue;
trackedColumns.Add(prop.Name);
}
return trackedColumns;
}
}