pgx icon indicating copy to clipboard operation
pgx copied to clipboard

Support scanning time type into string

Open matthewmueller opened this issue 1 year ago • 0 comments

Describe the bug

In pgx/v5, you can no longer scan postgres time types into a string. While in a prior version, this was possible.

To Reproduce

In pgx/v5

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/jackc/pgx/v5"
)

func main() {
	if err := run(); err != nil {
		log.Fatal(err)
	}
}

func run() error {
	conn, err := pgx.Connect(context.TODO(), "postgres://localhost:5432/pogo?sslmode=disable")
	if err != nil {
		return err
	}
	defer conn.Close(context.TODO())
	result := ""
	if err := conn.QueryRow(context.TODO(), "SELECT '08:00'::time without time zone").Scan(&result); err != nil {
		return err
	}
	fmt.Println(result)
	return nil
}

Results in can't scan into dest[0]: cannot scan time (OID 1083) in binary format into *string.

While in pgx (v3.6.2+incompatible):

package main

import (
	"fmt"
	"log"

	"github.com/jackc/pgx"
)

func main() {
	if err := run(); err != nil {
		log.Fatal(err)
	}
}

func run() error {
	cfg, err := pgx.ParseConnectionString("postgres://localhost:5432/pogo?sslmode=disable")
	if err != nil {
		return err
	}
	conn, err := pgx.Connect(cfg)
	if err != nil {
		return err
	}
	defer conn.Close()
	result := ""
	if err := conn.QueryRow("SELECT '08:00'::time without time zone").Scan(&result); err != nil {
		return err
	}
	fmt.Println(result)
	return nil
}

It returns 08:00:00.

Expected behavior

It'd be nice to support scanning into a string. Currently the time type in pgx/v5 supports scanning into time.Time, but that can be a bit unclear in Go because time.Time includes the date.

Actual behavior

I think it'd be great to support both options, scanning into a string to bring back the old behavior as well as scanning into a time.Time

Version

  • Go: go version go1.22.0 darwin/arm64
  • PostgreSQL: PostgreSQL 14.11 (Homebrew) on aarch64-apple-darwin23.2.0, compiled by Apple clang version 15.0.0 (clang-1500.1.0.2.5), 64-bit
  • pgx: v5.5.5

Additional context

Thanks!

matthewmueller avatar May 05 '24 19:05 matthewmueller