Skip to content

Instantly share code, notes, and snippets.

@rueian
Last active March 16, 2020 12:04
Show Gist options
  • Save rueian/8ee0eb34ccfa66c3bb81b347f49ca4fe to your computer and use it in GitHub Desktop.
Save rueian/8ee0eb34ccfa66c3bb81b347f49ca4fe to your computer and use it in GitHub Desktop.
libpq prep test
package main
import (
"crypto/rand"
"database/sql"
_ "github.com/lib/pq"
"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() {
db, _ := sql.Open("postgres", pgURL)
defer db.Close()
stmt, _ := db.Prepare(query)
defer stmt.Close()
v := make([]byte, 10)
rand.Read(v)
stmt.Exec("foo", v, time.Now())
}
// Proxy Output
//
// Parse Query: name=1 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=text len=31 value=>\216\205\303h\203\236\330:\205
// Bind Parameter: 2 format=text len=32 value=2020-03-16 19:44:23.040841+08:00
// Bind ResultFormatCodes: column=0 format=text
// Bind ResultFormatCodes: column=1 format=byte
// Bind ResultFormatCodes: column=2 format=text
// Result DataRaw: column=0 format=text len=3 value=foo
// Result DataRaw: column=1 format=byte len=10 hex=3e8e85c368839ed83a85
// Result DataRaw: column=2 format=text len=29 value=2020-03-16 11:44:23.040841+00
// Proxy Output With &binary_parameters=yes
//
// Parse Query: name=1 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=8c24e688401333a8a335
// Bind Parameter: 2 format=text len=32 value=2020-03-16 19:43:11.368192+08:00
// Bind ResultFormatCodes: column=0 format=text
// Bind ResultFormatCodes: column=1 format=byte
// Bind ResultFormatCodes: column=2 format=text
// Result DataRaw: column=0 format=text len=3 value=foo
// Result DataRaw: column=1 format=byte len=10 hex=8c24e688401333a8a335
// Result DataRaw: column=2 format=text len=29 value=2020-03-16 11:43:11.368192+00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment