Skip to content

Conversation

@elcritch
Copy link
Contributor

@elcritch elcritch commented Oct 14, 2025

This takes a different, simpler approach than the other PR. Transactions are just simple try excepts that handle setting up transactions instead of trying make query support multi-statements and then including transactions in the DSL.

  test "commit on success":
    transaction:
      query:
        insert person(id = ?(101), name = ?"john101", password = ?"p101", email = ?"john101@mail.com", salt = ?"s101", status = ?"ok")
    check db.getValue(sql"select count(*) from person where id = 101") == "1"
  test "rollback on error with else":
    # prepare one row
    var failed = false
    query:
      insert person(id = ?(501), name = ?"john501", password = ?"p501", email = ?"john501@mail.com", salt = ?"s501", status = ?"ok")
    # in transaction insert a new row and then violate PK
    transaction:
      query:
        insert person(id = ?(502), name = ?"john502", password = ?"p502", email = ?"john502@mail.com", salt = ?"s502", status = ?"ok")
      # duplicate key error
      query:
        insert person(id = ?(501), name = ?"dup", password = ?"p", email = ?"e", salt = ?"s", status = ?"x")
      check false # should not reach
    else:
      echo "do something else..."
      failed = true

Outer try excepts:

  test "rollback on error with manual try except":
    # prepare one row
    query:
      insert person(id = ?(201), name = ?"john201", password = ?"p201", email = ?"john201@mail.com", salt = ?"s201", status = ?"ok")
    # in transaction insert a new row and then violate PK
    try:
      transaction:
        query:
          insert person(id = ?(202), name = ?"john202", password = ?"p202", email = ?"john202@mail.com", salt = ?"s202", status = ?"ok")
        # duplicate key error
        query:
          insert person(id = ?(201), name = ?"dup", password = ?"p", email = ?"e", salt = ?"s", status = ?"x")
      check false # should not reach
    except DbError as e:
      discard

Co-authored-by: Andreas Rumpf <araq4k@proton.me>
retExpr: NimNode

# Execute a non-row SQL statement strictly (errors on failure)
template execNoRowsStrict*(sqlStmt: string) =
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so that is something GPT5 added. I'd committed the first changes already when I saw these. It looked odd but I figured they already existed.

I'm not sure if these are any better to use than a plain db.exec. Is doing the prepare statement and query steps beneficial? Seems like a bit of AI slop to drop.

elcritch and others added 2 commits October 14, 2025 23:21
Co-authored-by: Andreas Rumpf <araq4k@proton.me>
@Araq Araq merged commit c172414 into Araq:master Oct 16, 2025
1 check passed
@Araq
Copy link
Owner

Araq commented Oct 16, 2025

Good enough for me. Further improvements can be done as new PRs.

@elcritch elcritch deleted the transactions branch October 17, 2025 00:01
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