-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathQueriesGen.cs
More file actions
131 lines (115 loc) · 5.32 KB
/
Copy pathQueriesGen.cs
File metadata and controls
131 lines (115 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Plugin;
using SqlcGenCsharp.Drivers;
using System;
using System.Collections.Generic;
using System.Linq;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace SqlcGenCsharp.Generators;
internal class QueriesGen(DbDriver dbDriver, string namespaceName)
{
private static readonly string[] ResharperDisables =
[
"UnusedAutoPropertyAccessor.Global",
"NotAccessedPositionalProperty.Global",
"ConvertToUsingDeclaration",
"UseAwaitUsing",
"UseObjectOrCollectionInitializer"
];
private RootGen RootGen { get; } = new(dbDriver.Options);
private DataClassesGen DataClassesGen { get; } = new(dbDriver);
public File GenerateFile(IEnumerable<Query> queries, string className)
{
var (usingDirectives, classDeclaration) = GenerateClass(queries, className);
var root = RootGen.CompilationRootGen(
IdentifierName(namespaceName), usingDirectives, [classDeclaration]);
root = AddResharperDisables(root);
root = root.AddCommentOnTop(Consts.AutoGeneratedComment);
return new File
{
Name = $"{className}.cs",
Contents = root.ToByteString()
};
}
private static CompilationUnitSyntax AddResharperDisables(CompilationUnitSyntax compilationUnit)
{
return ResharperDisables
.Aggregate(compilationUnit, (current, resharperDisable) =>
current.AddCommentOnTop($"// ReSharper disable {resharperDisable}"));
}
private (UsingDirectiveSyntax[], MemberDeclarationSyntax) GenerateClass(IEnumerable<Query> queries,
string className)
{
var usingDirectives = dbDriver.GetUsingDirectivesForQueries();
var classMembers = queries.SelectMany(GetMembersForSingleQuery);
return (usingDirectives, GetClassDeclaration(className, classMembers));
}
private ClassDeclarationSyntax GetClassDeclaration(string className, IEnumerable<MemberDeclarationSyntax> classMembers)
{
var classDeclaration = (ClassDeclarationSyntax)ParseMemberDeclaration(
$$"""
public class {{className}}
{
public {{className}}(string {{Variable.ConnectionString.AsVarName()}})
{
{{dbDriver.GetConstructorStatements().JoinByNewLine()}}
}
private string {{Variable.ConnectionString.AsPropertyName()}} { get; }
}
""")!;
return classDeclaration.AddMembers(classMembers.ToArray());
}
private IEnumerable<MemberDeclarationSyntax> GetMembersForSingleQuery(Query query)
{
try
{
return new List<MemberDeclarationSyntax>()
.AppendIfNotNull(GetQueryTextConstant(query))
.AppendIfNotNull(GetQueryColumnsDataclass(query))
.AppendIfNotNull(GetQueryParamsDataclass(query))
.Append(AddMethodDeclaration(query));
}
catch (NotSupportedException e)
{
throw new SystemException($"Failed to get members for query: {query.Name}", e);
}
}
private MemberDeclarationSyntax? GetQueryColumnsDataclass(Query query)
{
if (query.Columns.Count <= 0) return null;
return DataClassesGen.Generate(query.Name, ClassMember.Row, query.Columns, dbDriver.Options, query);
}
private MemberDeclarationSyntax? GetQueryParamsDataclass(Query query)
{
if (query.Params.Count <= 0) return null;
var columns = query.Params.Select(p => dbDriver.GetColumnFromParam(p, query)).ToList();
return DataClassesGen.Generate(query.Name, ClassMember.Args, columns, dbDriver.Options, query);
}
private MemberDeclarationSyntax? GetQueryTextConstant(Query query)
{
var transformQueryText = dbDriver.TransformQueryText(query);
if (transformQueryText == string.Empty)
return null;
return ParseMemberDeclaration(
$"""
private const string {ClassMember.Sql.Name(query.Name)} = "{transformQueryText}";
""")!
.AppendNewLine();
}
private MemberDeclarationSyntax AddMethodDeclaration(Query query)
{
var queryTextConstant = ClassMember.Sql.Name(query.Name);
var argInterface = ClassMember.Args.Name(query.Name);
var returnInterface = ClassMember.Row.Name(query.Name);
return query.Cmd switch
{
":exec" => ((IExec)dbDriver).ExecDeclare(queryTextConstant, argInterface, query),
":one" => ((IOne)dbDriver).OneDeclare(queryTextConstant, argInterface, returnInterface, query),
":many" => ((IMany)dbDriver).ManyDeclare(queryTextConstant, argInterface, returnInterface, query),
":execrows" => ((IExecRows)dbDriver).ExecRowsDeclare(queryTextConstant, argInterface, query),
":execlastid" => ((IExecLastId)dbDriver).ExecLastIdDeclare(queryTextConstant, argInterface, query),
":copyfrom" => ((ICopyFrom)dbDriver).CopyFromDeclare(queryTextConstant, argInterface, query),
_ => throw new NotSupportedException($"{query.Cmd} is not supported")
};
}
}