public void ShouldMapDBColumn()
        {
            var mapper = new DBColumnMapper();
            var column = new Column
            {
                Name       = "Age",
                DataType   = "Int",
                IsNullable = true
            };

            Assert.That(mapper.Map(column, new PascalCaseTextFormatter()), Is.EqualTo("Map(x => x.Age).Column(\"Age\");"));
        }
示例#2
0
        public CodeCompileUnit GetCompleteCompileUnit(string className)
        {
            var codeGenerationHelper = new CodeGenerationHelper();
            var compileUnit          = codeGenerationHelper.GetCodeCompileUnit(nameSpace, className);

            var newType = compileUnit.Namespaces[0].Types[0];

            newType.IsPartial = appPrefs.GeneratePartialClasses;
            var pascalCaseTextFormatter = new PascalCaseTextFormatter {
                PrefixRemovalList = appPrefs.FieldPrefixRemovalList
            };

            newType.BaseTypes.Add(string.Format("ClassMap<{0}{1}>", appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(Table.Name)));

            var constructor = new CodeConstructor {
                Attributes = MemberAttributes.Public
            };

            constructor.Statements.Add(new CodeSnippetStatement(TABS + "Table(\"" + Table.Name + "\");"));
            constructor.Statements.Add(new CodeSnippetStatement(TABS + "LazyLoad();"));

            if (UsesSequence)
            {
                constructor.Statements.Add(new CodeSnippetStatement(String.Format(TABS + "Id(x => x.{0}).Column(x => x.{1}).GeneratedBy.Sequence(\"{2}\")",
                                                                                  Formatter.FormatText(Table.PrimaryKey.Columns[0].Name), Table.PrimaryKey.Columns[0].Name, appPrefs.Sequence)));
            }
            else if (Table.PrimaryKey.Type == PrimaryKeyType.PrimaryKey)
            {
                constructor.Statements.Add(GetIdMapCodeSnippetStatement(this.appPrefs, Table.PrimaryKey.Columns[0].Name, Table.PrimaryKey.Columns[0].DataType, Formatter));
            }
            else
            {
                constructor.Statements.Add(GetIdMapCodeSnippetStatement(Table.PrimaryKey, Formatter));
            }

            foreach (var fk in Table.ForeignKeys.Where(fk => fk.Columns.First().IsForeignKey&& appPrefs.IncludeForeignKeys))
            {
                constructor.Statements.Add(new CodeSnippetStatement(string.Format(TABS + "References(x => x.{0}).Column(\"{1}\");", Formatter.FormatSingular(fk.UniquePropertyName), fk.Columns.First().Name)));
            }

            foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys)))
            {
                var columnMapping = new DBColumnMapper().Map(column, Formatter, appPrefs.IncludeLengthAndScale);
                constructor.Statements.Add(new CodeSnippetStatement(TABS + columnMapping));
            }

            Table.HasManyRelationships.ToList().ForEach(x =>
                                                        constructor.Statements.Add(new OneToMany(Formatter).Create(x))
                                                        );

            newType.Members.Add(constructor);
            return(compileUnit);
        }
示例#3
0
        public void ShouldMapDBColumn()
        {
            var mapper = new DBColumnMapper(new TestApplicationSettings {
                GenerateColumnNameMapping = true
            });
            var column = new Column
            {
                Name       = "Age",
                DataType   = "Int",
                IsNullable = true
            };

            Assert.That(mapper.Map(column, "Age", new PascalCaseTextFormatter()), Is.EqualTo("Map(x => x.Age).Column(\"Age\");"));
        }
示例#4
0
        public CodeCompileUnit GetCompleteCompileUnit(string className)
        {
            var codeGenerationHelper = new CodeGenerationHelper();
            var compileUnit          = codeGenerationHelper.GetCodeCompileUnit(nameSpace, className);

            var newType = compileUnit.Namespaces[0].Types[0];

            newType.IsPartial = appPrefs.GeneratePartialClasses;

            newType.BaseTypes.Add(string.Format("ClassMapping<{0}{1}>", appPrefs.ClassNamePrefix, Formatter.FormatSingular(Table.Name)));

            var constructor = new CodeConstructor {
                Attributes = MemberAttributes.Public
            };

            constructor.Statements.Add(new CodeSnippetStatement(TABS + "Table(\"" + Table.Name + "\");"));
            constructor.Statements.Add(new CodeSnippetStatement(TABS + string.Format("Lazy({0});", appPrefs.UseLazy ? "true" : "false")));
            var mapper = new DBColumnMapper();

            if (UsesSequence)
            {
                constructor.Statements.Add(new CodeSnippetStatement(TABS + mapper.IdSequenceMap(Table.PrimaryKey.Columns[0], appPrefs.Sequence, Formatter)));
            }
            else if (Table.PrimaryKey.Type == PrimaryKeyType.PrimaryKey)
            {
                constructor.Statements.Add(new CodeSnippetStatement(TABS + mapper.IdMap(Table.PrimaryKey.Columns[0], Formatter)));
            }
            else
            {
            }

            foreach (var column in Table.Columns.Where(x => x.IsPrimaryKey != true))
            {
                if (!appPrefs.IncludeForeignKeys && column.IsForeignKey)
                {
                    continue;
                }
                constructor.Statements.Add(new CodeSnippetStatement(TABS + mapper.Map(column, Formatter)));
            }

            foreach (var fk in Table.ForeignKeys.Where(fk => !string.IsNullOrEmpty(fk.References)))
            {
                constructor.Statements.Add(new CodeSnippetStatement(mapper.Reference(fk, Formatter)));
            }

            Table.HasManyRelationships.ToList().ForEach(x => constructor.Statements.Add(new CodeSnippetStatement(mapper.Bag(x, Formatter))));

            newType.Members.Add(constructor);
            return(compileUnit);
        }
        public void ShouldMapDBColumnWithProperties()
        {
            var mapper = new DBColumnMapper();
            var column = new Column
            {
                Name           = "Name",
                DataLength     = 16,
                DataType       = "varchar",
                IsForeignKey   = false,
                IsNullable     = false,
                IsPrimaryKey   = false,
                MappedDataType = "string"
            };

            Assert.That(mapper.Map(column, new PascalCaseTextFormatter()), Is.EqualTo("Map(x => x.Name).Column(\"Name\").Not.Nullable().Length(16);"));
        }
示例#6
0
        public void ShouldMapDBColumnWithProperties()
        {
            var mapper = new DBColumnMapper(new TestApplicationSettings {
                GenerateColumnNameMapping = true
            });
            var column = new Column
            {
                Name           = "Name",
                DataLength     = 16,
                DataType       = "varchar",
                IsForeignKey   = false,
                IsNullable     = false,
                IsPrimaryKey   = false,
                MappedDataType = typeof(string)
            };

            Assert.That(mapper.Map(column, "Name", new PascalCaseTextFormatter()), Is.EqualTo("Map(x => x.Name).Column(\"Name\").Not.Nullable().Length(16);"));
        }
示例#7
0
        public CodeCompileUnit GetCompleteCompileUnit(string className)
        {
            var codeGenerationHelper = new CodeGenerationHelper();
            var compileUnit          = codeGenerationHelper.GetCodeCompileUnit(nameSpace, className);

            var newType = compileUnit.Namespaces[0].Types[0];

            newType.IsPartial = appPrefs.GeneratePartialClasses;
            var pascalCaseTextFormatter = new PascalCaseTextFormatter {
                PrefixRemovalList = appPrefs.FieldPrefixRemovalList
            };

            newType.BaseTypes.Add(string.Format("EntityTypeConfiguration<{0}{1}>", appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(Table.Name)));

            var constructor = new CodeConstructor {
                Attributes = MemberAttributes.Public
            };

            constructor.Statements.Add(new CodeSnippetStatement(TABS + "ToTable(\"" + Table.Name + "\");"));
            if (appPrefs.UseLazy)
            {
                constructor.Statements.Add(new CodeSnippetStatement(TABS + "LazyLoad();"));
            }

            if (UsesSequence)
            {
                var fieldName = FixPropertyWithSameClassName(Table.PrimaryKey.Columns[0].Name, Table.Name);
                constructor.Statements.Add(new CodeSnippetStatement(String.Format(TABS + "Id(x => x.{0}).Column(x => x.{1}).GeneratedBy.Sequence(\"{2}\")", Formatter.FormatText(fieldName), fieldName, appPrefs.Sequence)));
            }
            else if (Table.PrimaryKey != null && Table.PrimaryKey.Type == PrimaryKeyType.PrimaryKey)
            {
                var fieldName = FixPropertyWithSameClassName(Table.PrimaryKey.Columns[0].Name, Table.Name);
                constructor.Statements.Add(GetIdMapCodeSnippetStatement(appPrefs, Table, Table.PrimaryKey.Columns[0].Name, fieldName, Table.PrimaryKey.Columns[0].DataType, Formatter));
            }
            else if (Table.PrimaryKey != null)
            {
                constructor.Statements.Add(GetIdMapCodeSnippetStatement(Table.PrimaryKey, Table, Formatter));
            }

            // Many To One Mapping
            foreach (var fk in Table.ForeignKeys.Where(fk => fk.Columns.First().IsForeignKey&& appPrefs.IncludeForeignKeys))
            {
                var propertyName = appPrefs.NameFkAsForeignTable ? fk.UniquePropertyName : fk.Columns.First().Name;
                propertyName = Formatter.FormatSingular(propertyName);
                var fieldName = FixPropertyWithSameClassName(propertyName, Table.Name);

                var propertyMapType = "HasRequired";
                if (fk.IsNullable)
                {
                    propertyMapType = "HasOptional";
                }

                var codeSnippet = string.Format(TABS + "{0}(x => x.{1}).WithMany(t => t.{2}).HasForeignKey(d => d.{3});", propertyMapType, fieldName, fk.Columns.First().ForeignKeyTableName, fk.Columns.First().ForeignKeyColumnName);
                constructor.Statements.Add(new CodeSnippetStatement(codeSnippet));
            }

            var columnMapper = new DBColumnMapper(appPrefs);

            foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys)))
            {
                var propertyName  = Formatter.FormatText(column.Name);
                var fieldName     = FixPropertyWithSameClassName(propertyName, Table.Name);
                var columnMapping = columnMapper.Map(column, fieldName, Formatter, appPrefs.IncludeLengthAndScale);
                constructor.Statements.Add(new CodeSnippetStatement(TABS + columnMapping));
            }

            if (appPrefs.IncludeHasMany)
            {
                Table.HasManyRelationships.ToList().ForEach(x => constructor.Statements.Add(new EFOneToMany(Formatter, pascalCaseTextFormatter).Create(x)));
            }

            newType.Members.Add(constructor);
            return(compileUnit);
        }
示例#8
0
        public CodeCompileUnit GetCompleteCompileUnit(string mapName)
        {
            var codeGenerationHelper = new CodeGenerationHelper();
            var compileUnit          = codeGenerationHelper.GetCodeCompileUnit(nameSpace, mapName);

            var newType = compileUnit.Namespaces[0].Types[0];

            newType.IsPartial = appPrefs.GeneratePartialClasses;

            var className = Formatter.FormatSingular(Table.Name);

            switch (appPrefs.Language)
            {
            case Language.CSharp:
                newType.BaseTypes.Add(string.Format("ClassMapping<{0}{1}>", appPrefs.ClassNamePrefix, className));
                break;

            case Language.VB:
                newType.BaseTypes.Add(string.Format("ClassMapping(Of {0}{1})", appPrefs.ClassNamePrefix, className));
                break;
            }

            var constructor = new CodeConstructor {
                Attributes = MemberAttributes.Public
            };

            // Table Name - Only ouput if table is different than the class name.
            if (Table.Name.ToLower() != className.ToLower())
            {
                constructor.Statements.Add(new CodeSnippetStatement(TABS + "Table(\"" + Table.Name + "\");"));
            }
            // Scheme / Owner Name
            if (!string.IsNullOrEmpty(Table.Owner))
            {
                constructor.Statements.Add(new CodeSnippetStatement(TABS + "Schema(\"" + Table.Owner + "\");"));
            }

            constructor.Statements.Add(new CodeSnippetStatement(TABS + string.Format("Lazy({0});", appPrefs.UseLazy ? "true" : "false")));

            var mapper = new DBColumnMapper(appPrefs);

            // Id or ComposedId Map
            if (Table.PrimaryKey != null)
            {
                if (UsesSequence)
                {
                    constructor.Statements.Add(
                        new CodeSnippetStatement(TABS +
                                                 mapper.IdSequenceMap(Table.PrimaryKey.Columns[0], appPrefs.Sequence,
                                                                      Formatter)));
                }
                else if (Table.PrimaryKey.Type == PrimaryKeyType.PrimaryKey)
                {
                    constructor.Statements.Add(
                        new CodeSnippetStatement(TABS + mapper.IdMap(Table.PrimaryKey.Columns[0], Formatter)));
                }
                else if (Table.PrimaryKey.Type == PrimaryKeyType.CompositeKey)
                {
                    var pkColumns = Table.PrimaryKey.Columns;
                    constructor.Statements.Add(
                        new CodeSnippetStatement(TABS + mapper.CompositeIdMap(pkColumns, Formatter)));
                }
            }

            // Property Map
            foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys)))
            {
                constructor.Statements.Add(new CodeSnippetStatement(TABS + mapper.Map(column, Formatter, appPrefs.IncludeLengthAndScale)));
            }

            // Many To One Mapping
            foreach (var fk in Table.ForeignKeys.Where(fk => fk.Columns.First().IsForeignKey&& appPrefs.IncludeForeignKeys))
            {
                constructor.Statements.Add(new CodeSnippetStatement(mapper.Reference(fk, Formatter)));
            }

            // Bag
            Table.HasManyRelationships.ToList().ForEach(x => constructor.Statements.Add(new CodeSnippetStatement(mapper.Bag(x, Formatter))));

            newType.Members.Add(constructor);

            // Strip out any semicolons
            if (appPrefs.Language == Language.VB)
            {
                foreach (CodeSnippetStatement statement in constructor.Statements)
                {
                    statement.Value = statement.Value.Replace(";", string.Empty);
                }
            }

            return(compileUnit);
        }
        public CodeCompileUnit GetCompleteCompileUnit(string className)
        {
            var codeGenerationHelper = new CodeGenerationHelper();
            var compileUnit          = codeGenerationHelper.GetCodeCompileUnit(nameSpace, className);

            var newType = compileUnit.Namespaces[0].Types[0];

            newType.IsPartial = appPrefs.GeneratePartialClasses;
            var pascalCaseTextFormatter = new PascalCaseTextFormatter {
                PrefixRemovalList = appPrefs.FieldPrefixRemovalList
            };

            newType.BaseTypes.Add(string.Format("ClassMap<{0}{1}>", appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(Table.Name)));

            var constructor = new CodeConstructor {
                Attributes = MemberAttributes.Public
            };

            constructor.Statements.Add(new CodeSnippetStatement(TABS + "Table(\"" + Table.Name + "\");"));
            if (appPrefs.UseLazy)
            {
                constructor.Statements.Add(new CodeSnippetStatement(TABS + "LazyLoad();"));
            }

            if (UsesSequence)
            {
                var fieldName = FixPropertyWithSameClassName(Table.PrimaryKey.Columns[0].Name, Table.Name);
                constructor.Statements.Add(new CodeSnippetStatement(String.Format(TABS + "Id(x => x.{0}).Column(x => x.{1}).GeneratedBy.Sequence(\"{2}\")",
                                                                                  Formatter.FormatText(fieldName), fieldName, appPrefs.Sequence)));
            }
            else if (Table.PrimaryKey != null && Table.PrimaryKey.Type == PrimaryKeyType.PrimaryKey)
            {
                var fieldName = FixPropertyWithSameClassName(Table.PrimaryKey.Columns[0].Name, Table.Name);
                constructor.Statements.Add(GetIdMapCodeSnippetStatement(this.appPrefs, Table, Table.PrimaryKey.Columns[0].Name, fieldName, Table.PrimaryKey.Columns[0].DataType, Formatter));
            }
            else if (Table.PrimaryKey != null)
            {
                constructor.Statements.Add(GetIdMapCodeSnippetStatement(Table.PrimaryKey, Table, Formatter));
            }

            // Many To One Mapping
            foreach (var fk in Table.ForeignKeys.Where(fk => fk.Columns.First().IsForeignKey&& appPrefs.IncludeForeignKeys))
            {
                var    propertyName = appPrefs.NameFkAsForeignTable ? fk.UniquePropertyName : fk.Columns.First().Name;
                string name         = propertyName;
                propertyName = Formatter.FormatSingular(propertyName);
                var fieldName   = FixPropertyWithSameClassName(propertyName, Table.Name);
                var pkAlsoFkQty = (from fks in Table.ForeignKeys.Where(fks => fks.UniquePropertyName == name) select fks).Count();
                if (pkAlsoFkQty > 1)
                {
                    constructor.Statements.Add(new CodeSnippetStatement(string.Format(TABS + "References(x => x.{0}).Column(\"{1}\").ForeignKey(\"{2}\");", fieldName, fk.Columns.First().Name, fk.Columns.First().ConstraintName)));
                }
                else
                {
                    constructor.Statements.Add(new CodeSnippetStatement(string.Format(TABS + "References(x => x.{0}).Column(\"{1}\");", fieldName, fk.Columns.First().Name)));
                }
            }

            // Property Map
            foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys)))
            {
                var propertyName  = Formatter.FormatText(column.Name);
                var fieldName     = FixPropertyWithSameClassName(propertyName, Table.Name);
                var columnMapping = new DBColumnMapper().Map(column, fieldName, Formatter, appPrefs.IncludeLengthAndScale);
                constructor.Statements.Add(new CodeSnippetStatement(TABS + columnMapping));
            }

            // Bag (HasMany in FluentMapping)
            if (appPrefs.IncludeHasMany)
            {
                Table.HasManyRelationships.ToList().ForEach(x => constructor.Statements.Add(new OneToMany(Formatter).Create(x)));
            }

            newType.Members.Add(constructor);
            return(compileUnit);
        }