private void RemoveTable(PairedNodeCollection <Schema, Table> tables, Table tableToRemove)
 {
     foreach (var foreignKey in foreignKeysOfSchema)
     {
         if (foreignKey.Owner == tableToRemove || foreignKey.ReferencedTable == tableToRemove)
         {
             if (foreignKey.Owner == tableToRemove)
             {
                 var resolvedTableName = mappingResolver.GetNodeName(foreignKey.ReferencedTable);
                 if (!targetModel.LockedTables.ContainsKey(resolvedTableName))
                 {
                     targetModel.LockedTables.Add(resolvedTableName, string.Format(Strings.ExTableXCantBeRemovedDueToForeignKeyYOfIgnoredTableOrColumn, foreignKey.ReferencedTable.Name, foreignKey.Name));
                 }
             }
             foreignKey.Owner.TableConstraints.Remove(foreignKey);
         }
     }
     tables.Remove(tableToRemove);
 }
        /// <inheritdoc/>
        protected override IPathNode VisitTable(Table table)
        {
            var tableInfo = new TableInfo(targetModel, resolver.GetNodeName(table));

            currentTable = tableInfo;

            foreach (var column in table.TableColumns)
            {
                Visit(column);
            }

            var primaryKey = table.TableConstraints
                             .SingleOrDefault(constraint => constraint is PrimaryKey);

            if (primaryKey != null)
            {
                Visit(primaryKey);
            }

            foreach (var index in table.Indexes)
            {
                Visit(index);
            }

            currentTable = null;

            return(tableInfo);
        }
Esempio n. 3
0
        public PartialIndexInfoMap(MappingResolver resolver, IEnumerable <StoredPartialIndexFilterInfo> indexes)
        {
            ArgumentValidator.EnsureArgumentNotNull(resolver, "resolver");
            ArgumentValidator.EnsureArgumentNotNull(indexes, "indexes");

            items = new Dictionary <string, Dictionary <string, StoredPartialIndexFilterInfo> >(Comparer);

            foreach (var index in indexes)
            {
                var tableName = resolver.GetNodeName(index.Database, index.Schema, index.Table);
                Dictionary <string, StoredPartialIndexFilterInfo> indexesForTable;
                if (!items.TryGetValue(tableName, out indexesForTable))
                {
                    indexesForTable = new Dictionary <string, StoredPartialIndexFilterInfo>(Comparer);
                    items.Add(tableName, indexesForTable);
                }
                indexesForTable.Add(index.Name, index);
            }
        }
        private Dictionary <string, string> CloneSchemas(Catalog newCatalog, Catalog sourceCatalog, MappingResolver mappingResolver)
        {
            var schemaMap = new Dictionary <string, string>();

            foreach (var schema in sourceCatalog.Schemas)
            {
                var complexName = mappingResolver.GetNodeName(newCatalog.Name, schema.Name, "Dummy");
                var names       = complexName.Split(NameElementSeparator);
                var schemaName  = schema.Name;
                switch (names.Length)
                {
                case 3:
                    schemaName = names[1];
                    break;

                case 2:
                    schemaName = names[0];
                    break;
                }
                schemaMap.Add(schema.Name, schemaName);
            }
            return(schemaMap);
        }
        /// <inheritdoc/>
        protected override IPathNode VisitDomainModel(DomainModel domainModel)
        {
            // Build tables, columns and primary indexes
            foreach (var primaryIndex in domainModel.RealIndexes.Where(i => i.IsPrimary))
            {
                Visit(primaryIndex);
            }

            // Build full-text indexes
            foreach (var fullTextIndex in domainModel.FullTextIndexes)
            {
                Visit(fullTextIndex);
            }

            // Build foreign keys
            var buildForeignKeys = BuildForeignKeys &&
                                   providerInfo.Supports(ProviderFeatures.ForeignKeyConstraints);

            if (buildForeignKeys)
            {
                foreach (var group in domainModel.Associations.Where(a => a.Ancestors.Count == 0))
                {
                    Visit(group);
                }
            }

            // Build keys and sequences
            foreach (KeyInfo keyInfo in domainModel.Hierarchies.Select(h => h.Key))
            {
                Visit(keyInfo);
            }

            var buildHierarchyForeignKeys = BuildHierarchyForeignKeys &&
                                            providerInfo.Supports(ProviderFeatures.ForeignKeyConstraints);

            if (!buildHierarchyForeignKeys)
            {
                return(targetModel);
            }

            // Build hierarchy foreign keys
            var indexPairs = new Dictionary <Pair <IndexInfo>, object>();

            foreach (var type in domainModel.Types.Entities)
            {
                if (type.Hierarchy == null || type.Hierarchy.InheritanceSchema == InheritanceSchema.ConcreteTable)
                {
                    continue;
                }
                if (type.Indexes.PrimaryIndex.IsVirtual)
                {
                    Dictionary <TypeInfo, int> typeOrder = type.GetAncestors()
                                                           .AddOne(type)
                                                           .Select((t, i) => new { Type = t, Index = i })
                                                           .ToDictionary(a => a.Type, a => a.Index);
                    List <IndexInfo> realPrimaryIndexes = type.Indexes.RealPrimaryIndexes
                                                          .OrderBy(index => typeOrder[index.ReflectedType])
                                                          .ToList();
                    for (int i = 0; i < realPrimaryIndexes.Count - 1; i++)
                    {
                        if (realPrimaryIndexes[i] != realPrimaryIndexes[i + 1])
                        {
                            var pair = new Pair <IndexInfo>(realPrimaryIndexes[i], realPrimaryIndexes[i + 1]);
                            indexPairs[pair] = null;
                        }
                    }
                }
            }
            foreach (var indexPair in indexPairs.Keys)
            {
                var referencedIndex         = indexPair.First;
                var referencingIndex        = indexPair.Second;
                var referencingTable        = targetModel.Tables[resolver.GetNodeName(referencingIndex.ReflectedType)];
                var referencedTable         = targetModel.Tables[resolver.GetNodeName(referencedIndex.ReflectedType)];
                var storageReferencingIndex = FindIndex(
                    referencingTable, referencingIndex.KeyColumns.Select(ci => ci.Key.Name).ToList());

                string foreignKeyName = nameBuilder.BuildHierarchyForeignKeyName(referencingIndex.ReflectedType, referencedIndex.ReflectedType);
                CreateHierarchyForeignKey(referencingTable, referencedTable, storageReferencingIndex, foreignKeyName);
            }

            return(targetModel);
        }
Esempio n. 6
0
 private string GetTableName(StoredTypeInfo type)
 {
     return(resolver.GetNodeName(
                type.MappingDatabase, type.MappingSchema, type.MappingName));
 }
        public static ModelMapping Build(HandlerAccessor handlers, SchemaExtractionResult sqlModel, MappingResolver resolver, NodeConfiguration nodeConfiguration, bool isLegacy)
        {
            var domainModel   = handlers.Domain.Model;
            var configuration = handlers.Domain.Configuration;
            var providerInfo  = handlers.ProviderInfo;

            var mapping = new ModelMapping();

            // Register persistent types

            var typesToProcess = domainModel.Types.AsEnumerable();

            if (isLegacy || configuration.IsMultidatabase)
            {
                typesToProcess = typesToProcess.Where(t => !t.IsSystem);
            }

            foreach (var type in typesToProcess)
            {
                var primaryIndex = type.Indexes.FindFirst(IndexAttributes.Real | IndexAttributes.Primary);
                if (primaryIndex == null || primaryIndex.IsAbstract)
                {
                    continue;
                }
                var reflectedType = primaryIndex.ReflectedType;
                var nodeName      = resolver.GetNodeName(reflectedType);
                var storageTable  = resolver.Resolve(sqlModel, nodeName).GetTable();
                if (storageTable == null)
                {
                    throw new DomainBuilderException(String.Format(Strings.ExTableXIsNotFound, nodeName));
                }
                mapping.Register(reflectedType, storageTable);
            }

            // Register key generators

            var keyGenerator = domainModel.Hierarchies
                               .Select(h => h.Key.Sequence)
                               .Where(s => s != null)
                               .Distinct();

            Func <MappingResolveResult, SchemaNode> nodeResolver;

            if (providerInfo.Supports(ProviderFeatures.Sequences))
            {
                nodeResolver = r => r.GetSequence();
            }
            else
            {
                nodeResolver = r => r.GetTable();
            }

            foreach (var sequence in keyGenerator)
            {
                var nodeResult = resolver.Resolve(sqlModel, resolver.GetNodeName(sequence));
                var node       = nodeResolver.Invoke(nodeResult);
                mapping.Register(sequence, node);
            }

            // Fill information for TemporaryTableManager

            var defaultSchema = resolver.ResolveSchema(sqlModel, configuration.DefaultDatabase, configuration.DefaultSchema);

            if (configuration.ShareStorageSchemaOverNodes && handlers.ProviderInfo.Supports(ProviderFeatures.Multischema))
            {
                mapping.TemporaryTableDatabase = nodeConfiguration.GetActualNameFor(defaultSchema.Catalog);
                mapping.TemporaryTableSchema   = nodeConfiguration.GetActualNameFor(defaultSchema);
            }
            else
            {
                mapping.TemporaryTableDatabase = defaultSchema.Catalog.GetNameInternal();
                mapping.TemporaryTableSchema   = defaultSchema.GetNameInternal();
            }

            if (providerInfo.Supports(ProviderFeatures.Collations))
            {
                if (!string.IsNullOrEmpty(configuration.Collation))
                {
                    // If user explicitly specified collation use that
                    mapping.TemporaryTableCollation = configuration.Collation;
                }
                else
                {
                    // Otherwise use first available collation
                    var collation = defaultSchema.Collations.FirstOrDefault();
                    mapping.TemporaryTableCollation = collation != null ? collation.Name : null;
                }
            }

            mapping.Lock();
            return(mapping);
        }
Esempio n. 8
0
 public static string GetTableName(this MappingResolver resolver, StoredTypeInfo type)
 {
     return(resolver.GetNodeName(
                type.MappingDatabase, type.MappingSchema, type.MappingName));
 }