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 3898670
Show file tree
Hide file tree
Showing 3 changed files with 16 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 all lists provided as query parameters, regardless of whether the underlying
/// database has native support for lists or not. Enabling this makes it possible to write queries expecting list parameters that work
/// on databases that support lists, as well as database that do not. 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
9 changes: 8 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 have support for passing a list as a parameter.

For example:

Expand All @@ -244,6 +244,13 @@ 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, as it is not required. However, automatic expansion can be enabled by setting a global flag, if the need arises, such as in writing SQL that is used by multiple databases, where only some of the target databases support lists natively.

```csharp
// This setting is false, by default.
SqlMapper.Settings.ForceListExpansion = true;
```

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

0 comments on commit 3898670

Please sign in to comment.