public void GenerateSchema()
        {
            var     Temp   = new Utilities.ORM.Manager.Schema.Default.Database.SQLServer.SQLServerSchemaGenerator(Utilities.IoC.Manager.Bootstrapper.Resolve <Utilities.ORM.Manager.QueryProvider.Manager>(), Utilities.IoC.Manager.Bootstrapper.Resolve <Utilities.ORM.Manager.SourceProvider.Manager>());
            ISource Source = Temp.GetSourceStructure(TestDatabaseSource);

            Source.Tables.First().AddColumn <string>("A", DbType.Int16);
            ITable Table = Source.AddTable("TestTable2");

            Table.AddColumn <string>("A", DbType.Int16);
            IEnumerable <string> Commands = Temp.GenerateSchema(Source, TestDatabaseSource);

            Assert.Equal(2, Commands.Count());
            Assert.Equal("EXEC dbo.sp_executesql @statement = N'ALTER TABLE TestTable ADD A SmallInt'", Commands.First());
            Assert.Equal("EXEC dbo.sp_executesql @statement = N'CREATE TABLE TestTable2(A SmallInt)'", Commands.Last());
        }
Beispiel #2
0
        private static void SetupTable(ISource database, dynamic item)
        {
            if (database is null)
            {
                throw new ArgumentNullException(nameof(database));
            }
            if (item is null)
            {
                throw new ArgumentNullException(nameof(item));
            }
            string TableName = item.TABLE_NAME;
            string TableType = item.TABLE_TYPE;

            if (TableType == "BASE TABLE")
            {
                database.AddTable(TableName, item.TABLE_SCHEMA);
            }
            else if (TableType == "VIEW")
            {
                database.AddView(TableName, item.TABLE_SCHEMA, string.Empty);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Sets up the property (used internally)
        /// </summary>
        /// <param name="mappings">The mappings.</param>
        /// <param name="sourceSpec">The source spec.</param>
        /// <exception cref="ArgumentException">Foreign key IDs could not be found for {typeof(ClassType).Name}.{Name}</exception>
        public override void Setup(IMappingSource mappings, ISource sourceSpec)
        {
            ForeignMapping = mappings.GetChildMappings <DataType>()
                             .SelectMany(x => mappings.GetParentMapping(x.ObjectType))
                             .Where(x => x.IDProperties.Count > 0)
                             .Distinct()
                             .ToList();
            if (ForeignMapping is null)
            {
                throw new ArgumentException($"Foreign key IDs could not be found for {typeof(ClassType).Name}.{Name}");
            }

            var ParentMappings = mappings.GetChildMappings(ParentMapping.ObjectType).SelectMany(x => mappings.GetParentMapping(x.ObjectType)).Distinct();
            var ParentWithID   = ParentMappings.FirstOrDefault(x => x.IDProperties.Count > 0);

            if (string.IsNullOrEmpty(TableName))
            {
                var Class1 = ParentWithID.ObjectType.Name;
                var Class2 = ForeignMapping.OrderBy(x => x.ObjectType.Name).FirstOrDefault()?.ObjectType.Name ?? string.Empty;
                if (string.CompareOrdinal(Class1, Class2) < 0)
                {
                    SetTableName(Class1 + "_" + Class2);
                }
                else
                {
                    SetTableName(Class2 + "_" + Class1);
                }
            }
            if (sourceSpec.Tables.Any(x => x.Name == TableName))
            {
                return;
            }

            var JoinTable = sourceSpec.AddTable(TableName ?? string.Empty, ParentMapping.SchemaName);

            JoinTable.AddColumn <long>("ID_", DbType.UInt64, 0, false, true, false, true, false);
            var ParentIDMappings = ParentMappings.SelectMany(x => x.IDProperties);

            DatabaseJoinsCascade = ForeignMapping.Any(TempMapping => !ParentMappings.Contains(TempMapping));
            var Prefix = string.Empty;

            if (ForeignMapping.Any(TempMapping => ParentWithID == TempMapping))
            {
                Prefix = "Parent_";
            }

            foreach (var ParentIDMapping in ParentIDMappings)
            {
                JoinTable.AddColumn <object>(Prefix + ParentIDMapping.ParentMapping.TableName + ParentIDMapping.ColumnName,
                                             ParentIDMapping.PropertyType.To(DbType.Int32),
                                             ParentIDMapping.MaxLength,
                                             false,
                                             false,
                                             false,
                                             false,
                                             false,
                                             ParentIDMapping.ParentMapping.TableName,
                                             ParentIDMapping.ColumnName,
                                             null !,
                                             "",
                                             !OnDeleteDoNothingValue && DatabaseJoinsCascade,
                                             !OnDeleteDoNothingValue && DatabaseJoinsCascade,
                                             !OnDeleteDoNothingValue && !DatabaseJoinsCascade);
            }
            foreach (var TempMapping in ForeignMapping)
            {
                foreach (var ForeignIDMapping in TempMapping.IDProperties)
                {
                    JoinTable.AddColumn <object>(ForeignIDMapping.ParentMapping.TableName + ForeignIDMapping.ColumnName,
                                                 ForeignIDMapping.PropertyType.To(DbType.Int32),
                                                 ForeignIDMapping.MaxLength,
                                                 false,
                                                 false,
                                                 false,
                                                 false,
                                                 false,
                                                 ForeignIDMapping.ParentMapping.TableName,
                                                 ForeignIDMapping.ColumnName,
                                                 null !,
                                                 "",
                                                 !OnDeleteDoNothingValue && DatabaseJoinsCascade,
                                                 !OnDeleteDoNothingValue && DatabaseJoinsCascade,
                                                 !OnDeleteDoNothingValue && !DatabaseJoinsCascade);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Sets up the tables.
        /// </summary>
        /// <param name="logger">The logger.</param>
        /// <param name="sourceConnection">The source connection.</param>
        /// <param name="source">The source.</param>
        /// <param name="sourceSpec">The source spec.</param>
        private static void SetupTables(ILogger logger, IConnection sourceConnection, IMappingSource source, ISource sourceSpec)
        {
            logger.Information("Setting up table structure for {Info:l}", sourceConnection.DatabaseName);
            foreach (var Mapping in source.Mappings.Values.OrderBy(x => x.Order))
            {
                if (!DefaultSchemas.Contains(Mapping.SchemaName))
                {
                    sourceSpec.Schemas.AddIfUnique(Mapping.SchemaName);
                }

                var Table          = sourceSpec.AddTable(Mapping.TableName, Mapping.SchemaName);
                var Tree           = source.TypeGraphs[Mapping.ObjectType];
                var ParentMappings = Tree?.Root.Nodes.ForEach(x => source.Mappings[x.Data]) ?? Array.Empty <IMapping>();
                foreach (var ID in Mapping.IDProperties)
                {
                    ID.Setup();
                    ID.AddToTable(Table);
                }
                foreach (var ID in Mapping.AutoIDProperties)
                {
                    ID.Setup();
                    ID.AddToTable(Table);
                }
                foreach (var Reference in Mapping.ReferenceProperties)
                {
                    Reference.Setup();
                    Reference.AddToTable(Table);
                }
                foreach (var Map in Mapping.MapProperties)
                {
                    Map.Setup(source);
                    Map.AddToTable(Table);
                }
                foreach (var Map in Mapping.ManyToManyProperties)
                {
                    Map.Setup(source, sourceSpec);
                }
                foreach (var ParentMapping in ParentMappings)
                {
                    foreach (var ID in ParentMapping.IDProperties)
                    {
                        ID.Setup();
                        ID.AddToChildTable(Table);
                    }
                    foreach (var ID in ParentMapping.AutoIDProperties)
                    {
                        ID.Setup();
                        ID.AddToChildTable(Table);
                    }
                }
            }
            foreach (var Mapping in source.Mappings.Values.OrderBy(x => x.Order))
            {
                foreach (var Map in Mapping.ManyToOneProperties)
                {
                    Map.Setup(source, sourceSpec);
                }
            }
            foreach (var Mapping in source.Mappings.Values.OrderBy(x => x.Order))
            {
                foreach (var Map in Mapping.ManyToOneProperties)
                {
                    Map.SetColumnInfo(source);
                }
                foreach (var Map in Mapping.ManyToManyProperties)
                {
                    Map.SetColumnInfo(source);
                }
                foreach (var Map in Mapping.MapProperties)
                {
                    Map.SetColumnInfo(source);
                }
            }
        }