Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.

Commit 1644bcb

Browse files
the-kennyConnorRigby
authored andcommitted
Improve documentation of with_transaction
1 parent 03b87ae commit 1644bcb

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/sqlitex.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ defmodule Sqlitex do
150150
exec(db, stmt, call_opts)
151151
end
152152

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+
"""
153166
@spec with_transaction(Sqlitex.connection, (Sqlitex.connection -> any()), Keyword.t) :: any
154167
def with_transaction(db, fun, opts \\ []) do
155168
with :ok <- exec(db, "begin", opts),

lib/sqlitex/server.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,16 @@ defmodule Sqlitex.Server do
200200
Runs `fun` inside a transaction. If `fun` returns without raising an exception,
201201
the transaction will be commited via `commit`. Otherwise, `rollback` will be called.
202202
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+
203206
## 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) ->
206209
...> Sqlitex.exec(db, "create table foo(id integer)")
207210
...> Sqlitex.exec(db, "insert into foo (id) values(42)")
208211
...> end)
209-
iex> Sqlitex.query(db, "select * from foo")
212+
iex> Sqlitex.Server.query(server, "select * from foo")
210213
{:ok, [[{:id, 42}]]}
211214
"""
212215
@spec with_transaction(pid(), (Sqlitex.connection -> any()), Keyword.t) :: any

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy