Skip to content

SQLiteStmt::Use: Rename operator() -> apply()#491

Merged
cole-h merged 1 commit into
mainfrom
sqlite-operator
Jun 9, 2026
Merged

SQLiteStmt::Use: Rename operator() -> apply()#491
cole-h merged 1 commit into
mainfrom
sqlite-operator

Conversation

@edolstra

@edolstra edolstra commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

Motivation

Upstream NixOS#15989.

Using operator() causes very ugly clang formatting, e.g.

    state.stmts->RegisterValidPath
        .use()(printStorePath(info.path))(info.narHash.to_string(HashFormat::Base16, true))(
            info.registrationTime == 0 ? time(nullptr) : info.registrationTime)(
            info.deriver ? printStorePath(*info.deriver) : "",
            (bool) info.deriver)(info.narSize, info.narSize != 0)(info.ultimate ? 1 : 0, info.ultimate)(
            concatStringsSep(" ", Signature::toStrings(info.sigs)),
            !info.sigs.empty())(renderContentAddress(info.ca), (bool) info.ca)

This was especially painful when there was a merge conflict involving this type of code.

Now it looks like this:

    state.stmts->RegisterValidPath.use()
        .apply(printStorePath(info.path))
        .apply(info.narHash.to_string(HashFormat::Base16, true))
        .apply(info.registrationTime == 0 ? time(nullptr) : info.registrationTime)
        .apply(info.deriver ? printStorePath(*info.deriver) : "", (bool) info.deriver)
        .apply(info.narSize, info.narSize != 0)
        .apply(info.ultimate ? 1 : 0, info.ultimate)
        .apply(concatStringsSep(" ", Signature::toStrings(info.sigs)), !info.sigs.empty())
        .apply(renderContentAddress(info.ca), (bool) info.ca)

Context

Summary by CodeRabbit

  • Refactor
    • Standardized database statement parameter binding patterns across the codebase for improved code consistency and maintainability.

Using operator() caused very ugly clang formatting, e.g.

    state.stmts->RegisterValidPath
        .use()(printStorePath(info.path))(info.narHash.to_string(HashFormat::Base16, true))(
            info.registrationTime == 0 ? time(nullptr) : info.registrationTime)(
            info.deriver ? printStorePath(*info.deriver) : "",
            (bool) info.deriver)(info.narSize, info.narSize != 0)(info.ultimate ? 1 : 0, info.ultimate)(
            concatStringsSep(" ", Signature::toStrings(info.sigs)),
            !info.sigs.empty())(renderContentAddress(info.ca), (bool) info.ca)

This was especially painful when there was a merge conflict involving
this type of code.

Now it looks like this:

    state.stmts->RegisterValidPath.use()
        .apply(printStorePath(info.path))
        .apply(info.narHash.to_string(HashFormat::Base16, true))
        .apply(info.registrationTime == 0 ? time(nullptr) : info.registrationTime)
        .apply(info.deriver ? printStorePath(*info.deriver) : "", (bool) info.deriver)
        .apply(info.narSize, info.narSize != 0)
        .apply(info.ultimate ? 1 : 0, info.ultimate)
        .apply(concatStringsSep(" ", Signature::toStrings(info.sigs)), !info.sigs.empty())
        .apply(renderContentAddress(info.ca), (bool) info.ca)

(cherry picked from commit 509aaa0)
@coderabbitai

coderabbitai Bot commented Jun 9, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0aaf6cbc-4854-4c1b-a006-afaab24250bc

📥 Commits

Reviewing files that changed from the base of the PR and between 356af9d and cc69b29.

📒 Files selected for processing (6)
  • src/libexpr/eval-cache.cc
  • src/libfetchers/cache.cc
  • src/libstore/include/nix/store/sqlite.hh
  • src/libstore/local-store.cc
  • src/libstore/nar-info-disk-cache.cc
  • src/libstore/sqlite.cc

📝 Walkthrough

Walkthrough

This PR refactors SQLite statement parameter binding across the Nix codebase. The SQLiteStmt::Use fluent interface switches from operator() overloads to explicit apply() methods, and all call sites are updated from use()(a)(b) to use().apply(a).apply(b) chaining, with no functional logic changes.

Changes

SQLiteStmt API migration to apply()

Layer / File(s) Summary
SQLiteStmt::Use apply() API definition
src/libstore/include/nix/store/sqlite.hh, src/libstore/sqlite.cc
Replace operator() overloads with apply() methods for std::string_view, byte buffer (const unsigned char *, size_t), and int64_t parameter binding; internal binding logic and null-handling behavior unchanged.
Eval cache adapter to apply()
src/libexpr/eval-cache.cc
Update AttrDb write methods (setAttrs, setString, setBool, setInt, setListOfStrings, setPlaceholder, setMissing, setMisc, setFailed) and read method (getAttr) to chain .use().apply(...).exec() instead of .use()(...).exec().
Fetchers cache adapter to apply()
src/libfetchers/cache.cc
Update CacheImpl::upsert and lookupExpired statement invocations to bind parameters via chained apply() calls.
Local store adapter to apply()
src/libstore/local-store.cc
Migrate fourteen query/write operations across derived output registration, store path info queries, reference tracking, and realisation lookup to use fluent use().apply(...) binding.
NAR info disk cache adapter to apply()
src/libstore/nar-info-disk-cache.cc
Update cache purge bookkeeping, cache lookup/creation, and NAR/realisation upserts in NarInfoDiskCacheImpl to bind parameters via apply() chaining.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 With whiskers twitching, off we go,
Refactoring SQLite to make it flow,
From operator() calls to apply() chains,
The fluent API eases all the strains!
No logic shifts, just style divine—
The rabbit hops through every line. 🌿

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 5.13% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: renaming operator() to apply() in SQLiteStmt::Use, which is the core refactoring across all modified files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sqlite-operator

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Infer (1.2.0)
src/libfetchers/cache.cc

src/libfetchers/cache.cc:1:10: fatal error: 'nix/fetchers/cache.hh' file not found
1 | #include "nix/fetchers/cache.hh"
| ^~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
src/libfetchers/cache.cc:44:5-58:5: ERROR translating statement 'CompoundStmt'
Aborting translation of method 'nix::fetchers::CacheImpl::CacheImpl' in file 'src/libfetchers/cache.cc': "Assert_failure src/clang/cAst_utils.ml:249:53"
Uncaught Internal Error: "Assert_failure src/clang/cAst_utils.ml:249:53"
Error backtrace:
Raised at ClangFrontend__CAst_utils.get_decl_from_typ_ptr in file "src/clang/cAst_utils.ml", line 249, characters 53-65
Called from ClangFrontend__CTrans.CTrans_funct.get_destructor_decl_ref in file "src/clang/cTrans.ml", line 658, characters 12-59
Called from ClangFrontend__CTrans.CTrans_funct.destructor_calls.(fun) in file "src/clang/cTrans.ml", line 2048, characters 12-69
Called from Base__List.rev_filter_map.loop in file "src/list.ml", line 944, characters 13-17
Called from Base__List.

... [truncated 2200 characters] ...

ontend_decl.ml" (inlined), line 54, characters 4-52
Called from ClangFrontend__CFrontend_decl.CFrontend_decl_funct.process_method_decl.add_method_if_create_procdesc in file "src/clang/cFrontend_decl.ml" (inlined), line 123, characters 16-158
Called from ClangFrontend__CFrontend_decl.CFrontend_decl_funct.process_method_decl in file "src/clang/cFrontend_decl.ml", line 126, characters 17-97
Called from ClangFrontend__CFrontend_decl.CFrontend_decl_funct.process_methods in file "src/clang/cFrontend_decl.ml" (inlined), line 270, characters 8-122
Called from Stdlib__List.iter in file "list.ml" (inlined), line 110, characters 12-15
Called from Stdlib__List.iter in file "list.ml" (inlined), line 108, characters 13-64
Called from Base__List0.iter in file "src/list0.ml" (inlined), line 25, characters

src/libexpr/eval-cache.cc

src/libexpr/eval-cache.cc:1:10: fatal error: 'nix/util/users.hh' file not found
1 | #include "nix/util/users.hh"
| ^~~~~~~~~~~~~~~~~~~
1 error generated.
src/libexpr/eval-cache.cc:70:5-94:5: ERROR translating statement 'CompoundStmt'
Aborting translation of method 'nix::eval_cache::AttrDb::AttrDb' in file 'src/libexpr/eval-cache.cc': "Assert_failure src/clang/cAst_utils.ml:249:53"
Uncaught Internal Error: "Assert_failure src/clang/cAst_utils.ml:249:53"
Error backtrace:
Raised at ClangFrontend__CAst_utils.get_decl_from_typ_ptr in file "src/clang/cAst_utils.ml", line 249, characters 53-65
Called from ClangFrontend__CTrans.CTrans_funct.get_destructor_decl_ref in file "src/clang/cTrans.ml", line 658, characters 12-59
Called from ClangFrontend__CTrans.CTrans_funct.destructor_calls.(fun) in file "src/clang/cTrans.ml", line 2048, characters 12-69
Called from Base__List.rev_filter_map.loop in file "src/list.ml", line 944, characters 13-17
Called from Base__List.filter_map in

... [truncated 2200 characters] ...

l" (inlined), line 54, characters 4-52
Called from ClangFrontend__CFrontend_decl.CFrontend_decl_funct.process_method_decl.add_method_if_create_procdesc in file "src/clang/cFrontend_decl.ml" (inlined), line 123, characters 16-158
Called from ClangFrontend__CFrontend_decl.CFrontend_decl_funct.process_method_decl in file "src/clang/cFrontend_decl.ml", line 126, characters 17-97
Called from ClangFrontend__CFrontend_decl.CFrontend_decl_funct.process_methods in file "src/clang/cFrontend_decl.ml" (inlined), line 270, characters 8-122
Called from Stdlib__List.iter in file "list.ml" (inlined), line 110, characters 12-15
Called from Stdlib__List.iter in file "list.ml" (inlined), line 108, characters 13-64
Called from Base__List0.iter in file "src/list0.ml" (inlined), line 25, characters 16-35
Called

src/libstore/nar-info-disk-cache.cc

src/libstore/nar-info-disk-cache.cc:1:10: fatal error: 'nix/store/nar-info-disk-cache.hh' file not found
1 | #include "nix/store/nar-info-disk-cache.hh"
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
src/libstore/nar-info-disk-cache.cc:91:5-171:5: ERROR translating statement 'CompoundStmt'
Aborting translation of method 'nix::NarInfoDiskCacheImpl::NarInfoDiskCacheImpl' in file 'src/libstore/nar-info-disk-cache.cc': "Assert_failure src/clang/cAst_utils.ml:249:53"
Uncaught Internal Error: "Assert_failure src/clang/cAst_utils.ml:249:53"
Error backtrace:
Raised at ClangFrontend__CAst_utils.get_decl_from_typ_ptr in file "src/clang/cAst_utils.ml", line 249, characters 53-65
Called from ClangFrontend__CTrans.CTrans_funct.get_destructor_decl_ref in file "src/clang/cTrans.ml", line 658, characters 12-59
Called from ClangFrontend__CTrans.CTrans_funct.destructor_calls.(fun) in file "src/clang/cTrans.ml", line 2048, characters 12-69
Called from Base__List.rev_filter_map

... [truncated 2200 characters] ...

Frontend__CFrontend_decl.CFrontend_decl_funct.add_method in file "src/clang/cFrontend_decl.ml" (inlined), line 54, characters 4-52
Called from ClangFrontend__CFrontend_decl.CFrontend_decl_funct.process_method_decl.add_method_if_create_procdesc in file "src/clang/cFrontend_decl.ml" (inlined), line 123, characters 16-158
Called from ClangFrontend__CFrontend_decl.CFrontend_decl_funct.process_method_decl in file "src/clang/cFrontend_decl.ml", line 126, characters 17-97
Called from ClangFrontend__CFrontend_decl.CFrontend_decl_funct.process_methods in file "src/clang/cFrontend_decl.ml" (inlined), line 270, characters 8-122
Called from Stdlib__List.iter in file "list.ml" (inlined), line 110, characters 12-15
Called from Stdlib__List.iter in file "list.ml" (inlined), line 108, characters 13-64
Cal

  • 2 others

Comment @coderabbitai help to get the list of available commands and usage tips.

@edolstra edolstra enabled auto-merge June 9, 2026 13:32
@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown

@github-actions github-actions Bot temporarily deployed to pull request June 9, 2026 13:35 Inactive
@edolstra edolstra added this pull request to the merge queue Jun 9, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 9, 2026
@cole-h cole-h added this pull request to the merge queue Jun 9, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 9, 2026
@cole-h cole-h added this pull request to the merge queue Jun 9, 2026
Merged via the queue into main with commit 8cde257 Jun 9, 2026
30 checks passed
@cole-h cole-h deleted the sqlite-operator branch June 9, 2026 19:29
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.

2 participants