Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Drivers/ColumnMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ColumnMapping(
Func<string, bool, bool, string>? writerFn = null,
Func<string, string>? convertFunc = null,
string? sqlMapper = null,
string? sqlMapperImpl = null)
Func<bool, string>? sqlMapperImpl = null)
{
public Dictionary<string, DbTypeInfo> DbTypes { get; } = dbTypes;
public Func<int, string> ReaderFn { get; } = readerFn;
Expand All @@ -22,5 +22,5 @@ public class ColumnMapping(
public Func<string, bool, bool, string>? WriterFn { get; } = writerFn;
public Func<string, string>? ConvertFunc { get; } = convertFunc;
public string? SqlMapper { get; } = sqlMapper;
public string? SqlMapperImpl { get; } = sqlMapperImpl;
public Func<bool, string>? SqlMapperImpl { get; } = sqlMapperImpl;
}
2 changes: 1 addition & 1 deletion Drivers/DbDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private MemberDeclarationSyntax[] GetSqlMapperMemberDeclarations()
{
return [.. ColumnMappings
.Where(m => TypeExistsInQueries(m.Key) && m.Value.SqlMapperImpl is not null)
.Select(m => ParseMemberDeclaration(m.Value.SqlMapperImpl!)!)];
.Select(m => ParseMemberDeclaration(m.Value.SqlMapperImpl!(Options.DotnetFramework.IsDotnetCore()))!)];
}

public abstract string TransformQueryText(Query query);
Expand Down
14 changes: 8 additions & 6 deletions Drivers/MySqlConnectorDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public sealed partial class MySqlConnectorDriver(

public override string TransactionClassName => "MySqlTransaction";

private const string JsonElementTypeHandler = """
private static readonly Func<bool, string> JsonElementTypeHandler = _ => $$"""
private class JsonElementTypeHandler : SqlMapper.TypeHandler<JsonElement>
{
public override JsonElement Parse(object value)
Expand Down Expand Up @@ -291,7 +291,8 @@ public override MemberDeclarationSyntax[] GetEnumExtensionsMembers(string name,

private MemberDeclarationSyntax[] GetSetTypeHandlers()
{
var setTypeHandlerFunc = (string x) =>
var optionalNullableSuffix = Options.DotnetFramework.IsDotnetCore() ? "?" : string.Empty;
string setTypeHandlerFunc(string x) =>
$$"""
private class {{x}}TypeHandler : SqlMapper.TypeHandler<HashSet<{{x}}>>
{
Expand All @@ -302,14 +303,16 @@ private class {{x}}TypeHandler : SqlMapper.TypeHandler<HashSet<{{x}}>>
throw new DataException($"Cannot convert {value?.GetType()} to HashSet<{{x}}>");
}

public override void SetValue(IDbDataParameter parameter, HashSet<{{x}}> value)
public override void SetValue(IDbDataParameter parameter, HashSet<{{x}}>{{optionalNullableSuffix}} value)
{
if (value is null)
return;
parameter.Value = string.Join(",", value);
}
}
""";

return Queries
return [.. Queries
.SelectMany(q => q.Columns)
.Where(c =>
{
Expand All @@ -318,8 +321,7 @@ public override void SetValue(IDbDataParameter parameter, HashSet<{{x}}> value)
})
.Select(c => setTypeHandlerFunc(EnumToModelName(c)))
.Distinct()
.Select(m => ParseMemberDeclaration(m)!)
.ToArray();
.Select(m => ParseMemberDeclaration(m)!)];
}

public override MemberDeclarationSyntax[] GetMemberDeclarationsForUtils()
Expand Down
9 changes: 5 additions & 4 deletions Drivers/NpgsqlDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public NpgsqlDriver(

public override string TransactionClassName => "NpgsqlTransaction";

private const string JsonElementTypeHandler = """
private static readonly Func<bool, string> JsonElementTypeHandler = _ => $$"""
private class JsonElementTypeHandler : SqlMapper.TypeHandler<JsonElement>
{
public override JsonElement Parse(object value)
Expand All @@ -362,8 +362,7 @@ public override void SetValue(IDbDataParameter parameter, JsonElement value)
}
""";

private const string XmlDocumentTypeHandler =
"""
private static readonly Func<bool, string> XmlDocumentTypeHandler = isDotnetCore => $$"""
private class XmlDocumentTypeHandler : SqlMapper.TypeHandler<XmlDocument>
{
public override XmlDocument Parse(object value)
Expand All @@ -377,8 +376,10 @@ public override XmlDocument Parse(object value)
throw new DataException($"Cannot convert {value?.GetType()} to XmlDocument");
}

public override void SetValue(IDbDataParameter parameter, XmlDocument value)
public override void SetValue(IDbDataParameter parameter, XmlDocument{{(isDotnetCore ? "?" : string.Empty)}} value)
{
if (value is null)
return;
parameter.Value = value.OuterXml;
}
}
Expand Down
8 changes: 6 additions & 2 deletions examples/MySqlConnectorDapperExample/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ public override HashSet<BiosAuthorType> Parse(object value)
throw new DataException($"Cannot convert {value?.GetType()} to HashSet<BiosAuthorType>");
}

public override void SetValue(IDbDataParameter parameter, HashSet<BiosAuthorType> value)
public override void SetValue(IDbDataParameter parameter, HashSet<BiosAuthorType>? value)
{
if (value is null)
return;
parameter.Value = string.Join(",", value);
}
}
Expand All @@ -63,8 +65,10 @@ public override HashSet<MysqlStringTypesCSet> Parse(object value)
throw new DataException($"Cannot convert {value?.GetType()} to HashSet<MysqlStringTypesCSet>");
}

public override void SetValue(IDbDataParameter parameter, HashSet<MysqlStringTypesCSet> value)
public override void SetValue(IDbDataParameter parameter, HashSet<MysqlStringTypesCSet>? value)
{
if (value is null)
return;
parameter.Value = string.Join(",", value);
}
}
Expand Down
4 changes: 4 additions & 0 deletions examples/MySqlConnectorDapperLegacyExample/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public override HashSet<BiosAuthorType> Parse(object value)

public override void SetValue(IDbDataParameter parameter, HashSet<BiosAuthorType> value)
{
if (value is null)
return;
parameter.Value = string.Join(",", value);
}
}
Expand All @@ -66,6 +68,8 @@ public override HashSet<MysqlStringTypesCSet> Parse(object value)

public override void SetValue(IDbDataParameter parameter, HashSet<MysqlStringTypesCSet> value)
{
if (value is null)
return;
parameter.Value = string.Join(",", value);
}
}
Expand Down
4 changes: 3 additions & 1 deletion examples/NpgsqlDapperExample/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public override XmlDocument Parse(object value)
throw new DataException($"Cannot convert {value?.GetType()} to XmlDocument");
}

public override void SetValue(IDbDataParameter parameter, XmlDocument value)
public override void SetValue(IDbDataParameter parameter, XmlDocument? value)
{
if (value is null)
return;
parameter.Value = value.OuterXml;
}
}
Expand Down
2 changes: 2 additions & 0 deletions examples/NpgsqlDapperLegacyExample/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public override XmlDocument Parse(object value)

public override void SetValue(IDbDataParameter parameter, XmlDocument value)
{
if (value is null)
return;
parameter.Value = value.OuterXml;
}
}
Expand Down
Loading