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
|
%%% File : pgsql.erl
%%% Author : Christian Sunesson <chsu79@gmail.com>
%%% Description : PostgresQL interface
%%% Created : 11 May 2005
%%
%% API for accessing the postgres driver.
%%
-module(pgsql).
-export([connect/1, connect/4, connect/5]).
-export([squery/2,
pquery/3,
terminate/1,
prepare/3, unprepare/2,
execute/3]).
connect(Host, Database, User, Password) ->
connect([{database, Database},
{host, Host},
{user, User},
{password, Password}]).
connect(Host, Database, User, Password, Port) ->
connect([{database, Database},
{host, Host},
{user, User},
{port, Port},
{password, Password}]).
connect(Options) ->
pgsql_proto:start(Options).
%% Close a connection
terminate(Db) ->
gen_server:call(Db, terminate).
%%% In the "simple query" protocol, the frontend just sends a
%%% textual query string, which is parsed and immediately
%%% executed by the backend.
%% A simple query can contain multiple statements (separated with a semi-colon),
%% and each statement's response.
%%% squery(Db, Query) -> {ok, Results} | ... no real error handling
%%% Query = string()
%%% Results = [Result]
%%% Result = {"SELECT", RowDesc, ResultSet} | ...
squery(Db, Query) ->
gen_server:call(Db, {squery, Query}, infinity).
%%% In the "extended query" protocol, processing of queries is
%%% separated into multiple steps: parsing, binding of parameter
%%% values, and execution. This offers flexibility and performance
%%% benefits, at the cost of extra complexity.
%%% pquery(Db, Query, Params) -> {ok, Command, Status, NameTypes, Rows} | timeout | ...
%%% Query = string()
%%% Params = [term()]
%%% Command = string()
%%% Status = idle | transaction | failed_transaction
%%% NameTypes = [{ColName, ColType}]
%%% Rows = [list()]
pquery(Db, Query, Params) ->
gen_server:call(Db, {equery, {Query, Params}}).
%%% prepare(Db, Name, Query) -> {ok, Status, ParamTypes, ResultTypes}
%%% Status = idle | transaction | failed_transaction
%%% ParamTypes = [atom()]
%%% ResultTypes = [{ColName, ColType}]
prepare(Db, Name, Query) when is_atom(Name) ->
gen_server:call(Db, {prepare, {atom_to_list(Name), Query}}).
%%% unprepare(Db, Name) -> ok | timeout | ...
%%% Name = atom()
unprepare(Db, Name) when is_atom(Name) ->
gen_server:call(Db, {unprepare, atom_to_list(Name)}).
%%% execute(Db, Name, Params) -> {ok, Result} | timeout | ...
%%% Result = {'INSERT', NRows} |
%%% {'DELETE', NRows} |
%%% {'SELECT', ResultSet} |
%%% ...
%%% ResultSet = [Row]
%%% Row = list()
execute(Db, Name, Params) when is_atom(Name), is_list(Params) ->
Ref = make_ref(),
Db ! {execute, Ref, self(), {atom_to_list(Name), Params}},
receive
{pgsql, Ref, Result} ->
{ok, Result}
after 5000 ->
timeout
end.
|