-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtilsGen.cs
125 lines (107 loc) · 5.11 KB
/
UtilsGen.cs
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
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using SqlcGenCsharp.Drivers;
using System.Collections.Generic;
using System.Linq;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using File = Plugin.File;
namespace SqlcGenCsharp.Generators;
internal class UtilsGen(DbDriver dbDriver, string namespaceName)
{
private const string ClassName = "Utils";
private string NamespaceName { get; } = namespaceName;
private RootGen RootGen { get; } = new(dbDriver.Options);
public File GenerateFile()
{
var root = RootGen.CompilationRootGen(
IdentifierName(NamespaceName), GetUsingDirectives(), [GetUtilsClass()]);
root = root.AddCommentOnTop(Consts.AutoGeneratedComment);
return new File
{
Name = $"{ClassName}.cs",
Contents = root.ToByteString()
};
}
private UsingDirectiveSyntax[] GetUsingDirectives()
{
IEnumerable<UsingDirectiveSyntax> usingDirectives =
[
UsingDirective(ParseName("System")),
UsingDirective(ParseName("System.Data")),
UsingDirective(ParseName("System.Linq")),
UsingDirective(ParseName("System.Text.RegularExpressions"))
];
if (dbDriver.Options.DriverName is DriverName.MySqlConnector)
{
usingDirectives = usingDirectives.Concat([
UsingDirective(ParseName("CsvHelper.TypeConversion")),
UsingDirective(ParseName("CsvHelper")),
UsingDirective(ParseName("CsvHelper.Configuration"))
]);
}
return usingDirectives.ToArray();
}
private MemberDeclarationSyntax GetUtilsClass()
{
var optionalTransformQueryForSqliteBatch = dbDriver.Options.DriverName is DriverName.Sqlite
? """
private static readonly Regex ValuesRegex = new Regex(@"VALUES\s*\((?<params>[^)]*)\)", RegexOptions.IgnoreCase);
public static string TransformQueryForSqliteBatch(string originalSql, int cntRecords)
{
var match = ValuesRegex.Match(originalSql);
if (!match.Success)
throw new ArgumentException("The query does not contain a valid VALUES clause.");
var valuesParams = match.Groups["params"].Value
.Split(',')
.Select(p => p.Trim())
.ToList();
var batchRows = Enumerable.Range(0, cntRecords)
.Select(i => "(" + string.Join(", ", valuesParams.Select(p => $"{p}{i}")) + ")");
var batchValuesClause = "VALUES " + string.Join(",\n", batchRows);
return ValuesRegex.Replace(originalSql, batchValuesClause);
}
""" :
string.Empty;
var optionalNullToNStringConverter = dbDriver.Options.DriverName is DriverName.MySqlConnector
? $$"""
public class NullToStringConverter : DefaultTypeConverter
{
public override {{dbDriver.AddNullableSuffixIfNeeded("string", true)}} ConvertToString(
{{dbDriver.AddNullableSuffixIfNeeded("object", false)}} value, IWriterRow row, MemberMapData memberMapData)
{
return value == null ? @"\N" : base.ConvertToString(value, row, memberMapData);
}
}
public class BoolToBitConverter : DefaultTypeConverter
{
public override {{dbDriver.AddNullableSuffixIfNeeded("string", true)}} ConvertToString(
{{dbDriver.AddNullableSuffixIfNeeded("object", false)}} value, IWriterRow row, MemberMapData memberMapData)
{
switch (value)
{
case null:
return @"\N";
case bool b:
return b ? "1" : "0";
default:
return base.ConvertToString(value, row, memberMapData);
}
}
}
"""
: string.Empty;
var utilsClassDeclaration = $$"""
public static class {{ClassName}}
{
{{optionalNullToNStringConverter}}
public static string TransformQueryForSliceArgs(string originalSql, int sliceSize, string paramName)
{
var paramArgs = Enumerable.Range(0, sliceSize).Select(i => $"@{paramName}Arg{i}").ToList();
return originalSql.Replace($"/*SLICE:{paramName}*/@{paramName}", string.Join(",", paramArgs));
}
{{optionalTransformQueryForSqliteBatch}}
}
""";
return ParseMemberDeclaration(utilsClassDeclaration)!.NormalizeWhitespace();
}
}