README 2.2 KB

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