README 3.0 KB

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