Documentation ¶
Overview ¶
Package pgtypes implements some PostgreSQL types in the GoLang. Implemented types can be simply used in DB communication and/or separately. Currently implemented types are Interval, Numeric and UUID.
Index ¶
- Constants
- type Interval
- func Day() Interval
- func Diff(from, to time.Time) Interval
- func DiffExtended(from, to time.Time) (i Interval)
- func FromDuration(d time.Duration) Interval
- func Hour() Interval
- func Microsecond() Interval
- func Millisecond() Interval
- func Minute() Interval
- func Month() Interval
- func Nanosecond() Interval
- func NewGoInterval() Interval
- func NewInterval(p uint8) Interval
- func NewPgInterval() Interval
- func ParseInterval(s string, p uint8) (i Interval, err error)
- func Picosecond() Interval
- func Second() Interval
- func Since(t time.Time) Interval
- func SinceExtended(t time.Time) Interval
- func Year() Interval
- func (i Interval) Add(add Interval) Interval
- func (i Interval) AddTo(t time.Time) time.Time
- func (i Interval) Cmp(i2 Interval) (sign int, ok bool)
- func (i Interval) Comparable(i2 Interval) bool
- func (i Interval) Div(div int64) Interval
- func (i Interval) Duration(daysInMonth uint8, minutesInDay uint32) time.Duration
- func (i Interval) Encode(w *pgx.WriteBuf, oid pgx.Oid) error
- func (i Interval) Equal(i2 Interval) bool
- func (i Interval) FormatCode() int16
- func (i Interval) Greater(i2 Interval) bool
- func (i Interval) GreaterOrEqual(i2 Interval) bool
- func (i Interval) In(i2 Interval) int64
- func (i Interval) Less(i2 Interval) bool
- func (i Interval) LessOrEqual(i2 Interval) bool
- func (i Interval) Mul(mul int64) Interval
- func (i Interval) NormalDays() int32
- func (i Interval) NormalHours() int64
- func (i Interval) NormalMinutes() int64
- func (i Interval) NormalMonths() int32
- func (i Interval) NormalNanoseconds() int64
- func (i Interval) NormalSeconds() int64
- func (i Interval) NormalYears() int32
- func (i Interval) Nullable() NullInterval
- func (i Interval) Precision() uint8
- func (i Interval) SafePrec() uint8
- func (i *Interval) Scan(src interface{}) (err error)
- func (i *Interval) ScanPgx(vr *pgx.ValueReader) error
- func (i Interval) SetPrecision(p uint8) Interval
- func (i Interval) String() string
- func (i Interval) Sub(sub Interval) Interval
- func (i Interval) SubFrom(t time.Time) time.Time
- func (i Interval) Value() (driver.Value, error)
- type NullInterval
- type NullNumeric
- type NullUUID
- type Numeric
- func NewInt(x int) *Numeric
- func NewInt16(x int16) *Numeric
- func NewInt32(x int32) *Numeric
- func NewInt64(x int64) *Numeric
- func NewInt8(x int8) *Numeric
- func NewNumeric() *Numeric
- func NewUint(x uint) *Numeric
- func NewUint16(x uint16) *Numeric
- func NewUint32(x uint32) *Numeric
- func NewUint64(x uint64) *Numeric
- func NewUint8(x uint8) *Numeric
- func (z *Numeric) Abs(x *Numeric) *Numeric
- func (z *Numeric) Add(x, y *Numeric) *Numeric
- func (x *Numeric) Cmp(y *Numeric) (r int)
- func (z *Numeric) Copy(x *Numeric) *Numeric
- func (n Numeric) Encode(w *pgx.WriteBuf, oid pgx.Oid) error
- func (n Numeric) FormatCode() int16
- func (x *Numeric) Int() int
- func (x *Numeric) Int16() int16
- func (x *Numeric) Int32() int32
- func (x *Numeric) Int64() int64
- func (x *Numeric) Int8() int8
- func (x *Numeric) IsNaN() bool
- func (x *Numeric) IsZero() bool
- func (z *Numeric) Mul(x, y *Numeric) *Numeric
- func (z *Numeric) Neg(x *Numeric) *Numeric
- func (n Numeric) Nullable() NullNumeric
- func (z *Numeric) Quo(x, y *Numeric) *Numeric
- func (z *Numeric) QuoPrec(x, y *Numeric, scale int16, round bool) *Numeric
- func (z *Numeric) QuoRem(x, y, m *Numeric) (*Numeric, *Numeric)
- func (z *Numeric) Rem(x, y *Numeric) *Numeric
- func (n *Numeric) Scan(src interface{}) (err error)
- func (n *Numeric) ScanPgx(vr *pgx.ValueReader) error
- func (z *Numeric) SetInt(x int) *Numeric
- func (z *Numeric) SetInt16(x int16) *Numeric
- func (z *Numeric) SetInt32(x int32) *Numeric
- func (z *Numeric) SetInt64(x int64) *Numeric
- func (z *Numeric) SetInt8(x int8) *Numeric
- func (z *Numeric) SetNaN() *Numeric
- func (z *Numeric) SetString(s string) (*Numeric, bool)
- func (z *Numeric) SetUint(x uint) *Numeric
- func (z *Numeric) SetUint16(x uint16) *Numeric
- func (z *Numeric) SetUint32(x uint32) *Numeric
- func (z *Numeric) SetUint64(x uint64) *Numeric
- func (z *Numeric) SetUint8(x uint8) *Numeric
- func (z *Numeric) SetZero() *Numeric
- func (x *Numeric) Sign() int
- func (x *Numeric) String() (r string)
- func (z *Numeric) Sub(x, y *Numeric) *Numeric
- func (x *Numeric) Uint() uint
- func (x *Numeric) Uint16() uint16
- func (x *Numeric) Uint32() uint32
- func (x *Numeric) Uint64() uint64
- func (x *Numeric) Uint8() uint8
- func (n Numeric) Value() (driver.Value, error)
- type UUID
- func (u UUID) Bytes() []byte
- func (u UUID) CleanString() string
- func (u UUID) Encode(w *pgx.WriteBuf, oid pgx.Oid) error
- func (u UUID) FormatCode() int16
- func (u UUID) IsZero() bool
- func (u UUID) Nullable() NullUUID
- func (u *UUID) Scan(src interface{}) (err error)
- func (u *UUID) ScanPgx(vr *pgx.ValueReader) error
- func (u UUID) String() string
- func (u UUID) Value() (driver.Value, error)
- Bugs
Constants ¶
const ( IntervalSecondPrecision = 0 IntervalMillisecondPrecision = 3 IntervalMicrosecondPrecision = 6 IntervalNanosecondPrecision = 9 IntervalPicosecondPrecision = 12 IntervalGoPrecision = IntervalNanosecondPrecision IntervalPgPrecision = IntervalMicrosecondPrecision IntervalMaxPrecision = 12 )
Predefined precisions for Interval.
const ( // UUIDLen is a length of UUID in bytes UUIDLen = 16 // UUIDCleanStringLen is a length of raw UUID string representation length in chars ("6ba7b8149dad11d180b400c04fd430c8") UUIDCleanStringLen = UUIDLen * 2 // UUIDStringLen is a length of default UUID string representation length in chars ("6ba7b814-9dad-11d1-80b4-00c04fd430c8") UUIDStringLen = UUIDLen*2 + 4 )
const (
// IntervalOid is an object identifier (OID) of Interval type used in PostgreSQL.
IntervalOid = 1186
)
const (
// NumericOid is an object identifier (OID) of Numeric type used in PostgreSQL.
NumericOid = 1700
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Interval ¶
type Interval struct { Months int32 Days int32 SomeSeconds int64 // contains filtered or unexported fields }
Interval represent time interval in Postgres-compatible way. It consists of 3 public fields:
Months - number months Days - number of days SomeSeconds - number of seconds or some smaller units (depends on precision).
All fields are signed. Sign of one field is independent from sign of others. Interval internally stores precision. Precision is number of digits after comma in 10-based representation of seconds. Precision can be from [0; 12] where 0 means that SomeSeconds is seconds and 12 means that SomeSeconds is picoseconds. If Interval created without calling constructor when it has 0 precision (i.e. SomeSeconds is just seconds). If Interval created with calling constructor and its documentation does not say another when it has precision = 9 (i.e. SomeSeconds is nanoseconds). This is because default Go time type has nanosecond precision. If interval is used to store PostgreSQL Interval when recommended precision is 6 (microsecond) because PostgreSQL use microsecond precision. This type is similar to Postgres interval data type. Value from one field is never automatically translated to value of another field, so <60*60*24 seconds> != <1 days> and so on. This is because of compatibility with Postgres, moreover day may have different amount of seconds and month may have different amount of days.
func Diff ¶
Diff calculates difference between given timestamps (time.Time) as nanoseconds and returns result as Interval (=to-from). Result always have months & days parts set to zero.
func DiffExtended ¶
DiffExtended is similar to Diff but calculates difference in months, days & nanoseconds instead of just nanoseconds (=to-from). Result may have non-zero months & days parts. DiffExtended use Location of both passed times while calculation. Most of time it is better to pass times with the same Location (UTC or not).
func FromDuration ¶
FromDuration returns new Interval equivalent for given time.Duration (convert time.Duration to Interval).
func Microsecond ¶
func Microsecond() Interval
Microsecond returns new Interval equal to 1 microsecond.
func Millisecond ¶
func Millisecond() Interval
Millisecond returns new Interval equal to 1 millisecond.
func NewGoInterval ¶
func NewGoInterval() Interval
NewGoInterval returns zero interval with GoLang precision (= nanosecond).
func NewInterval ¶
NewInterval returns zero interval with specified precision p.
func NewPgInterval ¶
func NewPgInterval() Interval
NewPgInterval returns zero interval with PostgreSQL precision (= microsecond).
func ParseInterval ¶
ParseInterval parses incoming string and extract interval with requested precision p. Format is postgres style specification for interval output format. Examples:
-1 year 2 mons -3 days 04:05:06.789 1 mons 2 year -34:56:78 00:00:00
BUG(unsacrificed): ParseInterval may overflow SomeSeconds if computed SomeSeconds should be MinInt64.
func Picosecond ¶
func Picosecond() Interval
Picosecond returns new Interval equal to 1 picosecond. This constructor return interval with precision = 12 (picosecond).
func Since ¶
Since returns elapsed time since given timestamp as Interval (=Diff(t, time.New()). Result always have months & days parts set to zero.
func SinceExtended ¶
SinceExtended returns elapsed time since given timestamp as Interval (=DiffExtended(t, time.New()). Result may have non-zero months & days parts.
func (Interval) Cmp ¶
Cmp returns compare two Intervals. ok indicates is i and i2 comparable. sign means the following:
sign<0 => i<i2 sign=0 => i=i2 sign>0 => i>i2
func (Interval) Comparable ¶
Comparable returns true only if it is possible to compare Intervals. Intervals "A" and "B" can be compared only if:
- all parts of "A" are less or equal to relative parts of "B" or
- all parts of "B" are less or equal to relative parts of "A".
In the other words, it is impossible to compare "30 days"-Interval with "1 month"-Interval.
func (Interval) Div ¶
Div divides interval by mul and returns result. Each part of Interval divides independently. Round rule: 0.4=>0 ; 0.5=>1 ; 0.6=>1 ; -0.4=>0 ; -0.5=>-1 ; -0.6=>-1
func (Interval) Duration ¶
Duration convert Interval to time.Duration. It is required to pass number of days in month (usually 30 or something near) and number of minutes in day (usually 1440) because of converting months and days parts of original Interval to time.Duration nanoseconds. Warning: this method is inaccuracy because in real life daysInMonth & minutesInDay vary and depends on relative timestamp.
func (Interval) FormatCode ¶
FormatCode implements the pgx.Encoder interface.
func (Interval) Greater ¶
Greater returns true if at least one part of original Interval is greater then relative part of i2 and all other parts of original Interval are greater or equal to relative parts of i2.
func (Interval) GreaterOrEqual ¶
GreaterOrEqual returns true if all parts of original Interval are greater or equal to relative parts of i2.
func (Interval) In ¶
In counts how many i contains in i2 (=i2/i). Round rule: 0.4=>0 ; 0.5=>1 ; 0.6=>1 ; -0.4=>0 ; -0.5=>-1 ; -0.6=>-1
func (Interval) Less ¶
Less returns true if at least one part of original Interval is less then relative part of i2 and all other parts of original Interval are less or equal to relative parts of i2.
func (Interval) LessOrEqual ¶
LessOrEqual returns true if all parts of original Interval are less or equal to relative parts of i2.
func (Interval) Mul ¶
Mul returns interval i multiplied by mul. Each part of Interval multiples independently.
func (Interval) NormalDays ¶
NormalDays just returns Days part.
func (Interval) NormalHours ¶
NormalHours returns number of hours in seconds part.
func (Interval) NormalMinutes ¶
NormalMinutes returns number of hours in seconds part after subtracting NormalHours.
func (Interval) NormalMonths ¶
NormalMonths return number of months in month part after subtracting NormalYears*12 (as i.Months % 12). Examples: if .Months = 11 then NormalMonths = 11, but if .Months = 13 then NormalMonths = 1.
func (Interval) NormalNanoseconds ¶
NormalNanoseconds returns number of nanoseconds in fraction part of seconds part.
func (Interval) NormalSeconds ¶
NormalSeconds returns number of seconds in seconds part after subtracting NormalHours*3600 and NormalMinutes*60 (as i.Seconds % 60).
func (Interval) NormalYears ¶
NormalYears return number of years in month part (as i.Months / 12).
func (Interval) Nullable ¶
func (i Interval) Nullable() NullInterval
Nullable returns valid NullInterval with Interval i.
func (Interval) SafePrec ¶
SafePrec returns minimal precision which can be used for current Interval without data loss. Examples:
10 seconds => 0 (second precision, can not be less) 10 milliseconds => 2 (2 digit after decimal point precision) 123 microseconds => 6 (microsecond precision)
func (*Interval) ScanPgx ¶
func (i *Interval) ScanPgx(vr *pgx.ValueReader) error
ScanPgx implements the pgx.PgxScanner interface.
func (Interval) SetPrecision ¶
SetPrecision returns new interval with changed precision (and do appropriate recalculation). Possible precision is 0..12 where 0 means second precision and 9 means nanosecond precision. If passed p>12 it will be silently replaced with p=12.
func (Interval) String ¶
String returns string representation of interval. Output format is the same as for Parse.
BUG(unsacrificed): String may overflow SomeSeconds if SomeSeconds is MinInt64.
type NullInterval ¶
NullInterval represents an Interval that may be null. It implements the pgx.PgxScanner and pgx.Encoder interfaces so it may be used both as an argument to Query[Row] and a destination for ScanPgx.
If Valid is false then the value is NULL.
func (NullInterval) FormatCode ¶
func (n NullInterval) FormatCode() int16
FormatCode implements the pgx.Encoder interface.
func (*NullInterval) Scan ¶
func (n *NullInterval) Scan(src interface{}) (err error)
Scan implements the sql.Scanner interface.
func (*NullInterval) ScanPgx ¶
func (n *NullInterval) ScanPgx(vr *pgx.ValueReader) error
ScanPgx implements the pgx.PgxScanner interface.
type NullNumeric ¶
NullNumeric represents an Numeric that may be NULL. NullNumeric implements the pgx.PgxScanner and pgx.Encoder interfaces so it may be used both as an argument to Query[Row] and a destination for ScanPgx.
If Valid is false then the value is NULL.
func (NullNumeric) FormatCode ¶
func (n NullNumeric) FormatCode() int16
FormatCode implements the pgx.Encoder interface.
func (*NullNumeric) Scan ¶
func (n *NullNumeric) Scan(src interface{}) (err error)
Scan implements the sql.Scanner interface.
func (*NullNumeric) ScanPgx ¶
func (n *NullNumeric) ScanPgx(vr *pgx.ValueReader) error
ScanPgx implements the pgx.PgxScanner interface.
type NullUUID ¶
NullUUID represents an UUID that may be NULL. NullUUID implements the pgx.PgxScanner and pgx.Encoder interfaces so it may be used both as an argument to Query[Row] and a destination for ScanPgx.
If Valid is false then the value is NULL.
func (NullUUID) FormatCode ¶
FormatCode implements the pgx.Encoder interface.
type Numeric ¶
type Numeric struct {
// contains filtered or unexported fields
}
Numeric is a PostgreSQL Numeric type implementation in GoLang. It is arbitrary precision numbers type which can store numbers with a very large number of digits. It is especially recommended for storing monetary amounts and other quantities where exactness is required. Calculations with Numeric values yield exact results where possible, e.g. addition, subtraction, multiplication. However, calculations on Numeric values are very slow compared to the integer types, or to the floating-point types. Internally Numeric type has the same structure (except dscale field) as a PostgreSQL numeric type so it perfect for using in DB communications.
func NewNumeric ¶
func NewNumeric() *Numeric
NewNumeric allocates and returns a new Numeric set to 0.
func (*Numeric) Cmp ¶
Cmp compare two Numeric.
-1 if x < y 0 if x == y +1 if x > y
NaN treats as equal to other NaN and greater then any other number. This is as in PostgreSQL.
func (*Numeric) Copy ¶
Copy sets z to x and returns z. x is not changed even if z and x are the same.
func (Numeric) FormatCode ¶
FormatCode implements the pgx.Encoder interface.
func (*Numeric) Int ¶
Int returns the int representation of x. If x is NaN, the result is 0. If x cannot be represented in an int, the result is undefined.
func (*Numeric) Int16 ¶
Int16 returns the int16 representation of x. If x is NaN, the result is 0. If x cannot be represented in an int16, the result is undefined.
func (*Numeric) Int32 ¶
Int32 returns the int32 representation of x. If x is NaN, the result is 0. If x cannot be represented in an int32, the result is undefined.
func (*Numeric) Int64 ¶
Int64 returns the int64 representation of x. If x is NaN, the result is 0. If x cannot be represented in an int64, the result is undefined.
func (*Numeric) Int8 ¶
Int8 returns the int8 representation of x. If x is NaN, the result is 0. If x cannot be represented in an int8, the result is undefined.
func (Numeric) Nullable ¶
func (n Numeric) Nullable() NullNumeric
Nullable returns valid NullNumeric with Numeric n.
func (*Numeric) Quo ¶
Quo is just a shorthand for QuoPrec with default scale and rounding enabled. Default scale calculates as in PostgreSQL (more or less).
func (*Numeric) QuoPrec ¶
QuoPrec sets z to the quotient x/y for y != 0 and returns z. Result will truncated or rounded up to scale decimal digits after decimal point. Result will be rounded if round is true, otherwise result will be truncated. If y == 0, a division-by-zero run-time panic occurs. QuoPrec implements truncated division (like Go); see QuoRem for more details.
func (*Numeric) QuoRem ¶
QuoRem sets z to the quotient x/y and r to the remainder x%y and returns the pair (z, r) for y != 0. If y == 0, a division-by-zero run-time panic occurs.
QuoRem implements T-division and modulus (like Go):
q = x/y with the result truncated to zero r = x - y*q
(See Daan Leijen, “Division and Modulus for Computer Scientists”.) Euclidean division and modulus (unlike Go) do not currently implemented for Numeric.
func (*Numeric) Rem ¶
Rem sets z to the remainder x%y for y != 0 and returns z. If y == 0, a division-by-zero run-time panic occurs. Rem implements truncated modulus (like Go); see QuoRem for more details.
func (*Numeric) ScanPgx ¶
func (n *Numeric) ScanPgx(vr *pgx.ValueReader) error
ScanPgx implements the pgx.PgxScanner interface.
func (*Numeric) SetString ¶
SetString sets z to the value of s and returns z and a boolean indicating success. s must be a floating-point number of one of format
[+-]?[0-9]*\.[0-9]* // "123.456", "123.", ".456", ".", "-123.456" [+-]?[0-9]+ // "123", "-123"
func (*Numeric) Sign ¶
Sign returns first value as following:
-1 if x < 0 0 if x is 0 +1 if x > 0 +2 if x is NaN
func (*Numeric) Uint ¶
Uint returns the uint representation of x. If x is NaN, the result is 0. If x cannot be represented in an uint, the result is undefined.
func (*Numeric) Uint16 ¶
Uint16 returns the uint16 representation of x. If x is NaN, the result is 0. If x cannot be represented in an uint16, the result is undefined.
func (*Numeric) Uint32 ¶
Uint32 returns the uint32 representation of x. If x is NaN, the result is 0. If x cannot be represented in an uint32, the result is undefined.
func (*Numeric) Uint64 ¶
Uint64 returns the uint64 representation of x. If x is NaN, the result is 0. If x cannot be represented in an uint64, the result is undefined.
type UUID ¶
UUID representation compliant with specification described in RFC 4122.
func ParseUUID ¶
ParseUUID parses an UUID standard string representation (like "6ba7b814-9dad-11d1-80b4-00c04fd430c8").
func ParseUUIDBytes ¶
ParseUUIDBytes parses byte slice (as-is) with UUID.
func ParseUUIDClean ¶
ParseUUIDClean parses an UUID clean string representation (like "6ba7b8149dad11d180b400c04fd430c8").
func (UUID) CleanString ¶
CleanString returns clean string representation of UUID (without any additional chars and in lower case).
func (UUID) FormatCode ¶
FormatCode implements the pgx.Encoder interface.
func (*UUID) Scan ¶
Scan implements the sql.Scanner interface. Can parse:
bytes as raw UUID representations (as-is) string/bytes as default UUID string representation
func (*UUID) ScanPgx ¶
func (u *UUID) ScanPgx(vr *pgx.ValueReader) error
ScanPgx implements the pgx.PgxScanner interface.
Notes ¶
Bugs ¶
ParseInterval may overflow SomeSeconds if computed SomeSeconds should be MinInt64.
String may overflow SomeSeconds if SomeSeconds is MinInt64.