README 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. Erlang PostgreSQL Database Client
  2. * Connect
  3. {ok, C} = pgsql:connect(Host, [Username], [Password], Opts).
  4. Host - host to connect to.
  5. Username - username to connect as, defaults to $USER.
  6. Password - optional password to authenticate with.
  7. Opts - property list of extra options. Supported properties:
  8. + database
  9. + port
  10. + ssl (true | false | required)
  11. + ssl_opts (see ssl docs in OTP)
  12. + timeout (milliseconds, defaults to 5000)
  13. {ok, C} = pgsql:connect("localhost", "username", [{database, "test_db"}]).
  14. ok = pgsql:close(C).
  15. The timeout parameter is applied to all operations. In the case of equery
  16. this means that total execution time may exceed the timeout value.
  17. * Simple Query
  18. {ok, Columns, Rows} = pgsql:squery(C, "select ...").
  19. {ok, Count} = pgsql:squery(C, "update ...").
  20. {ok, Count, Columns, Rows} = pgsql:squery(C, "insert ... returning ...").
  21. {error, Error} = pgsql:squery(C, "invalid SQL").
  22. Columns - list of column records, see pgsql.hrl for definition.
  23. Rows - list of tuples, one for each row.
  24. Count - integer count of rows inserted/updated/etc
  25. The simple query protocol returns all columns as text (Erlang binaries)
  26. and does not support binding parameters.
  27. * Extended Query
  28. {ok, Columns, Rows} = pgsql:equery(C, "select ...", [Parameters]).
  29. {ok, Count} = pgsql:equery(C, "update ...", [Parameters]).
  30. {ok, Count, Columns, Rows} = pgsql:equery(C, "insert ... returning ...", [Parameters]).
  31. {error, Error} = pgsql:equery(C, "invalid SQL", [Parameters]).
  32. Parameters - optional list of values to be bound to $1, $2, $3, etc.
  33. The extended query protocol combines parse, bind, and execute using
  34. the unnamed prepared statement and portal. A "select" statement returns
  35. {ok, Columns, Rows}, "insert/update/delete" returns {ok, Count} or
  36. {ok, Count, Columns, Rows} when a "returning" clause is present. When
  37. an error occurs, all statements result in {error, #error{}}.
  38. PostgreSQL's binary format is used to return integers as Erlang
  39. integers, floats as floats, bytea/text/varchar columns as binaries,
  40. bools as true/false, etc. For details see pgsql_binary.erl and the
  41. Data Representation section below.
  42. * Parse/Bind/Execute
  43. {ok, Statement} = pgsql:parse(C, [StatementName], Sql, [ParameterTypes]).
  44. StatementName - optional, reusable, name for the prepared statement.
  45. ParameterTypes - optional list of PostgreSQL types for each parameter.
  46. For valid type names see pgsql_types.erl.
  47. ok = pgsql:bind(C, Statement, [PortalName], ParameterValues).
  48. PortalName - optional name for the result portal.
  49. {ok | partial, Rows} = pgsql:execute(C, Statement, [PortalName], [MaxRows]).
  50. {ok, Count} = pgsql:execute(C, Statement, [PortalName]).
  51. {ok, Count, Rows} = pgsql:execute(C, Statement, [PortalName]).
  52. PortalName - optional portal name used in bind/4.
  53. MaxRows - maximum number of rows to return (0 for all rows).
  54. execute returns {partial, Rows} when more rows are available.
  55. ok = pgsql:close(C, Statement).
  56. ok = pgsql:close(C, statement | portal, Name).
  57. ok = pgsql:sync(C).
  58. All functions return {error, Error} when an error occurs.
  59. * Data Representation
  60. null = null
  61. bool = true | false
  62. char = $A | binary
  63. intX = 1
  64. floatX = 1.0
  65. date = {Year, Month, Day}
  66. time = {Hour, Minute, Second.Microsecond}
  67. timetz = {time, Timezone}
  68. timestamp = {date, time}
  69. timestamptz = {date, time}
  70. interval = {time, Days, Months}
  71. text = <<"a">>
  72. varchar = <<"a">>
  73. bytea = <<1, 2>>
  74. record = {int2, time, text, ...} (decode only)
  75. * Errors
  76. Errors originating from the PostgreSQL backend are returned as {error, #error{}},
  77. see pgsql.hrl for the record definition. epgsql may also return {error, Atom}
  78. where Atom is 'timeout' or 'closed'.