Ejemplo n.º 1
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);
        }
Ejemplo n.º 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;

            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);
        }