From cba72d47c5f74ca539a66f2a37affb8878579d23 Mon Sep 17 00:00:00 2001 From: Manni Wood Date: Sat, 10 Sep 2016 19:44:33 -0400 Subject: [PATCH 01/10] Fixes typo --- values.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/values.go b/values.go index 80bd3ed8..dcc378d1 100644 --- a/values.go +++ b/values.go @@ -340,7 +340,7 @@ func (n NullInt32) Encode(w *WriteBuf, oid Oid) error { // // it is the data type of the xmin and xmax hidden system columns. // -// It is currently implemented as an unsigned for byte integer. +// It is currently implemented as an unsigned four byte integer. // Its definition can be found in src/include/postgres_ext.h as TransactionId // in the PostgreSQL sources. type Xid uint32 From f49b92d5a89033429c07a3fcfe74fd76426c2a13 Mon Sep 17 00:00:00 2001 From: Manni Wood Date: Sat, 10 Sep 2016 19:47:51 -0400 Subject: [PATCH 02/10] Fixes typo --- values.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/values.go b/values.go index de5bcfd5..1bf224d5 100644 --- a/values.go +++ b/values.go @@ -342,7 +342,7 @@ func (n NullInt32) Encode(w *WriteBuf, oid Oid) error { // // it is the data type of the xmin and xmax hidden system columns. // -// It is currently implemented as an unsigned for byte integer. +// It is currently implemented as an unsigned four byte integer. // Its definition can be found in src/include/postgres_ext.h as TransactionId // in the PostgreSQL sources. type Xid uint32 @@ -394,7 +394,7 @@ func (n NullXid) Encode(w *WriteBuf, oid Oid) error { // // it is the data type of the cmin and cmax hidden system columns. // -// It is currently implemented as an unsigned for byte integer. +// It is currently implemented as an unsigned four byte integer. // Its definition can be found in src/include/c.h as CommandId // in the PostgreSQL sources. type Cid uint32 From cd21fd9035b2dfddebbceb4cd30e47cfd2313bba Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Mon, 19 Sep 2016 07:47:16 -0500 Subject: [PATCH 03/10] Fix missing documentation fixes #177 --- values.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/values.go b/values.go index 1bf224d5..4d542bf1 100644 --- a/values.go +++ b/values.go @@ -66,7 +66,8 @@ const minInt = -maxInt - 1 // or binary). In theory the Scanner interface should be the one to determine // the format of the returned values. However, the query has already been // executed by the time Scan is called so it has no chance to set the format. -// So for types that should be returned in binary th +// So for types that should always be returned in binary the format should be +// set here. var DefaultTypeFormats map[string]int16 func init() { From f5b269d56a0e0da79e9affe11a960c2380393fa5 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Thu, 22 Sep 2016 07:37:18 -0500 Subject: [PATCH 04/10] Update Go versions on Travis --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 314e26e8..b63c864c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: go go: - - 1.6.2 - - 1.5.2 + - 1.7.1 + - 1.6.3 - tip # Derived from https://github.com/lib/pq/blob/master/.travis.yml From 79acbeac0efc90dda7356cf5d3f586de8f7b223e Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Thu, 22 Sep 2016 07:47:28 -0500 Subject: [PATCH 05/10] Tweak test for better travis compat. --- conn_pool_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/conn_pool_test.go b/conn_pool_test.go index e3ae0036..2163f515 100644 --- a/conn_pool_test.go +++ b/conn_pool_test.go @@ -366,12 +366,12 @@ func TestPoolAcquireAndReleaseCycleAutoConnect(t *testing.T) { if err != nil { t.Fatalf("Unable to Acquire: %v", err) } - rows, _ := c.Query("select 1") + rows, _ := c.Query("select 1, pg_sleep(0.02)") rows.Close() pool.Release(c) } - for i := 0; i < 1000; i++ { + for i := 0; i < 10; i++ { doSomething() } @@ -381,7 +381,7 @@ func TestPoolAcquireAndReleaseCycleAutoConnect(t *testing.T) { } var wg sync.WaitGroup - for i := 0; i < 1000; i++ { + for i := 0; i < 10; i++ { wg.Add(1) go func() { defer wg.Done() From 2ccec0026e6b7d34625c59d1f0d1d43befe4d58d Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Thu, 22 Sep 2016 07:51:47 -0500 Subject: [PATCH 06/10] go get test dependencies on travis --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index b63c864c..ca2306a1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,6 +37,9 @@ before_script: - psql -U postgres -c "create user pgx_md5 SUPERUSER PASSWORD 'secret'" - psql -U postgres -c "create user pgx_pw SUPERUSER PASSWORD 'secret'" +install: + - go get -u github.com/shopspring/decimal + script: - go test -v -race -short ./... From dd7d777682e135e19355c45afdfa4a609759dc06 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Thu, 22 Sep 2016 08:32:54 -0500 Subject: [PATCH 07/10] Fetch another dependency for travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index ca2306a1..5bf5a167 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,6 +39,7 @@ before_script: install: - go get -u github.com/shopspring/decimal + - go get -u gopkg.in/inconshreveable/log15.v2 script: - go test -v -race -short ./... From b1a77cfa3116f319ef00fb785e0776a1fa0d4fb4 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Thu, 22 Sep 2016 08:38:16 -0500 Subject: [PATCH 08/10] And another dependency for travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 5bf5a167..537dca8d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -40,6 +40,7 @@ before_script: install: - go get -u github.com/shopspring/decimal - go get -u gopkg.in/inconshreveable/log15.v2 + - go get -u github.com/jackc/fake script: - go test -v -race -short ./... From bcfb1f4d7cbc45ee96a42ec1efbb4d1d8cc5e0d3 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Thu, 22 Sep 2016 08:56:46 -0500 Subject: [PATCH 09/10] Use pointer for decimal.Decimal Fix breakage caused by 54efccb61ffe0a31b6a1908bdc8a35f491da01ea in https://github.com/shopspring/decimal --- query_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/query_test.go b/query_test.go index 06a18ffe..457bc1fb 100644 --- a/query_test.go +++ b/query_test.go @@ -1282,7 +1282,7 @@ func TestConnQueryDatabaseSQLDriverValuer(t *testing.T) { } var num decimal.Decimal - err = conn.QueryRow("select $1::decimal", expected).Scan(&num) + err = conn.QueryRow("select $1::decimal", &expected).Scan(&num) if err != nil { t.Fatalf("Scan failed: %v", err) } From 383c12177e7736901470195a5da96ec52e6c5002 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 24 Sep 2016 12:49:51 -0500 Subject: [PATCH 10/10] Add mapping information for core types. refs #183 --- doc.go | 134 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 84 insertions(+), 50 deletions(-) diff --git a/doc.go b/doc.go index c202f861..980c5a74 100644 --- a/doc.go +++ b/doc.go @@ -81,63 +81,39 @@ releasing connections when you do not need that level of control. return err } -Transactions +Base Type Mapping -Transactions are started by calling Begin or BeginIso. The BeginIso variant -creates a transaction with a specified isolation level. +pgx maps between all common base types directly between Go and PostgreSQL. In +particular: - tx, err := conn.Begin() - if err != nil { - return err - } - // Rollback is safe to call even if the tx is already closed, so if - // the tx commits successfully, this is a no-op - defer tx.Rollback() + Go PostgreSQL + ----------------------- + string varchar + text - _, err = tx.Exec("insert into foo(id) values (1)") - if err != nil { - return err - } + // Integers are automatically be converted to any other integer type if + // it can be done without overflow or underflow. + int8 + int16 smallint + int32 int + int64 bigint + int + uint8 + uint16 + uint32 + uint64 + uint - err = tx.Commit() - if err != nil { - return err - } + // Floats are strict and do not automatically convert like integers. + float32 float4 + float64 float8 -Copy Protocol + time.Time date + timestamp + timestamptz -Use CopyTo to efficiently insert multiple rows at a time using the PostgreSQL -copy protocol. CopyTo accepts a CopyToSource interface. If the data is already -in a [][]interface{} use CopyToRows to wrap it in a CopyToSource interface. Or -implement CopyToSource to avoid buffering the entire data set in memory. + []byte bytea - rows := [][]interface{}{ - {"John", "Smith", int32(36)}, - {"Jane", "Doe", int32(29)}, - } - - copyCount, err := conn.CopyTo( - "people", - []string{"first_name", "last_name", "age"}, - pgx.CopyToRows(rows), - ) - -CopyTo can be faster than an insert with as few as 5 rows. - -Listen and Notify - -pgx can listen to the PostgreSQL notification system with the -WaitForNotification function. It takes a maximum time to wait for a -notification. - - err := conn.Listen("channelname") - if err != nil { - return nil - } - - if notification, err := conn.WaitForNotification(time.Second); err != nil { - // do something with notification - } Null Mapping @@ -212,6 +188,64 @@ the raw bytes returned by PostgreSQL. This can be especially useful for reading varchar, text, json, and jsonb values directly into a []byte and avoiding the type conversion from string. +Transactions + +Transactions are started by calling Begin or BeginIso. The BeginIso variant +creates a transaction with a specified isolation level. + + tx, err := conn.Begin() + if err != nil { + return err + } + // Rollback is safe to call even if the tx is already closed, so if + // the tx commits successfully, this is a no-op + defer tx.Rollback() + + _, err = tx.Exec("insert into foo(id) values (1)") + if err != nil { + return err + } + + err = tx.Commit() + if err != nil { + return err + } + +Copy Protocol + +Use CopyTo to efficiently insert multiple rows at a time using the PostgreSQL +copy protocol. CopyTo accepts a CopyToSource interface. If the data is already +in a [][]interface{} use CopyToRows to wrap it in a CopyToSource interface. Or +implement CopyToSource to avoid buffering the entire data set in memory. + + rows := [][]interface{}{ + {"John", "Smith", int32(36)}, + {"Jane", "Doe", int32(29)}, + } + + copyCount, err := conn.CopyTo( + "people", + []string{"first_name", "last_name", "age"}, + pgx.CopyToRows(rows), + ) + +CopyTo can be faster than an insert with as few as 5 rows. + +Listen and Notify + +pgx can listen to the PostgreSQL notification system with the +WaitForNotification function. It takes a maximum time to wait for a +notification. + + err := conn.Listen("channelname") + if err != nil { + return nil + } + + if notification, err := conn.WaitForNotification(time.Second); err != nil { + // do something with notification + } + TLS The pgx ConnConfig struct has a TLSConfig field. If this field is