README 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. Erlang PostgreSQL Database Client
  2. Asynchronous fork of https://github.com/wg/epgsql
  3. Asynchronous API is implemented by pgqsl_sock.erl, see pgsql.erl for examples.
  4. It passes all tests from original driver except 3 timeout tests.
  5. Backward compatibility is preserved by module pgsql.
  6. Differences from original driver:
  7. + internal queue of client requests, so you don't need to wait response to send next request
  8. + responses are delivered as regular erlang messages
  9. + single process to hold driver state and receive socket data
  10. + slight code reorganization
  11. Original epgsql API
  12. * Connect
  13. {ok, C} = pgsql:connect(Host, [Username], [Password], Opts).
  14. Host - host to connect to.
  15. Username - username to connect as, defaults to $USER.
  16. Password - optional password to authenticate with.
  17. Opts - property list of extra options. Supported properties:
  18. + {database, String}
  19. + {port, Integer}
  20. + {ssl, Atom} true | false | required
  21. + {ssl_opts, List} see ssl application docs in OTP
  22. + {timeout, Integer} milliseconds, defaults to 5000
  23. + {async, Pid} see Asynchronous Messages section
  24. {ok, C} = pgsql:connect("localhost", "username", [{database, "test_db"}]).
  25. ok = pgsql:close(C).
  26. The timeout parameter will trigger an {error, timeout} result when the
  27. server fails to respond within Timeout milliseconds.
  28. * Simple Query
  29. {ok, Columns, Rows} = pgsql:squery(C, "select ...").
  30. {ok, Count} = pgsql:squery(C, "update ...").
  31. {ok, Count, Columns, Rows} = pgsql:squery(C, "insert ... returning ...").
  32. {error, Error} = pgsql:squery(C, "invalid SQL").
  33. Columns - list of column records, see pgsql.hrl for definition.
  34. Rows - list of tuples, one for each row.
  35. Count - integer count of rows inserted/updated/etc
  36. The simple query protocol returns all columns as text (Erlang binaries)
  37. and does not support binding parameters.
  38. * Extended Query
  39. {ok, Columns, Rows} = pgsql:equery(C, "select ...", [Parameters]).
  40. {ok, Count} = pgsql:equery(C, "update ...", [Parameters]).
  41. {ok, Count, Columns, Rows} = pgsql:equery(C, "insert ... returning ...", [Parameters]).
  42. {error, Error} = pgsql:equery(C, "invalid SQL", [Parameters]).
  43. Parameters - optional list of values to be bound to $1, $2, $3, etc.
  44. The extended query protocol combines parse, bind, and execute using
  45. the unnamed prepared statement and portal. A "select" statement returns
  46. {ok, Columns, Rows}, "insert/update/delete" returns {ok, Count} or
  47. {ok, Count, Columns, Rows} when a "returning" clause is present. When
  48. an error occurs, all statements result in {error, #error{}}.
  49. PostgreSQL's binary format is used to return integers as Erlang
  50. integers, floats as floats, bytea/text/varchar columns as binaries,
  51. bools as true/false, etc. For details see pgsql_binary.erl and the
  52. Data Representation section below.
  53. * Parse/Bind/Execute
  54. {ok, Statement} = pgsql:parse(C, [StatementName], Sql, [ParameterTypes]).
  55. StatementName - optional, reusable, name for the prepared statement.
  56. ParameterTypes - optional list of PostgreSQL types for each parameter.
  57. For valid type names see pgsql_types.erl.
  58. ok = pgsql:bind(C, Statement, [PortalName], ParameterValues).
  59. PortalName - optional name for the result portal.
  60. {ok | partial, Rows} = pgsql:execute(C, Statement, [PortalName], [MaxRows]).
  61. {ok, Count} = pgsql:execute(C, Statement, [PortalName]).
  62. {ok, Count, Rows} = pgsql:execute(C, Statement, [PortalName]).
  63. PortalName - optional portal name used in bind/4.
  64. MaxRows - maximum number of rows to return (0 for all rows).
  65. execute returns {partial, Rows} when more rows are available.
  66. ok = pgsql:close(C, Statement).
  67. ok = pgsql:close(C, statement | portal, Name).
  68. ok = pgsql:sync(C).
  69. All functions return {error, Error} when an error occurs.
  70. * Data Representation
  71. null = null
  72. bool = true | false
  73. char = $A | binary
  74. intX = 1
  75. floatX = 1.0
  76. date = {Year, Month, Day}
  77. time = {Hour, Minute, Second.Microsecond}
  78. timetz = {time, Timezone}
  79. timestamp = {date, time}
  80. timestamptz = {date, time}
  81. interval = {time, Days, Months}
  82. text = <<"a">>
  83. varchar = <<"a">>
  84. bytea = <<1, 2>>
  85. array = [1, 2, 3]
  86. record = {int2, time, text, ...} (decode only)
  87. * Errors
  88. Errors originating from the PostgreSQL backend are returned as {error, #error{}},
  89. see pgsql.hrl for the record definition. epgsql functions may also return
  90. {error, What} where What is one of the following:
  91. {unsupported_auth_method, Method} - required auth method is unsupported
  92. timeout - request timed out
  93. closed - connection was closed
  94. sync_required - error occured and pgsql:sync must be called
  95. * Asynchronous Messages
  96. PostgreSQL may deliver two types of asynchronous message: "notices" in response
  97. to notice and warning messages generated by the server, and "notifications" which
  98. are generated by the LISTEN/NOTIFY mechanism.
  99. Passing the {async, Pid} option to pgsql:connect will result in these async
  100. messages being sent to the specified process, otherwise they will be dropped.
  101. Message formats:
  102. {pgsql, Connection, {notification, Channel, Pid, Payload}}
  103. Connection - connection the notification occured on
  104. Channel - channel the notification occured on
  105. Pid - database session pid that sent notification
  106. Payload - optional payload, only available from PostgreSQL >= 9.0
  107. {pgsql, Connection, {notice, Error}}
  108. Connection - connection the notice occured on
  109. Error - an #error{} record, see pgsql.hrl