< Summary

Information
Class: ValidateLib.Metadata.Validators.ForeignKeyValidator
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\Metadata\Validators\ForeignKeyValidator.cs
Line coverage
93%
Covered lines: 60
Uncovered lines: 4
Coverable lines: 64
Total lines: 167
Line coverage: 93.7%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\Metadata\Validators\ForeignKeyValidator.cs

#LineLine coverage
 1using ValidateLib.ErrorsAndWarnings.Errors;
 2using ValidateLib.ErrorsAndWarnings.Warnings;
 3using ValidateLib.Metadata.Descriptors;
 4
 5namespace ValidateLib.Metadata.Validators
 6{
 7    public class ForeignKeyValidator : IMValidator<TableGroupDescriptor>
 8    {
 19        TableDescriptor? referencingTable { get; set; }
 110        ForeignKeyDescriptor? referencingFKDescriptor { get; set; }
 11        public List<Warning> Validate(TableGroupDescriptor tableGroupDescriptor)
 12        {
 113            foreach (var table in tableGroupDescriptor!.tables!._value!)
 14            {
 115                ResolveForeignKeysForTable(table, tableGroupDescriptor);
 16            }
 117            return new List<Warning>();
 18        }
 19
 20        void ResolveForeignKeysForTable(TableDescriptor table, TableGroupDescriptor tableGroupDescriptor)
 21        {
 122            if (table.tableSchema is not null && table!.tableSchema?._value?.foreignKeys is not null)
 23            {
 124                var foreignKeys = table.tableSchema._value.foreignKeys._value;
 125                var tableSchema = table.tableSchema._value;
 126                referencingTable = table;
 127                foreach (var foreignKeyDescriptor in foreignKeys!)
 28                {
 129                    var referencedSchema = FindReferencedSchema(tableGroupDescriptor, foreignKeyDescriptor);
 130                    for (int i = 0; i < foreignKeyDescriptor.columnReference!._value!.Count; i++)
 31                    {
 132                        var referencingColumn = foreignKeyDescriptor.columnReference._value[i];
 133                        var referencedColumn = foreignKeyDescriptor!.reference!._value!.columnReference!._value![i]!;
 134                        if (!ColumnExistsWithName(tableSchema, referencingColumn)) ErrorFactory.ThrowReferencingColumnDo
 135                        if (!ColumnExistsWithName(referencedSchema, referencedColumn)) ErrorFactory.ThrowReferencedColum
 36
 37                    }
 38                }
 39            }
 140        }
 41        static bool ColumnExistsWithName(SchemaDescriptor schemaDescriptor, string name)
 42        {
 143            foreach (var column in schemaDescriptor?.columns?._value!)
 44            {
 145                if (column.name is not null)
 46                {
 147                    if (column.name._value == name) return true;
 48                }
 49            }
 150            return false;
 151        }
 52
 53        SchemaDescriptor FindReferencedSchema(TableGroupDescriptor tableGroupDescriptor, ForeignKeyDescriptor foreignKey
 54        {
 155            referencingFKDescriptor = foreignKeyDescriptor;
 156            if (foreignKeyDescriptor!.reference!._value!.schemaReference is not null)
 57            {
 158                return FindReferencedSchemaDescriptorWithSchemaId(tableGroupDescriptor, foreignKeyDescriptor!.reference!
 59            }
 60            else
 61            {
 162                return FindReferencedSchemaDescriptorWithTableUrl(tableGroupDescriptor, foreignKeyDescriptor!.reference.
 63            }
 64        }
 65
 66        public static TableDescriptor FindReferencedTable(TableGroupDescriptor tableGroupDescriptor, ForeignKeyDescripto
 67        {
 168            ForeignKeyValidator fkValidator = new ForeignKeyValidator();
 169            if (foreignKeyDescriptor!.reference!._value!.schemaReference is not null)
 70            {
 171                return fkValidator.FindReferencedTableDescriptorWithSchemaId(tableGroupDescriptor, foreignKeyDescriptor!
 72            }
 73            else
 74            {
 175                return fkValidator.FindReferencedTableDescriptorWithTableUrl(tableGroupDescriptor, foreignKeyDescriptor!
 76            }
 77        }
 78        SchemaDescriptor FindReferencedSchemaDescriptorWithTableUrl(TableGroupDescriptor tableGroupDescriptor, string ta
 79        {
 180            SchemaDescriptor? desiredSchema = null;
 181            foreach (var table in tableGroupDescriptor?.tables?._value!)
 82            {
 183                if (table!.url!._value == tableUrl)
 84                {
 185                    if (desiredSchema is null)
 86                    {
 187                        desiredSchema = table!.tableSchema!._value;
 88                    }
 89                    else
 090                        ErrorFactory.ThrowTwoSatisfyingReferencedTablesErrorError(referencingTable!, referencingFKDescri
 91
 92                }
 93            }
 194            if (desiredSchema is null) ErrorFactory.ThrowNoSatisfyingReferencedTableError(referencingTable!, referencing
 195            return desiredSchema!;
 96        }
 97
 98        TableDescriptor FindReferencedTableDescriptorWithTableUrl(TableGroupDescriptor tableGroupDescriptor, string tabl
 99        {
 1100            TableDescriptor? desiredTable = null;
 1101            foreach (var table in tableGroupDescriptor?.tables?._value!)
 102            {
 1103                if (table!.url!._value == tableUrl)
 104                {
 1105                    if (desiredTable is null)
 106                    {
 1107                        desiredTable = table;
 108                    }
 109                    else
 0110                        ErrorFactory.ThrowTwoSatisfyingReferencedTablesErrorError(referencingTable!, referencingFKDescri
 111
 112                }
 113            }
 1114            if (desiredTable is null) ErrorFactory.ThrowNoSatisfyingReferencedTableError(referencingTable!, referencingF
 1115            return desiredTable!;
 116        }
 117
 118        SchemaDescriptor FindReferencedSchemaDescriptorWithSchemaId(TableGroupDescriptor tableGroupDescriptor, string sc
 119        {
 1120            SchemaDescriptor? desiredSchema = null;
 1121            foreach (var table in tableGroupDescriptor?.tables?._value!)
 122            {
 1123                if (table.tableSchema is not null && table!.tableSchema._value!.id is not null)
 124                {
 1125                    if (table!.tableSchema._value.id._value == schemaId)
 126                    {
 1127                        if (desiredSchema is null)
 128                        {
 1129                            desiredSchema = table.tableSchema._value;
 130                        }
 131                        else
 0132                            ErrorFactory.ThrowTwoSatisfyingReferencedTablesErrorError(referencingTable!, referencingFKDe
 133
 134                    }
 135                }
 136
 137            }
 1138            if (desiredSchema is null) ErrorFactory.ThrowNoSatisfyingReferencedTableError(referencingTable!, referencing
 1139            return desiredSchema!;
 140        }
 141
 142        TableDescriptor FindReferencedTableDescriptorWithSchemaId(TableGroupDescriptor tableGroupDescriptor, string sche
 143        {
 1144            TableDescriptor? tableDescriptor = null;
 1145            foreach (var table in tableGroupDescriptor?.tables?._value!)
 146            {
 1147                if (table.tableSchema is not null && table!.tableSchema._value!.id is not null)
 148                {
 1149                    if (table!.tableSchema._value.id._value == schemaId)
 150                    {
 1151                        if (tableDescriptor is null)
 152                        {
 1153                            tableDescriptor = table;
 154                        }
 155                        else
 0156                            ErrorFactory.ThrowTwoSatisfyingReferencedTablesErrorError(referencingTable!, referencingFKDe
 157
 158                    }
 159                }
 160
 161            }
 1162            if (tableDescriptor is null) ErrorFactory.ThrowNoSatisfyingReferencedTableError(referencingTable!, referenci
 1163            return tableDescriptor!;
 164        }
 165
 166    }
 167}