/erts/example/pg_sync.erl

https://github.com/dustin/otp · Erlang · 46 lines · 22 code · 6 blank · 18 comment · 0 complexity · 3d1c1e525591656d6634367bb677a67b MD5 · raw file

  1. %%
  2. %% %CopyrightBegin%
  3. %%
  4. %% Copyright Ericsson AB 2006-2009. All Rights Reserved.
  5. %%
  6. %% The contents of this file are subject to the Erlang Public License,
  7. %% Version 1.1, (the "License"); you may not use this file except in
  8. %% compliance with the License. You should have received a copy of the
  9. %% Erlang Public License along with this software. If not, it can be
  10. %% retrieved online at http://www.erlang.org/.
  11. %%
  12. %% Software distributed under the License is distributed on an "AS IS"
  13. %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  14. %% the License for the specific language governing rights and limitations
  15. %% under the License.
  16. %%
  17. %% %CopyrightEnd%
  18. %%
  19. -module(pg_sync).
  20. -define(DRV_CONNECT, $C).
  21. -define(DRV_DISCONNECT, $D).
  22. -define(DRV_SELECT, $S).
  23. -export([connect/1, disconnect/1, select/2]).
  24. connect(ConnectStr) ->
  25. case erl_ddll:load_driver(".", "pg_sync") of
  26. ok -> ok;
  27. {error, already_loaded} -> ok;
  28. E -> exit(E)
  29. end,
  30. Port = open_port({spawn, ?MODULE}, []),
  31. case binary_to_term(port_control(Port, ?DRV_CONNECT, ConnectStr)) of
  32. ok -> {ok, Port};
  33. Error -> Error
  34. end.
  35. disconnect(Port) ->
  36. R = binary_to_term(port_control(Port, ?DRV_DISCONNECT, "")),
  37. port_close(Port),
  38. R.
  39. select(Port, Query) ->
  40. binary_to_term(port_control(Port, ?DRV_SELECT, Query)).