Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 593f842e2ef1c6e544ce5a9468e9c4562b08749d @devinus committed Aug 9, 2012
Showing with 113 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +34 −0 Makefile
  3. BIN rebar
  4. +4 −0 rebar.config
  5. +7 −0 src/epgsql_pool.app.src
  6. +18 −0 src/epgsql_pool.erl
  7. +48 −0 src/epgsql_pool_worker.erl
@@ -0,0 +1,2 @@
+deps/
+ebin/
@@ -0,0 +1,34 @@
+APP = zuul
+REBAR = ./rebar
+DIALYZER = dialyzer
+
+DIALYZER_WARNINGS = -Wunmatched_returns -Werror_handling \
+ -Wrace_conditions -Wunderspecs
+
+.PHONY: all compile test clean get-deps build-plt dialyze generate
+
+all: compile
+
+compile:
+ @$(REBAR) compile
+
+test: compile
+ @$(REBAR) eunit skip_deps=true
+
+clean:
+ @$(REBAR) clean
+ @rm -rf rel/$(APP)
+
+get-deps:
+ @$(REBAR) get-deps
+
+build-plt:
+ @$(DIALYZER) --build_plt --output_plt .dialyzer_plt \
+ --apps kernel stdlib
+
+dialyze: compile
+ @$(DIALYZER) --src src --plt .dialyzer_plt $(DIALYZER_WARNINGS)
+
+generate: compile
+ @rm -rf rel/$(APP)
+ @(cd rel && ../$(REBAR) generate)
BIN rebar
Binary file not shown.
@@ -0,0 +1,4 @@
+{erl_opts, [warnings_as_errors]}.
+{eunit_opts, [verbose]}.
+{cover_enabled, true}.
+{deps, [{poolboy, ".*", {git, "git@github.com:devinus/poolboy.git", master}}]}.
@@ -0,0 +1,7 @@
+{application, epgsql_pool, [
+ {description, "Pooled epgsql connections using Poolboy"},
+ {vsn, "0.1.0"},
+ {applications, [kernel, stdlib]},
+ {registered, []},
+ {mod, {epgsql_pool, []}}
+]}.
@@ -0,0 +1,18 @@
+-module(epgsql_pool).
+
+-export([child_spec/2, squery/2, equery/3]).
+
+child_spec(Name, Args) ->
+ WorkerModule = {worker_module, epgsql_pool_worker},
+ PoolArgs = lists:keystore(worker_module, 1, Args, WorkerModule),
+ poolboy:child_spec(Name, PoolArgs).
+
+squery(PoolName, Sql) ->
+ poolboy:transaction(PoolName, fun(Worker) ->
+ epgsql_pool_worker:squery(Worker, Sql)
+ end).
+
+equery(PoolName, Stmt, Params) ->
+ poolboy:transaction(PoolName, fun(Worker) ->
+ epgsql_pool_worker:equery(Worker, Stmt, Params)
+ end).
@@ -0,0 +1,48 @@
+-module(epgsql_pool_worker).
+-behaviour(gen_server).
+-behaviour(poolboy_worker).
+
+-export([start_link/1, squery/2, equery/3]).
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
+ code_change/3]).
+
+-record(state, {conn}).
+
+start_link(Args) ->
+ gen_server:start_link(?MODULE, Args, []).
+
+squery(Worker, Sql) ->
+ gen_server:call(Worker, {squery, Sql}).
+
+equery(Worker, Stmt, Params) ->
+ gen_server:call(Worker, {equery, Stmt, Params}).
+
+init(Args) ->
+ Hostname = proplists:get_value(hostname, Args),
+ Database = proplists:get_value(database, Args),
+ Username = proplists:get_value(username, Args),
+ Password = proplists:get_value(password, Args),
+ {ok, Conn} = pgsql:connect(Hostname, Username, Password, [
+ {database, Database}
+ ]),
+ {ok, #state{conn=Conn}}.
+
+handle_call({squery, Sql}, _From, #state{conn=Conn}=State) ->
+ {reply, pgsql:squery(Conn, Sql), State};
+handle_call({equery, Stmt, Params}, _From, #state{conn=Conn}=State) ->
+ {reply, pgsql:equery(Conn, Stmt, Params), State};
+handle_call(_Request, _From, State) ->
+ {reply, ok, State}.
+
+handle_cast(_Msg, State) ->
+ {noreply, State}.
+
+handle_info(_Info, State) ->
+ {noreply, State}.
+
+terminate(_Reason, #state{conn=Conn}) ->
+ ok = pgsql:close(Conn),
+ ok.
+
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.

0 comments on commit 593f842

Please sign in to comment.