Merge branch 'master' into add-null-oid
This commit is contained in:
+7
-2
@@ -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
|
||||
@@ -37,6 +37,11 @@ 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
|
||||
- go get -u gopkg.in/inconshreveable/log15.v2
|
||||
- go get -u github.com/jackc/fake
|
||||
|
||||
script:
|
||||
- go test -v -race -short ./...
|
||||
|
||||
|
||||
+3
-3
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
+1
-1
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user