public override void Visit(CreateProcedureStatement node)
        {
            var    rawProcedureName             = node.ProcedureReference.Name.BaseIdentifier.Value;
            string procedureName                = GetMemberNameWithoutVersionSuffix(node.ProcedureReference.Name);
            string schemaQualifiedProcedureName = $"{node.ProcedureReference.Name.SchemaIdentifier.Value}.{rawProcedureName}";
            string className = $"{procedureName}Procedure";

            ClassDeclarationSyntax classDeclarationSyntax =
                ClassDeclaration(className)
                .WithModifiers(TokenList(Token(SyntaxKind.InternalKeyword)))

                // derive from StoredProcedure
                .WithBaseList(
                    BaseList(
                        SingletonSeparatedList <BaseTypeSyntax>(
                            SimpleBaseType(
                                IdentifierName("StoredProcedure")))))

                // call base("dbo.StoredProcedure")
                .AddMembers(
                    ConstructorDeclaration(
                        Identifier(className))
                    .WithModifiers(
                        TokenList(
                            Token(SyntaxKind.InternalKeyword)))
                    .WithInitializer(
                        ConstructorInitializer(
                            SyntaxKind.BaseConstructorInitializer,
                            ArgumentList(
                                SingletonSeparatedList(
                                    Argument(
                                        LiteralExpression(
                                            SyntaxKind.StringLiteralExpression,
                                            Literal(schemaQualifiedProcedureName)))))))
                    .WithBody(Block()))

                // add fields for each parameter
                .AddMembers(node.Parameters.Select(CreateFieldForParameter).ToArray())

                // add the PopulateCommand method
                .AddMembers(AddPopulateCommandMethod(node, schemaQualifiedProcedureName), AddPopulateCommandMethodForTableValuedParameters(node, procedureName));

            FieldDeclarationSyntax fieldDeclarationSyntax = CreateStaticFieldForClass(className, procedureName);

            var(tvpGeneratorClass, tvpHolderStruct) = CreateTvpGeneratorTypes(node, procedureName);

            MembersToAdd.Add(classDeclarationSyntax.AddSortingKey(this, procedureName));
            MembersToAdd.Add(fieldDeclarationSyntax.AddSortingKey(this, procedureName));
            MembersToAdd.Add(tvpGeneratorClass.AddSortingKey(this, procedureName));
            MembersToAdd.Add(tvpHolderStruct.AddSortingKey(this, procedureName));

            base.Visit(node);
        }
コード例 #2
0
        public override void Visit(CreateTableStatement node)
        {
            string tableName = node.SchemaObjectName.BaseIdentifier.Value;
            string schemaQualifiedTableName = $"{node.SchemaObjectName.SchemaIdentifier.Value}.{tableName}";
            string className = GetClassNameForTable(tableName);

            ClassDeclarationSyntax classDeclarationSyntax =
                CreateSkeletalClass(className, schemaQualifiedTableName)
                .AddMembers(node.Definition.ColumnDefinitions.Select(CreatePropertyForTableColumn).ToArray());

            FieldDeclarationSyntax field = CreateStaticFieldForClass(className, tableName);

            MembersToAdd.Add(field.AddSortingKey(this, tableName));
            MembersToAdd.Add(classDeclarationSyntax.AddSortingKey(this, tableName));

            base.Visit(node);
        }
コード例 #3
0
        public override void Visit(CreateTableStatement node)
        {
            string tableName = node.SchemaObjectName.BaseIdentifier.Value;
            string schemaQualifiedTableName = $"{node.SchemaObjectName.SchemaIdentifier.Value}.{tableName}";
            string className = $"{tableName}Table";

            ClassDeclarationSyntax classDeclarationSyntax =
                ClassDeclaration(className)
                .WithModifiers(TokenList(Token(SyntaxKind.InternalKeyword)))
                .WithBaseList(
                    BaseList(
                        SingletonSeparatedList <BaseTypeSyntax>(
                            SimpleBaseType(
                                IdentifierName("Table")))))
                .AddMembers(
                    ConstructorDeclaration(
                        Identifier(className))
                    .WithModifiers(
                        TokenList(
                            Token(SyntaxKind.InternalKeyword)))
                    .WithInitializer(
                        ConstructorInitializer(
                            SyntaxKind.BaseConstructorInitializer,
                            ArgumentList(
                                SingletonSeparatedList(
                                    Argument(
                                        LiteralExpression(
                                            SyntaxKind.StringLiteralExpression,
                                            Literal(schemaQualifiedTableName)))))))
                    .WithBody(Block()))
                .AddMembers(node.Definition.ColumnDefinitions.Select(CreatePropertyForColumn).ToArray());

            FieldDeclarationSyntax field = CreateStaticFieldForClass(className, tableName);

            MembersToAdd.Add(field.AddSortingKey(this, tableName));
            MembersToAdd.Add(classDeclarationSyntax.AddSortingKey(this, tableName));

            base.Visit(node);
        }
        public override void Visit(CreateTypeTableStatement node)
        {
            string tableTypeName = node.Name.BaseIdentifier.Value;

            if (!_visitedTypes.Add(tableTypeName))
            {
                // This table type has already been added from a previous .sql file.
                return;
            }

            string schemaQualifiedTableTypeName = $"{node.Name.SchemaIdentifier.Value}.{tableTypeName}";
            string className     = GetClassNameForTableValuedParameterDefinition(node.Name);
            string rowStructName = GetRowStructNameForTableType(node.Name);

            TypeSyntax columnsEnumerableType = TypeExtensions.CreateGenericTypeFromGenericTypeDefinition(
                typeof(IEnumerable <>).ToTypeSyntax(true),
                IdentifierName("Column"));

            ArrayTypeSyntax columnsArrayType = ArrayType(IdentifierName("Column")).AddRankSpecifiers(ArrayRankSpecifier());

            ClassDeclarationSyntax classDeclarationSyntax =
                ClassDeclaration(className)
                .WithModifiers(TokenList(Token(SyntaxKind.InternalKeyword)))
                .AddBaseListTypes(
                    SimpleBaseType(
                        GenericName("TableValuedParameterDefinition")
                        .AddTypeArgumentListArguments(IdentifierName(rowStructName))))
                .AddMembers(
                    ConstructorDeclaration(
                        Identifier(className))
                    .WithModifiers(
                        TokenList(
                            Token(SyntaxKind.InternalKeyword)))
                    .AddParameterListParameters(
                        Parameter(Identifier("parameterName")).WithType(typeof(string).ToTypeSyntax(true)))
                    .WithInitializer(
                        ConstructorInitializer(
                            SyntaxKind.BaseConstructorInitializer,
                            ArgumentList(SeparatedList(new[]
            {
                Argument(IdentifierName("parameterName")),
                Argument(
                    LiteralExpression(
                        SyntaxKind.StringLiteralExpression,
                        Literal(schemaQualifiedTableTypeName))),
            }))))
                    .WithBody(Block()))
                .AddMembers(node.Definition.ColumnDefinitions.Select(CreatePropertyForTableColumn).ToArray())

                // Add Columns property override
                .AddMembers(
                    PropertyDeclaration(
                        columnsEnumerableType,
                        Identifier("Columns"))
                    .AddModifiers(Token(SyntaxKind.ProtectedKeyword), Token(SyntaxKind.OverrideKeyword))
                    .WithExpressionBody(
                        ArrowExpressionClause(
                            ArrayCreationExpression(columnsArrayType)
                            .WithInitializer(
                                InitializerExpression(
                                    SyntaxKind.ArrayInitializerExpression,
                                    SeparatedList <ExpressionSyntax>(
                                        node.Definition.ColumnDefinitions.Select(c => IdentifierName(c.ColumnIdentifier.Value)))))))
                    .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)))

                // Add FillSqlDataRecord implementation
                .AddMembers(
                    MethodDeclaration(typeof(void).ToTypeSyntax(), Identifier("FillSqlDataRecord"))
                    .AddModifiers(Token(SyntaxKind.ProtectedKeyword), Token(SyntaxKind.OverrideKeyword))
                    .AddParameterListParameters(
                        Parameter(Identifier("record")).WithType(typeof(SqlDataRecord).ToTypeSyntax(useGlobalAlias: true)),
                        Parameter(
                            Identifier("rowData")).WithType(IdentifierName(rowStructName)))
                    .WithBody(
                        Block(node.Definition.ColumnDefinitions.Select((c, i) =>
                                                                       ExpressionStatement(
                                                                           InvocationExpression(
                                                                               MemberAccessExpression(
                                                                                   SyntaxKind.SimpleMemberAccessExpression,
                                                                                   IdentifierName(c.ColumnIdentifier.Value),
                                                                                   IdentifierName("Set")))
                                                                           .AddArgumentListArguments(
                                                                               Argument(IdentifierName("record")),
                                                                               Argument(
                                                                                   LiteralExpression(
                                                                                       SyntaxKind.NumericLiteralExpression,
                                                                                       Literal(i))),
                                                                               Argument(
                                                                                   MemberAccessExpression(
                                                                                       SyntaxKind.SimpleMemberAccessExpression,
                                                                                       IdentifierName("rowData"),
                                                                                       IdentifierName(c.ColumnIdentifier.Value)))))))));

            StructDeclarationSyntax rowStruct = StructDeclaration(rowStructName)
                                                .AddModifiers(Token(SyntaxKind.InternalKeyword))

                                                // Add a constructor with parameters for each column, setting the associate property for each column.
                                                .AddMembers(
                ConstructorDeclaration(
                    Identifier(rowStructName))
                .WithModifiers(
                    TokenList(
                        Token(SyntaxKind.InternalKeyword)))
                .AddParameterListParameters(
                    node.Definition.ColumnDefinitions.Select(c =>
                                                             Parameter(Identifier(c.ColumnIdentifier.Value))
                                                             .WithType(DataTypeReferenceToClrType(c.DataType, IsColumnNullable(c)))).ToArray())
                .WithBody(
                    Block(node.Definition.ColumnDefinitions.Select(c =>
                                                                   ExpressionStatement(
                                                                       AssignmentExpression(
                                                                           SyntaxKind.SimpleAssignmentExpression,
                                                                           left: MemberAccessExpression(
                                                                               SyntaxKind.SimpleMemberAccessExpression,
                                                                               ThisExpression(),
                                                                               IdentifierName(c.ColumnIdentifier.Value)),
                                                                           right: IdentifierName(c.ColumnIdentifier.Value)))))))

                                                // Add a property for each column
                                                .AddMembers(node.Definition.ColumnDefinitions.Select(c =>
                                                                                                     (MemberDeclarationSyntax)PropertyDeclaration(
                                                                                                         DataTypeReferenceToClrType(c.DataType, IsColumnNullable(c)),
                                                                                                         Identifier(c.ColumnIdentifier.Value))
                                                                                                     .AddModifiers(Token(SyntaxKind.InternalKeyword))
                                                                                                     .AddAccessorListAccessors(AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                                                                                                                               .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)))).ToArray());

            MembersToAdd.Add(classDeclarationSyntax.AddSortingKey(this, tableTypeName));
            MembersToAdd.Add(rowStruct.AddSortingKey(this, tableTypeName));

            base.Visit(node);
        }