epgsqli.erl 5.0 KB

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