Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ Types:
Params = list()
```

##### Transaction

```erlang
pgpool:transaction(DatabaseName, Fun) -> (see epgsql for reply/error types)

Types:
DatabaseName = atom()
Fun = fun()
```

For example:

```erlang
pgpool:transaction(db1_name,
fun(C) ->
pgpool:squery(C, "DELETE FROM users WHERE foo = true"),
pgpool:squery(C, "UPDATE users SET bar = 100"),
pgpool:squery(C, "SELECT * FROM users")
end).
```

## Contributing
So you want to contribute? That's great! Please follow the guidelines below. It will make it easier to get merged in.

Expand Down
11 changes: 10 additions & 1 deletion src/pgpool_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

%% API
-export([start_link/1]).
-export([squery/2, equery/3]).
-export([squery/2, equery/3, transaction/2]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
Expand Down Expand Up @@ -43,6 +43,12 @@ equery(DatabaseName, Statement, Params) ->
gen_server:call(Worker, {equery, Statement, Params})
end).

-spec transaction(DatabaseName :: atom(), Fun :: fun()) -> any() | {error, no_connection}.
transaction(DatabaseName, Fun) ->
poolboy:transaction(DatabaseName, fun(Worker) ->
gen_server:call(Worker, {transaction, Fun})
end).

%% ===================================================================
%% Callbacks
%% ===================================================================
Expand Down Expand Up @@ -102,6 +108,9 @@ handle_call({squery, Sql}, _From, #state{conn = Conn} = State) ->
handle_call({equery, Statement, Params}, _From, #state{conn = Conn} = State) ->
{reply, epgsql:equery(Conn, Statement, Params), State};

handle_call({transaction, Fun}, _From, #state{conn = Conn} = State) ->
{reply, epgsql:with_transaction(Conn, Fun), State};

handle_call(Request, From, State) ->
error_logger:warning_msg("Received from ~p an unknown call message: ~p", [Request, From]),
{reply, undefined, State}.
Expand Down