Add more docs

pull/594/head
Jack Christensen 2019-09-02 12:14:04 -05:00
parent 88e31ea5be
commit afa83095a8
1 changed files with 36 additions and 2 deletions

38
doc.go
View File

@ -4,6 +4,24 @@ pgx provides lower level access to PostgreSQL than the standard database/sql. It
interface as possible while providing better speed and access to PostgreSQL specific features. Import interface as possible while providing better speed and access to PostgreSQL specific features. Import
github.com/jackc/pgx/v4/stdlib to use pgx as a database/sql compatible driver. github.com/jackc/pgx/v4/stdlib to use pgx as a database/sql compatible driver.
Establishing a Connection
The primary way of establishing a connection is with `pgx.Connect`.
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
The database connection string can be in URL or DSN format. Both PostgreSQL settings and pgx settings can be specified
here. In addition, a config struct can be created by `ParseConfig` and modified before establishing the connection with
`ConnectConfig`.
config, err := pgx.ParseConfig(os.Getenv("DATABASE_URL"))
if err != nil {
// ...
}
config.Logger = log15adapter.NewLogger(log.New("module", "pgx"))
conn, err := pgx.ConnectConfig(context.Background(), config)
Query Interface Query Interface
pgx implements Query and Scan in the familiar database/sql style. pgx implements Query and Scan in the familiar database/sql style.
@ -61,7 +79,7 @@ Use Exec to execute a query that does not return a result set.
Connection Pool Connection Pool
See sub-package pgxpool for connection pool. See sub-package pgxpool for a connection pool.
Base Type Mapping Base Type Mapping
@ -200,7 +218,18 @@ CopyFrom can be faster than an insert with as few as 5 rows.
Listen and Notify Listen and Notify
Use the underlying pgconn.PgConn for listen and notify. pgx can listen to the PostgreSQL notification system with the `Conn.WaitForNotification` method. It blocks until a
context is received or the context is canceled.
_, err := conn.Exec(context.Background(), "listen channelname")
if err != nil {
return nil
}
if notification, err := conn.WaitForNotification(context.Background()); err != nil {
// do something with notification
}
Logging Logging
@ -208,6 +237,11 @@ pgx defines a simple logger interface. Connections optionally accept a logger th
LogLevel to control logging verbosity. Adapters for github.com/inconshreveable/log15, github.com/sirupsen/logrus, LogLevel to control logging verbosity. Adapters for github.com/inconshreveable/log15, github.com/sirupsen/logrus,
go.uber.org/zap, github.com/rs/zerolog, and the testing log are provided in the log directory. go.uber.org/zap, github.com/rs/zerolog, and the testing log are provided in the log directory.
Lower Level PostgreSQL Functionality
pgx is implemented on top of github.com/jackc/pgconn a lower level PostgreSQL driver. The Conn.PgConn() method can be
used to access this lower layer.
PgBouncer PgBouncer
pgx is compatible with PgBouncer in two modes. One is when the connection has a statement cache in "describe" mode. The pgx is compatible with PgBouncer in two modes. One is when the connection has a statement cache in "describe" mode. The