Skip to content

Instantly share code, notes, and snippets.

@rueian
Last active March 16, 2020 11:56
Show Gist options
  • Save rueian/978aa0e96247b651856665f604a9b2cb to your computer and use it in GitHub Desktop.
Save rueian/978aa0e96247b651856665f604a9b2cb to your computer and use it in GitHub Desktop.
pgx prep test
package main
import (
"context"
"crypto/rand"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
"time"
)
const (
pgURL = "postgres://postgres@localhost:5433/postgres?sslmode=disable"
query = "INSERT INTO kv (key, value, updated_at) VALUES ($1::text, $2::bytea, $3::timestamptz) ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = EXCLUDED.updated_at RETURNING *;"
)
func main() {
ctx := context.Background()
conn, _ := pgx.Connect(ctx, pgURL)
defer conn.Close(ctx)
v := &pgtype.Bytea{Bytes: make([]byte, 10), Status: pgtype.Present}
rand.Read(v.Bytes)
t := &pgtype.Timestamptz{}
t.Set(time.Now())
conn.Exec(ctx, query, "foo", v, t)
}
// Proxy Output
//
// Parse Query: name=lrupsc_1_0 query=INSERT INTO kv (key, value, updated_at) VALUES ($1::text, $2::bytea, $3::timestamptz) ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = EXCLUDED.updated_at RETURNING *;
// Bind Parameter: 0 format=text len=3 value=foo
// Bind Parameter: 1 format=byte len=10 hex=1e4c4dd808f8fbfa34c2
// Bind Parameter: 2 format=byte len=8 hex=000243f6458dc0ae
// Bind ResultFormatCodes: column=0 format=byte
// Bind ResultFormatCodes: column=1 format=byte
// Bind ResultFormatCodes: column=2 format=byte
// Result DataRaw: column=0 format=byte len=3 hex=666f6f
// Result DataRaw: column=1 format=byte len=10 hex=1e4c4dd808f8fbfa34c2
// Result DataRaw: column=2 format=byte len=8 hex=000243f6458dc0ae
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment