예제 #1
0
        private SyntaxNode[] GeneratePkParams(SyntaxGenerator g, NamespaceSet imports, DataSet <PrimaryKeyEntry> entries)
        {
            var _      = entries._;
            var result = new SyntaxNode[entries.Count];

            for (int i = 0; i < result.Length; i++)
            {
                var type = _.Column[i].Type;
                imports.Add(type);
                var        parameter     = g.ParameterDeclaration(_.ConstructorParamName[i], g.IdentifierName(type.Name));
                var        sortDirection = _.SortDirection[i];
                SyntaxNode sortAttribute = null;
                if (sortDirection == SortDirection.Ascending)
                {
                    sortAttribute = g.Attribute(g.IdentifierName("Asc"));
                }
                else if (sortDirection == SortDirection.Descending)
                {
                    sortAttribute = g.Attribute(g.IdentifierName("Desc"));
                }
                if (sortAttribute != null)
                {
                    imports.Add(Compilation.GetKnownType(KnownTypes.AscAttribute));
                    parameter = g.AddAttributes(parameter, sortAttribute);
                }
                result[i] = parameter;
            }

            return(result);
        }
예제 #2
0
        private async Task<Document> AddDbTableAsync(INamedTypeSymbol modelType, string name, string dbName, string description, CancellationToken ct = default(CancellationToken))
        {
            var editor = await DocumentEditor.CreateAsync(Document, ct);
            var g = editor.Generator;
            var imports = new NamespaceSet
            {
                modelType,
                Compilation.GetKnownType(KnownTypes.DbTableOf)
            };
            var needDbTableAttribute = NeedTableAttribute(dbName, description);
            if (needDbTableAttribute)
                imports.Add(Compilation.GetKnownType(KnownTypes.DbTableAttribute));

            var backingFieldName = GetBackingFieldName(name);
            var backingFieldAnnotation = new SyntaxAnnotation();
            var fieldDeclaration = g.FieldDeclaration(backingFieldName, GenerateDbTableType(g, modelType), Accessibility.Private).WithAdditionalAnnotations(Formatter.Annotation, backingFieldAnnotation);

            var getTable = g.InvocationExpression(g.IdentifierName("GetTable"), g.Argument(RefKind.Ref, g.IdentifierName(backingFieldName)));
            var propertyGetter = g.ReturnStatement(getTable);
            var property = g.PropertyDeclaration(name, GenerateDbTableType(g, modelType), Accessibility.Public, DeclarationModifiers.ReadOnly, getAccessorStatements: new SyntaxNode[] { propertyGetter });
            if (needDbTableAttribute)
                property = GenerateDbTableAttribute(g, property, dbName, description);
            var propertyAnnotation = new SyntaxAnnotation();
            property = property.WithAdditionalAnnotations(Formatter.Annotation, propertyAnnotation);

            await AddMissingNamespaces(editor, imports, ct);
            editor.AddMember(DbClass, fieldDeclaration);
            editor.AddMember(DbClass, property);

            var result = await KeepTogetherAsync(await editor.FormatAsync(ct), backingFieldAnnotation, propertyAnnotation, ct);
            return result;
        }
예제 #3
0
        public void AddMissingNamespaces_VB()
        {
            var src =
                @"Namespace Test
{
}
";

            var document    = src.CreateDocument(LanguageNames.VisualBasic);
            var compilation = document.Project.GetCompilationAsync().Result;
            var namespaces  = new NamespaceSet();

            namespaces.Add(compilation.GetKnownType(KnownTypes.Attribute));

            var editor = DocumentEditor.CreateAsync(document).Result;

            editor.AddMissingNamespaces(namespaces, null, ImportedNamespace.AddImports, CancellationToken.None).Wait();

            var expected =
                @"Imports System

Namespace Test
{
}
";

            Assert.AreEqual(expected, editor.FormatAsync().Result.GetSourceCode());
        }
예제 #4
0
        public static void Add(this NamespaceSet namespaces, ITypeSymbol type)
        {
            var containingNamespace = type.ContainingNamespace;

            if (containingNamespace != null)
            {
                namespaces.Add(containingNamespace);
            }
        }
예제 #5
0
        private SyntaxNode GenerateProjectionClass(SyntaxGenerator g, NamespaceSet imports, string className, DataSet <ProjectionEntry> entries)
        {
            imports.Add(Compilation.GetKnownType(KnownTypes.Projection));
            var baseType    = g.IdentifierName("Projection");
            var constructor = GenerateStaticConstructorForColumnRegistration(g, imports, Language, ModelType, className, entries);
            var properties  = GenerateColumnProperties(g, imports, entries);

            return(g.ClassDeclaration(className, accessibility: Accessibility.Public, baseType: baseType, members: constructor.Concat(null, properties)));
        }
예제 #6
0
        private static SyntaxNode GenerateMounterIdentifier(SyntaxGenerator g, NamespaceSet imports, INamedTypeSymbol modelType, IFieldSymbol mounter)
        {
            if (mounter.ContainingType == modelType)
            {
                return(g.IdentifierName(mounter.Name));
            }

            imports.Add(mounter.ContainingType);
            return(g.QualifiedName(g.IdentifierName(mounter.ContainingType.Name), g.IdentifierName(mounter.Name)));
        }
예제 #7
0
        private SyntaxNode GenerateKeyOrRefClass(SyntaxGenerator g, NamespaceSet imports, string className, string baseTypeName, string pkTypeName, string pkCreateMethodName, DataSet <PrimaryKeyEntry> entries)
        {
            imports.Add(Compilation.GetKnownType(KnownTypes.KeyOf));
            var baseType    = g.GenericName(baseTypeName, g.IdentifierName(pkTypeName));
            var constructor = GenerateStaticConstructorForColumnRegistration(g, imports, Language, ModelType, className, entries);
            var pkCreate    = GeneratePkCreate(g, pkCreateMethodName, pkTypeName, entries);
            var properties  = GenerateColumnProperties(g, imports, entries);

            return(g.ClassDeclaration(className, accessibility: Accessibility.Public, baseType: baseType, members: constructor.Concat(pkCreate, properties)));
        }
예제 #8
0
        private SyntaxNode GeneratePkClass(SyntaxGenerator g, NamespaceSet imports, string pkTypeName, DataSet <PrimaryKeyEntry> entries)
        {
            imports.Add(Compilation.GetKnownType(KnownTypes.CandidateKey));
            var baseType    = g.IdentifierName(nameof(KnownTypes.CandidateKey));
            var parameters  = GeneratePkParams(g, imports, entries);
            var arguments   = GeneratePkArguments(g, entries);
            var constructor = g.ConstructorDeclaration(pkTypeName, accessibility: Accessibility.Public, parameters: parameters, baseConstructorArguments: arguments);

            return(g.ClassDeclaration(pkTypeName, accessibility: Accessibility.Public, modifiers: DeclarationModifiers.Sealed, baseType: baseType, members: new SyntaxNode[] { constructor }));
        }
예제 #9
0
        private async Task<Document> AddRelationshipAsync(IPropertySymbol dbTable, string name, IPropertySymbol foreignKey, IPropertySymbol refTable,
            string description, ForeignKeyRule deleteRule, ForeignKeyRule updateRule, CancellationToken ct = default(CancellationToken))
        {
            var editor = await DocumentEditor.CreateAsync(Document, ct);
            var g = editor.Generator;
            var declarationAttributeType = Compilation.GetKnownType(KnownTypes.RelationshipAttribute);
            var ruleType = deleteRule != ForeignKeyRule.None || updateRule != ForeignKeyRule.None ? Compilation.GetKnownType(KnownTypes.ForeignKeyRule) : null;
            var implementationAttributeType = Compilation.GetKnownType(KnownTypes._RelationshipAttribute);
            var modelType = dbTable.GetModelType();
            var keyMappingType = Compilation.GetKnownType(KnownTypes.KeyMapping);
            var imports = new NamespaceSet
            {
                declarationAttributeType,
                implementationAttributeType,
                modelType,
                keyMappingType
            };
            if (ruleType != null)
                imports.Add(ruleType);

            var paramName = ModelParamName;
            var methodBody = GenerateImplementationMethodBody();
            var method = g.MethodDeclaration(name, new SyntaxNode[] { g.ParameterDeclaration(paramName, g.IdentifierName(modelType.Name)) }, null,
                g.IdentifierName(keyMappingType.Name), Accessibility.Private, default(DeclarationModifiers), statements: new SyntaxNode[] { methodBody });
            method = GenerateAttribute(method, implementationAttributeType.Name.ToAttributeName()).WithAdditionalAnnotations(Formatter.Annotation);

            editor.AddMember(DbClass, method);

            var argument = g.NameOfExpression(g.IdentifierName(name));
            var arguments = g.AttributeArgument(argument).Concat(GenerateAdditionalArguments()).ToArray();
            var dbTableNode = GetSyntaxNode(dbTable);
            editor.ReplaceNode(dbTableNode, GenerateAttribute(dbTableNode, declarationAttributeType.Name.ToAttributeName(), GetLeadingWhitespaceTrivia(dbTableNode), arguments));

            await AddMissingNamespaces(editor, imports, ct);

            return await editor.FormatAsync(ct);

            IEnumerable<SyntaxNode> GenerateAdditionalArguments()
            {
                if (!string.IsNullOrWhiteSpace(description))
                    yield return g.AttributeArgument("Description", g.LiteralExpression(description));
                if (deleteRule != ForeignKeyRule.None)
                    yield return g.AttributeArgument("DeleteRule", g.DottedName(string.Format("{0}.{1}", ruleType.Name, deleteRule)));
                if (updateRule != ForeignKeyRule.None)
                    yield return g.AttributeArgument("UpdateRule", g.DottedName(string.Format("{0}.{1}", ruleType.Name, updateRule)));
            }

            SyntaxNode GenerateImplementationMethodBody()
            {
                var fkExpr = g.DottedName(string.Format("{0}.{1}", paramName, foreignKey.Name));
                var joinParam = dbTable == refTable ? g.IdentifierName(paramName) : GenerateJoinParam(g, refTable);
                return g.ReturnStatement(g.InvocationExpression(g.MemberAccessExpression(fkExpr, "Join"), joinParam));
            }
        }
예제 #10
0
        private IEnumerable <SyntaxNode> GenerateColumnProperties <T>(SyntaxGenerator g, NamespaceSet imports, DataSet <T> entries)
            where T : ProjectionEntry, new()
        {
            var _ = entries._;

            for (int i = 0; i < entries.Count; i++)
            {
                var column = _.Column[i];
                var type   = column.Type;
                imports.Add(type);
                foreach (var result in GenerateColumnProperty(g, type, column.Name))
                {
                    yield return(result);
                }
            }
        }
예제 #11
0
        private async Task <(Document Document, SyntaxAnnotation PropertyAnnotation)> AddCheckPropertyAsync(string name,
                                                                                                            INamedTypeSymbol modelAttributeType, INamedTypeSymbol modelMemberAttributeType, CancellationToken ct = default(CancellationToken), params INamedTypeSymbol[] typesToImport)
        {
            var editor = await DocumentEditor.CreateAsync(Document, ct);

            var g           = editor.Generator;
            var booleanType = Compilation.GetKnownType(KnownTypes._Boolean);
            var imports     = new NamespaceSet
            {
                modelAttributeType,
                modelMemberAttributeType,
                booleanType
            };

            if (typesToImport != null && typesToImport.Length > 0)
            {
                for (int i = 0; i < typesToImport.Length; i++)
                {
                    var typeToImport = typesToImport[i];
                    if (typeToImport != null)
                    {
                        imports.Add(typeToImport);
                    }
                }
            }

            var propertyGetter     = g.GenerateNotImplemented(Compilation);
            var property           = g.PropertyDeclaration(name, g.IdentifierName(booleanType.Name), Accessibility.Private, DeclarationModifiers.ReadOnly, getAccessorStatements: new SyntaxNode[] { propertyGetter });
            var propertyAnnotation = new SyntaxAnnotation();

            property = GenerateAttribute(property, modelMemberAttributeType.Name.ToAttributeName()).WithAdditionalAnnotations(Formatter.Annotation, propertyAnnotation);

            editor.AddMember(ModelClass, property);

            await AddMissingNamespaces(editor, imports, ct);

            var result = await editor.FormatAsync(ct);

            return(result, propertyAnnotation);
        }
        private async Task <(Document Document, SyntaxAnnotation MethodAnnotation)> AddComputationMethodAsync(string name,
                                                                                                              INamedTypeSymbol modelAttributeType, INamedTypeSymbol modelMemberAttributeType, CancellationToken ct = default(CancellationToken), params INamedTypeSymbol[] typesToImport)
        {
            var editor = await DocumentEditor.CreateAsync(Document, ct);

            var g        = editor.Generator;
            var voidType = Compilation.GetSpecialType(SpecialType.System_Void);
            var imports  = new NamespaceSet
            {
                modelAttributeType,
                modelMemberAttributeType
            };

            if (typesToImport != null && typesToImport.Length > 0)
            {
                for (int i = 0; i < typesToImport.Length; i++)
                {
                    var typeToImport = typesToImport[i];
                    if (typeToImport != null)
                    {
                        imports.Add(typeToImport);
                    }
                }
            }

            var methodBody       = g.GenerateNotImplemented(Compilation);
            var method           = g.MethodDeclaration(name, null, null, null, Accessibility.Private, default(DeclarationModifiers), statements: new SyntaxNode[] { methodBody });
            var methodAnnotation = new SyntaxAnnotation();

            method = GenerateAttribute(method, modelMemberAttributeType.Name.ToAttributeName()).WithAdditionalAnnotations(Formatter.Annotation, methodAnnotation);

            editor.AddMember(ModelClass, method);

            await AddMissingNamespaces(editor, imports, ct);

            var result = await editor.FormatAsync(ct);

            return(result, methodAnnotation);
        }
예제 #13
0
        private static NamespaceSet GetImports(this DataSet <DataSetEntry> entries, Compilation compilation)
        {
            var result = new NamespaceSet();
            var _      = entries._;

            for (int i = 0; i < entries.Count; i++)
            {
                var referencedTypes = _.ReferencedTypes[i];
                using (StringReader reader = new StringReader(referencedTypes))
                {
                    for (var referencedType = reader.ReadLine(); referencedType != null; referencedType = reader.ReadLine())
                    {
                        var type = compilation.GetTypeByMetadataName(referencedType);
                        if (type != null)
                        {
                            result.Add(type);
                        }
                    }
                }
            }

            return(result);
        }