123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- %%% Copyright (C) 2011 - Anton Lebedevich. All rights reserved.
- -module(epgsqli).
- -export([start_link/0,
- connect/1, connect/2, connect/3, connect/4, connect/5,
- close/1,
- get_parameter/2,
- set_notice_receiver/2,
- get_cmd_status/1,
- squery/2,
- equery/2, equery/3,
- prepared_query/3,
- parse/2, parse/3, parse/4,
- describe/2, describe/3,
- bind/3, bind/4,
- execute/2, execute/3, execute/4,
- execute_batch/2,
- close/2, close/3,
- sync/1,
- cancel/1]).
- -include("epgsql.hrl").
- %% -- client interface --
- start_link() ->
- epgsql_sock:start_link().
- connect(Opts) ->
- Settings = epgsql:to_proplist(Opts),
- Host = proplists:get_value(host, Settings, "localhost"),
- Username = proplists:get_value(username, Settings, os:getenv("USER")),
- Password = proplists:get_value(password, Settings, ""),
- connect(Host, Username, Password, Settings).
- connect(Host, Opts) ->
- connect(Host, os:getenv("USER"), "", Opts).
- connect(Host, Username, Opts) ->
- connect(Host, Username, "", Opts).
- connect(Host, Username, Password, Opts) ->
- {ok, C} = epgsql_sock:start_link(),
- connect(C, Host, Username, Password, Opts).
- -spec connect(epgsql:connection(), inet:ip_address() | inet:hostname(),
- string(), string(), [epgsql:connect_option()]) -> reference().
- connect(C, Host, Username, Password, Opts) ->
- epgsqla:complete_connect(C, incremental(C, {connect, Host, Username, Password, epgsql:to_proplist(Opts)})).
- -spec close(epgsql:connection()) -> ok.
- close(C) ->
- epgsql_sock:close(C).
- -spec get_parameter(epgsql:connection(), binary()) -> binary() | undefined.
- get_parameter(C, Name) ->
- epgsql_sock:get_parameter(C, Name).
- -spec set_notice_receiver(epgsql:connection(), undefined | pid() | atom()) ->
- {ok, Previous :: pid() | atom()}.
- set_notice_receiver(C, PidOrName) ->
- epgsql_sock:set_notice_receiver(C, PidOrName).
- -spec get_cmd_status(epgsql:connection()) -> {ok, Status}
- when
- Status :: undefined | atom() | {atom(), integer()}.
- get_cmd_status(C) ->
- epgsql_sock:get_cmd_status(C).
- -spec squery(epgsql:connection(), string()) -> reference().
- squery(C, Sql) ->
- incremental(C, {squery, Sql}).
- equery(C, Sql) ->
- equery(C, Sql, []).
- -spec equery(epgsql:connection(), #statement{}, [epgsql:typed_param()]) -> reference().
- equery(C, Statement, TypedParameters) ->
- incremental(C, {equery, Statement, TypedParameters}).
- -spec prepared_query(epgsql:connection(), #statement{}, [epgsql:typed_param()]) -> reference().
- prepared_query(C, Statement, TypedParameters) ->
- incremental(C, {prepared_query, Statement, TypedParameters}).
- parse(C, Sql) ->
- parse(C, "", Sql, []).
- parse(C, Sql, Types) ->
- parse(C, "", Sql, Types).
- -spec parse(epgsql:connection(), iolist(), string(), [epgsql_type()]) -> reference().
- parse(C, Name, Sql, Types) ->
- incremental(C, {parse, Name, Sql, Types}).
- bind(C, Statement, Parameters) ->
- bind(C, Statement, "", Parameters).
- -spec bind(epgsql:connection(), #statement{}, string(), [epgsql:bind_param()]) -> reference().
- bind(C, Statement, PortalName, Parameters) ->
- incremental(C, {bind, Statement, PortalName, Parameters}).
- execute(C, S) ->
- execute(C, S, "", 0).
- execute(C, S, N) ->
- execute(C, S, "", N).
- -spec execute(epgsql:connection(), #statement{}, string(), non_neg_integer()) -> reference().
- execute(C, Statement, PortalName, MaxRows) ->
- incremental(C, {execute, Statement, PortalName, MaxRows}).
- -spec execute_batch(epgsql:connection(), [{#statement{}, [epgsql:bind_param()]}]) -> reference().
- execute_batch(C, Batch) ->
- incremental(C, {execute_batch, Batch}).
- describe(C, #statement{name = Name}) ->
- describe(C, statement, Name).
- describe(C, statement, Name) ->
- incremental(C, {describe_statement, Name});
- describe(C, portal, Name) ->
- incremental(C, {describe_portal, Name}).
- close(C, #statement{name = Name}) ->
- close(C, statement, Name).
- close(C, Type, Name) ->
- incremental(C, {close, Type, Name}).
- sync(C) ->
- incremental(C, sync).
- -spec cancel(epgsql:connection()) -> ok.
- cancel(C) ->
- epgsql_sock:cancel(C).
- %% -- internal functions --
- incremental(C, Command) ->
- Ref = make_ref(),
- gen_server:cast(C, {{incremental, self(), Ref}, Command}),
- Ref.
|