1%%% Copyright (C) 2011 - Anton Lebedevich.  All rights reserved.
2
3-module(epgsqli).
4
5-export([start_link/0,
6         connect/1, connect/2, connect/3, connect/4, connect/5,
7         close/1,
8         get_parameter/2,
9         set_notice_receiver/2,
10         get_cmd_status/1,
11         squery/2,
12         equery/2, equery/3,
13         prepared_query/3,
14         parse/2, parse/3, parse/4,
15         describe/2, describe/3,
16         bind/3, bind/4,
17         execute/2, execute/3, execute/4,
18         execute_batch/2,
19         close/2, close/3,
20         sync/1,
21         cancel/1]).
22
23-include("epgsql.hrl").
24
25%% -- client interface --
26
27start_link() ->
28    epgsql_sock:start_link().
29
30connect(Opts) ->
31    Settings = epgsql:to_proplist(Opts),
32    Host = proplists:get_value(host, Settings, "localhost"),
33    Username = proplists:get_value(username, Settings, os:getenv("USER")),
34    Password = proplists:get_value(password, Settings, ""),
35    connect(Host, Username, Password, Settings).
36
37connect(Host, Opts) ->
38    connect(Host, os:getenv("USER"), "", Opts).
39
40connect(Host, Username, Opts) ->
41    connect(Host, Username, "", Opts).
42
43connect(Host, Username, Password, Opts) ->
44    {ok, C} = epgsql_sock:start_link(),
45    connect(C, Host, Username, Password, Opts).
46
47-spec connect(epgsql:connection(), inet:ip_address() | inet:hostname(),
48              string(), string(), [epgsql:connect_option()]) -> reference().
49connect(C, Host, Username, Password, Opts) ->
50    Opts1 = epgsql:to_proplist(Opts),
51    epgsqla:complete_connect(
52      C, incremental(C, epgsql_cmd_connect, {Host, Username, Password, Opts1}), Opts1).
53
54-spec close(epgsql:connection()) -> ok.
55close(C) ->
56    epgsql_sock:close(C).
57
58-spec get_parameter(epgsql:connection(), binary()) -> binary() | undefined.
59get_parameter(C, Name) ->
60    epgsql_sock:get_parameter(C, Name).
61
62-spec set_notice_receiver(epgsql:connection(), undefined | pid() | atom()) ->
63                                 {ok, Previous :: pid() | atom()}.
64set_notice_receiver(C, PidOrName) ->
65    epgsql_sock:set_notice_receiver(C, PidOrName).
66
67-spec get_cmd_status(epgsql:connection()) -> {ok, Status} when
68          Status :: undefined | atom() | {atom(), integer()}.
69get_cmd_status(C) ->
70    epgsql_sock:get_cmd_status(C).
71
72-spec squery(epgsql:connection(), epgsql:sql_query()) -> reference().
73squery(C, Sql) ->
74    incremental(C, epgsql_cmd_squery, Sql).
75
76-spec equery(epgsql:connection(), epgsql:statement()) -> reference().
77equery(C, Statement) ->
78    equery(C, Statement, []).
79
80-spec equery(epgsql:connection(), epgsql:statement(), [epgsql:typed_param()]) -> reference().
81equery(C, Statement, TypedParameters) ->
82    incremental(C, epgsql_cmd_equery, {Statement, TypedParameters}).
83
84-spec prepared_query(epgsql:connection(), epgsql:statement(), [epgsql:typed_param()]) -> reference().
85prepared_query(C, Statement, TypedParameters) ->
86    incremental(C, epgsql_cmd_prepared_query, {Statement, TypedParameters}).
87
88-spec parse(epgsql:connection(), epgsql:sql_query()) -> reference().
89parse(C, Sql) ->
90    parse(C, "", Sql, []).
91
92-spec parse(epgsql:connection(), epgsql:sql_query(), [epgsql:epgsql_type()]) -> reference().
93parse(C, Sql, Types) ->
94    parse(C, "", Sql, Types).
95
96-spec parse(epgsql:connection(), iolist(), epgsql:sql_query(), [epgsql:epgsql_type()]) -> reference().
97parse(C, Name, Sql, Types) ->
98    incremental(C, epgsql_cmd_parse, {Name, Sql, Types}).
99
100bind(C, Statement, Parameters) ->
101    bind(C, Statement, "", Parameters).
102
103-spec bind(epgsql:connection(), epgsql:statement(), string(), [epgsql:bind_param()]) -> reference().
104bind(C, Statement, PortalName, Parameters) ->
105    incremental(C, epgsql_cmd_bind, {Statement, PortalName, Parameters}).
106
107execute(C, S) ->
108    execute(C, S, "", 0).
109
110execute(C, S, N) ->
111    execute(C, S, "", N).
112
113-spec execute(epgsql:connection(), epgsql:statement(), string(), non_neg_integer()) -> reference().
114execute(C, Statement, PortalName, MaxRows) ->
115    incremental(C, epgsql_cmd_execute, {Statement, PortalName, MaxRows}).
116
117-spec execute_batch(epgsql:connection(), [{epgsql:statement(), [epgsql:bind_param()]}]) -> reference().
118execute_batch(C, Batch) ->
119    incremental(C, epgsql_cmd_batch, Batch).
120
121describe(C, #statement{name = Name}) ->
122    describe(C, statement, Name).
123
124describe(C, statement, Name) ->
125    incremental(C, epgsql_cmd_describe_statement, Name);
126
127describe(C, portal, Name) ->
128    incremental(C, epgsql_cmd_describe_portal, Name).
129
130close(C, #statement{name = Name}) ->
131    close(C, statement, Name).
132
133close(C, Type, Name) ->
134    incremental(C, epgsql_cmd_close, {Type, Name}).
135
136sync(C) ->
137    incremental(C, epgsql_cmd_sync, []).
138
139-spec cancel(epgsql:connection()) -> ok.
140cancel(C) ->
141    epgsql_sock:cancel(C).
142
143
144%% -- internal functions --
145
146incremental(C, Command, Args) ->
147    epgsql_sock:async_command(C, incremental, Command, Args).
148