pqinterval

package module
v0.0.0-...-f5c890b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2022 License: BSD-3-Clause Imports: 7 Imported by: 0

README

pqinterval

Scan targets for PostgreSQL's interval type.

The Interval type supports the full range of PostgreSQL intervals. Any database interval should successfully Scan() into a pqinterval.Interval.

Example:

var ival pqinterval.Interval

err := conn.QueryRow("SELECT '4 days'::INTERVAL").Scan(&ival)
if err != nil {
    log.Fatal(err)
}

fmt.Println(ival.Hours())

The Duration type is an alias of time.Duration, but which supports scanning from PostgreSQL intervals (potentially failing with ErrTooBig).

Example:

var since time.Duration
d := (*pqinterval.Duration)(&since)

err = conn.QueryRow("SELECT '2 days'::INTERVAL").Scan(d)
if err != nil {
    log.Fatal(err)
}

fmt.Println(since)

One caveat to be aware of: while PostgreSQL intervals are capable of modeling a number of "months", the Interval and Duration types here are not. Any time they scan an "x months" interval they will assume 30 days in a month. PostgreSQL avoids this ambiguity however when subtracting distant timestamps, by providing a large number of days rather than breaking them down into months.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTooBig = errors.New("interval overflows time.Duration")

ErrTooBig is returned by Interval.Duration and Duration.Scan if the interval would overflow a time.Duration.

Functions

This section is empty.

Types

type Duration

type Duration time.Duration

Duration is a time.Duration alias that supports the driver.Valuer and sql.Scanner interfaces.

func (*Duration) Scan

func (d *Duration) Scan(src interface{}) error

Scan implements sql.Scanner.

func (Duration) Value

func (d Duration) Value() (driver.Value, error)

Value implements driver.Valuer.

type Interval

type Interval struct {
	// contains filtered or unexported fields
}

Interval can represent the full range of PostgreSQL's interval type.

func New

func New(years, days, hours, minutes, seconds, microseconds int) Interval

New creates an Interval.

func (Interval) Duration

func (ival Interval) Duration() (time.Duration, error)

Duration converts an Interval into a time.Duration with the same semantics as `EXTRACT(EPOCH from <interval>)` in PostgreSQL.

func (Interval) Hours

func (ival Interval) Hours() int32

Hours returns the number of hours in the interval.

func (Interval) Microseconds

func (ival Interval) Microseconds() int64

Microseconds returns the number of microseconds in the interval, up to the number of microseconds in an hour.

func (*Interval) Scan

func (ival *Interval) Scan(src interface{}) error

Scan implements sql.Scanner.

func (Interval) Value

func (ival Interval) Value() (driver.Value, error)

Value implements driver.Valuer.

func (Interval) Years

func (ival Interval) Years() int32

Years returns the number of years in the interval.

type ParseErr

type ParseErr struct {
	String string
	Cause  error
}

ParseErr is returned on a failure to parse a postgres result into an Interval or Duration.

func (ParseErr) Error

func (pe ParseErr) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL