pqinterval

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2023 License: BSD-3-Clause Imports: 8 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 following additional interfaces: - driver.Valuer - sql.Scanner - json.Marshaler - json.Unmarshaler

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. IMPORTANT NOTE: We are serializing Duration as an *integer* number of minutes. For our use case, this is sufficient: we don't need any more precision than minute.

func (Duration) Milliseconds

func (d Duration) Milliseconds() float64

func (Duration) Minutes added in v0.0.2

func (d Duration) Minutes() float64

func (*Duration) Scan

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

Scan implements sql.Scanner.

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler

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 NullDuration

type NullDuration struct {
	Duration
	Valid bool
}

func NewNullDuration

func NewNullDuration(d Duration, valid bool) NullDuration

func (NullDuration) MarshalJSON

func (nd NullDuration) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. IMPORTANT NOTE: We are serializing NullDuration as an *integer* number of milliseconds. For our use case, this is sufficient: we don't need any more precision than millisecond.

func (NullDuration) Milliseconds

func (nd NullDuration) Milliseconds() float64

Milliseconds returns the number of milliseconds in the duration

func (*NullDuration) Scan

func (nd *NullDuration) Scan(src interface{}) error

func (*NullDuration) UnmarshalJSON

func (nd *NullDuration) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler

func (NullDuration) Value

func (nd NullDuration) Value() (driver.Value, error)

type NullInterval

type NullInterval struct {
	Interval
	Valid bool // Valid is true if Interval is not NULL
}

func (*NullInterval) Scan

func (nival *NullInterval) Scan(src interface{}) error

func (NullInterval) Value

func (nival NullInterval) Value() (driver.Value, error)

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