README 3.0 KB

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