epgsqli.erl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. %%% @doc Incremental interface
  2. %%%
  3. %%% All the functions return `reference()' immediately. Each data row as well
  4. %%% as metadata are delivered as separate messages in a form of
  5. %%% `{connection(), reference(), Payload}' where `Payload' depends on command
  6. %%% being executed.
  7. %%% @end
  8. %%% Copyright (C) 2011 - Anton Lebedevich. All rights reserved.
  9. -module(epgsqli).
  10. -export([start_link/0,
  11. connect/1, connect/2, connect/3, connect/4, connect/5,
  12. close/1,
  13. get_parameter/2,
  14. set_notice_receiver/2,
  15. get_cmd_status/1,
  16. squery/2,
  17. equery/2, equery/3,
  18. prepared_query/3,
  19. parse/2, parse/3, parse/4,
  20. describe/2, describe/3,
  21. bind/3, bind/4,
  22. execute/2, execute/3, execute/4,
  23. execute_batch/2, execute_batch/3,
  24. close/2, close/3,
  25. sync/1,
  26. cancel/1]).
  27. -include("epgsql.hrl").
  28. %% -- client interface --
  29. start_link() ->
  30. epgsql_sock:start_link().
  31. connect(Opts) ->
  32. {ok, C} = epgsql_sock:start_link(),
  33. call_connect(C, Opts).
  34. connect(Host, Opts) ->
  35. connect(Host, os:getenv("USER"), "", Opts).
  36. connect(Host, Username, Opts) ->
  37. connect(Host, Username, "", Opts).
  38. connect(Host, Username, Password, Opts) ->
  39. {ok, C} = epgsql_sock:start_link(),
  40. connect(C, Host, Username, Password, Opts).
  41. -spec connect(epgsql:connection(), inet:ip_address() | inet:hostname(),
  42. string(), string(), epgsql:connect_opts()) -> reference().
  43. connect(C, Host, Username, Password, Opts) ->
  44. Opts1 = maps:merge(epgsql:to_map(Opts),
  45. #{host => Host,
  46. username => Username,
  47. password => Password}),
  48. call_connect(C, Opts1).
  49. call_connect(C, Opts) ->
  50. Opts1 = epgsql_cmd_connect:opts_hide_password(epgsql:to_map(Opts)),
  51. epgsqla:complete_connect(
  52. C, incremental(C, epgsql_cmd_connect, Opts1), Opts1).
  53. -spec close(epgsql:connection()) -> ok.
  54. close(C) ->
  55. epgsql_sock:close(C).
  56. -spec get_parameter(epgsql:connection(), binary()) -> binary() | undefined.
  57. get_parameter(C, Name) ->
  58. epgsql_sock:get_parameter(C, Name).
  59. -spec set_notice_receiver(epgsql:connection(), undefined | pid() | atom()) ->
  60. {ok, Previous :: pid() | atom()}.
  61. set_notice_receiver(C, PidOrName) ->
  62. epgsql_sock:set_notice_receiver(C, PidOrName).
  63. -spec get_cmd_status(epgsql:connection()) -> {ok, Status} when
  64. Status :: undefined | atom() | {atom(), integer()}.
  65. get_cmd_status(C) ->
  66. epgsql_sock:get_cmd_status(C).
  67. -spec squery(epgsql:connection(), epgsql:sql_query()) -> reference().
  68. squery(C, Sql) ->
  69. incremental(C, epgsql_cmd_squery, Sql).
  70. -spec equery(epgsql:connection(), epgsql:statement()) -> reference().
  71. equery(C, Statement) ->
  72. equery(C, Statement, []).
  73. -spec equery(epgsql:connection(), epgsql:statement(), [epgsql:typed_param()]) -> reference().
  74. equery(C, Statement, TypedParameters) ->
  75. incremental(C, epgsql_cmd_equery, {Statement, TypedParameters}).
  76. -spec prepared_query(epgsql:connection(), epgsql:statement(), [epgsql:typed_param()]) -> reference().
  77. prepared_query(C, Statement, TypedParameters) ->
  78. incremental(C, epgsql_cmd_prepared_query, {Statement, TypedParameters}).
  79. -spec parse(epgsql:connection(), epgsql:sql_query()) -> reference().
  80. parse(C, Sql) ->
  81. parse(C, "", Sql, []).
  82. -spec parse(epgsql:connection(), epgsql:sql_query(), [epgsql:epgsql_type()]) -> reference().
  83. parse(C, Sql, Types) ->
  84. parse(C, "", Sql, Types).
  85. -spec parse(epgsql:connection(), iolist(), epgsql:sql_query(), [epgsql:epgsql_type()]) -> reference().
  86. parse(C, Name, Sql, Types) ->
  87. incremental(C, epgsql_cmd_parse, {Name, Sql, Types}).
  88. bind(C, Statement, Parameters) ->
  89. bind(C, Statement, "", Parameters).
  90. -spec bind(epgsql:connection(), epgsql:statement(), string(), [epgsql:bind_param()]) -> reference().
  91. bind(C, Statement, PortalName, Parameters) ->
  92. incremental(C, epgsql_cmd_bind, {Statement, PortalName, Parameters}).
  93. execute(C, S) ->
  94. execute(C, S, "", 0).
  95. execute(C, S, N) ->
  96. execute(C, S, "", N).
  97. -spec execute(epgsql:connection(), epgsql:statement(), string(), non_neg_integer()) -> reference().
  98. execute(C, Statement, PortalName, MaxRows) ->
  99. incremental(C, epgsql_cmd_execute, {Statement, PortalName, MaxRows}).
  100. -spec execute_batch(epgsql:connection(), [{epgsql:statement(), [epgsql:bind_param()]}]) -> reference().
  101. execute_batch(C, Batch) ->
  102. incremental(C, epgsql_cmd_batch, Batch).
  103. -spec execute_batch(epgsql:connection(), epgsql:statement(), [ [epgsql:bind_param()] ]) -> reference().
  104. execute_batch(C, #statement{} = Statement, Batch) ->
  105. incremental(C, epgsql_cmd_batch, {Statement, Batch}).
  106. describe(C, #statement{name = Name}) ->
  107. describe(C, statement, Name).
  108. describe(C, statement, Name) ->
  109. incremental(C, epgsql_cmd_describe_statement, Name);
  110. describe(C, portal, Name) ->
  111. incremental(C, epgsql_cmd_describe_portal, Name).
  112. close(C, #statement{name = Name}) ->
  113. close(C, statement, Name).
  114. close(C, Type, Name) ->
  115. incremental(C, epgsql_cmd_close, {Type, Name}).
  116. sync(C) ->
  117. incremental(C, epgsql_cmd_sync, []).
  118. -spec cancel(epgsql:connection()) -> ok.
  119. cancel(C) ->
  120. epgsql_sock:cancel(C).
  121. %% -- internal functions --
  122. incremental(C, Command, Args) ->
  123. epgsql_sock:async_command(C, incremental, Command, Args).