This repository was archived by the owner on Mar 19, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -150,6 +150,19 @@ defmodule Sqlitex do
150
150
exec ( db , stmt , call_opts )
151
151
end
152
152
153
+ @ doc """
154
+ Runs `fun` inside a transaction. If `fun` returns without raising an exception,
155
+ the transaction will be commited via `commit`. Otherwise, `rollback` will be called.
156
+
157
+ ## Examples
158
+ iex> {:ok, db} = Sqlitex.open(":memory:")
159
+ iex> Sqlitex.with_transaction(db, fn(db) ->
160
+ ...> Sqlitex.exec(db, "create table foo(id integer)")
161
+ ...> Sqlitex.exec(db, "insert into foo (id) values(42)")
162
+ ...> end)
163
+ iex> Sqlitex.query(db, "select * from foo")
164
+ {:ok, [[{:id, 42}]]}
165
+ """
153
166
@ spec with_transaction ( Sqlitex . connection , ( Sqlitex . connection -> any ( ) ) , Keyword . t ) :: any
154
167
def with_transaction ( db , fun , opts \\ [ ] ) do
155
168
with :ok <- exec ( db , "begin" , opts ) ,
Original file line number Diff line number Diff line change @@ -200,13 +200,16 @@ defmodule Sqlitex.Server do
200
200
Runs `fun` inside a transaction. If `fun` returns without raising an exception,
201
201
the transaction will be commited via `commit`. Otherwise, `rollback` will be called.
202
202
203
+ Be careful if `fun` might take a long time to run. The function is executed in the
204
+ context of the server and therefore blocks other requests until it's finished.
205
+
203
206
## Examples
204
- iex> {:ok, db } = Sqlitex.open (":memory:")
205
- iex> Sqlitex.with_transaction(db , fn(db) ->
207
+ iex> {:ok, server } = Sqlitex.Server.start_link (":memory:")
208
+ iex> Sqlitex.Server. with_transaction(server , fn(db) ->
206
209
...> Sqlitex.exec(db, "create table foo(id integer)")
207
210
...> Sqlitex.exec(db, "insert into foo (id) values(42)")
208
211
...> end)
209
- iex> Sqlitex.query(db , "select * from foo")
212
+ iex> Sqlitex.Server. query(server , "select * from foo")
210
213
{:ok, [[{:id, 42}]]}
211
214
"""
212
215
@ spec with_transaction ( pid ( ) , ( Sqlitex . connection -> any ( ) ) , Keyword . t ) :: any
You can’t perform that action at this time.
0 commit comments