microsoft/healthcare-shared-components
Publicmirrored from https://github.com/microsoft/healthcare-shared-componentsAvailable
tools/Microsoft.Health.Extensions.BuildTimeCodeGenerator/Sql/SqlModelGenerator.cs
75lines · modecode
| 1 | // ------------------------------------------------------------------------------------------------- |
| 2 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. |
| 4 | // ------------------------------------------------------------------------------------------------- |
| 5 | |
| 6 | using System.Collections.Generic; |
| 7 | using System.IO; |
| 8 | using System.Linq; |
| 9 | using EnsureThat; |
| 10 | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 11 | using Microsoft.SqlServer.TransactSql.ScriptDom; |
| 12 | using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
| 13 | |
| 14 | namespace Microsoft.Health.Extensions.BuildTimeCodeGenerator.Sql |
| 15 | { |
| 16 | /// <summary> |
| 17 | /// Base class for generating C# types based on the CREATE TABLE, CREATE TABLE TYPE, and CREATE PROCEDURE statements in one or more .sql files. |
| 18 | /// </summary> |
| 19 | public abstract class SqlModelGenerator : ICodeGenerator |
| 20 | { |
| 21 | private readonly string[] _sqlFiles; |
| 22 | |
| 23 | protected SqlModelGenerator(string[] args) |
| 24 | { |
| 25 | EnsureArg.IsNotNull(args, nameof(args)); |
| 26 | |
| 27 | _sqlFiles = args; |
| 28 | } |
| 29 | |
| 30 | protected abstract SqlVisitor[] Visitors { get; } |
| 31 | |
| 32 | public (MemberDeclarationSyntax[], UsingDirectiveSyntax[]) Generate(string typeName) |
| 33 | { |
| 34 | IEnumerable<TSqlFragment> sqlFragments = ParseSqlFiles(); |
| 35 | |
| 36 | var visitors = Visitors; |
| 37 | |
| 38 | foreach (TSqlFragment sqlFragment in sqlFragments) |
| 39 | { |
| 40 | foreach (var sqlVisitor in visitors) |
| 41 | { |
| 42 | sqlFragment.Accept(sqlVisitor); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | MemberDeclarationSyntax[] members = visitors |
| 47 | .SelectMany(v => v.MembersToAdd) |
| 48 | .OrderBy(m => m, MemberSorting.Comparer) |
| 49 | .ToArray(); |
| 50 | |
| 51 | return ( |
| 52 | WrapMembers(members, typeName), |
| 53 | new[] |
| 54 | { |
| 55 | UsingDirective(ParseName("Microsoft.Health.SqlServer.Features.Client")), |
| 56 | UsingDirective(ParseName("Microsoft.Health.SqlServer.Features.Schema.Model")), |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | protected abstract MemberDeclarationSyntax[] WrapMembers(MemberDeclarationSyntax[] members, string containingTypeName); |
| 61 | |
| 62 | private IEnumerable<TSqlFragment> ParseSqlFiles() |
| 63 | { |
| 64 | foreach (var sqlFile in _sqlFiles) |
| 65 | { |
| 66 | using (var stream = File.OpenRead(sqlFile)) |
| 67 | using (var reader = new StreamReader(stream)) |
| 68 | { |
| 69 | var parser = new TSql150Parser(true); |
| 70 | yield return parser.Parse(reader, out var errors); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |