PageRenderTime 20ms CodeModel.GetById 15ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/src/dbdrivers/postgresql/README-epgsql

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