-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathColumnConfig.cs
More file actions
424 lines (346 loc) · 14.1 KB
/
Copy pathColumnConfig.cs
File metadata and controls
424 lines (346 loc) · 14.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef
using DbEx.DbSchema;
using OnRamp.Config;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Beef.CodeGen.Config.Database
{
/// <summary>
/// Defines the column configuration.
/// </summary>
public interface IColumnConfig
{
/// <summary>
/// Gets the column name.
/// </summary>
string? Name { get; }
/// <summary>
/// Gets the database <see cref="DbColumnSchema"/> configuration.
/// </summary>
DbColumnSchema? DbColumn { get; }
/// <summary>
/// Gets the qualified name (includes the alias).
/// </summary>
public string QualifiedName { get; }
/// <summary>
/// Gets the parameter name.
/// </summary>
public string ParameterName { get; }
/// <summary>
/// Gets the SQL type.
/// </summary>
public string? SqlType { get; }
/// <summary>
/// Gets the parameter SQL definition.
/// </summary>
public string? ParameterSql { get; }
/// <summary>
/// Gets the UDT SQL definition.
/// </summary>
public string? UdtSql { get; }
/// <summary>
/// Gets the where equality clause.
/// </summary>
public string WhereEquals { get; }
/// <summary>
/// Gets the SQL for defining initial value for comparisons.
/// </summary>
public string SqlInitialValue { get; }
/// <summary>
/// Indicates where the column is the "TenantId" column.
/// </summary>
public bool IsTenantIdColumn { get; }
/// <summary>
/// Indicates where the column is the "OrgUnitId" column.
/// </summary>
public bool IsOrgUnitIdColumn { get; }
/// <summary>
/// Indicates where the column is the "RowVersion" column.
/// </summary>
public bool IsRowVersionColumn { get; }
/// <summary>
/// Indicates where the column is the "IsDeleted" column.
/// </summary>
public bool IsIsDeletedColumn { get; }
/// <summary>
/// Indicates whether the column is considered an audit column.
/// </summary>
public bool IsAudit { get; }
/// <summary>
/// Indicates whether the column is "CreatedBy" or "CreatedDate".
/// </summary>
public bool IsCreated { get; }
/// <summary>
/// Indicates whether the column is "CreatedBy".
/// </summary>
public bool IsCreatedBy { get; }
/// <summary>
/// Indicates whether the column is "CreatedDate".
/// </summary>
public bool IsCreatedDate { get; }
/// <summary>
/// Indicates whether the column is "UpdatedBy" or "UpdatedDate".
/// </summary>
public bool IsUpdated { get; }
/// <summary>
/// Indicates whether the column is "UpdatedBy".
/// </summary>
public bool IsUpdatedBy { get; }
/// <summary>
/// Indicates whether the column is "UpdatedDate".
/// </summary>
public bool IsUpdatedDate { get; }
/// <summary>
/// Indicates whether the column is "DeletedBy" or "DeletedDate".
/// </summary>
public bool IsDeleted { get; }
/// <summary>
/// Indicates whether the column is "DeletedBy".
/// </summary>
public bool IsDeletedBy { get; }
/// <summary>
/// Indicates whether the column is "DeletedDate".
/// </summary>
public bool IsDeletedDate { get; }
/// <summary>
/// Indicates where the column should be considered for a 'Create' operation.
/// </summary>
public bool IsCreateColumn { get; }
/// <summary>
/// Indicates where the column should be considered for a 'Update' operation.
/// </summary>
public bool IsUpdateColumn { get; }
/// <summary>
/// Indicates where the column should be considered for a 'Delete' operation.
/// </summary>
public bool IsDeleteColumn { get; }
/// <summary>
/// Gets the EF SQL Type.
/// </summary>
public string? EfSqlType { get; }
/// <summary>
/// Gets the corresponding .NET <see cref="System.Type"/> name.
/// </summary>
public string DotNetType { get; }
/// <summary>
/// Indicates whether the .NET property is nullable.
/// </summary>
public bool IsDotNetNullable { get; }
/// <summary>
/// Gets the name alias.
/// </summary>
public string? NameAlias { get; }
/// <summary>
/// Gets the qualified name with the alias (used in a select).
/// </summary>
public string QualifiedNameWithAlias { get; }
}
/// <summary>
/// Represents the base column configuration.
/// </summary>
/// <typeparam name="TParent">The parent <see cref="Type"/>.</typeparam>
public abstract class ColumnConfigBase<TParent> : ConfigBase<CodeGenConfig, TParent>, IColumnConfig where TParent : ConfigBase, ITableReference, ISpecialColumns
{
private static readonly string[] _intTypes = ["int", "short", "long", "unit", "ushort", "ulong", "byte"];
/// <summary>
/// Gets or sets the column name.
/// </summary>
public string? Name { get; set; }
/// <summary>
/// Gets or sets the database <see cref="DbColumnSchema"/> configuration.
/// </summary>
public DbColumnSchema? DbColumn { get; set; }
/// <summary>
/// Gets the qualified name (includes the alias).
/// </summary>
public string QualifiedName => $"[{Parent!.Alias}].[{Name}]";
/// <summary>
/// Gets the schema/table qualified name.
/// </summary>
public string TableQualififiedName => "[].[]";
/// <summary>
/// Gets the parameter name.
/// </summary>
public string ParameterName => "@" + Name;
/// <summary>
/// Gets the SQL type.
/// </summary>
public string? SqlType { get; private set; }
/// <summary>
/// Gets the parameter SQL definition.
/// </summary>
public string? ParameterSql { get; private set; }
/// <summary>
/// Gets the UDT SQL definition.
/// </summary>
public string? UdtSql { get; private set; }
/// <summary>
/// Indicates whether the column should be included on a delete.
/// </summary>
public bool IncludeColumnOnDelete { get; set; }
/// <summary>
/// Gets or sets the JSON name for the column.
/// </summary>
public string? JsonName { get; set; }
/// <summary>
/// Gets the where equality clause.
/// </summary>
public string WhereEquals => Name == Parent?.ColumnIsDeleted?.Name ? $"({QualifiedName} IS NULL OR {QualifiedName} = 0)" : $"{QualifiedName} = {ParameterName}";
/// <summary>
/// Gets the SQL for defining initial value for comparisons.
/// </summary>
public string SqlInitialValue => Root!.Migrator!.SchemaConfig.ToFormattedSqlStatementValue(DbColumn!,
IsDbTypeInteger || string.Equals("decimal", DbColumn!.DotNetType, StringComparison.OrdinalIgnoreCase) ? 0
: (string.Equals("Guid", DbColumn!.DotNetType, StringComparison.OrdinalIgnoreCase) ? Guid.Empty : string.Empty));
/// <summary>
/// Indicates whether the db type is an integer.
/// </summary>
public bool IsDbTypeInteger => _intTypes.Contains(DbColumn!.DotNetType, StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Indicates where the column is the "TenantId" column.
/// </summary>
public bool IsTenantIdColumn => Name == Parent!.ColumnTenantId?.Name;
/// <summary>
/// Indicates where the column is the "OrgUnitId" column.
/// </summary>
public bool IsOrgUnitIdColumn => Name == Parent!.ColumnOrgUnitId?.Name;
/// <summary>
/// Indicates where the column is the "RowVersion" column.
/// </summary>
public bool IsRowVersionColumn => Name == Parent!.ColumnRowVersion?.Name;
/// <summary>
/// Indicates where the column is the "IsDeleted" column.
/// </summary>
public bool IsIsDeletedColumn => Name == Parent!.ColumnIsDeleted?.Name;
/// <summary>
/// Indicates whether the column is considered an audit column.
/// </summary>
public bool IsAudit => IsCreated || IsUpdated || IsDeleted;
/// <summary>
/// Indicates whether the column is "CreatedBy" or "CreatedDate".
/// </summary>
public bool IsCreated => IsCreatedBy || IsCreatedDate;
/// <summary>
/// Indicates whether the column is "CreatedBy".
/// </summary>
public bool IsCreatedBy => Name == Parent!.ColumnCreatedBy?.Name;
/// <summary>
/// Indicates whether the column is "CreatedDate".
/// </summary>
public bool IsCreatedDate => Name == Parent!.ColumnCreatedDate?.Name;
/// <summary>
/// Indicates whether the column is "UpdatedBy" or "UpdatedDate".
/// </summary>
public bool IsUpdated => IsUpdatedBy || IsUpdatedDate;
/// <summary>
/// Indicates whether the column is "UpdatedBy".
/// </summary>
public bool IsUpdatedBy => Name == Parent!.ColumnUpdatedBy?.Name;
/// <summary>
/// Indicates whether the column is "UpdatedDate".
/// </summary>
public bool IsUpdatedDate => Name == Parent!.ColumnUpdatedDate?.Name;
/// <summary>
/// Indicates whether the column is "DeletedBy" or "DeletedDate".
/// </summary>
public bool IsDeleted => IsDeletedBy || IsDeletedDate;
/// <summary>
/// Indicates whether the column is "DeletedBy".
/// </summary>
public bool IsDeletedBy => Name == Parent!.ColumnDeletedBy?.Name;
/// <summary>
/// Indicates whether the column is "DeletedDate".
/// </summary>
public bool IsDeletedDate => Name == Parent!.ColumnDeletedDate?.Name;
/// <summary>
/// Indicates where the column should be considered for a 'Create' operation.
/// </summary>
public bool IsCreateColumn => (!DbColumn!.IsComputed && !IsAudit) || IsCreated;
/// <summary>
/// Indicates where the column should be considered for a 'Update' operation.
/// </summary>
public bool IsUpdateColumn => (!DbColumn!.IsComputed && !IsAudit) || IsUpdated;
/// <summary>
/// Indicates where the column should be considered for a 'Delete' operation.
/// </summary>
public bool IsDeleteColumn => (!DbColumn!.IsComputed && !IsAudit) || IsDeleted;
/// <summary>
/// Gets or sets the EF SQL Type.
/// </summary>
public string? EfSqlType { get; set; }
/// <summary>
/// Gets the corresponding .NET <see cref="System.Type"/> name.
/// </summary>
public string DotNetType => DbColumn!.DotNetType;
/// <summary>
/// Indicates whether the .NET property is nullable.
/// </summary>
public bool IsDotNetNullable => DbColumn!.IsNullable || DotNetType == "string" || DotNetType == "byte[]";
/// <summary>
/// Gets or sets the name alias.
/// </summary>
public string? NameAlias { get; set; }
/// <summary>
/// Gets the qualified name with the alias (used in a select).
/// </summary>
public string QualifiedNameWithAlias => string.IsNullOrEmpty(NameAlias) || NameAlias == Name ? QualifiedName : $"{QualifiedName} AS [{NameAlias}]";
/// <summary>
/// Indicates whether the column should not be serialized when creating an .NET entity equivalent.
/// </summary>
public bool IgnoreSerialization { get; set; }
/// <summary>
/// <inheritdoc/>
/// </summary>
protected override Task PrepareAsync()
{
NameAlias = DefaultWhereNull(NameAlias, () => Root!.RenameForDotNet(Name));
UpdateSqlProperties();
return Task.CompletedTask;
}
/// <summary>
/// Update the required SQL properties.
/// </summary>
private void UpdateSqlProperties()
{
EfSqlType = DbColumn!.DbTable.Migration.SchemaConfig.ToFormattedSqlType(DbColumn, false);
SqlType = DbColumn!.SqlType;
ParameterSql = $"{ParameterName} AS {SqlType}";
UdtSql = $"[{Name}] {SqlType}";
}
}
/// <summary>
/// Represents the <see cref="TableConfig"/> column configuration.
/// </summary>
public class TableColumnConfig : ColumnConfigBase<TableConfig> { }
/// <summary>
/// Represents the <see cref="QueryConfig"/> column configuration.
/// </summary>
public class QueryColumnConfig : ColumnConfigBase<QueryConfig> { }
/// <summary>
/// Represents the <see cref="QueryJoinConfig"/> column configuration.
/// </summary>
public class QueryJoinColumnConfig : ColumnConfigBase<QueryJoinConfig> { }
/// <summary>
/// Enables the Identifier Mapping column configuration.
/// </summary>
public interface IIdentifierMappingColumn<T> : IColumnConfig where T : class
{
/// <summary>
/// Gets or sets the identifier mapping schema name.
/// </summary>
string? IdentifierMappingSchema { get; set; }
/// <summary>
/// Gets or sets the identifier mapping table name.
/// </summary>
string? IdentifierMappingTable { get; set; }
/// <summary>
/// Gets or sets the identifier mapping alias.
/// </summary>
string? IdentifierMappingAlias { get; set; }
/// <summary>
/// Gets or sets the identifier mapping parent column configuration.
/// </summary>
T? IdentifierMappingParent { get; set; }
}
}