Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds setting ForceListExpansion to provide a way to enable consistent handling of list parameters regardless of RDBMS #1874

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Dapper/SqlMapper.Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public static void SetDefaults()
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; }

/// If assigned a non-negative value, then that value is applied to any commands <c>FetchSize</c> property, if it exists;
/// see https://docs.oracle.com/en/database/oracle/oracle-database/18/odpnt/CommandFetchSize.html; note that this value
/// can only be set globally - it is not intended for frequent/contextual changing.
Expand Down
2 changes: 1 addition & 1 deletion Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2132,7 +2132,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 @@ -259,7 +259,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 @@ -273,6 +273,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