Esempio n. 1
0
 private static ClassDeclarationSyntax GenerateAllTableList(IReadOnlyList <TableModel> tables, string tablePrefix)
 {
     return(SyntaxFactory.ClassDeclaration(AllTablesClassName)
            .WithModifiers(SyntaxHelpers.Modifiers(SyntaxKind.PublicKeyword, SyntaxKind.StaticKeyword))
            .AddMembers(GenerateMethods(tables, tablePrefix))
            .NormalizeWhitespace());
 }
        private static IEnumerable <CdPath> ExploreTableDescriptors(this IEnumerable <SyntaxTree> fileNodes)
        {
            foreach (var nodePath in fileNodes)
            {
                var classes = nodePath.GetRoot()
                              .DescendantNodesAndSelf()
                              .OfType <ClassDeclarationSyntax>()
                              .Select(cd => (Class: cd, BaseTypeKind: SyntaxHelpers.GetTableClassKind(cd)))
                              .Where(p => p.BaseTypeKind != null);

                foreach (var tuple in classes)
                {
                    ConstructorInitializerSyntax?baseConstCall = tuple.Class
                                                                 .DescendantNodes()
                                                                 .OfType <ConstructorInitializerSyntax>()
                                                                 .FirstOrDefault(c =>
                                                                                 c.Kind() == SyntaxKind.BaseConstructorInitializer);

                    BaseTypeKindTag baseTypeKindTag = tuple.BaseTypeKind !.Value;

                    if (baseTypeKindTag == BaseTypeKindTag.DerivedTableBase)
                    {
                        yield return(new CdPath(tuple.Class, baseTypeKindTag, null));
                    }
                    else if (baseConstCall != null)
                    {
                        string schema;
                        string tableName;

                        if (baseTypeKindTag == BaseTypeKindTag.TableBase)
                        {
                            if (baseConstCall.ArgumentList.Arguments.Count != 3)
                            {
                                continue;
                            }

                            if (baseConstCall.ArgumentList.Arguments[0].Expression is LiteralExpressionSyntax slSh &&
                                slSh.Kind() == SyntaxKind.StringLiteralExpression)
                            {
                                schema = slSh.Token.ValueText;
                            }
                            else
                            {
                                continue;
                            }

                            if (baseConstCall.ArgumentList.Arguments[1].Expression is LiteralExpressionSyntax slDb &&
                                slDb.Kind() == SyntaxKind.StringLiteralExpression)
                            {
                                tableName = slDb.Token.ValueText;
                            }
                            else
                            {
                                continue;
                            }
                        }
Esempio n. 3
0
        private static MemberDeclarationSyntax[] GenerateMethods(IReadOnlyList <TableModel> tables, string tablePrefix)
        {
            var result = new List <MemberDeclarationSyntax>(tables.Count * 2 + 2);

            var identifierAliasType = SyntaxFactory.IdentifierName(nameof(Alias));

            var arrayItems = tables.Select(t => SyntaxFactory.IdentifierName(GetMethodName(t, tablePrefix)).Invoke(identifierAliasType.MemberAccess(nameof(Alias.Empty))));
            var arrayType  = SyntaxFactory.ArrayType(SyntaxFactory.IdentifierName(nameof(TableBase)),
                                                     new SyntaxList <ArrayRankSpecifierSyntax>(new[]
            {
                SyntaxFactory.ArrayRankSpecifier(SyntaxFactory.Token(SyntaxKind.OpenBracketToken),
                                                 new SeparatedSyntaxList <ExpressionSyntax>(),
                                                 SyntaxFactory.Token(SyntaxKind.CloseBracketToken))
            }));
            var array = SyntaxFactory.ArrayCreationExpression(
                arrayType,
                SyntaxFactory.InitializerExpression(SyntaxKind.ArrayInitializerExpression,
                                                    new SeparatedSyntaxList <ExpressionSyntax>().AddRange(arrayItems))
                );

            result.Add(
                SyntaxFactory.MethodDeclaration(arrayType, "BuildAllTableList")
                .WithModifiers(SyntaxHelpers.Modifiers(SyntaxKind.PublicKeyword, SyntaxKind.StaticKeyword))
                .WithExpressionBody(SyntaxFactory.ArrowExpressionClause(
                                        array
                                        ))
                .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));

            foreach (var t in tables)
            {
                var aliasParamName = "alias";

                result.Add(SyntaxFactory.MethodDeclaration(SyntaxFactory.ParseTypeName(t.Name), GetMethodName(t, tablePrefix))
                           .WithModifiers(SyntaxHelpers.Modifiers(SyntaxKind.PublicKeyword, SyntaxKind.StaticKeyword))
                           .AddParameterListParameters(SyntaxHelpers.FuncParameter(aliasParamName, nameof(Alias)))
                           .WithExpressionBody(SyntaxFactory.ArrowExpressionClause(
                                                   SyntaxFactory.ObjectCreationExpression(SyntaxFactory.ParseTypeName(t.Name),
                                                                                          SyntaxHelpers.ArgumentList(SyntaxFactory.IdentifierName(aliasParamName)),
                                                                                          null)))
                           .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));

                result.Add(SyntaxFactory.MethodDeclaration(SyntaxFactory.ParseTypeName(t.Name), GetMethodName(t, tablePrefix))
                           .WithModifiers(SyntaxHelpers.Modifiers(SyntaxKind.PublicKeyword, SyntaxKind.StaticKeyword))
                           .WithExpressionBody(SyntaxFactory.ArrowExpressionClause(
                                                   SyntaxFactory.ObjectCreationExpression(SyntaxFactory.ParseTypeName(t.Name),
                                                                                          SyntaxHelpers.ArgumentList(identifierAliasType.MemberAccess(nameof(Alias.Auto))),
                                                                                          null)))
                           .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));
            }

            return(result.ToArray());
Esempio n. 4
0
        public static IEnumerable <SqModelMetaRaw> ParseAttribute(this IEnumerable <AttributeSyntax> attributes, bool nullRefTypes)
        {
            foreach (var attribute in attributes)
            {
                var propertySyntax = attribute.FindParentOrDefault <PropertyDeclarationSyntax>() !;

                var classSyntax = propertySyntax.FindParentOrDefault <ClassDeclarationSyntax>() !;

                var baseTypeKindTag = SyntaxHelpers.GetTableClassKind(classSyntax)
                                      ?? throw new SqExpressCodeGenException($"Unknown base class in '{classSyntax.Identifier.ValueText}'");

                var namespaceSyntax = classSyntax.FindParentOrDefault <NamespaceDeclarationSyntax>() !;

                var tableNamespace = namespaceSyntax?.Name.ToString() ?? string.Empty;

                var tableName = classSyntax.Identifier.ValueText;

                var columnName = propertySyntax.Identifier.ValueText;

                var identity = AnalyzeColumnMetadata(classSyntax: classSyntax, columnName: columnName);

                var modelName = (attribute.ArgumentList?.Arguments.FirstOrDefault()?.Expression as LiteralExpressionSyntax)?.Token.ValueText
                                ?? throw new SqExpressCodeGenException($"Could not find {nameof(SqModelAttribute)} name parameter");

                var modelPropertyName =
                    GetAttributeProperty <LiteralExpressionSyntax>(attribute, nameof(SqModelAttribute.PropertyName))
                    ?.Token.ValueText ??
                    columnName;

                var castType =
                    GetAttributeProperty <TypeOfExpressionSyntax>(attribute, nameof(SqModelAttribute.CastType))
                    ?.Type.ToString();

                var clrType = ColumnPropertyTypeParser.Parse(
                    propertySyntax.Type.ToString(),
                    ModelColumnClrTypeGenerator.Instance,
                    nullRefTypes);

                yield return(new SqModelMetaRaw(
                                 modelName: modelName,
                                 fieldName: modelPropertyName,
                                 fieldTypeName: clrType,
                                 castTypeName: castType,
                                 tableNamespace: tableNamespace,
                                 tableName: tableName,
                                 columnName: columnName,
                                 isPrimaryKey: identity.Pk,
                                 isIdentity: identity.Idendity,
                                 baseTypeKindTag: baseTypeKindTag));
            }