Skip to content

Commit 549e8e1

Browse files
feat: support JSON data type in MySQL
1 parent c4a5e29 commit 549e8e1

58 files changed

Lines changed: 1052 additions & 396 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CodeGenerator/Generators/CsprojGen.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ internal class CsprojGen(string outputDirectory, string projectName, string name
1212
private const string DefaultMysqlConnectorVersion = "2.4.0";
1313
private const string DefaultSqliteVersion = "9.0.0";
1414
private const string DefaultCsvHelperVersion = "33.0.1";
15+
private const string DefaultSystemTextJsonVersion = "9.0.6";
1516

1617
public File GenerateFile()
1718
{
@@ -48,18 +49,28 @@ string GetPackageReferences()
4849
{
4950
var optionalDapperPackageReference = options.UseDapper
5051
? Environment.NewLine + $""" <PackageReference Include="Dapper" Version="{GetDapperVersion(options)}"/>"""
51-
: "";
52-
var optionalCsvHelper = options.DriverName == DriverName.MySqlConnector
52+
: string.Empty;
53+
var optionalCsvHelper = options.DriverName is DriverName.MySqlConnector
5354
? Environment.NewLine + $""" <PackageReference Include="CsvHelper" Version="{DefaultCsvHelperVersion}"/>"""
54-
: "";
55+
: string.Empty;
56+
var optionalSystemTextJson = IsSystemTextJsonNeeded()
57+
? Environment.NewLine + $""" <PackageReference Include="System.Text.Json" Version="{DefaultSystemTextJsonVersion}"/>"""
58+
: string.Empty;
5559
return $"""
5660
<ItemGroup>
57-
<PackageReference Include="{options.DriverName.ToName()}" Version="{GetDriverVersion(options)}"/>{optionalDapperPackageReference}{optionalCsvHelper}
61+
<PackageReference Include="{options.DriverName.ToName()}" Version="{GetDriverVersion(options)}"/>{optionalDapperPackageReference}{optionalCsvHelper}{optionalSystemTextJson}
5862
</ItemGroup>
5963
""";
6064
}
6165
}
6266

67+
private bool IsSystemTextJsonNeeded()
68+
{
69+
if (options.DotnetFramework.IsDotnetCore())
70+
return false;
71+
return options.DriverName is DriverName.MySqlConnector or DriverName.Npgsql;
72+
}
73+
6374
private static string GetDriverVersion(Options options)
6475
{
6576
if (string.IsNullOrEmpty(options.OverrideDriverVersion))

CodeGenerator/Generators/ModelsGen.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ public File GenerateFile(
2323
Dictionary<string, Dictionary<string, Table>> tables,
2424
Dictionary<string, Dictionary<string, Enum>> enums)
2525
{
26-
var usingDirectives = dbDriver.GetUsingDirectivesForModels();
26+
var usingDirectives = dbDriver
27+
.GetUsingDirectivesForModels()
28+
.Select(x => UsingDirective(ParseName(x)))
29+
.ToArray();
2730
var dataclassModels = GenerateDataClasses(tables);
2831
var enumModels = GenerateEnums(enums);
32+
var models = dataclassModels.Concat(enumModels).ToArray();
2933

30-
var root = RootGen.CompilationRootGen(
31-
IdentifierName(namespaceName),
32-
usingDirectives,
33-
dataclassModels.Concat(enumModels).ToArray());
34+
var root = RootGen.CompilationRootGen(IdentifierName(namespaceName), usingDirectives, models);
3435
root = root.AddCommentOnTop(Consts.AutoGeneratedComment);
3536

3637
return new File

CodeGenerator/Generators/QueriesGen.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ private static CompilationUnitSyntax AddResharperDisables(CompilationUnitSyntax
4949
private (UsingDirectiveSyntax[], MemberDeclarationSyntax) GenerateClass(IEnumerable<Query> queries,
5050
string className)
5151
{
52-
var usingDirectives = dbDriver.GetUsingDirectivesForQueries();
52+
var usingDirectives = dbDriver
53+
.GetUsingDirectivesForQueries()
54+
.Select(x => UsingDirective(ParseName(x)))
55+
.ToArray();
5356
var classMembers = queries.SelectMany(GetMembersForSingleQuery);
5457
return (usingDirectives, GetClassDeclaration(className, classMembers));
5558
}

CodeGenerator/Generators/UtilsGen.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.CodeAnalysis.CSharp.Syntax;
22
using SqlcGenCsharp.Drivers;
3+
using System.Linq;
34
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
45
using File = Plugin.File;
56

@@ -19,8 +20,12 @@ internal class UtilsGen(DbDriver dbDriver, string namespaceName)
1920
if (classImpl == null)
2021
return null;
2122

23+
var usingDirectives = dbDriver
24+
.GetUsingDirectivesForUtils()
25+
.Select(x => UsingDirective(ParseName(x)))
26+
.ToArray();
2227
var root = RootGen.CompilationRootGen(
23-
IdentifierName(NamespaceName), dbDriver.GetUsingDirectivesForUtils(), [classImpl]);
28+
IdentifierName(NamespaceName), usingDirectives, [classImpl]);
2429
root = root.AddCommentOnTop(Consts.AutoGeneratedComment);
2530

2631
return new File

Drivers/ColumnMapping.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ namespace SqlcGenCsharp.Drivers;
66
public record DbTypeInfo(int? Length = null, string? NpgsqlTypeOverride = null);
77

88
public class ColumnMapping(
9-
Dictionary<string, DbTypeInfo> dbTypes, Func<int, string> readerFn, Func<int, string>? readerArrayFn = null)
9+
Dictionary<string, DbTypeInfo> dbTypes,
10+
Func<int, string> readerFn,
11+
Func<int, string>? readerArrayFn = null,
12+
string? usingDirective = null,
13+
Func<string, bool, bool, string>? writerFn = null)
1014
{
1115
public Func<int, string> ReaderFn { get; } = readerFn;
1216
public Func<int, string>? ReaderArrayFn { get; } = readerArrayFn;
17+
public string? UsingDirective { get; } = usingDirective;
18+
public Func<string, bool, bool, string>? WriterFn { get; } = writerFn;
1319
public Dictionary<string, DbTypeInfo> DbTypes { get; } = dbTypes;
1420
}

0 commit comments

Comments
 (0)