Add a setting to turn off Ole Db "smell check" (#1974)

* Add a setting to turn off Ole Db "smell check"

* Rename setting, fix logic

* Test for SupportLegacyParameterTokens

* Fix exception type

* Add release note
This commit is contained in:
Giorgi Dalakishvili
2023-10-11 16:10:00 +04:00
committed by GitHub
parent 2837480903
commit 19193b52bb
5 changed files with 49 additions and 2 deletions
+2
View File
@@ -303,6 +303,8 @@ static Dapper.SqlMapper.SanitizeParameterValue(object? value) -> object!
static Dapper.SqlMapper.SetDbType(System.Data.IDataParameter! parameter, object! value) -> void
static Dapper.SqlMapper.Settings.ApplyNullValues.get -> bool
static Dapper.SqlMapper.Settings.ApplyNullValues.set -> void
static Dapper.SqlMapper.Settings.SupportLegacyParameterTokens.get -> bool
static Dapper.SqlMapper.Settings.SupportLegacyParameterTokens.set -> void
static Dapper.SqlMapper.Settings.CommandTimeout.get -> int?
static Dapper.SqlMapper.Settings.CommandTimeout.set -> void
static Dapper.SqlMapper.Settings.FetchSize.get -> long
+7
View File
@@ -121,6 +121,13 @@ namespace Dapper
}
}
/// <summary>
/// Indicates whether single-character parameter tokens (<c>?</c> etc) will be detected and used where possible;
/// this feature is not recommended and will be disabled by default in future versions;
/// where possible, prefer named parameters (<c>@yourParam</c> etc) or Dapper's "pseudo-positional" parameters (<c>?yourParam? etc</c>).
/// </summary>
public static bool SupportLegacyParameterTokens { get; set; } = true;
private static long s_FetchSize = -1;
}
}
+4 -2
View File
@@ -2534,11 +2534,13 @@ namespace Dapper
throw new NotSupportedException("ValueTuple should not be used for parameters - the language-level names are not available to use as parameter names, and it adds unnecessary boxing");
}
bool filterParams = false;
if (removeUnused && identity.CommandType.GetValueOrDefault(CommandType.Text) == CommandType.Text)
bool filterParams = removeUnused && identity.CommandType.GetValueOrDefault(CommandType.Text) == CommandType.Text;
if (filterParams && Settings.SupportLegacyParameterTokens)
{
filterParams = !smellsLikeOleDb.IsMatch(identity.Sql);
}
var dm = new DynamicMethod("ParamInfo" + Guid.NewGuid().ToString(), null, new[] { typeof(IDbCommand), typeof(object) }, type, true);
var il = dm.GetILGenerator();
+1
View File
@@ -25,6 +25,7 @@ Note: to get the latest pre-release build, add ` -Pre` to the end of the command
(note: new PRs will not be merged until they add release note wording here)
- infer command text without any whitespace as stored-procedure (#1975 via @mgravell)
- add global `SupportLegacyParameterTokens` setting to enable or disable single-character parameter tokens (#1974 via @Giorgi)
### 2.1.4
+35
View File
@@ -1511,6 +1511,36 @@ SELECT * FROM @Issue192 WHERE Field IN @µ AND Field_1 IN @µµ",
[FactLongRunning]
public void TestListExpansionPadding_Disabled() => TestListExpansionPadding(false);
[Theory]
[InlineData(true)]
[InlineData(false)]
public void OleDbParamFilterFails(bool legacyParameterToken)
{
SqlMapper.PurgeQueryCache();
var oldValue = SqlMapper.Settings.SupportLegacyParameterTokens;
try
{
SqlMapper.Settings.SupportLegacyParameterTokens = legacyParameterToken;
if (legacyParameterToken) // OLE DB parameter support enabled; can false-positive
{
Assert.Throws<NotSupportedException>(() => GetValue(connection));
}
else // OLE DB parameter support disabled; more reliable
{
Assert.Equal("this ? could be awkward", GetValue(connection));
}
}
finally
{
SqlMapper.Settings.SupportLegacyParameterTokens = oldValue;
}
static string GetValue(DbConnection connection)
=> connection.QuerySingle<string>("select 'this ? could be awkward'",
new TypeWithDodgyProperties());
}
private void TestListExpansionPadding(bool enabled)
{
bool oldVal = SqlMapper.Settings.PadListExpansions;
@@ -1706,5 +1736,10 @@ create table #Issue1907 (
public int Id { get; set; }
public SqlDecimal? Value { get; set; }
}
class TypeWithDodgyProperties
{
public string Name => throw new NotSupportedException();
}
}
}