Skip to content

Commit

Permalink
mirror Dapper behaviour re string length; see #95 (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell authored Dec 8, 2023
1 parent 3271110 commit 4343cab
Show file tree
Hide file tree
Showing 34 changed files with 85 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace Dapper.CodeAnalysis;
Expand Down Expand Up @@ -1034,6 +1033,7 @@ private static void WriteArgs(ITypeSymbol? parameterType, CodeWriter sb, WriteAr

var dbType = member.GetDbType(out _);
var size = member.TryGetValue<int>("Size");
bool useSetValueWithDefaultSize = false;
if (dbType is not null)
{
sb.Append("p.DbType = global::System.Data.DbType.").Append(dbType.GetValueOrDefault().ToString()).Append(";").NewLine();
Expand All @@ -1044,7 +1044,14 @@ private static void WriteArgs(ITypeSymbol? parameterType, CodeWriter sb, WriteAr
case DbType.Binary:
case DbType.String:
case DbType.AnsiString:
size = -1; // default to [n]varchar(max)/varbinary(max)
if (member.CodeType.SpecialType == SpecialType.System_String)
{
useSetValueWithDefaultSize = true;
}
else
{
size = -1; // default to [n]varchar(max)/varbinary(max)
}
break;
}
}
Expand All @@ -1066,15 +1073,23 @@ private static void WriteArgs(ITypeSymbol? parameterType, CodeWriter sb, WriteAr
ParameterDirection.Output => nameof(ParameterDirection.Output),
ParameterDirection.ReturnValue => nameof(ParameterDirection.ReturnValue),
_ => direction.ToString(),
}).Append(";").NewLine().Append("p.Value = ");
}).Append(";").NewLine();
// the actual value expression
switch (direction)
{
case ParameterDirection.Input:
case ParameterDirection.InputOutput:
sb.Append("AsValue(").Append(source).Append(".").Append(member.CodeName).Append(");").NewLine();
if (useSetValueWithDefaultSize)
{
sb.Append("SetValueWithDefaultSize(p, ").Append(source).Append(".").Append(member.CodeName).Append(");").NewLine();
}
else
{
sb.Append("p.Value = ").Append("AsValue(").Append(source).Append(".").Append(member.CodeName).Append(");").NewLine();
}
break;
default:
sb.Append("global::System.DBNull.Value;").NewLine();
sb.Append("p.Value = global::System.DBNull.Value;").NewLine();
break;
}
sb.Append("ps.Add(p);").NewLine();
Expand Down
23 changes: 23 additions & 0 deletions src/Dapper.AOT/CommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,29 @@ internal static object AsGenericValue<T>(T value)
return AsValue((object?)value);
}

/// <summary>
/// Gets the default size to use for parameters; to override, see <see cref="DbValueAttribute.Size"/>
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected static void SetValueWithDefaultSize(DbParameter parameter, string? value)
{
// fixing the defined length avoids query plan mismatch due to constantly different
// parameters; mirror Dapper vanilla size behaviour; see
// https://github.com/DapperLib/DapperAOT/issues/95 for more context
const int DefaultLength = 4000, Max = -1; // see DbString.DefaultLength in Dapper
if (value is null)
{
parameter.Value = DBNull.Value;
parameter.Size = DefaultLength;
}
else
{
parameter.Value = value;
var len = value.Length;
parameter.Size = len <= DefaultLength ? DefaultLength : Max;
}
}

/// <summary>
/// Flexibly parse an <see cref="object"/> as a value of type <typeparamref name="T"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
6 changes: 2 additions & 4 deletions test/Dapper.AOT.Test/Interceptors/BatchSize.output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down Expand Up @@ -104,9 +103,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
6 changes: 2 additions & 4 deletions test/Dapper.AOT.Test/Interceptors/BatchSize.output.netfx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down Expand Up @@ -104,9 +103,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
6 changes: 2 additions & 4 deletions test/Dapper.AOT.Test/Interceptors/CacheCommand.output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down Expand Up @@ -197,9 +196,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down Expand Up @@ -197,9 +196,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
6 changes: 2 additions & 4 deletions test/Dapper.AOT.Test/Interceptors/CommandProperties.output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down Expand Up @@ -342,9 +341,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down Expand Up @@ -342,9 +341,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
3 changes: 1 addition & 2 deletions test/Dapper.AOT.Test/Interceptors/Execute.output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
3 changes: 1 addition & 2 deletions test/Dapper.AOT.Test/Interceptors/Execute.output.netfx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
6 changes: 2 additions & 4 deletions test/Dapper.AOT.Test/Interceptors/ExecuteBatch.output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, global:
p = cmd.CreateParameter();
p.ParameterName = "Y";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(args.Y);
SetValueWithDefaultSize(p, args.Y);
ps.Add(p);

p = cmd.CreateParameter();
Expand Down Expand Up @@ -208,9 +207,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, global:
p = cmd.CreateParameter();
p.ParameterName = "Y";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(args.Y);
SetValueWithDefaultSize(p, args.Y);
ps.Add(p);

p = cmd.CreateParameter();
Expand Down Expand Up @@ -208,9 +207,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
3 changes: 1 addition & 2 deletions test/Dapper.AOT.Test/Interceptors/ExecuteScalar.output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
3 changes: 1 addition & 2 deletions test/Dapper.AOT.Test/Interceptors/GlobalFetchSize.output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
3 changes: 1 addition & 2 deletions test/Dapper.AOT.Test/Interceptors/Query.output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
3 changes: 1 addition & 2 deletions test/Dapper.AOT.Test/Interceptors/Query.output.netfx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
p = cmd.CreateParameter();
p.ParameterName = "bar";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(typed.bar);
SetValueWithDefaultSize(p, typed.bar);
ps.Add(p);

}
Expand Down
3 changes: 1 addition & 2 deletions test/Dapper.AOT.Test/Interceptors/QueryDetection.output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, global:
p = cmd.CreateParameter();
p.ParameterName = "Name";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(args.Name);
SetValueWithDefaultSize(p, args.Name);
ps.Add(p);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, global:
p = cmd.CreateParameter();
p.ParameterName = "Name";
p.DbType = global::System.Data.DbType.String;
p.Size = -1;
p.Direction = global::System.Data.ParameterDirection.Input;
p.Value = AsValue(args.Name);
SetValueWithDefaultSize(p, args.Name);
ps.Add(p);

}
Expand Down
Loading

0 comments on commit 4343cab

Please sign in to comment.