88-behaviour (gen_server ).
99-behaviour (poolboy_worker ).
1010
11- -export ([squery /1 , squery /2 , squery /3 ,
12- equery /2 , equery /3 , equery /4 ,
13- with_transaction /2 , with_transaction /3 ]).
11+ -export ([squery /1 , squery /2 , squery /3 ]).
12+ -export ([equery /2 , equery /3 , equery /4 ]).
13+ -export ([with_transaction /2 , with_transaction /3 ]).
14+ -export ([is_connected /1 ]).
1415
1516-export ([start_link /1 ]).
1617
2930-define (STATE_VAR , '$pgapp_state' ).
3031
3132squery (Sql ) ->
33+ squery (Sql , ? TIMEOUT ).
34+
35+ squery (PoolName , Sql ) when is_atom (PoolName ) ->
36+ squery (PoolName , Sql , ? TIMEOUT );
37+ squery (Sql , Timeout ) ->
3238 case get (? STATE_VAR ) of
3339 undefined ->
34- squery (epgsql_pool , Sql );
40+ squery (epgsql_pool , Sql , Timeout );
3541 Conn ->
3642 epgsql :squery (Conn , Sql )
3743 end .
3844
39- squery (PoolName , Sql ) ->
40- squery (PoolName , Sql , ? TIMEOUT ).
41-
4245squery (PoolName , Sql , Timeout ) ->
4346 poolboy :transaction (PoolName ,
4447 fun (Worker ) ->
4548 gen_server :call (Worker , {squery , Sql }, Timeout )
4649 end , Timeout ).
4750
48-
4951equery (Sql , Params ) ->
52+ equery (Sql , Params , ? TIMEOUT ).
53+
54+ equery (PoolName , Sql , Params ) when is_atom (PoolName ) ->
55+ equery (PoolName , Sql , Params , ? TIMEOUT );
56+ equery (Sql , Params , Timeout ) ->
5057 case get (? STATE_VAR ) of
5158 undefined ->
52- equery (epgsql_pool , Sql , Params );
59+ equery (epgsql_pool , Sql , Params , Timeout );
5360 Conn ->
5461 epgsql :equery (Conn , Sql , Params )
5562 end .
5663
57- equery (PoolName , Sql , Params ) ->
58- equery (PoolName , Sql , Params , ? TIMEOUT ).
59-
6064equery (PoolName , Sql , Params , Timeout ) ->
6165 poolboy :transaction (PoolName ,
6266 fun (Worker ) ->
@@ -74,6 +78,9 @@ with_transaction(PoolName, Fun, Timeout) ->
7478 {transaction , Fun }, Timeout )
7579 end , Timeout ).
7680
81+ is_connected (WorkerPid ) ->
82+ gen_server :call (WorkerPid , {is_connected }, ? TIMEOUT ).
83+
7784start_link (Args ) ->
7885 gen_server :start_link (? MODULE , Args , []).
7986
@@ -93,7 +100,12 @@ handle_call({transaction, Fun}, _From,
93100 put (? STATE_VAR , Conn ),
94101 Result = epgsql :with_transaction (Conn , fun (_ ) -> Fun () end ),
95102 erase (? STATE_VAR ),
96- {reply , Result , State }.
103+ {reply , Result , State };
104+
105+ handle_call ({is_connected }, _From , # state {conn = undefined } = State ) ->
106+ {reply , false , State };
107+ handle_call ({is_connected }, _From , # state {conn = _Conn } = State ) ->
108+ {reply , true , State }.
97109
98110handle_cast (reconnect , State ) ->
99111 {noreply , connect (State )}.
0 commit comments