2
0

Merge branch 'master' into add-null-oid

This commit is contained in:
Manni Wood
2016-09-25 09:48:28 -04:00
4 changed files with 95 additions and 56 deletions
+7 -2
View File
@@ -1,8 +1,8 @@
language: go language: go
go: go:
- 1.6.2 - 1.7.1
- 1.5.2 - 1.6.3
- tip - tip
# Derived from https://github.com/lib/pq/blob/master/.travis.yml # Derived from https://github.com/lib/pq/blob/master/.travis.yml
@@ -37,6 +37,11 @@ before_script:
- psql -U postgres -c "create user pgx_md5 SUPERUSER PASSWORD 'secret'" - psql -U postgres -c "create user pgx_md5 SUPERUSER PASSWORD 'secret'"
- psql -U postgres -c "create user pgx_pw SUPERUSER PASSWORD 'secret'" - psql -U postgres -c "create user pgx_pw SUPERUSER PASSWORD 'secret'"
install:
- go get -u github.com/shopspring/decimal
- go get -u gopkg.in/inconshreveable/log15.v2
- go get -u github.com/jackc/fake
script: script:
- go test -v -race -short ./... - go test -v -race -short ./...
+3 -3
View File
@@ -366,12 +366,12 @@ func TestPoolAcquireAndReleaseCycleAutoConnect(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Unable to Acquire: %v", err) t.Fatalf("Unable to Acquire: %v", err)
} }
rows, _ := c.Query("select 1") rows, _ := c.Query("select 1, pg_sleep(0.02)")
rows.Close() rows.Close()
pool.Release(c) pool.Release(c)
} }
for i := 0; i < 1000; i++ { for i := 0; i < 10; i++ {
doSomething() doSomething()
} }
@@ -381,7 +381,7 @@ func TestPoolAcquireAndReleaseCycleAutoConnect(t *testing.T) {
} }
var wg sync.WaitGroup var wg sync.WaitGroup
for i := 0; i < 1000; i++ { for i := 0; i < 10; i++ {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
+84 -50
View File
@@ -81,63 +81,39 @@ releasing connections when you do not need that level of control.
return err return err
} }
Transactions Base Type Mapping
Transactions are started by calling Begin or BeginIso. The BeginIso variant pgx maps between all common base types directly between Go and PostgreSQL. In
creates a transaction with a specified isolation level. particular:
tx, err := conn.Begin() Go PostgreSQL
if err != nil { -----------------------
return err string varchar
} text
// 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)") // Integers are automatically be converted to any other integer type if
if err != nil { // it can be done without overflow or underflow.
return err int8
} int16 smallint
int32 int
int64 bigint
int
uint8
uint16
uint32
uint64
uint
err = tx.Commit() // Floats are strict and do not automatically convert like integers.
if err != nil { float32 float4
return err float64 float8
}
Copy Protocol time.Time date
timestamp
timestamptz
Use CopyTo to efficiently insert multiple rows at a time using the PostgreSQL []byte bytea
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
}
Null Mapping 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 varchar, text, json, and jsonb values directly into a []byte and avoiding the
type conversion from string. 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 TLS
The pgx ConnConfig struct has a TLSConfig field. If this field is The pgx ConnConfig struct has a TLSConfig field. If this field is
+1 -1
View File
@@ -1282,7 +1282,7 @@ func TestConnQueryDatabaseSQLDriverValuer(t *testing.T) {
} }
var num decimal.Decimal 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 { if err != nil {
t.Fatalf("Scan failed: %v", err) t.Fatalf("Scan failed: %v", err)
} }