Skip to content

Commit 19193b5

Browse files
authored
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
1 parent 2837480 commit 19193b5

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

Dapper/PublicAPI.Shipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ static Dapper.SqlMapper.SanitizeParameterValue(object? value) -> object!
303303
static Dapper.SqlMapper.SetDbType(System.Data.IDataParameter! parameter, object! value) -> void
304304
static Dapper.SqlMapper.Settings.ApplyNullValues.get -> bool
305305
static Dapper.SqlMapper.Settings.ApplyNullValues.set -> void
306+
static Dapper.SqlMapper.Settings.SupportLegacyParameterTokens.get -> bool
307+
static Dapper.SqlMapper.Settings.SupportLegacyParameterTokens.set -> void
306308
static Dapper.SqlMapper.Settings.CommandTimeout.get -> int?
307309
static Dapper.SqlMapper.Settings.CommandTimeout.set -> void
308310
static Dapper.SqlMapper.Settings.FetchSize.get -> long

Dapper/SqlMapper.Settings.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ public static long FetchSize
121121
}
122122
}
123123

124+
/// <summary>
125+
/// Indicates whether single-character parameter tokens (<c>?</c> etc) will be detected and used where possible;
126+
/// this feature is not recommended and will be disabled by default in future versions;
127+
/// where possible, prefer named parameters (<c>@yourParam</c> etc) or Dapper's "pseudo-positional" parameters (<c>?yourParam? etc</c>).
128+
/// </summary>
129+
public static bool SupportLegacyParameterTokens { get; set; } = true;
130+
124131
private static long s_FetchSize = -1;
125132
}
126133
}

Dapper/SqlMapper.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,11 +2534,13 @@ private static bool IsValueTuple(Type? type) => (type?.IsValueType == true
25342534
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");
25352535
}
25362536

2537-
bool filterParams = false;
2538-
if (removeUnused && identity.CommandType.GetValueOrDefault(CommandType.Text) == CommandType.Text)
2537+
bool filterParams = removeUnused && identity.CommandType.GetValueOrDefault(CommandType.Text) == CommandType.Text;
2538+
2539+
if (filterParams && Settings.SupportLegacyParameterTokens)
25392540
{
25402541
filterParams = !smellsLikeOleDb.IsMatch(identity.Sql);
25412542
}
2543+
25422544
var dm = new DynamicMethod("ParamInfo" + Guid.NewGuid().ToString(), null, new[] { typeof(IDbCommand), typeof(object) }, type, true);
25432545

25442546
var il = dm.GetILGenerator();

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Note: to get the latest pre-release build, add ` -Pre` to the end of the command
2525
(note: new PRs will not be merged until they add release note wording here)
2626

2727
- infer command text without any whitespace as stored-procedure (#1975 via @mgravell)
28+
- add global `SupportLegacyParameterTokens` setting to enable or disable single-character parameter tokens (#1974 via @Giorgi)
2829

2930
### 2.1.4
3031

tests/Dapper.Tests/ParameterTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,36 @@ public void Issue601_InternationalParameterNamesWork()
15111511
[FactLongRunning]
15121512
public void TestListExpansionPadding_Disabled() => TestListExpansionPadding(false);
15131513

1514+
[Theory]
1515+
[InlineData(true)]
1516+
[InlineData(false)]
1517+
public void OleDbParamFilterFails(bool legacyParameterToken)
1518+
{
1519+
SqlMapper.PurgeQueryCache();
1520+
var oldValue = SqlMapper.Settings.SupportLegacyParameterTokens;
1521+
try
1522+
{
1523+
SqlMapper.Settings.SupportLegacyParameterTokens = legacyParameterToken;
1524+
1525+
if (legacyParameterToken) // OLE DB parameter support enabled; can false-positive
1526+
{
1527+
Assert.Throws<NotSupportedException>(() => GetValue(connection));
1528+
}
1529+
else // OLE DB parameter support disabled; more reliable
1530+
{
1531+
Assert.Equal("this ? could be awkward", GetValue(connection));
1532+
}
1533+
}
1534+
finally
1535+
{
1536+
SqlMapper.Settings.SupportLegacyParameterTokens = oldValue;
1537+
}
1538+
1539+
static string GetValue(DbConnection connection)
1540+
=> connection.QuerySingle<string>("select 'this ? could be awkward'",
1541+
new TypeWithDodgyProperties());
1542+
}
1543+
15141544
private void TestListExpansionPadding(bool enabled)
15151545
{
15161546
bool oldVal = SqlMapper.Settings.PadListExpansions;
@@ -1706,5 +1736,10 @@ class HazNullableSqlDecimal
17061736
public int Id { get; set; }
17071737
public SqlDecimal? Value { get; set; }
17081738
}
1739+
1740+
class TypeWithDodgyProperties
1741+
{
1742+
public string Name => throw new NotSupportedException();
1743+
}
17091744
}
17101745
}

0 commit comments

Comments
 (0)