Exemple #1
0
 public override DynamicMetadataProvider CreateMetadataProvider(InformationSchemaSettings informationSchemaSettings)
 {
     return(new DynamicMetadataProvider(this, informationSchemaSettings));
 }
        public static SchemaCache Create(ProviderSpecificSchema informationSchema, InformationSchemaSettings informationSchemaSettings)
        {
            IEqualityComparer <String>        comparer          = informationSchema.IsCaseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase;
            IEqualityComparer <TableFullName> tableNameComparer = informationSchema.IsCaseSensitive ? TableFullName.OrdinalComparer : TableFullName.OrdinalIgnoreCaseComparer;
            StringComparison comparison = informationSchema.IsCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

            var tableEdmNameFullNames = new Dictionary <String, TableFullName>(comparer);
            var tableFullNameEdmNames = new Dictionary <TableFullName, (String tableEdmName, bool isQueryType)>(tableNameComparer);
            var navigationMappings    = new Dictionary <TableFullName, IReadOnlyList <NavigationMapping> >(tableNameComparer);
            Dictionary <(String constraintSchema, String constraintName), IReadOnlyList <KeyColumnUsage> > keyColumns = GetKeyColumns(informationSchema, informationSchemaSettings);
            List <ReferentialConstraint> referentialConstraints;

            using SchemaContext schemaContext = informationSchema.GetSchemaContext();

            Dictionary <String, TableMapping>?dbNameTableMappings = null;

            if (informationSchemaSettings.Tables != null)
            {
                dbNameTableMappings = informationSchemaSettings.Tables.ToDictionary(t => t.DbName, StringComparer.OrdinalIgnoreCase);
            }

            IQueryable <Table> tableQuery = schemaContext.Tables.AsQueryable();
            IQueryable <ReferentialConstraint> referentialConstraintsQuery = schemaContext.ReferentialConstraints;

            if (informationSchemaSettings.IncludedSchemas != null && informationSchemaSettings.IncludedSchemas.Count > 0)
            {
                tableQuery = tableQuery.Where(t => informationSchemaSettings.IncludedSchemas.Contains(t.TableSchema));
                referentialConstraintsQuery = referentialConstraintsQuery.Where(t => informationSchemaSettings.IncludedSchemas.Contains(t.ConstraintSchema));
            }
            if (informationSchemaSettings.ExcludedSchemas != null && informationSchemaSettings.ExcludedSchemas.Count > 0)
            {
                tableQuery = tableQuery.Where(t => !informationSchemaSettings.ExcludedSchemas.Contains(t.TableSchema));
                referentialConstraintsQuery = referentialConstraintsQuery.Where(t => !informationSchemaSettings.ExcludedSchemas.Contains(t.ConstraintSchema));
            }

            List <Table> tables = tableQuery.ToList();

            referentialConstraints = referentialConstraintsQuery.ToList();

            foreach (Table table in tables)
            {
                String tableName;
                if (informationSchemaSettings.DefaultSchema != null && String.Compare(informationSchemaSettings.DefaultSchema, table.TableName, comparison) == 0)
                {
                    tableName = table.TableName;
                }
                else
                {
                    tableName = table.TableSchema + "." + table.TableName;
                }

                if (dbNameTableMappings != null)
                {
                    if (dbNameTableMappings.TryGetValue(table.TableName, out TableMapping? tableMapping) ||
                        dbNameTableMappings.TryGetValue(table.TableSchema + "." + table.TableName, out tableMapping))
                    {
                        if (tableMapping.Exclude)
                        {
                            continue;
                        }

                        if (!String.IsNullOrEmpty(tableMapping.EdmName))
                        {
                            tableName = tableMapping.EdmName;
                            if (tableEdmNameFullNames.ContainsKey(tableName))
                            {
                                throw new InvalidOperationException("Duplicate TableMapping.EdmName = '" + tableName + "'");
                            }
                        }

                        if (tableMapping.Navigations != null && tableMapping.Navigations.Count > 0)
                        {
                            foreach (NavigationMapping navigationMapping in tableMapping.Navigations)
                            {
                                if (!String.IsNullOrEmpty(navigationMapping.NavigationName) && String.IsNullOrEmpty(navigationMapping.ConstraintName))
                                {
                                    String?tableName2 = navigationMapping.TargetTableName;
                                    if (tableName2 != null)
                                    {
                                        int i = tableName2.IndexOf('.');
                                        if (i != -1)
                                        {
                                            tableName2 = tableName2.Substring(i + 1);
                                        }

                                        navigationMapping.ConstraintName = GetFKeyConstraintName(referentialConstraints, keyColumns, table.TableSchema, table.TableName, tableName2);
                                    }
                                }
                            }

                            navigationMappings.Add(new TableFullName(table.TableSchema, table.TableName), tableMapping.Navigations);
                        }
                    }
                    else
                    {
                        if (informationSchemaSettings.ObjectFilter == DbObjectFilter.Mapping)
                        {
                            continue;
                        }
                    }
                }

                var tableFullName = new TableFullName(table.TableSchema, table.TableName);
                tableEdmNameFullNames.Add(tableName, tableFullName);
                tableFullNameEdmNames.Add(tableFullName, (tableName, table.TableType == "VIEW"));
            }

            Dictionary <TableFullName, List <Column> >     tableColumns     = GetTableColumns(informationSchema);
            Dictionary <TableFullName, List <Navigation> > tableNavigations = Navigation.GetNavigations(
                referentialConstraints,
                keyColumns,
                tableFullNameEdmNames,
                navigationMappings,
                tableColumns);

            return(new SchemaCache(
                       informationSchema,
                       keyColumns,
                       tableFullNameEdmNames,
                       tableColumns,
                       GetKeyConstraintNames(informationSchema),
                       tableNavigations,
                       GetManyToManyProperties(navigationMappings, tableEdmNameFullNames)));
        }
 public DynamicMetadataProvider CreateMetadataProvider(InformationSchemaSettings informationSchemaSettings)
 {
     DefaultSchema = informationSchemaSettings.DefaultSchema;
     return(new DynamicMetadataProvider(this, informationSchemaSettings));
 }