Skip to content

Commit e88d10c

Browse files
fix: move package references in .csproj to drivers
1 parent f6a9b60 commit e88d10c

49 files changed

Lines changed: 167 additions & 127 deletions

Some content is hidden

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

CodeGenerator/CodeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private void InitGenerators(GenerateRequest generateRequest)
8585
DbDriver = InstantiateDriver();
8686

8787
// initialize file generators
88-
CsprojGen = new(outputDirectory, projectName, namespaceName, Options);
88+
CsprojGen = new(DbDriver, outputDirectory, projectName, namespaceName);
8989
QueriesGen = new(DbDriver, namespaceName);
9090
ModelsGen = new(DbDriver, namespaceName);
9191
UtilsGen = new(DbDriver, namespaceName);
Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
using Google.Protobuf;
2+
using SqlcGenCsharp.Drivers;
23
using System;
4+
using System.Linq;
35
using File = Plugin.File;
46

57

68
namespace SqlcGenCsharp.Generators;
79

8-
internal class CsprojGen(string outputDirectory, string projectName, string namespaceName, Options options)
10+
internal class CsprojGen(DbDriver dbDriver, string outputDirectory, string projectName, string namespaceName)
911
{
10-
// TODO this logic needs to be moved to the Drivers project
11-
private const string DefaultDapperVersion = "2.1.66";
12-
private const string DefaultNpgsqlVersion = "8.0.6";
13-
private const string DefaultMysqlConnectorVersion = "2.4.0";
14-
private const string DefaultSqliteVersion = "9.0.0";
15-
private const string DefaultCsvHelperVersion = "33.0.1";
16-
private const string DefaultSystemTextJsonVersion = "9.0.6";
17-
1812
public File GenerateFile()
1913
{
2014
var csprojContents = GetFileContents();
@@ -27,7 +21,11 @@ public File GenerateFile()
2721

2822
private string GetFileContents()
2923
{
30-
var optionalNullableProperty = options.DotnetFramework.IsDotnetCore() ? Environment.NewLine + " <Nullable>enable</Nullable>" : "";
24+
var optionalNullableProperty = dbDriver.Options.DotnetFramework.IsDotnetCore() ? Environment.NewLine + " <Nullable>enable</Nullable>" : "";
25+
var referenceItems = dbDriver.GetPackageReferences()
26+
.Select(p => $""" <PackageReference Include="{p.Key}" Version="{p.Value}"/>""")
27+
.JoinByNewLine();
28+
3129
return $"""
3230
<!--{Consts.AutoGeneratedComment}-->
3331
<!--Run the following to add the project to the solution:
@@ -36,60 +34,16 @@ dotnet sln add {outputDirectory}/{projectName}.csproj
3634
<Project Sdk="Microsoft.NET.Sdk">
3735
3836
<PropertyGroup>
39-
<TargetFramework>{options.DotnetFramework.ToName()}</TargetFramework>
37+
<TargetFramework>{dbDriver.Options.DotnetFramework.ToName()}</TargetFramework>
4038
<RootNamespace>{namespaceName}</RootNamespace>
4139
<OutputType>Library</OutputType>{optionalNullableProperty}
4240
</PropertyGroup>
4341
44-
{GetPackageReferences()}
42+
<ItemGroup>
43+
{referenceItems}
44+
</ItemGroup>
4545
4646
</Project>
4747
""";
48-
49-
string GetPackageReferences()
50-
{
51-
var optionalDapperPackageReference = options.UseDapper
52-
? Environment.NewLine + $""" <PackageReference Include="Dapper" Version="{GetDapperVersion(options)}"/>"""
53-
: string.Empty;
54-
var optionalCsvHelper = options.DriverName is DriverName.MySqlConnector
55-
? Environment.NewLine + $""" <PackageReference Include="CsvHelper" Version="{DefaultCsvHelperVersion}"/>"""
56-
: string.Empty;
57-
var optionalSystemTextJson = IsSystemTextJsonNeeded()
58-
? Environment.NewLine + $""" <PackageReference Include="System.Text.Json" Version="{DefaultSystemTextJsonVersion}"/>"""
59-
: string.Empty;
60-
return $"""
61-
<ItemGroup>
62-
<PackageReference Include="{options.DriverName.ToName()}" Version="{GetDriverVersion(options)}"/>{optionalDapperPackageReference}{optionalCsvHelper}{optionalSystemTextJson}
63-
<PackageReference Include="NodaTime" Version="3.2.0"/>
64-
</ItemGroup>
65-
""";
66-
}
67-
}
68-
69-
private bool IsSystemTextJsonNeeded()
70-
{
71-
if (options.DotnetFramework.IsDotnetCore())
72-
return false;
73-
return options.DriverName is DriverName.MySqlConnector or DriverName.Npgsql;
74-
}
75-
76-
private static string GetDriverVersion(Options options)
77-
{
78-
if (string.IsNullOrEmpty(options.OverrideDriverVersion))
79-
return options.DriverName switch
80-
{
81-
DriverName.Npgsql => DefaultNpgsqlVersion,
82-
DriverName.MySqlConnector => DefaultMysqlConnectorVersion,
83-
DriverName.Sqlite => DefaultSqliteVersion,
84-
_ => throw new NotSupportedException($"unsupported driver: {options.DriverName}")
85-
};
86-
return options.OverrideDriverVersion;
87-
}
88-
89-
private static string GetDapperVersion(Options options)
90-
{
91-
return string.IsNullOrEmpty(options.OverrideDapperVersion)
92-
? DefaultDapperVersion
93-
: options.OverrideDapperVersion;
9448
}
9549
}

Drivers/DbDriver.cs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public record ConnectionGenCommands(string EstablishConnection, string Connectio
1111

1212
public abstract class DbDriver
1313
{
14+
protected const string DefaultDapperVersion = "2.1.66";
15+
protected const string DefaultSystemTextJsonVersion = "9.0.6";
16+
protected const string DefaultNodaTimeVersion = "3.2.0";
17+
1418
public Options Options { get; }
1519

1620
public string DefaultSchema { get; }
@@ -123,6 +127,21 @@ private static Dictionary<string, Dictionary<string, Table>> ConstructTablesLook
123127
);
124128
}
125129

130+
public virtual IDictionary<string, string> GetPackageReferences()
131+
{
132+
return new Dictionary<string, string> {
133+
{ "Dapper", Options.OverrideDapperVersion != string.Empty ? Options.OverrideDapperVersion : DefaultDapperVersion }
134+
}
135+
.MergeIf(new Dictionary<string, string>
136+
{
137+
{ "System.Text.Json", DefaultSystemTextJsonVersion }
138+
}, IsSystemTextJsonNeeded())
139+
.MergeIf(new Dictionary<string, string>
140+
{
141+
{ "NodaTime", DefaultNodaTimeVersion }
142+
}, TypeExistsInQueries("Instant"));
143+
}
144+
126145
public virtual ISet<string> GetUsingDirectivesForQueries()
127146
{
128147
return new HashSet<string>
@@ -332,19 +351,6 @@ public string AddNullableSuffixIfNeeded(string csharpType, bool notNull)
332351
return IsTypeNullable(csharpType) ? $"{csharpType}?" : csharpType;
333352
}
334353

335-
protected string? GetColumnDbTypeOverride(Column column)
336-
{
337-
if (column.IsArray)
338-
return null;
339-
var columnType = column.Type.Name.ToLower();
340-
foreach (var columnMapping in ColumnMappings.Values)
341-
{
342-
if (columnMapping.DbTypes.TryGetValue(columnType, out var dbTypeOverride))
343-
return dbTypeOverride.NpgsqlTypeOverride;
344-
}
345-
return null;
346-
}
347-
348354
public bool IsTypeNullable(string csharpType)
349355
{
350356
if (NullableTypes.Contains(csharpType.Replace("?", ""))) return true;
@@ -414,4 +420,11 @@ public virtual string GetColumnReader(Column column, int ordinal, Query? query)
414420
}
415421
throw new NotSupportedException($"column {column.Name} has unsupported column type: {column.Type.Name} in {GetType().Name}");
416422
}
423+
424+
private bool IsSystemTextJsonNeeded()
425+
{
426+
if (Options.DotnetFramework.IsDotnetCore())
427+
return false;
428+
return TypeExistsInQueries("JsonElement");
429+
}
417430
}

Drivers/MySqlConnectorDriver.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public sealed partial class MySqlConnectorDriver(
1717
EnumDbDriver(options, catalog, queries),
1818
IOne, IMany, IExec, IExecRows, IExecLastId, ICopyFrom
1919
{
20+
private const string DefaultMysqlConnectorVersion = "2.4.0";
21+
private const string DefaultCsvHelperVersion = "33.0.1";
22+
2023
protected override Dictionary<string, ColumnMapping> ColumnMappings { get; } =
2124
new()
2225
{
@@ -228,6 +231,20 @@ public MemberDeclarationSyntax CopyFromDeclare(string queryTextConstant, string
228231
return new CopyFromDeclareGen(this).Generate(queryTextConstant, argInterface, query);
229232
}
230233

234+
public override IDictionary<string, string> GetPackageReferences()
235+
{
236+
return base
237+
.GetPackageReferences()
238+
.Merge(new Dictionary<string, string>
239+
{
240+
{ "MySqlConnector", Options.OverrideDriverVersion != string.Empty ? Options.OverrideDriverVersion : DefaultMysqlConnectorVersion }
241+
})
242+
.MergeIf(new Dictionary<string, string>
243+
{
244+
{ "CsvHelper", DefaultCsvHelperVersion }
245+
}, CopyFromQueryExists());
246+
}
247+
231248
public override ISet<string> GetUsingDirectivesForQueries()
232249
{
233250
return base

Drivers/NpgsqlDriver.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace SqlcGenCsharp.Drivers;
1111

1212
public sealed class NpgsqlDriver : EnumDbDriver, IOne, IMany, IExec, IExecRows, IExecLastId, ICopyFrom
1313
{
14+
private const string DefaultNpgsqlVersion = "8.0.6";
15+
1416
public NpgsqlDriver(
1517
Options options,
1618
Catalog catalog,
@@ -438,6 +440,16 @@ public MemberDeclarationSyntax CopyFromDeclare(string queryTextConstant, string
438440
return new CopyFromDeclareGen(this).Generate(queryTextConstant, argInterface, query);
439441
}
440442

443+
public override IDictionary<string, string> GetPackageReferences()
444+
{
445+
return base
446+
.GetPackageReferences()
447+
.Merge(new Dictionary<string, string>
448+
{
449+
{ "Npgsql", Options.OverrideDriverVersion != string.Empty ? Options.OverrideDriverVersion : DefaultNpgsqlVersion }
450+
});
451+
}
452+
441453
public override ISet<string> GetUsingDirectivesForQueries()
442454
{
443455
return base.GetUsingDirectivesForQueries().AddRangeExcludeNulls(
@@ -624,6 +636,19 @@ string AddRowsToCopyCommand()
624636
}
625637
}
626638

639+
private string? GetColumnDbTypeOverride(Column column)
640+
{
641+
if (column.IsArray)
642+
return null; // TODO: handle array columns
643+
var columnType = column.Type.Name.ToLower();
644+
foreach (var columnMapping in ColumnMappings.Values)
645+
{
646+
if (columnMapping.DbTypes.TryGetValue(columnType, out var dbTypeOverride))
647+
return dbTypeOverride.NpgsqlTypeOverride;
648+
}
649+
return null;
650+
}
651+
627652
public override WriterFn? GetWriterFn(Column column, Query query)
628653
{
629654
var csharpType = GetCsharpTypeWithoutNullableSuffix(column, query);

Drivers/SqliteDriver.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Microsoft.CodeAnalysis.CSharp.Syntax;
22
using Plugin;
33
using SqlcGenCsharp.Drivers.Generators;
4-
using System;
54
using System.Collections.Generic;
65
using System.Linq;
76
using System.Text.RegularExpressions;
@@ -15,6 +14,8 @@ public sealed partial class SqliteDriver(
1514
IList<Query> queries) :
1615
DbDriver(options, catalog, queries), IOne, IMany, IExec, IExecRows, IExecLastId, ICopyFrom
1716
{
17+
private const string DefaultSqliteVersion = "9.0.0";
18+
1819
private static readonly HashSet<string> IntegerDbTypes = ["integer", "integernotnulldefaultunixepoch"];
1920

2021
private const string DateTimeStringFormat = "yyyy-MM-dd HH:mm:ss"; // Default format for DateTime strings - TODO make configurable via Options
@@ -154,6 +155,16 @@ public override void SetValue(IDbDataParameter parameter, DateTime value)
154155

155156
public override string TransactionClassName => "SqliteTransaction";
156157

158+
public override IDictionary<string, string> GetPackageReferences()
159+
{
160+
return base
161+
.GetPackageReferences()
162+
.Merge(new Dictionary<string, string>
163+
{
164+
{ "Microsoft.Data.Sqlite", Options.OverrideDriverVersion != string.Empty ? Options.OverrideDriverVersion : DefaultSqliteVersion }
165+
});
166+
}
167+
157168
public override ISet<string> GetUsingDirectivesForQueries()
158169
{
159170
var usingDirectives = base.GetUsingDirectivesForQueries();

Extensions/DictionaryExtensions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace SqlcGenCsharp;
2+
3+
public static class DictionaryExtensions
4+
{
5+
public static IDictionary<K, V> Merge<K, V>(this IDictionary<K, V> me, IDictionary<K, V>? items)
6+
{
7+
if (items is null)
8+
return me;
9+
foreach (var item in items)
10+
me[item.Key] = item.Value;
11+
return me;
12+
}
13+
14+
public static IDictionary<K, V> MergeIf<K, V>(this IDictionary<K, V> me, IDictionary<K, V>? items, bool condition)
15+
{
16+
if (!condition)
17+
return me;
18+
return me.Merge(items);
19+
}
20+
}

PluginOptions/Options.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public Options(GenerateRequest generateRequest)
1919
UseDapper = rawOptions.UseDapper;
2020
OverrideDapperVersion = rawOptions.OverrideDapperVersion;
2121
NamespaceName = rawOptions.NamespaceName;
22-
UseNodaTime = rawOptions.UseNodaTime;
2322
DotnetFramework = DotnetFrameworkExtensions.ParseName(rawOptions.TargetFramework);
2423
Overrides = rawOptions.Overrides ?? [];
2524

@@ -40,8 +39,6 @@ public Options(GenerateRequest generateRequest)
4039

4140
public string OverrideDapperVersion { get; }
4241

43-
public bool UseNodaTime { get; }
44-
4542
public string NamespaceName { get; }
4643

4744
public List<OverrideOption> Overrides { get; }

PluginOptions/RawOptions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ public record RawOptions
2323
[JsonPropertyName("overrideDapperVersion")]
2424
public string OverrideDapperVersion { get; init; } = string.Empty;
2525

26-
[JsonPropertyName("useNodaTime")]
27-
public bool UseNodaTime { get; init; }
28-
2926
[JsonPropertyName("overrides")]
3027
public List<OverrideOption>? Overrides { get; init; }
3128

examples/MySqlConnectorDapperExample/MySqlConnectorDapperExample.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="MySqlConnector" Version="2.4.0"/>
1615
<PackageReference Include="Dapper" Version="2.1.66"/>
16+
<PackageReference Include="NodaTime" Version="3.2.0"/>
17+
<PackageReference Include="MySqlConnector" Version="2.4.0"/>
1718
<PackageReference Include="CsvHelper" Version="33.0.1"/>
18-
<PackageReference Include="NodaTime" Version="3.2.0"/>
1919
</ItemGroup>
2020

2121
</Project>

0 commit comments

Comments
 (0)