epgsqli.erl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. %%% Copyright (C) 2011 - Anton Lebedevich. All rights reserved.
  2. -module(epgsqli).
  3. -export([start_link/0,
  4. connect/2, connect/3, connect/4, connect/5,
  5. close/1,
  6. get_parameter/2,
  7. squery/2,
  8. equery/2, equery/3,
  9. parse/2, parse/3, parse/4,
  10. describe/2, describe/3,
  11. bind/3, bind/4,
  12. execute/2, execute/3, execute/4,
  13. execute_batch/2,
  14. close/2, close/3,
  15. sync/1,
  16. cancel/1]).
  17. -include("epgsql.hrl").
  18. %% -- client interface --
  19. start_link() ->
  20. epgsql_sock:start_link().
  21. connect(Host, Opts) ->
  22. connect(Host, os:getenv("USER"), "", Opts).
  23. connect(Host, Username, Opts) ->
  24. connect(Host, Username, "", Opts).
  25. connect(Host, Username, Password, Opts) ->
  26. {ok, C} = epgsql_sock:start_link(),
  27. connect(C, Host, Username, Password, Opts).
  28. -spec connect(epgsql:connection(), inet:ip_address() | inet:hostname(),
  29. string(), string(), [epgsql:connect_option()]) -> reference().
  30. connect(C, Host, Username, Password, Opts) ->
  31. epgsqla:complete_connect(C, incremental(C, {connect, Host, Username, Password, Opts})).
  32. -spec close(epgsql:connection()) -> ok.
  33. close(C) ->
  34. epgsql_sock:close(C).
  35. -spec get_parameter(epgsql:connection(), binary()) -> binary() | undefined.
  36. get_parameter(C, Name) ->
  37. epgsql_sock:get_parameter(C, Name).
  38. -spec squery(epgsql:connection(), string()) -> reference().
  39. squery(C, Sql) ->
  40. incremental(C, {squery, Sql}).
  41. equery(C, Sql) ->
  42. equery(C, Sql, []).
  43. -spec equery(epgsql:connection(), #statement{}, [epgsql:bind_param()]) -> reference().
  44. equery(C, Statement, Parameters) ->
  45. incremental(C, {equery, Statement, Parameters}).
  46. parse(C, Sql) ->
  47. parse(C, "", Sql, []).
  48. parse(C, Sql, Types) ->
  49. parse(C, "", Sql, Types).
  50. -spec parse(epgsql:connection(), iolist(), string(), [epgsql_type()]) -> reference().
  51. parse(C, Name, Sql, Types) ->
  52. incremental(C, {parse, Name, Sql, Types}).
  53. bind(C, Statement, Parameters) ->
  54. bind(C, Statement, "", Parameters).
  55. -spec bind(epgsql:connection(), #statement{}, string(), [epgsql:bind_param()]) -> reference().
  56. bind(C, Statement, PortalName, Parameters) ->
  57. incremental(C, {bind, Statement, PortalName, Parameters}).
  58. execute(C, S) ->
  59. execute(C, S, "", 0).
  60. execute(C, S, N) ->
  61. execute(C, S, "", N).
  62. -spec execute(epgsql:connection(), #statement{}, string(), non_neg_integer()) -> reference().
  63. execute(C, Statement, PortalName, MaxRows) ->
  64. incremental(C, {execute, Statement, PortalName, MaxRows}).
  65. -spec execute_batch(epgsql:connection(), [{#statement{}, [epgsql:bind_param()]}]) -> reference().
  66. execute_batch(C, Batch) ->
  67. incremental(C, {execute_batch, Batch}).
  68. describe(C, #statement{name = Name}) ->
  69. describe(C, statement, Name).
  70. describe(C, statement, Name) ->
  71. incremental(C, {describe_statement, Name});
  72. describe(C, portal, Name) ->
  73. incremental(C, {describe_portal, Name}).
  74. close(C, #statement{name = Name}) ->
  75. close(C, statement, Name).
  76. close(C, Type, Name) ->
  77. incremental(C, {close, Type, Name}).
  78. sync(C) ->
  79. incremental(C, sync).
  80. -spec cancel(epgsql:connection()) -> ok.
  81. cancel(C) ->
  82. epgsql_sock:cancel(C).
  83. %% -- internal functions --
  84. incremental(C, Command) ->
  85. Ref = make_ref(),
  86. gen_server:cast(C, {{incremental, self(), Ref}, Command}),
  87. Ref.