README 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. {ok, C} = pgsql:connect("localhost", "username", [{database, "test_db"}]).
  13. ok = pgsql:close(C).
  14. * Simple Query
  15. {ok, Columns, Rows} = pgsql:squery(C, Sql).
  16. {error, #error{}} = pgsql:squery(C, InvalidSql).
  17. Columns - list of column records, see pgsql.hrl for definition.
  18. Rows - list of tuples, one for each row.
  19. The simple query protocol returns all columns as text (Erlang binaries)
  20. and does not support binding parameters.
  21. * Extended Query
  22. {ok, Columns, Rows} = pgsql:equery(C, "select ...", [Parameters]).
  23. {ok, Count} = pgsql:equery(C, "update ...", [Parameters]).
  24. {ok, Count, Columns, Rows} = pgsql:equery(C, "insert ... returning ...", [Parameters]).
  25. {error, #error{}} = pgsql:equery(C, "invalid SQL", [Parameters]).
  26. Parameters - optional list of values to be bound to $1, $2, $3, etc.
  27. The extended query protocol combines parse, bind, and execute using
  28. the unnamed prepared statement and portal. A "select" statement returns
  29. {ok, Columns, Rows}, "insert/update/delete" returns {ok, Count} or
  30. {ok, Count, Columns, Rows} when a "returning" clause is present. When
  31. an error occurs, all statements result in {error, #error{}}.
  32. PostgreSQL's binary format is used to return integers as Erlang
  33. integers, floats as floats, bytea/text/varchar columns as binaries,
  34. bools as true/false, etc. For details see pgsql_binary.erl and the
  35. Data Representation section below.
  36. * Parse/Bind/Execute
  37. {ok, Statement} = pgsql:parse(C, [StatementName], Sql, [ParameterTypes]).
  38. StatementName - optional, reusable, name for the prepared statement.
  39. ParameterTypes - optional list of PostgreSQL types for each parameter.
  40. For valid type names see pgsql_types.erl.
  41. ok = pgsql:bind(C, Statement, [PortalName], ParameterValues).
  42. PortalName - optional name for the result portal.
  43. {ok | partial, Rows} = pgsql:execute(C, Statement, [PortalName], [MaxRows]).
  44. {ok, Count} = pgsql:execute(C, Statement, [PortalName]).
  45. {ok, Count, Rows} = pgsql:execute(C, Statement, [PortalName]).
  46. PortalName - optional portal name used in bind/4.
  47. MaxRows - maximum number of rows to return (0 for all rows).
  48. execute returns {partial, Rows} when more rows are available.
  49. ok = pgsql:close(C, Statement).
  50. ok = pgsql:close(C, statement | portal, Name).
  51. ok = pgsql:sync(C).
  52. All functions return {error, #error{}} when an error occurs.
  53. * Data Representation
  54. null = null
  55. bool = true | false
  56. char = $A
  57. intX = 1
  58. floatX = 1.0
  59. date = {Year, Month, Day}
  60. time = {Hour, Minute, Second.Microsecond}
  61. timetz = {time, Timezone}
  62. timestamp = {date, time}
  63. timestamptz = {date, time}
  64. interval = {time, Days, Months}
  65. text = <<"a">>
  66. varchar = <<"a">>
  67. bytea = <<1, 2>>
  68. record = {int2, time, text, ...} (decode only)