lpil / sqlight

💡 Use SQLite from Gleam!
https://hexdocs.pm/sqlight
79 stars 11 forks source link

Transaction Support #6

Open schurhammer opened 5 months ago

schurhammer commented 5 months ago

Has there been any thoughts about transaction support? Not only is it good for correctness, putting inserts in a transaction often improves performance too!

lpil commented 5 months ago

There's no connection pooling or anything so you can just run exec(conn, "begin") to start a transaction. Just be sure not to share the connection with other processes

nozomi-core commented 1 month ago

@lpil Sorry to add this here, but I wanted to ask a QS about the interop between gleam/erlang. I have a fork of this library because I intent to implement more from the esqlite3 library for erlang. I want to implement prepared statements using bind and step

step(Stmt) -> esqlite3:step(Stmt).

The issue I have is that the esqlite3:step function in erlang does not simply return {ok, error} clauses but a value of '$done' I am not sure what atom.create_from_string("$done") means in gleam runtime. I am not super familiar with erlang code, just enough to interop with gleam for simple stuff. I believe '$done' is an atom but not sure how it gets read by gleam

sqlight_test.prepare_test
     No case clause matched
     Value: atom.create_from_string("$done")
     stacktrace:
       sqlight_ffi.step
       sqlight.bind_step
       sqlight_test.prepare_test
     output: 

In the esqlite3.erl library, the step function is implemented as

-spec step(Statement) -> StepResult 
    when Statement :: esqlite3_stmt(),
         StepResult:: row() | '$done' | error().
step(#esqlite3_stmt{stmt=Stmt}) ->
    esqlite3_nif:step(Stmt).

I just want to return {ok, error} to gleam if it was success Thanks

lpil commented 1 month ago

It's an atom, Gleam doesn't do anything special with it. You'd need to pattern match on it in your Erlang bindings.

We'd happily support whatever extra functionality you need here

nozomi-core commented 1 month ago

Ah thanks, what I am trying to do is perform a prepared statement in the form on insert into X(name) values(?) or update X set name = ? I am not sure if this can be done with sqlight.query since that is expecting a returned cursor with expecting some data types, correct me if I am wrong, thanks for the reply

EDIT: You can ignore this comment. I just realised you can call sqlight.query for inserts and updates expecting an Ok