-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathSqlServerDatabaseModelFactory.cs
733 lines (647 loc) · 31.5 KB
/
SqlServerDatabaseModelFactory.cs
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Diagnostics;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;
namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public class SqlServerDatabaseModelFactory : IDatabaseModelFactory
{
private DbConnection _connection;
private Version _serverVersion;
private TableSelectionSet _tableSelectionSet;
private DatabaseModel _databaseModel;
private Dictionary<string, TableModel> _tables;
private Dictionary<string, ColumnModel> _tableColumns;
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public static string SchemaQualifiedKey([NotNull] string name, [CanBeNull] string schema = null) => "[" + (schema ?? "") + "].[" + name + "]";
private static string TableKey(TableModel table) => SchemaQualifiedKey(table.Name, table.SchemaName);
private static string ColumnKey(TableModel table, string columnName) => TableKey(table) + ".[" + columnName + "]";
private static readonly ISet<string> _dateTimePrecisionTypes = new HashSet<string> { "datetimeoffset", "datetime2", "time" };
private const int DefaultDateTimePrecision = 7;
// see https://msdn.microsoft.com/en-us/library/ff878091.aspx
private static readonly Dictionary<string, long[]> _defaultSequenceMinMax = new Dictionary<string, long[]>(StringComparer.OrdinalIgnoreCase)
{
{ "tinyint", new[] { 0L, 255L } },
{ "smallint", new[] { -32768L, 32767L } },
{ "int", new[] { -2147483648L, 2147483647L } },
{ "bigint", new[] { -9223372036854775808L, 9223372036854775807L } },
{ "decimal", new[] { -999999999999999999L, 999999999999999999L } },
{ "numeric", new[] { -999999999999999999L, 999999999999999999L } }
};
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public SqlServerDatabaseModelFactory([NotNull] IDiagnosticsLogger<DbLoggerCategory.Scaffolding> logger)
{
Check.NotNull(logger, nameof(logger));
Logger = logger;
}
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual IDiagnosticsLogger<DbLoggerCategory.Scaffolding> Logger { get; }
private void ResetState()
{
_connection = null;
_serverVersion = null;
_tableSelectionSet = null;
_databaseModel = new DatabaseModel();
_tables = new Dictionary<string, TableModel>();
_tableColumns = new Dictionary<string, ColumnModel>(StringComparer.OrdinalIgnoreCase);
}
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual DatabaseModel Create(string connectionString, TableSelectionSet tableSelectionSet)
{
Check.NotEmpty(connectionString, nameof(connectionString));
Check.NotNull(tableSelectionSet, nameof(tableSelectionSet));
using (var connection = new SqlConnection(connectionString))
{
return Create(connection, tableSelectionSet);
}
}
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
private DatabaseModel Create(DbConnection connection, TableSelectionSet tableSelectionSet)
{
ResetState();
_connection = connection;
var connectionStartedOpen = _connection.State == ConnectionState.Open;
if (!connectionStartedOpen)
{
_connection.Open();
}
try
{
_tableSelectionSet = tableSelectionSet;
_databaseModel.DatabaseName = _connection.Database;
Version.TryParse(_connection.ServerVersion, out _serverVersion);
if (SupportsSequences)
{
GetSequences();
}
GetDefaultSchema();
GetTypeAliases();
GetTables();
GetColumns();
GetIndexes();
GetForeignKeys();
return _databaseModel;
}
finally
{
if (!connectionStartedOpen)
{
_connection.Close();
}
}
}
private bool SupportsSequences => _serverVersion?.Major >= 11;
private string MemoryOptimizedTableColumn =>
_serverVersion?.Major >= 12
? @",
t.is_memory_optimized"
: string.Empty;
private string TemporalTableWhereClause =>
_serverVersion?.Major >= 13 ? " AND t.temporal_type <> 1" : string.Empty;
private string IsHiddenColumnWhereClause =>
_serverVersion?.Major >= 13 ? " AND c.is_hidden = 0" : string.Empty;
private void GetDefaultSchema()
{
var command = _connection.CreateCommand();
command.CommandText = "SELECT SCHEMA_NAME()";
if (command.ExecuteScalar() is string schema)
{
Logger.DefaultSchemaFound(schema);
_databaseModel.DefaultSchemaName = schema;
}
}
private void GetTypeAliases()
{
var command = _connection.CreateCommand();
command.CommandText = @"SELECT
[schema_name],
[type_name],
[underlying_system_type]
FROM
(SELECT
s1.[name] as [schema_name],
t1.[name] as [type_name],
( CASE WHEN t1.[xusertype] = t1.[xtype] THEN NULL
ELSE
( SELECT t2.[name]
FROM [sys].[systypes] AS t2
WHERE t2.[xusertype] = t2.[xtype]
AND t2.[xusertype] = t1.[xtype] )
END) as [underlying_system_type]
FROM [sys].[systypes] AS t1
LEFT JOIN [sys].[types] AS t3
ON t1.[xusertype] = t3.[user_type_id] AND t1.[xtype] = t3.[system_type_id]
LEFT JOIN [sys].[schemas] AS s1
ON t3.[schema_id] = s1.[schema_id]
) AS t
WHERE [underlying_system_type] IS NOT NULL";
var typeAliasMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var aliasSchema = reader.GetValueOrDefault<string>("schema_name");
var alias = reader.GetValueOrDefault<string>("type_name");
var underlyingSystemType = reader.GetValueOrDefault<string>("underlying_system_type");
Logger.TypeAliasFound(DisplayName(aliasSchema, alias), underlyingSystemType);
typeAliasMap.Add(SchemaQualifiedKey(alias, aliasSchema), underlyingSystemType);
}
}
_databaseModel.SqlServer().TypeAliases = typeAliasMap;
}
private void GetSequences()
{
var command = _connection.CreateCommand();
command.CommandText = @"SELECT name,
is_cycling,
CAST(minimum_value AS bigint) as [minimum_value],
CAST(maximum_value AS bigint) as [maximum_value],
CAST(start_value AS bigint) as [start_value],
CAST(increment AS int) as [increment],
TYPE_NAME(user_type_id) as [type_name],
OBJECT_SCHEMA_NAME(object_id) AS [schema_name]
FROM sys.sequences";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var sequence = new SequenceModel
{
Database = _databaseModel,
SchemaName = reader.GetValueOrDefault<string>("schema_name"),
Name = reader.GetValueOrDefault<string>("name"),
DataType = reader.GetValueOrDefault<string>("type_name"),
IsCyclic = reader.GetValueOrDefault<bool?>("is_cycling"),
IncrementBy = reader.GetValueOrDefault<int?>("increment"),
Start = reader.GetValueOrDefault<long?>("start_value"),
Min = reader.GetValueOrDefault<long?>("minimum_value"),
Max = reader.GetValueOrDefault<long?>("maximum_value")
};
Logger.SequenceFound(
sequence.DisplayName, sequence.DataType, sequence.IsCyclic,
sequence.IncrementBy, sequence.Start, sequence.Min, sequence.Max);
if (string.IsNullOrEmpty(sequence.Name))
{
Logger.SequenceNotNamedWarning();
continue;
}
if (_defaultSequenceMinMax.ContainsKey(sequence.DataType))
{
var defaultMin = _defaultSequenceMinMax[sequence.DataType][0];
sequence.Min = sequence.Min == defaultMin ? null : sequence.Min;
sequence.Start = sequence.Start == defaultMin ? null : sequence.Start;
var defaultMax = _defaultSequenceMinMax[sequence.DataType][1];
sequence.Max = sequence.Max == defaultMax ? null : sequence.Max;
}
_databaseModel.Sequences.Add(sequence);
}
}
}
private void GetTables()
{
var command = _connection.CreateCommand();
// for origin of the sys.extended_properties SELECT statement
// below see https://github.com/aspnet/EntityFramework/issues/5126
command.CommandText =
@"SELECT
schema_name(t.schema_id) AS [schema],
t.name" + MemoryOptimizedTableColumn + @"
FROM sys.tables AS t
WHERE t.is_ms_shipped = 0
AND NOT EXISTS (SELECT *
FROM sys.extended_properties
WHERE major_id = t.object_id
AND minor_id = 0
AND class = 1
AND name = N'microsoft_database_tools_support') " +
$"AND t.name <> '{HistoryRepository.DefaultTableName}'" + TemporalTableWhereClause; // Interpolation okay; strings
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var table = new TableModel
{
Database = _databaseModel,
SchemaName = reader.GetValueOrDefault<string>("schema"),
Name = reader.GetValueOrDefault<string>("name")
};
if (!string.IsNullOrEmpty(MemoryOptimizedTableColumn))
{
var isTableMemoryOptimized = reader.GetValueOrDefault<bool?>("is_memory_optimized");
if (isTableMemoryOptimized == true)
{
table[SqlServerAnnotationNames.MemoryOptimized] = true;
}
}
Logger.TableFound(table.DisplayName);
if (_tableSelectionSet.Allows(table.SchemaName, table.Name))
{
_databaseModel.Tables.Add(table);
_tables[TableKey(table)] = table;
}
else
{
Logger.TableSkipped(table.DisplayName);
}
}
}
}
private void GetColumns()
{
var command = _connection.CreateCommand();
command.CommandText = @"SELECT DISTINCT
schema_name(t.schema_id) AS [schema],
t.name AS [table],
type_name(c.user_type_id) AS [typename],
s.[name] as [datatype_schema_name],
c.name AS [column_name],
c.column_id AS [ordinal],
c.is_nullable AS [nullable],
CAST(ic.key_ordinal AS int) AS [primary_key_ordinal],
object_definition(c.default_object_id) AS [default_sql],
cc.definition AS [computed_sql],
CAST(CASE WHEN c.precision <> tp.precision
THEN c.precision
ELSE null
END AS int) AS [precision],
CAST(CASE WHEN c.scale <> tp.scale
THEN c.scale
ELSE null
END AS int) AS [scale],
CAST(CASE WHEN c.max_length <> tp.max_length
THEN c.max_length
ELSE null
END AS int) AS [max_length],
c.is_identity,
c.is_computed
FROM sys.index_columns ic
RIGHT JOIN (SELECT * FROM sys.indexes WHERE is_primary_key = 1) AS i ON i.object_id = ic.object_id AND i.index_id = ic.index_id
RIGHT JOIN sys.columns c ON ic.object_id = c.object_id AND c.column_id = ic.column_id
RIGHT JOIN sys.types tp ON tp.user_type_id = c.user_type_id
LEFT JOIN sys.schemas s ON s.[schema_id] = tp.[schema_id]
LEFT JOIN sys.computed_columns cc ON cc.object_id = c.object_id AND cc.column_id = c.column_id
JOIN sys.tables AS t ON t.object_id = c.object_id
WHERE t.name <> '" + HistoryRepository.DefaultTableName + "'" +
TemporalTableWhereClause +
IsHiddenColumnWhereClause;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var schemaName = reader.GetValueOrDefault<string>("schema");
var tableName = reader.GetValueOrDefault<string>("table");
var columnName = reader.GetValueOrDefault<string>("column_name");
var dataTypeName = reader.GetValueOrDefault<string>("typename");
var dataTypeSchemaName = reader.GetValueOrDefault<string>("datatype_schema_name");
var ordinal = reader.GetValueOrDefault<int>("ordinal");
var nullable = reader.GetValueOrDefault<bool>("nullable");
var primaryKeyOrdinal = reader.GetValueOrDefault<int?>("primary_key_ordinal");
var defaultValue = reader.GetValueOrDefault<string>("default_sql");
var computedValue = reader.GetValueOrDefault<string>("computed_sql");
var precision = reader.GetValueOrDefault<int?>("precision");
var scale = reader.GetValueOrDefault<int?>("scale");
var maxLength = reader.GetValueOrDefault<int?>("max_length");
var isIdentity = reader.GetValueOrDefault<bool>("is_identity");
var isComputed = reader.GetValueOrDefault<bool>("is_computed");
Logger.ColumnFound(
DisplayName(schemaName, tableName), columnName, DisplayName(dataTypeSchemaName, dataTypeName), ordinal, nullable,
primaryKeyOrdinal, defaultValue, computedValue, precision, scale, maxLength, isIdentity, isComputed);
if (!_tableSelectionSet.Allows(schemaName, tableName))
{
Logger.ColumnSkipped(DisplayName(schemaName, tableName), columnName);
continue;
}
if (string.IsNullOrEmpty(columnName))
{
Logger.ColumnNotNamedWarning(DisplayName(schemaName, tableName));
continue;
}
if (!_tables.TryGetValue(SchemaQualifiedKey(tableName, schemaName), out var table))
{
Logger.MissingTableWarning(DisplayName(schemaName, tableName));
continue;
}
// TODO: Check for aliased type and skip following processing altogether
if ((dataTypeName == "nvarchar"
|| dataTypeName == "nchar")
&& maxLength != -1)
{
maxLength /= 2;
}
if (dataTypeName == "decimal"
|| dataTypeName == "numeric")
{
dataTypeName = $"{dataTypeName}({precision}, {scale})";
}
else if (_dateTimePrecisionTypes.Contains(dataTypeName)
&& scale != null)
{
dataTypeName = $"{dataTypeName}({scale})";
}
else
{
if (maxLength == -1)
{
dataTypeName = $"{dataTypeName}(max)";
}
else if (maxLength.HasValue)
{
dataTypeName = $"{dataTypeName}({maxLength.Value})";
}
}
if (defaultValue == "(NULL)")
{
defaultValue = null;
}
if (computedValue == "(NULL)")
{
computedValue = null;
}
var column = new ColumnModel
{
Table = table,
Name = columnName,
StoreType = dataTypeName,
Ordinal = ordinal - 1,
IsNullable = nullable,
PrimaryKeyOrdinal = primaryKeyOrdinal,
DefaultValue = defaultValue,
ComputedValue = computedValue,
ValueGenerated = isIdentity
? ValueGenerated.OnAdd
: isComputed || dataTypeName == "timestamp"
? ValueGenerated.OnAddOrUpdate
: default(ValueGenerated?)
};
column.SqlServer().IsIdentity = isIdentity;
column.SqlServer().DataTypeSchemaName = dataTypeSchemaName;
table.Columns.Add(column);
_tableColumns.Add(ColumnKey(table, column.Name), column);
}
}
}
private void GetIndexes()
{
var command = _connection.CreateCommand();
command.CommandText = @"SELECT
object_schema_name(i.object_id) AS [schema_name],
object_name(i.object_id) AS [table_name],
i.name AS [index_name],
i.is_unique,
c.name AS [column_name],
i.type_desc,
ic.key_ordinal,
i.has_filter,
i.filter_definition
FROM sys.indexes i
INNER JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
INNER JOIN sys.columns c ON ic.object_id = c.object_id AND c.column_id = ic.column_id
INNER JOIN sys.tables t ON t.object_id = i.object_id
WHERE object_schema_name(i.object_id) <> 'sys'
AND i.is_hypothetical = 0
AND object_name(i.object_id) <> '" + HistoryRepository.DefaultTableName + @"'" +
TemporalTableWhereClause + @"
ORDER BY object_schema_name(i.object_id), object_name(i.object_id), i.name, ic.key_ordinal";
using (var reader = command.ExecuteReader())
{
IndexModel index = null;
while (reader.Read())
{
var schemaName = reader.GetValueOrDefault<string>("schema_name");
var tableName = reader.GetValueOrDefault<string>("table_name");
var indexName = reader.GetValueOrDefault<string>("index_name");
var isUnique = reader.GetValueOrDefault<bool>("is_unique");
var typeDesc = reader.GetValueOrDefault<string>("type_desc");
var columnName = reader.GetValueOrDefault<string>("column_name");
var indexOrdinal = reader.GetValueOrDefault<byte>("key_ordinal");
var hasFilter = reader.GetValueOrDefault<bool>("has_filter");
var filterDefinition = reader.GetValueOrDefault<string>("filter_definition");
Logger.IndexColumnFound(
DisplayName(schemaName, tableName), indexName, isUnique, columnName, indexOrdinal);
if (!_tableSelectionSet.Allows(schemaName, tableName))
{
Logger.IndexColumnSkipped(columnName, indexName, DisplayName(schemaName, tableName));
continue;
}
if (string.IsNullOrEmpty(indexName))
{
Logger.IndexNotNamedWarning(DisplayName(schemaName, tableName));
continue;
}
Debug.Assert(index == null || index.Table != null);
if (index == null
|| index.Name != indexName
|| index.Table.Name != tableName
|| index.Table.SchemaName != schemaName)
{
TableModel table;
if (!_tables.TryGetValue(SchemaQualifiedKey(tableName, schemaName), out table))
{
Logger.IndexTableMissingWarning(indexName, DisplayName(schemaName, tableName));
continue;
}
index = new IndexModel
{
Table = table,
Name = indexName,
IsUnique = isUnique,
Filter = hasFilter ? filterDefinition : null
};
if (typeDesc == "CLUSTERED")
{
index[SqlServerAnnotationNames.Clustered] = true;
}
table.Indexes.Add(index);
}
ColumnModel column;
if (string.IsNullOrEmpty(columnName))
{
Logger.IndexColumnNotNamedWarning(indexName, DisplayName(schemaName, tableName));
}
else if (!_tableColumns.TryGetValue(ColumnKey(index.Table, columnName), out column))
{
Logger.IndexColumnsNotMappedWarning(indexName, new[] { columnName });
}
else
{
var indexColumn = new IndexColumnModel
{
Index = index,
Column = column,
Ordinal = indexOrdinal
};
index.IndexColumns.Add(indexColumn);
}
}
}
}
private void GetForeignKeys()
{
var command = _connection.CreateCommand();
command.CommandText = @"SELECT
schema_name(f.schema_id) AS [schema_name],
object_name(f.parent_object_id) AS table_name,
f.name AS foreign_key_name,
object_schema_name(f.referenced_object_id) AS principal_table_schema_name,
object_name(f.referenced_object_id) AS principal_table_name,
col_name(fc.parent_object_id, fc.parent_column_id) AS constraint_column_name,
col_name(fc.referenced_object_id, fc.referenced_column_id) AS referenced_column_name,
is_disabled,
delete_referential_action_desc,
update_referential_action_desc,
fc.constraint_column_id
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc ON f.object_id = fc.constraint_object_id
ORDER BY schema_name(f.schema_id), object_name(f.parent_object_id), f.name";
using (var reader = command.ExecuteReader())
{
var lastFkName = string.Empty;
var lastFkSchemaName = string.Empty;
var lastFkTableName = string.Empty;
ForeignKeyModel fkInfo = null;
while (reader.Read())
{
var schemaName = reader.GetValueOrDefault<string>("schema_name");
var tableName = reader.GetValueOrDefault<string>("table_name");
var fkName = reader.GetValueOrDefault<string>("foreign_key_name");
var principalTableSchemaName = reader.GetValueOrDefault<string>("principal_table_schema_name");
var principalTableName = reader.GetValueOrDefault<string>("principal_table_name");
var fromColumnName = reader.GetValueOrDefault<string>("constraint_column_name");
var toColumnName = reader.GetValueOrDefault<string>("referenced_column_name");
var updateAction = reader.GetValueOrDefault<string>("update_referential_action_desc");
var deleteAction = reader.GetValueOrDefault<string>("delete_referential_action_desc");
var ordinal = reader.GetValueOrDefault<int>("constraint_column_id");
Logger.ForeignKeyColumnFound(
DisplayName(schemaName, tableName), fkName, DisplayName(principalTableSchemaName, principalTableName),
fromColumnName, toColumnName, updateAction, deleteAction, ordinal);
if (string.IsNullOrEmpty(fkName))
{
Logger.ForeignKeyNotNamedWarning(DisplayName(schemaName, tableName));
continue;
}
if (!_tableSelectionSet.Allows(schemaName, tableName))
{
Logger.ForeignKeyColumnMissingWarning(fromColumnName, fkName, DisplayName(schemaName, tableName));
continue;
}
if (fkInfo == null
|| lastFkSchemaName != schemaName
|| lastFkTableName != tableName
|| lastFkName != fkName)
{
lastFkName = fkName;
lastFkSchemaName = schemaName;
lastFkTableName = tableName;
var table = _tables[SchemaQualifiedKey(tableName, schemaName)];
TableModel principalTable = null;
if (!string.IsNullOrEmpty(principalTableSchemaName)
&& !string.IsNullOrEmpty(principalTableName))
{
_tables.TryGetValue(SchemaQualifiedKey(principalTableName, principalTableSchemaName), out principalTable);
}
if (principalTable == null)
{
Logger.ForeignKeyReferencesMissingPrincipalTableWarning(
fkName, DisplayName(schemaName, tableName), DisplayName(principalTableSchemaName, principalTableName));
}
fkInfo = new ForeignKeyModel
{
Name = fkName,
Table = table,
PrincipalTable = principalTable,
OnDelete = ConvertToReferentialAction(deleteAction)
};
table.ForeignKeys.Add(fkInfo);
}
var fkColumn = new ForeignKeyColumnModel
{
Ordinal = ordinal
};
ColumnModel fromColumn;
if ((fromColumn = FindColumnForForeignKey(fromColumnName, fkInfo.Table, fkName)) != null)
{
fkColumn.Column = fromColumn;
}
if (fkInfo.PrincipalTable != null)
{
ColumnModel toColumn;
if ((toColumn = FindColumnForForeignKey(toColumnName, fkInfo.PrincipalTable, fkName)) != null)
{
fkColumn.PrincipalColumn = toColumn;
}
}
fkInfo.Columns.Add(fkColumn);
}
}
}
private static string DisplayName(string schema, string name)
=> (!string.IsNullOrEmpty(schema) ? schema + "." : "") + name;
private ColumnModel FindColumnForForeignKey(
string columnName, TableModel table, string fkName)
{
ColumnModel column;
if (string.IsNullOrEmpty(columnName))
{
Logger.ForeignKeyColumnNotNamedWarning(fkName, DisplayName(table.SchemaName, table.Name));
return null;
}
if (!_tableColumns.TryGetValue(
ColumnKey(table, columnName), out column))
{
Logger.ForeignKeyColumnsNotMappedWarning(fkName, new[] { columnName });
return null;
}
return column;
}
private static ReferentialAction? ConvertToReferentialAction(string onDeleteAction)
{
switch (onDeleteAction.ToUpperInvariant())
{
case "RESTRICT":
return ReferentialAction.Restrict;
case "CASCADE":
return ReferentialAction.Cascade;
case "SET_NULL":
return ReferentialAction.SetNull;
case "SET_DEFAULT":
return ReferentialAction.SetDefault;
case "NO_ACTION":
return ReferentialAction.NoAction;
default:
return null;
}
}
}
}