Skip to content

Add UpdateMultiple for bulk updates - #320

Open
henkmollema wants to merge 1 commit into
masterfrom
add-update-multiple
Open

Add UpdateMultiple for bulk updates#320
henkmollema wants to merge 1 commit into
masterfrom
add-update-multiple

Conversation

@henkmollema

Copy link
Copy Markdown
Owner

Summary

Adds UpdateMultiple<T> / UpdateMultipleAsync<T> extension methods for bulk updates, modeled on EF Core''s ExecuteUpdate but kept simple and consistent with the rest of the Dommel API. They issue a single UPDATE ... SET ... WHERE statement without loading entities first.

await connection.UpdateMultipleAsync<Product>(
    update => update.SetProperty(p => p.Name, "<removed>")
                    .SetProperty(p => p.AmountInStock, p => p.AmountInStock - 1),
    x => x.AmountInStock == 0);

The properties to assign are described with a fluent SetProperty chain — either a constant value or a value selector that may reference entity columns and use arithmetic. Rows are filtered with a LINQ predicate. The methods return the number of affected rows (matching DeleteMultiple and ExecuteUpdate), and both sync and async variants are provided.

Implementation notes

  • New SetPropertyCalls<TEntity> fluent builder with both SetProperty overloads (constant value + value selector).
  • Single SqlExpression instance (obtained via the SqlExpressionFactory) builds both the set-clause and where-clause, so their auto-numbered parameters share numbering and never collide — and the where-clause stays JSON-aware under Dommel.Json.
  • SqlExpression now translates arithmetic operators (+ - * / %), enabling value selectors like p => p.Stock - 1 (also usable in Where).
  • Assignment targets are emitted unqualified (set [FullName] = ...) while where-clause columns stay qualified — some databases (e.g. Postgres) reject a table-qualified assignment target.
  • Constant values vs. rendered-SQL fragments are distinguished by whether the value expression references the entity parameter, so a constant string value is correctly parameterized.

Known limitations (documented in XML comments)

  • Value selectors support column op constant arithmetic; column op column is not supported (consistent with existing Where behavior).
  • Nested/precedence-sensitive arithmetic is not parenthesized.

Testing

  • Dommel.Tests: 206 passed (incl. new UpdateTests SQL-generation cases across SQL Server / MySQL / Postgres and an arithmetic-in-Where regression test); no regressions from the arithmetic-routing change.
  • Dommel.Json.Tests: 35 passed.
  • New sync + async integration tests added under the existing [ClassData(typeof(DatabaseTestData))] pattern (require local SQL Server / MySQL / PostgreSQL; not run in this environment).

Add UpdateMultiple/UpdateMultipleAsync extension methods that issue a
single UPDATE ... SET ... WHERE statement, modeled on EF Core's
ExecuteUpdate. The properties to assign are described with a fluent
SetProperty chain (constant values or column-referencing value
selectors); the rows are filtered with a LINQ predicate.

Both the set-clause and where-clause are built from a single
SqlExpression instance so their parameters share numbering, and the
SqlExpression visitor now translates arithmetic operators. Assignment
targets are emitted unqualified for cross-database compatibility.
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant