Browse files

Parameterized queries for Postgres

  • Loading branch information...
1 parent 1718e9d commit f50fd5d0f40b4a0bff8fb63b6512bdf1629dd277 Evan Miller committed May 30, 2012
View
8 src/boss_db.erl
@@ -27,6 +27,7 @@
depth/0,
dump/0,
execute/1,
+ execute/2,
transaction/1,
validate_record/1,
validate_record_types/1,
@@ -202,6 +203,11 @@ dump() ->
execute(Commands) ->
db_call({execute, Commands}).
+%% @spec execute( Commands::iolist(), Params::list() ) -> RetVal
+%% @doc Execute database commands with interpolated parameters on SQL databases
+execute(Commands, Params) ->
+ db_call({execute, Commands, Params}).
+
%% @spec transaction( TransactionFun::function() ) -> {atomic, Result} | {aborted, Reason}
%% @doc Execute a fun inside a transaction.
transaction(TransactionFun) ->
@@ -361,4 +367,4 @@ return_one(Result) ->
case Result of
[] -> undefined;
[Record] -> Record
- end.
+ end.
View
5 src/boss_db_controller.erl
@@ -135,6 +135,11 @@ handle_call({execute, Commands}, _From, State) ->
Conn = State#state.connection,
{reply, Adapter:execute(Conn, Commands), State};
+handle_call({execute, Commands, Params}, _From, State) ->
+ Adapter = State#state.adapter,
+ Conn = State#state.connection,
+ {reply, Adapter:execute(Conn, Commands, Params), State};
+
handle_call({transaction, TransactionFun}, _From, State) ->
Adapter = State#state.adapter,
Conn = State#state.connection,
View
2 src/db_adapters/boss_db_adapter_mysql.erl
@@ -2,7 +2,7 @@
-behaviour(boss_db_adapter).
-export([init/1, terminate/1, start/1, stop/0, find/2, find/7]).
-export([count/3, counter/2, incr/3, delete/2, save_record/2]).
--export([push/2, pop/2, dump/1, execute/2, transaction/2]).
+-export([push/2, pop/2, dump/1, execute/2, execute/3, transaction/2]).
start(_) ->
ok.
View
5 src/db_adapters/boss_db_adapter_pgsql.erl
@@ -2,7 +2,7 @@
-behaviour(boss_db_adapter).
-export([init/1, terminate/1, start/1, stop/0, find/2, find/7]).
-export([count/3, counter/2, incr/3, delete/2, save_record/2]).
--export([push/2, pop/2, dump/1, execute/2, transaction/2]).
+-export([push/2, pop/2, dump/1, execute/2, execute/3, transaction/2]).
start(_) ->
ok.
@@ -131,6 +131,9 @@ dump(_Conn) -> "".
execute(Conn, Commands) ->
pgsql:squery(Conn, Commands).
+execute(Conn, Commands, Params) ->
+ pgsql:equery(Conn, Commands, Params).
+
transaction(Conn, TransactionFun) ->
case pgsql:with_transaction(Conn, fun(_C) -> TransactionFun() end) of
{rollback, Reason} -> {aborted, Reason};

0 comments on commit f50fd5d

Please sign in to comment.