Skip to content

Commit

Permalink
Adds setting ForceListExpansion to provide a way to enable consiste…
Browse files Browse the repository at this point in the history
…nt handling of list parameters regardless of RDBMS (DapperLib#1871)
  • Loading branch information
Joshua Rogers committed Jan 10, 2023
1 parent a31dfd3 commit 0a140ef
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Dapper/SqlMapper.Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ public static void SetDefaults()
/// instead of the original name; for most scenarios, this is ignored since the name is redundant, but "snowflake" requires this.
/// </summary>
public static bool UseIncrementalPseudoPositionalParameterNames { get; set; }

/// <summary>
/// If set, this causes Dapper to perform list expansion on any list provided as a query parameter, even if the RDBMS driver has
/// native list support. Enabling this will be less efficient than using native support, but provides for consistent list
/// handling regardless of underlying RDBMS. See https://github.com/DapperLib/Dapper/issues/1871.
/// </summary>
public static bool ForceListExpansion { get; set; }
}
}
}
2 changes: 1 addition & 1 deletion Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2022,7 +2022,7 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
// initially we tried TVP, however it performs quite poorly.
// keep in mind SQL support up to 2000 params easily in sp_executesql, needing more is rare

if (FeatureSupport.Get(command.Connection).Arrays)
if (FeatureSupport.Get(command.Connection).Arrays && !Settings.ForceListExpansion)
{
var arrayParm = command.CreateParameter();
arrayParm.Value = SanitizeParameterValue(value);
Expand Down
4 changes: 3 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ When an object that implements the `IDynamicParameters` interface passed into `E

List Support
------------
Dapper allows you to pass in `IEnumerable<int>` and will automatically parameterize your query.
Dapper allows you to pass in `IEnumerable<int>` and will automatically parameterize your query if your database driver does not natively support passing a list as a parameter.

For example:

Expand All @@ -244,6 +244,8 @@ Will be translated to:
select * from (select 1 as Id union all select 2 union all select 3) as X where Id in (@Ids1, @Ids2, @Ids3)" // @Ids1 = 1 , @Ids2 = 2 , @Ids2 = 3
```

For database drivers that do support list parameters (at this time, Postgres and ClickHouse), list expansion is not performed since it is not required. However, automatic expansion can be used even if the driver does not require it by setting the global flag `SqlMapper.Settings.ForceListExpansion`. This is only needed when writing SQL that is intended for use by multiple databases, when trying to ensure consistent list handling and is disabled by default.

Literal replacements
------------
Dapper supports literal replacements for bool and numeric types.
Expand Down

0 comments on commit 0a140ef

Please sign in to comment.