overview.edoc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. @title epgsql - PostgreSQL driver for Erlang
  2. @doc
  3. == Interfaces ==
  4. Epgsql has 3 API interfaces:
  5. <ul>
  6. <li>{@link epgsql} - synchronous</li>
  7. <li>{@link epgsqla} - asynchronous</li>
  8. <li>{@link epgsqli} - incremental</li>
  9. </ul>
  10. They have the same set of functions, but differ by the way they deliver results.
  11. `epgsql' returns results as a function's return value;
  12. `epgsqla' delivers whole result as a single message;
  13. `epgsqli' delivers results and metadata in individual messages row-by-row.
  14. It's usualy not a problem to use different interfaces with the same connection.
  15. == Example session ==
  16. ```
  17. {ok, C} = epgsql:connect(#{host => "localhost",
  18. username => "test-user", password => "test",
  19. database => "test-db"}),
  20. {ok, _Columns, Rows} = epgsql:equery(C, "SELECT * FROM users WHERE id=$1", [42]),
  21. io:format("Users: ~p~n", [Rows]),
  22. Ref = epgsqla:squery(C, "SELECT count(*) FROM users"),
  23. receive
  24. {C, Ref, {ok, [#column{}], [{Count}]}} ->
  25. io:format("count: ~p~n", [binary_to_integer(Count)]);
  26. {C, Ref, #error{} = E} ->
  27. io:format("error: ~p~n", [E])
  28. after 10000 ->
  29. io:format("timeout"),
  30. ok = epgsql:cancel(C),
  31. % we still can receive normal result because `cancel' is always asynchronous
  32. % otherwise we will receive
  33. % `#error{code = <<"57014">>, codename = query_canceled}'
  34. receive {C, Ref, OkOrCancel} ->
  35. io:format("cancelation result: ~p~n", [OkOrCancel])
  36. end
  37. end,
  38. ok =
  39. epgsql:with_transaction(
  40. C,
  41. fun() ->
  42. [{ok, _}, {ok, _}] =
  43. epgsql:execute_batch(
  44. C, "INSERT INTO users (name, age) VALUES ($1, $2)",
  45. [
  46. [<<"Joe">>, 35],
  47. [<<"Mary">>, 24]
  48. ]),
  49. ok
  50. end, #{}),
  51. ok = epgsql:close(C).
  52. '''
  53. == Commands ==
  54. Client can execute a number of built-in commands as well as define
  55. their own. See {@link epgsql_command}.
  56. == Datatypes ==
  57. Epgsql supports both text and binary data encodings. There are a bunch
  58. of built-in codecs and it's possible to implement custom ones
  59. as well as fine-tune some of built-ins. See {@link epgsql_codec}.