forked from epgsql/pgapp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpgapp.erl
More file actions
128 lines (113 loc) · 5.37 KB
/
pgapp.erl
File metadata and controls
128 lines (113 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
%%%-------------------------------------------------------------------
%%% @author David N. Welton <davidw@dedasys.com>
%%% @copyright (C) 2015, David N. Welton
%%% @doc
%%%
%%% @end
%%% Created : 20 Feb 2015 by David N. Welton <davidw@dedasys.com>
%%%-------------------------------------------------------------------
-module(pgapp).
%% API
-export([connect/1, connect/2,
equery/2, equery/3, equery/4,
squery/1, squery/2, squery/3,
with_transaction/1, with_transaction/2, with_transaction/3]).
%%%===================================================================
%%% API
%%%===================================================================
connect(Settings) ->
connect(epgsql_pool, Settings).
connect(PoolName, Settings) ->
PoolSize = proplists:get_value(size, Settings, 5),
MaxOverflow = proplists:get_value(max_overflow, Settings, 5),
pgapp_sup:add_pool(PoolName, [{name, {local, PoolName}},
{worker_module, pgapp_worker},
{size, PoolSize},
{max_overflow, MaxOverflow}], Settings).
-spec equery(Sql :: epgsql:sql_query(),
Params :: list(epgsql:bind_param()))
-> epgsql:reply(epgsql:equery_row()) | {error, Reason :: any()}.
equery(Sql, Params) ->
pgapp_worker:equery(Sql, Params).
-spec equery(Sql :: epgsql:sql_query(),
Params :: list(epgsql:bind_param()),
Timeout :: atom() | integer())
-> epgsql:reply(epgsql:equery_row());
(PoolName :: atom(),
Sql::epgsql:sql_query(),
Params :: list(epgsql:bind_param()))
-> epgsql:reply(epgsql:equery_row()) | {error, Reason :: any()};
(Conn :: pid(),
Sql::epgsql:sql_query(),
Params :: list(epgsql:bind_param()))
-> epgsql:reply(epgsql:equery_row()) | {error, Reason :: any()}.
equery(P1, P2, P3) ->
pgapp_worker:equery(P1, P2, P3).
-spec equery(PoolName :: atom(),
Sql::epgsql:sql_query(),
Params :: list(epgsql:bind_param()),
Timeout :: atom() | integer())
-> epgsql:reply(epgsql:equery_row()) | {error, Reason :: any()}.
equery(PoolName, Sql, Params, Timeout) ->
pgapp_worker:equery(PoolName, Sql, Params, Timeout).
-spec squery(Sql :: epgsql:sql_query())
-> epgsql:reply(epgsql:squery_row()) |
[epgsql:reply(epgsql:squery_row())] | {error, Reason :: any()}.
squery(Sql) ->
pgapp_worker:squery(Sql).
-spec squery(Sql::epgsql:sql_query(),
Timeout :: atom() | integer())
-> epgsql:reply(epgsql:squery_row()) |
[epgsql:reply(epgsql:squery_row())] | {error, Reason :: any()};
(PoolName :: atom(),
Sql::epgsql:sql_query())
-> epgsql:reply(epgsql:squery_row()) |
[epgsql:reply(epgsql:squery_row())] | {error, Reason :: any()};
(Conn :: pid(),
Sql::epgsql:sql_query())
-> epgsql:reply(epgsql:squery_row()) |
[epgsql:reply(epgsql:squery_row())] | {error, Reason :: any()}.
squery(P1, P2) ->
pgapp_worker:squery(P1, P2).
-spec squery(PoolName :: atom(),
Sql :: epgsql:sql_query(),
Timeout :: atom() | integer())
-> epgsql:reply(epgsql:squery_row()) |
[epgsql:reply(epgsql:squery_row())] | {error, Reason :: any()}.
squery(PoolName, Sql, Timeout) ->
pgapp_worker:squery(PoolName, Sql, Timeout).
-spec with_transaction(Function :: fun((pid()) -> Reply))
-> Reply | {rollback | error, any()} when Reply :: any().
with_transaction(Fun) when is_function(Fun, 1) ->
with_transaction(epgsql_pool, Fun).
-spec with_transaction(PoolName :: atom(),
Function :: fun((pid()) -> Reply))
-> Reply | {rollback | error, any()} when Reply :: any();
(Conn :: pid(),
Function :: fun((pid()) -> Reply))
-> Reply | {rollback | error, any()} when Reply :: any();
(Function :: fun((pid()) -> Reply),
Timeout :: timeout())
-> Reply | {rollback | error, any()} when Reply :: any().
with_transaction(P1, Fun) when is_function(Fun, 1) ->
pgapp_worker:with_transaction(P1, Fun);
with_transaction(Fun, Timeout) when is_function(Fun, 1) ->
pgapp_worker:with_transaction(epgsql_pool, Fun, Timeout).
-spec with_transaction(PoolName :: atom(),
Function :: fun((pid()) -> Reply),
Timeout :: atom() | non_neg_integer())
-> Reply | {rollback | error, any()} when Reply :: any();
(Conn :: pid(),
Function :: fun((pid()) -> Reply),
Timeout :: atom() | non_neg_integer())
-> Reply | {rollback | error, any()} when Reply :: any().
with_transaction(P1, Fun, Timeout) when is_function(Fun, 1) ->
pgapp_worker:with_transaction(P1, Fun, Timeout).
%%--------------------------------------------------------------------
%% @doc
%% @spec
%% @end
%%--------------------------------------------------------------------
%%%===================================================================
%%% Internal functions
%%%===================================================================