-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathQueryJoinConfig.cs
More file actions
392 lines (335 loc) · 17 KB
/
Copy pathQueryJoinConfig.cs
File metadata and controls
392 lines (335 loc) · 17 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
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef
using DbEx.DbSchema;
using OnRamp;
using OnRamp.Config;
using OnRamp.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace Beef.CodeGen.Config.Database
{
/// <summary>
/// Represents the table join configuration.
/// </summary>
[CodeGenClass("QueryJoin", Title = "'QueryJoin' object (database-driven)",
Description = "The `QueryJoin` object defines a join to another (or same) table within a query. The `Type` defines the join type, such as inner join, etc."
+ " The `IncludeColumns` and `ExcludeColumns` provide a shorthand to include or exclude selected columns; with the `AliasColumns` providing a means to rename where required (for example duplicate name).",
ExampleMarkdown = @"A YAML configuration example is as follows:
``` yaml
queries:
- { name: Table, schema: Test, view: true, viewName: vwTestQuery, excludeColumns: [CreatedBy, UpdatedBy], permission: TestSec,
joins: [
{ name: Person, schema: Demo, excludeColumns: [CreatedDate, UpdatedDate], aliasColumns: [RowVersion ^ RowVersionP],
on: [
{ name: PersonId, toColumn: TableId }
]
}
]
}
```")]
[CodeGenCategory("Key", Title = "Provides the _key_ configuration.")]
[CodeGenCategory("Columns", Title = "Provides the _Columns_ configuration.")]
[CodeGenCategory("Infer", Title = "Provides the _special Column Name inference_ configuration.")]
public class QueryJoinConfig : ConfigBase<CodeGenConfig, QueryConfig>, ITableReference, ISpecialColumnNames, ISpecialColumns
{
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <remarks><inheritdoc/></remarks>
public override string? QualifiedKeyName => BuildQualifiedKeyName("QueryJoin", Name);
#region Key
/// <summary>
/// Gets or sets the name of the table to join.
/// </summary>
[JsonPropertyName("name")]
[CodeGenProperty("Key", Title = "The name of the table to join.", IsMandatory = true, IsImportant = true)]
public string? Name { get; set; }
/// <summary>
/// Gets or sets the schema name of the table to join.
/// </summary>
[JsonPropertyName("schema")]
[CodeGenProperty("Key", Title = "The schema name of the table to join.",
Description = "Defaults to `Table.Schema`; i.e. same schema.")]
public string? Schema { get; set; }
/// <summary>
/// Gets or sets the `Schema.Table` alias name.
/// </summary>
[JsonPropertyName("alias")]
[CodeGenProperty("Key", Title = "The `Schema.Table` alias name.",
Description = "Will automatically default where not specified.")]
public string? Alias { get; set; }
/// <summary>
/// Gets or sets the join type option.
/// </summary>
[JsonPropertyName("type")]
[CodeGenProperty("Key", Title = "The SQL join type.", IsImportant = true, Options = ["Inner", "Left", "Right", "Full"],
Description = "Defaults to `Inner`.")]
public string? Type { get; set; }
#endregion
#region Columns
/// <summary>
/// Gets or sets the list of `Column` names to be included in the underlying generated output.
/// </summary>
[JsonPropertyName("includeColumns")]
[CodeGenPropertyCollection("Columns", Title = "The list of `Column` names to be included in the underlying generated output.", IsImportant = true,
Description = "Where not specified this indicates that all `Columns` are to be included.")]
public List<string>? IncludeColumns { get; set; }
/// <summary>
/// Gets or sets the list of `Column` names to be excluded from the underlying generated output.
/// </summary>
[JsonPropertyName("excludeColumns")]
[CodeGenPropertyCollection("Columns", Title = "The list of `Column` names to be excluded from the underlying generated output.", IsImportant = true,
Description = "Where not specified this indicates no `Columns` are to be excluded.")]
public List<string>? ExcludeColumns { get; set; }
/// <summary>
/// Gets or sets the list of `Column` and `Alias` pairs to enable column renaming.
/// </summary>
[JsonPropertyName("aliasColumns")]
[CodeGenPropertyCollection("Columns", Title = "The list of `Column` and `Alias` pairs (split by a `^` lookup character) to enable column renaming.", IsImportant = true,
Description = "Each alias value should be formatted as `Column` + `^` + `Alias`; e.g. `PCODE^ProductCode`")]
public List<string>? AliasColumns { get; set; }
#endregion
#region Infer
/// <summary>
/// Gets or sets the column name for the `IsDeleted` capability.
/// </summary>
[JsonPropertyName("columnNameIsDeleted")]
[CodeGenProperty("Infer", Title = "The column name for the `IsDeleted` capability.",
Description = "Defaults to `CodeGeneration.IsDeleted`.")]
public string? ColumnNameIsDeleted { get; set; }
/// <summary>
/// Gets or sets the column name for the `TenantId` capability.
/// </summary>
[JsonPropertyName("columnNameTenantId")]
[CodeGenProperty("Infer", Title = "The column name for the `TenantId` capability.",
Description = "Defaults to `CodeGeneration.TenantId`.")]
public string? ColumnNameTenantId { get; set; }
/// <summary>
/// Gets or sets the column name for the `OrgUnitId` capability.
/// </summary>
[JsonPropertyName("columnNameOrgUnitId")]
[CodeGenProperty("Infer", Title = "The column name for the `OrgUnitId` capability.",
Description = "Defaults to `CodeGeneration.OrgUnitId`.")]
public string? ColumnNameOrgUnitId { get; set; }
/// <summary>
/// Gets or sets the column name for the `RowVersion` capability.
/// </summary>
[JsonPropertyName("columnNameRowVersion")]
[CodeGenProperty("Infer", Title = "The column name for the `RowVersion` capability.",
Description = "Defaults to `CodeGeneration.RowVersion`.")]
public string? ColumnNameRowVersion { get; set; }
/// <summary>
/// Gets or sets the column name for the `CreatedBy` capability.
/// </summary>
[JsonPropertyName("columnNameCreatedBy")]
[CodeGenProperty("Infer", Title = "The column name for the `CreatedBy` capability.",
Description = "Defaults to `CodeGeneration.CreatedBy`.")]
public string? ColumnNameCreatedBy { get; set; }
/// <summary>
/// Gets or sets the column name for the `CreatedDate` capability.
/// </summary>
[JsonPropertyName("columnNameCreatedDate")]
[CodeGenProperty("Infer", Title = "The column name for the `CreatedDate` capability.",
Description = "Defaults to `CodeGeneration.CreatedDate`.")]
public string? ColumnNameCreatedDate { get; set; }
/// <summary>
/// Gets or sets the column name for the `UpdatedBy` capability.
/// </summary>
[JsonPropertyName("columnNameUpdatedBy")]
[CodeGenProperty("Infer", Title = "The column name for the `UpdatedBy` capability.",
Description = "Defaults to `CodeGeneration.UpdatedBy`.")]
public string? ColumnNameUpdatedBy { get; set; }
/// <summary>
/// Gets or sets the column name for the `UpdatedDate` capability.
/// </summary>
[JsonPropertyName("columnNameUpdatedDate")]
[CodeGenProperty("Infer", Title = "The column name for the `UpdatedDate` capability.",
Description = "Defaults to `CodeGeneration.UpdatedDate`.")]
public string? ColumnNameUpdatedDate { get; set; }
/// <summary>
/// Gets or sets the column name for the `DeletedBy` capability.
/// </summary>
[JsonPropertyName("columnNameDeletedBy")]
[CodeGenProperty("Infer", Title = "The column name for the `DeletedBy` capability.",
Description = "Defaults to `CodeGeneration.UpdatedBy`.")]
public string? ColumnNameDeletedBy { get; set; }
/// <summary>
/// Gets or sets the column name for the `DeletedDate` capability.
/// </summary>
[JsonPropertyName("columnNameDeletedDate")]
[CodeGenProperty("Infer", Title = "The column name for the `DeletedDate` capability.",
Description = "Defaults to `CodeGeneration.UpdatedDate`.")]
public string? ColumnNameDeletedDate { get; set; }
#endregion
/// <summary>
/// Gets or sets the corresponding <see cref="QueryJoinOnConfig"/> collection.
/// </summary>
[JsonPropertyName("on")]
[CodeGenPropertyCollection("Collections", Title = "The corresponding `JoinOn` collection.")]
public List<QueryJoinOnConfig>? On { get; set; }
/// <summary>
/// Gets all the other joins excluding this.
/// </summary>
public List<QueryJoinConfig> OtherJoins => Parent!.Joins!.Where(x => x != this).ToList();
/// <summary>
/// Gets the selected column configurations.
/// </summary>
public List<QueryJoinColumnConfig> Columns { get; } = [];
/// <summary>
/// Gets the related IsDeleted column.
/// </summary>
public IColumnConfig? ColumnIsDeleted { get; private set; }
/// <summary>
/// Gets the related TenantId column.
/// </summary>
public IColumnConfig? ColumnTenantId { get; private set; }
/// <summary>
/// Gets the related OrgUnitId column.
/// </summary>
public IColumnConfig? ColumnOrgUnitId { get; private set; }
/// <summary>
/// Gets the related RowVersion column.
/// </summary>
public IColumnConfig? ColumnRowVersion { get; private set; }
/// <summary>
/// Gets the related CreatedBy column.
/// </summary>
public IColumnConfig? ColumnCreatedBy { get; private set; }
/// <summary>
/// Gets the related CreatedDate column.
/// </summary>
public IColumnConfig? ColumnCreatedDate { get; private set; }
/// <summary>
/// Gets the related UpdatedBy column.
/// </summary>
public IColumnConfig? ColumnUpdatedBy { get; private set; }
/// <summary>
/// Gets the related UpdatedDate column.
/// </summary>
public IColumnConfig? ColumnUpdatedDate { get; private set; }
/// <summary>
/// Gets the related DeletedBy column.
/// </summary>
public IColumnConfig? ColumnDeletedBy { get; private set; }
/// <summary>
/// Gets the related DeletedDate column.
/// </summary>
public IColumnConfig? ColumnDeletedDate { get; private set; }
/// <summary>
/// Gets the table name.
/// </summary>
public string? Table => Name;
/// <summary>
/// Gets the corresponding (actual) database table configuration.
/// </summary>
public DbTableSchema? DbTable { get; private set; }
/// <summary>
/// Gets the list of primary key columns.
/// </summary>
public List<QueryJoinColumnConfig> PrimaryKeyColumns { get; } = [];
/// <summary>
/// Gets the Join table qualified name.
/// </summary>
public string QualifiedName => $"[{Schema}].[{Name}]";
/// <summary>
/// Gets the join <see cref="Type"/> as SQL.
/// </summary>
public string JoinTypeSql => Type?.ToUpperInvariant() switch
{
"LEFT" => "LEFT OUTER JOIN",
"RIGHT" => "RIGHT OUTER JOIN",
"FULL" => "FULL OUTER JOIN",
_ => "INNER JOIN"
};
/// <summary>
/// <inheritdoc/>
/// </summary>
protected override async Task PrepareAsync()
{
if (Name != null && Name.StartsWith("@", StringComparison.OrdinalIgnoreCase))
Name = Name[1..];
Schema = DefaultWhereNull(Schema, () => Parent!.Schema);
DbTable = Root!.DbTables!.Where(x => x.Name == Name && x.Schema == Schema).SingleOrDefault();
if (DbTable == null)
throw new CodeGenException(this, nameof(Name), $"Specified Schema.Table '{Schema}.{Name}' not found in database.");
Alias = DefaultWhereNull(Alias, () => new string(StringConverter.ToSentenceCase(Name)!.Split(' ').Select(x => x.Substring(0, 1).ToLower(System.Globalization.CultureInfo.InvariantCulture).ToCharArray()[0]).ToArray()));
ColumnNameIsDeleted = DefaultWhereNull(ColumnNameIsDeleted, () => Parent!.ColumnNameIsDeleted);
ColumnNameTenantId = DefaultWhereNull(ColumnNameTenantId, () => Parent!.ColumnNameTenantId);
ColumnNameOrgUnitId = DefaultWhereNull(ColumnNameOrgUnitId, () => Parent!.ColumnNameOrgUnitId);
ColumnNameRowVersion = DefaultWhereNull(ColumnNameRowVersion, () => Parent!.ColumnNameRowVersion);
ColumnNameCreatedBy = DefaultWhereNull(ColumnNameCreatedBy, () => Parent!.ColumnNameCreatedBy);
ColumnNameCreatedDate = DefaultWhereNull(ColumnNameCreatedDate, () => Parent!.ColumnNameCreatedDate);
ColumnNameUpdatedBy = DefaultWhereNull(ColumnNameUpdatedBy, () => Parent!.ColumnNameUpdatedBy);
ColumnNameUpdatedDate = DefaultWhereNull(ColumnNameUpdatedDate, () => Parent!.ColumnNameUpdatedDate);
ColumnNameDeletedBy = DefaultWhereNull(ColumnNameDeletedBy, () => Parent!.ColumnNameDeletedBy);
ColumnNameDeletedDate = DefaultWhereNull(ColumnNameDeletedDate, () => Parent!.ColumnNameDeletedDate);
await PrepareSpecialColumnsAsync().ConfigureAwait(false);
foreach (var c in DbTable.Columns)
{
if (c.IsPrimaryKey)
{
var cc = new QueryJoinColumnConfig { Name = c.Name, DbColumn = c };
await cc.PrepareAsync(Root!, this).ConfigureAwait(false);
PrimaryKeyColumns.Add(cc);
}
if ((ExcludeColumns == null || !ExcludeColumns.Contains(c.Name!)) && (IncludeColumns == null || IncludeColumns.Contains(c.Name!)))
{
var cc = new QueryJoinColumnConfig { Name = c.Name, DbColumn = c };
var ca = AliasColumns?.Where(x => x.StartsWith(c.Name + "^", StringComparison.Ordinal)).FirstOrDefault();
if (ca != null)
{
var parts = ca.Split("^", StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 2)
cc.NameAlias = parts[1];
}
await cc.PrepareAsync(Root!, this).ConfigureAwait(false);
Columns.Add(cc);
}
}
}
/// <summary>
/// Perform the JoinOn preparation.
/// </summary>
public async Task PrepareJoinOnAsync()
{
On ??= [];
foreach (var on in On)
{
await on.PrepareAsync(Root!, this).ConfigureAwait(false);
}
}
/// <summary>
/// Prepare the special columns.
/// </summary>
/// <returns></returns>
private async Task PrepareSpecialColumnsAsync()
{
ColumnIsDeleted = await GetSpecialColumnAsync(ColumnNameIsDeleted).ConfigureAwait(false);
ColumnTenantId = await GetSpecialColumnAsync(ColumnNameTenantId).ConfigureAwait(false);
ColumnOrgUnitId = await GetSpecialColumnAsync(ColumnNameOrgUnitId).ConfigureAwait(false);
ColumnRowVersion = await GetSpecialColumnAsync(ColumnNameRowVersion).ConfigureAwait(false);
ColumnCreatedBy = await GetSpecialColumnAsync(ColumnNameCreatedBy).ConfigureAwait(false);
ColumnCreatedDate = await GetSpecialColumnAsync(ColumnNameCreatedDate).ConfigureAwait(false);
ColumnUpdatedBy = await GetSpecialColumnAsync(ColumnNameUpdatedBy).ConfigureAwait(false);
ColumnUpdatedDate = await GetSpecialColumnAsync(ColumnNameUpdatedDate).ConfigureAwait(false);
ColumnDeletedBy = await GetSpecialColumnAsync(ColumnNameDeletedBy).ConfigureAwait(false);
ColumnDeletedDate = await GetSpecialColumnAsync(ColumnNameDeletedDate).ConfigureAwait(false);
}
/// <summary>
/// Gets the named special column.
/// </summary>
private async Task<IColumnConfig?> GetSpecialColumnAsync(string? name)
{
if (string.IsNullOrEmpty(name))
return null;
var c = DbTable!.Columns.Where(x => x.Name == name && !x.IsPrimaryKey).SingleOrDefault();
if (c == null)
return null;
var cc = new QueryJoinColumnConfig { Name = c.Name, DbColumn = c };
await cc.PrepareAsync(Root!, this).ConfigureAwait(false);
return cc;
}
}
}