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