-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathDatabaseObject.cs
More file actions
464 lines (402 loc) · 16 KB
/
DatabaseObject.cs
File metadata and controls
464 lines (402 loc) · 16 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Data;
using System.Diagnostics;
using System.Text.Json.Serialization;
using Azure.DataApiBuilder.Config.ObjectModel;
namespace Azure.DataApiBuilder.Config.DatabasePrimitives;
/// <summary>
/// Represents a database object - which could be a view, table, or stored procedure.
/// </summary>
public abstract class DatabaseObject
{
public string SchemaName { get; set; } = null!;
public string Name { get; set; } = null!;
public EntitySourceType SourceType { get; set; } = EntitySourceType.Table;
public DatabaseObject(string schemaName, string tableName)
{
SchemaName = schemaName;
Name = tableName;
}
public DatabaseObject() { }
public string FullName
{
get
{
return string.IsNullOrEmpty(SchemaName) ? Name : $"{SchemaName}.{Name}";
}
}
public override bool Equals(object? other)
{
return Equals(other as DatabaseObject);
}
public bool Equals(DatabaseObject? other)
{
return other is not null &&
string.Equals(SchemaName, other.SchemaName, StringComparison.OrdinalIgnoreCase) &&
string.Equals(Name, other.Name, StringComparison.OrdinalIgnoreCase);
}
public override int GetHashCode()
{
return HashCode.Combine(
SchemaName is null ? 0 : StringComparer.OrdinalIgnoreCase.GetHashCode(SchemaName),
Name is null ? 0 : StringComparer.OrdinalIgnoreCase.GetHashCode(Name));
}
/// <summary>
/// Get the underlying SourceDefinition based on database object source type
/// </summary>
public virtual SourceDefinition SourceDefinition
{
get
{
return SourceType switch
{
EntitySourceType.Table => ((DatabaseTable)this).TableDefinition,
EntitySourceType.View => ((DatabaseView)this).ViewDefinition,
EntitySourceType.StoredProcedure => ((DatabaseStoredProcedure)this).StoredProcedureDefinition,
_ => throw new Exception(
message: $"Unsupported EntitySourceType. It can either be Table,View, or Stored Procedure.")
};
}
}
}
/// <summary>
/// Sub-class of DatabaseObject class, represents a table in the database.
/// </summary>
[DebuggerDisplay("Table: {FullName}")]
public class DatabaseTable : DatabaseObject
{
public DatabaseTable(string schemaName, string tableName)
: base(schemaName, tableName) { }
public DatabaseTable() { }
public SourceDefinition TableDefinition { get; set; } = null!;
}
/// <summary>
/// Sub-class of DatabaseObject class, represents a view in the database.
/// </summary>
public class DatabaseView : DatabaseObject
{
public DatabaseView(string schemaName, string tableName)
: base(schemaName, tableName) { }
public DatabaseView() { }
public ViewDefinition ViewDefinition { get; set; } = null!;
}
/// <summary>
/// Sub-class of DatabaseObject class, represents a stored procedure in the database.
/// </summary>
public class DatabaseStoredProcedure : DatabaseObject
{
public DatabaseStoredProcedure(string schemaName, string tableName)
: base(schemaName, tableName) { }
public DatabaseStoredProcedure() { }
public StoredProcedureDefinition StoredProcedureDefinition { get; set; } = null!;
}
public class StoredProcedureDefinition : SourceDefinition
{
/// <summary>
/// The list of input parameters
/// Key: parameter name, Value: ParameterDefinition object
/// </summary>
public Dictionary<string, ParameterDefinition> Parameters { get; set; } = new();
/// <inheritdoc/>
public override DbType? GetDbTypeForParam(string paramName)
{
if (Parameters.TryGetValue(paramName, out ParameterDefinition? paramDefinition))
{
return paramDefinition.DbType;
}
return null;
}
}
public class ParameterDefinition
{
public string Name { get; set; } = null!;
public bool? Required { get; set; } = false;
public string? Default { get; set; }
public string? Description { get; set; }
public Type SystemType { get; set; } = null!;
public DbType? DbType { get; set; }
public SqlDbType? SqlDbType { get; set; }
public bool HasConfigDefault { get; set; }
public object? ConfigDefaultValue { get; set; }
}
/// <summary>
/// Class to store database table definition. It contains properties that are
/// common between a database table and a view.
/// </summary>
public class SourceDefinition
{
/// <summary>
/// The list of columns that together form the primary key of the source.
/// </summary>
public List<string> PrimaryKey { get; set; } = new();
/// <summary>
/// The list of columns in this source.
/// </summary>
[JsonInclude]
public Dictionary<string, ColumnDefinition> Columns { get; private set; } =
new(StringComparer.InvariantCultureIgnoreCase);
/// <summary>
/// A dictionary mapping all the source entities to their relationship metadata.
/// All these entities share this source definition
/// as their underlying database object.
/// </summary>
[JsonInclude]
public Dictionary<string, RelationshipMetadata> SourceEntityRelationshipMap { get; private set; } =
new(StringComparer.InvariantCultureIgnoreCase);
/// <summary>
/// Indicates whether an update trigger enabled on the table.
/// The default value must be kept as false, meaning by default we assume no trigger is enabled.
/// Based on whether trigger is enabled, we use either OUTPUT (when no trigger is enabled) / or SELECT (when a trigger is enabled),
/// to return the data after a mutation operation.
/// </summary>
public bool IsUpdateDMLTriggerEnabled;
/// <summary>
/// Indicates whether an insert trigger enabled on the table.
/// The default value must be kept as false, meaning by default we assume no trigger is enabled.
/// Based on whether trigger is enabled, we use either OUTPUT (when no trigger is enabled) / or SELECT (when a trigger is enabled),
/// to return the data after a mutation operation.
public bool IsInsertDMLTriggerEnabled;
/// <summary>
/// Given the list of column names to check, evaluates
/// if any of them is a nullable column when matched with the columns in this source definition.
/// </summary>
/// <param name="columnsToCheck">List of column names.</param>
/// <returns>True if any of the columns is null, false otherwise.</returns>
public bool IsAnyColumnNullable(List<string> columnsToCheck)
{
// If any of the given columns are nullable, the relationship is nullable.
return columnsToCheck.Select(column =>
Columns.TryGetValue(column, out ColumnDefinition? definition) && definition.IsNullable)
.Where(isNullable => isNullable == true)
.Any();
}
/// <summary>
/// Method to get the DbType for:
/// 1. column for table/view,
/// 2. parameter for stored procedure.
/// </summary>
/// <param name="paramName">The parameter whose DbType is to be determined.
/// For table/view paramName refers to the backingColumnName if aliases are used.</param>
/// <returns>DbType for the parameter.</returns>
public virtual DbType? GetDbTypeForParam(string paramName)
{
if (Columns.TryGetValue(paramName, out ColumnDefinition? columnDefinition))
{
return columnDefinition.DbType;
}
return null;
}
/// <summary>
/// Method to get the SqlDbType for:
/// 1. column for table/view,
/// 2. parameter for stored procedure.
/// </summary>
/// <param name="paramName">The parameter whose SqlDbType is to be determined.
/// For table/view paramName refers to the backingColumnName if aliases are used.</param>
/// <returns>SqlDbType for the parameter.</returns>
public virtual SqlDbType? GetSqlDbTypeForParam(string paramName)
{
if (Columns.TryGetValue(paramName, out ColumnDefinition? columnDefinition))
{
return columnDefinition.SqlDbType;
}
return null;
}
public virtual int? GetLengthForParam(string paramName)
{
if (Columns.TryGetValue(paramName, out ColumnDefinition? columnDefinition))
{
return columnDefinition.Length;
}
return null;
}
}
/// <summary>
/// Class to store the database view definition.
/// </summary>
public class ViewDefinition : SourceDefinition { }
/// <summary>
/// Class encapsulating foreign keys corresponding to target entities.
/// </summary>
public class RelationshipMetadata
{
/// <summary>
/// Dictionary tracking ForeignKeyDefinitions for a given target entity.
/// key: {targetEntityName}
/// value: [{ForeignKeyDefinition},{...}]
/// </summary>
[JsonInclude]
public Dictionary<string, List<ForeignKeyDefinition>> TargetEntityToFkDefinitionMap { get; private set; }
= new(StringComparer.InvariantCultureIgnoreCase);
}
public class ColumnDefinition
{
/// <summary>
/// The database type of this column mapped to the SystemType.
/// </summary>
public Type SystemType { get; set; } = typeof(object);
public DbType? DbType { get; set; }
public SqlDbType? SqlDbType { get; set; }
public bool HasDefault { get; set; }
public bool IsAutoGenerated { get; set; }
public bool IsNullable { get; set; }
public bool IsReadOnly { get; set; }
public object? DefaultValue { get; set; }
public int? Length { get; set; }
/// <summary>
/// Indicates whether this column is a database array type (e.g., PostgreSQL int[], text[]).
/// </summary>
public bool IsArrayType { get; set; }
/// <summary>
/// The CLR type of the array element when <see cref="IsArrayType"/> is true.
/// For example, typeof(int) for an int[] column.
/// </summary>
public Type? ElementSystemType { get; set; }
public ColumnDefinition() { }
public ColumnDefinition(Type systemType)
{
SystemType = systemType;
}
}
[DebuggerDisplay("Relationship: {RelationshipName} ReferencingDbTable = {Pair.ReferencingDbTable.FullName} (Count = {ReferencingColumns.Count}), ReferencedDbTable = {Pair.ReferencedDbTable.FullName} (Count = {ReferencedColumns.Count})")]
public class ForeignKeyDefinition
{
public string SourceEntityName { get; set; } = string.Empty;
/// <summary>
/// Identifies whether the referencing entity is the target, source, or linking entity.
/// Enables Resolve{Target/Source}Columns() functions to return the correct columns.
/// </summary>
public RelationshipRole ReferencingEntityRole { get; init; } = RelationshipRole.None;
/// <summary>
/// Identifies whether the referenced entity is the target, source, or linking entity.
/// Enables Resolve{Target/Source}Columns() functions to return the correct columns.
/// </summary>
public RelationshipRole ReferencedEntityRole { get; init; } = RelationshipRole.None;
/// <summary>
/// The relationship name defined for the relationship in the runtime config.
/// </summary>
public string RelationshipName { get; init; } = string.Empty;
/// <summary>
/// The referencing and referenced table pair.
/// </summary>
public RelationShipPair Pair { get; set; } = new();
/// <summary>
/// The list of columns referenced in the reference table.
/// If this list is empty, the primary key columns of the referenced
/// table are implicitly assumed to be the referenced columns.
/// </summary>
public List<string> ReferencedColumns { get; set; } = new();
/// <summary>
/// The list of columns of the table that make up the foreign key.
/// If this list is empty, the primary key columns of the referencing
/// table are implicitly assumed to be the foreign key columns.
/// </summary>
public List<string> ReferencingColumns { get; set; } = new();
/// <summary>
/// Resolves the target columns based on the role of the referencing and referenced entities.
/// The role of the referencing and referenced entities is important because
/// target entity can either be the "referenced" or "referencing" entity.
/// </summary>
/// <returns>List of the target entity's columns</returns>
/// <exception cref="Exception">Raised when the ForeignKeyDefinition represents a relationship
/// between a source (source fields) and linking object (linking source fields).</exception>
public List<string> ResolveTargetColumns()
{
if (ReferencingEntityRole == RelationshipRole.Target)
{
return ReferencingColumns;
}
else if (ReferencedEntityRole == RelationshipRole.Target)
{
return ReferencedColumns;
}
else
{
throw new Exception(
message: "Unable to resolve target columns because this ForeignKeyDefinition relates a target entity and linking object.");
}
}
/// <summary>
/// Resolves the source columns based on the role of the referencing and referenced entities.
/// The role of the referencing and referenced entities is important because
/// source entity can either be the "referenced" or "referencing" entity.
/// </summary>
/// <returns>List of the source entity's columns</returns>
/// <exception cref="Exception">Raised when the ForeignKeyDefinition represents a relationship
/// between a target (target columns) and linking object (linking target fields).</exception>
public List<string> ResolveSourceColumns()
{
if (ReferencingEntityRole == RelationshipRole.Source)
{
return ReferencingColumns;
}
else if (ReferencedEntityRole == RelationshipRole.Source)
{
return ReferencedColumns;
}
else
{
throw new Exception(
message: "Unable to resolve source columns because this ForeignKeyDefinition relates a source entity and linking object.");
}
}
public override bool Equals(object? other)
{
return Equals(other as ForeignKeyDefinition);
}
public bool Equals(ForeignKeyDefinition? other)
{
return other != null &&
Pair.Equals(other.Pair) &&
ReferencedColumns.SequenceEqual(other.ReferencedColumns) &&
ReferencingColumns.SequenceEqual(other.ReferencingColumns);
}
public override int GetHashCode()
{
return HashCode.Combine(
Pair, ReferencedColumns, ReferencingColumns);
}
}
[DebuggerDisplay("ReferencingDbTable = {ReferencingDbTable.FullName}, ReferencedDbTable = {ReferencedDbTable.FullName}")]
public class RelationShipPair
{
/// <summary>
/// Relationship name specified in the runtime config.
/// </summary>
public string RelationshipName { get; set; } = string.Empty;
public RelationShipPair() { }
public RelationShipPair(
DatabaseTable referencingDbObject,
DatabaseTable referencedDbObject)
{
ReferencingDbTable = referencingDbObject;
ReferencedDbTable = referencedDbObject;
}
public RelationShipPair(
string relationshipName,
DatabaseTable referencingDbObject,
DatabaseTable referencedDbObject)
{
RelationshipName = relationshipName;
ReferencingDbTable = referencingDbObject;
ReferencedDbTable = referencedDbObject;
}
public DatabaseTable ReferencingDbTable { get; set; } = new();
public DatabaseTable ReferencedDbTable { get; set; } = new();
public override bool Equals(object? other)
{
return Equals(other as RelationShipPair);
}
public bool Equals(RelationShipPair? other)
{
return other != null &&
ReferencedDbTable.Equals(other.ReferencedDbTable) &&
ReferencingDbTable.Equals(other.ReferencingDbTable);
}
public override int GetHashCode()
{
return HashCode.Combine(
ReferencedDbTable, ReferencingDbTable);
}
}