Skip to content

feat(orm): use transactions and the existing async facades - #101

Merged
Antruly merged 1 commit into
masterfrom
feat/orm-tx-async
Nov 27, 2026
Merged

Antruly merged 1 commit into
masterfrom
feat/orm-tx-async

Conversation

@Antruly

@Antruly Antruly commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Closes #92. Parent: #88.

What changed

Transaction reuse

src/orm/uvcpp_orm_repository.h now runs through one of three executors:

  • uvcpp_db_client& — the existing synchronous path from [ORM] 结果映射与 CRUD #91;
  • uvcpp_db_tx& — every ORM statement goes through the transaction's own gate and connection; the ORM never calls begin()/commit()/rollback() itself. The first failure remains visible through tx.status()/tx.error();
  • uvcpp_db_pool& — synchronous borrow/release for callers not on the event loop.

The same overloads cover find_by_pk, find_all, insert, update, remove and count. Primary-key update/remove and insert/read-mapping semantics are unchanged.

Async facade

src/orm/uvcpp_orm_async.h adds callback-style:

  • find_by_pk_async, find_all_async, insert_async, update_async, remove_async.

It reuses uvcpp_db_pool's existing async paths; no new threads, pool or connection ownership. Submission failure returns a negative libuv code and does not call the callback; execution/mapping failure calls it exactly once with DB_ERROR and the underlying status name. insert_async uses the existing pool transaction path so it can return last_insert_id without adding a second async primitive.

Tests

tests/functional/orm_tx_async_func.cpp — 183 checks, real SQLite:

  • transaction commit, explicit rollback, statement failure/auto-rollback, transaction-body exception, same-connection use, first-error preservation;
  • callback exactly once, submission failure, bad SQL, unique conflict, pool max=1 mixed concurrency and borrow timeout;
  • async find/insert/update/remove and connection return counts.

Verification

ctest -R '^test_orm_(model|sql|sqlite|tx_async)_func$|^test_db_sqlite_func$'
1/5 test_db_sqlite_func      Passed 0.13
2/5 test_orm_model_func      Passed 0.00
3/5 test_orm_sql_func        Passed 0.01
4/5 test_orm_sqlite_func     Passed 0.02
5/5 test_orm_tx_async_func   Passed 0.29
100% tests passed, 0 tests failed out of 5

[orm_tx_async] checks=183 failures=0
[PASS] orm_tx_async: all 183 checks passed

C++11 -pedantic-errors compilation was exercised through the build. With ORM=OFF the orm_* targets are absent and the DB SQLite test remains.

Unverified / limits

  • PostgreSQL native async versus thread-pool path is not run: no PostgreSQL server is available here. The callback-count/pool tests cover the SQLite thread-pool path and the existing pool interfaces.
  • MySQL / ODBC / MongoDB real servers are not verified.
  • No new async cancellation, retry, coroutine or independent database thread pool was added, by design.
  • Full ctest and C ABI verification were not run; this change adds no C ABI surface.
  • find_all_async deliberately has no total parameter: that would require a second SQL round trip while promising one callback. Use a follow-up count query or the synchronous overload when a total is needed.

Factor the repository through client/tx/pool executors, add transaction overloads so ORM statements share the tx connection and preserve its first-error semantics, and add callback-style async CRUD on uvcpp_db_pool with submit-failure versus execution-failure separation.

Add SQLite transaction, callback-count, pool max=1 and borrow-timeout tests (183 checks); PostgreSQL native async remains unverified without a server.
@Antruly

Antruly commented Nov 27, 2026

Copy link
Copy Markdown
Owner Author

Local review before merge: transaction and async tests 183/183 on SQLite, ORM model 192/192, SQL 295/295, CRUD 117/117, DB SQLite 196 passed / 0 failed / 1 N/A. Callback counts, submission failures, pool max=1 and borrow timeout were exercised. PostgreSQL native async and other servers remain explicitly unverified; shared-runner CI is queued.

@Antruly
Antruly merged commit c20a98b into master Nov 27, 2026
46 of 52 checks passed
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.

[ORM] 事务与异步集成

1 participant