epgsqla.erl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. %%% Copyright (C) 2011 - Anton Lebedevich. All rights reserved.
  2. -module(epgsqla).
  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. prepared_query/3,
  10. parse/2, parse/3, parse/4,
  11. describe/2, describe/3,
  12. bind/3, bind/4,
  13. execute/2, execute/3, execute/4,
  14. execute_batch/2,
  15. close/2, close/3,
  16. sync/1,
  17. cancel/1,
  18. complete_connect/2]).
  19. -include("epgsql.hrl").
  20. %% -- client interface --
  21. -spec start_link() -> {ok, pid()}.
  22. start_link() ->
  23. epgsql_sock:start_link().
  24. connect(Host, Opts) ->
  25. connect(Host, os:getenv("USER"), "", Opts).
  26. connect(Host, Username, Opts) ->
  27. connect(Host, Username, "", Opts).
  28. connect(Host, Username, Password, Opts) ->
  29. {ok, C} = epgsql_sock:start_link(),
  30. connect(C, Host, Username, Password, Opts).
  31. -spec connect(epgsql:connection(), inet:ip_address() | inet:hostname(),
  32. string(), string(), [epgsql:connect_option()]) -> reference().
  33. connect(C, Host, Username, Password, Opts) ->
  34. complete_connect(C, cast(C, {connect, Host, Username, Password, Opts})).
  35. -spec close(epgsql:connection()) -> ok.
  36. close(C) ->
  37. epgsql_sock:close(C).
  38. -spec get_parameter(epgsql:connection(), binary()) -> binary() | undefined.
  39. get_parameter(C, Name) ->
  40. epgsql_sock:get_parameter(C, Name).
  41. -spec squery(epgsql:connection(), string()) -> reference().
  42. squery(C, Sql) ->
  43. cast(C, {squery, Sql}).
  44. equery(C, Sql) ->
  45. equery(C, Sql, []).
  46. -spec equery(epgsql:connection(), #statement{}, [epgsql:typed_param()]) -> reference().
  47. equery(C, Statement, TypedParameters) ->
  48. cast(C, {equery, Statement, TypedParameters}).
  49. -spec prepared_query(epgsql:connection(), #statement{}, [epgsql:typed_param()]) -> reference().
  50. prepared_query(C, Statement, TypedParameters) ->
  51. cast(C, {prepared_query, Statement, TypedParameters}).
  52. parse(C, Sql) ->
  53. parse(C, "", Sql, []).
  54. parse(C, Sql, Types) ->
  55. parse(C, "", Sql, Types).
  56. -spec parse(epgsql:connection(), iolist(), string(), [epgsql_type()]) -> reference().
  57. parse(C, Name, Sql, Types) ->
  58. cast(C, {parse, Name, Sql, Types}).
  59. bind(C, Statement, Parameters) ->
  60. bind(C, Statement, "", Parameters).
  61. -spec bind(epgsql:connection(), #statement{}, string(), [epgsql:bind_param()]) -> reference().
  62. bind(C, Statement, PortalName, Parameters) ->
  63. cast(C, {bind, Statement, PortalName, Parameters}).
  64. execute(C, S) ->
  65. execute(C, S, "", 0).
  66. execute(C, S, N) ->
  67. execute(C, S, "", N).
  68. -spec execute(epgsql:connection(), #statement{}, string(), non_neg_integer()) -> reference().
  69. execute(C, Statement, PortalName, MaxRows) ->
  70. cast(C, {execute, Statement, PortalName, MaxRows}).
  71. -spec execute_batch(epgsql:connection(), [{#statement{}, [epgsql:bind_param()]}]) -> reference().
  72. execute_batch(C, Batch) ->
  73. cast(C, {execute_batch, Batch}).
  74. describe(C, #statement{name = Name}) ->
  75. describe(C, statement, Name).
  76. describe(C, statement, Name) ->
  77. cast(C, {describe_statement, Name});
  78. describe(C, portal, Name) ->
  79. cast(C, {describe_portal, Name}).
  80. close(C, #statement{name = Name}) ->
  81. close(C, statement, Name).
  82. close(C, Type, Name) ->
  83. cast(C, {close, Type, Name}).
  84. sync(C) ->
  85. cast(C, sync).
  86. -spec cancel(epgsql:connection()) -> ok.
  87. cancel(C) ->
  88. epgsql_sock:cancel(C).
  89. %% -- internal functions --
  90. cast(C, Command) ->
  91. Ref = make_ref(),
  92. gen_server:cast(C, {{cast, self(), Ref}, Command}),
  93. Ref.
  94. complete_connect(C, Ref) ->
  95. receive
  96. %% If we connect, then try and update the type cache. When
  97. %% all is said and done, pass the result along as a message.
  98. {C, Ref, Msg} ->
  99. Retval =
  100. case Msg of
  101. connected ->
  102. ok = epgsql:update_type_cache(C),
  103. {C, Ref, connected};
  104. {error, Error} ->
  105. {C, Ref, {error, Error}}
  106. end,
  107. self() ! Retval;
  108. {'EXIT', C, Reason} ->
  109. self() ! {'EXIT', C, Reason}
  110. end,
  111. Ref.