pgt

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2020 License: Apache-2.0 Imports: 18 Imported by: 4

README

pgt

Go PostgreSQL type wrappers

GoDoc Go Report Card

pgt repository provides missing type wrappers to automatically decode and encode advanced PostgreSQL types.

Refer to the godoc for more information.

Contributions are welcome!

Documentation

Overview

Package pgt provides missing type wrappers to automatically decode and encode advanced PostgreSQL types.

* Real number arrays * Integer number arrays * String arrays * UUID * Time intervals (duration) * more ...

This types provides some interfaces for additional Marshallers (like JSON, YAML)

Index

Constants

This section is empty.

Variables

View Source
var EmptyArray = []byte("{}")

EmptyArray represents a value of empty Postgresql Array

Functions

func CheckPgNoRows

func CheckPgNoRows(title string, err error) errstack.E

CheckPgNoRows wraps pg error into errstack.E

func CheckRowsAffected

func CheckRowsAffected(title string, expected int, res orm.Result, err error) errstack.E

CheckRowsAffected asserts that expected number of rows has been affected in the SQL operation.

func ErrNotNoRows

func ErrNotNoRows(title string, err error) errstack.E

ErrNotNoRows if errors is not Nil and is not ErrNoRows then it returns nil. Otherwise it will wrap the error as an IO error.

func FilterValidStrings

func FilterValidStrings(ss ...String) []string

FilterValidStrings returns a slice of only valid strings

func ParseFloatArray

func ParseFloatArray(src []byte) ([]float64, error)

ParseFloatArray parses float array column

func ParseInt64Array

func ParseInt64Array(src []byte) ([]int64, error)

ParseInt64Array parses int64 array column

func SplitNestedSimpleArray

func SplitNestedSimpleArray(src []byte) [][]byte

SplitNestedSimpleArray splits Postgresql encoded Array of simple types which doesn't require any escape charaters into list of bytes of elements.

func SplitSimpleArray

func SplitSimpleArray(src []byte) [][]byte

SplitSimpleArray splits Postgresql encoded Array into list of bytes of elements. It trims {} characters and split by ','

Types

type BigInt

type BigInt struct {
	*big.Int
}

BigInt represents Postgresql numeric type for natural number

func NewBigInt

func NewBigInt(x int64) BigInt

NewInt allocates and returns a new Int set to x.

func (BigInt) IsNull

func (dst BigInt) IsNull() bool

IsNull returns tru iff the inner value is nil

func (BigInt) MarshalJSON

func (dst BigInt) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Unmarshaler interface.

func (*BigInt) Scan

func (dst *BigInt) Scan(src interface{}) error

Scan implements sql.Sanner interface

func (*BigInt) UnmarshalJSON

func (dst *BigInt) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (BigInt) Value

func (dst BigInt) Value() (driver.Value, error)

Value implements sql/driver.Valuer

type Float64

type Float64 sql.NullFloat64

Float64 is a database.sql.NullString Float64

func (Float64) Add

func (s Float64) Add(other Float64) Float64

Add adds given float64 into reveiver and returns copy

func (Float64) Equals

func (s Float64) Equals(other *Float64) bool

Equals compares if two Float64 slices are equal

func (Float64) MarshalJSON

func (s Float64) MarshalJSON() ([]byte, error)

MarshalJSON implements Marshaler interface

func (Float64) MarshalYAML

func (s Float64) MarshalYAML() (interface{}, error)

MarshalYAML implements Marshaler

func (*Float64) Scan

func (s *Float64) Scan(src interface{}) error

Scan implements sql.Scanner for the Float64 type

func (Float64) String

func (s Float64) String() string

String is the stringer implementation for nullable flaot64

func (*Float64) UnmarshalJSON

func (s *Float64) UnmarshalJSON(data []byte) error

UnmarshalJSON implements Unmarshaler interface

func (*Float64) UnmarshalYAML

func (s *Float64) UnmarshalYAML(unmarshal func(v interface{}) error) error

UnmarshalYAML implements Unmarshaler

func (Float64) Value

func (s Float64) Value() (driver.Value, error)

Value is the valuer for Float64 type. The error is always nil.

type Float64s

type Float64s []float64

Float64s is a slice of floats for valuer interface

func (*Float64s) Scan

func (f *Float64s) Scan(src interface{}) error

Scan implements scan methods for scanner

func (Float64s) Value

func (f Float64s) Value() (driver.Value, error)

Value is the valuer for float slice

type Int64

type Int64 sql.NullInt64

Int64 is a database.sql.Int64 wrapper

func (Int64) MarshalJSON

func (s Int64) MarshalJSON() ([]byte, error)

MarshalJSON implements Marshaler interface

func (*Int64) Scan

func (s *Int64) Scan(src interface{}) error

Scan implements sql.Scanner for the if type

func (*Int64) UnmarshalJSON

func (s *Int64) UnmarshalJSON(data []byte) error

UnmarshalJSON implements Unmarshaler interface

func (Int64) Value

func (s Int64) Value() (driver.Value, error)

Value is the valuer for if type. The error is always nil.

type Ints

type Ints []int64

Ints is a slice of long integers for valuer interface

func (*Ints) Scan

func (ls *Ints) Scan(src interface{}) error

Scan implements scan methods for scanner

func (Ints) Value

func (ls Ints) Value() (driver.Value, error)

Value is the valuer for integer slice

type Logger

type Logger interface {
	// Fatal should stop the execution
	Fatal(msg string, ctx ...interface{})
}

Logger defines the reporting interface for utils error functions

type String

type String sql.NullString

String is a database.sql.NullString wrapper

func NewString

func NewString(s string, emptyToNull bool) String

NewString created from the specified string

func (String) MarshalJSON

func (s String) MarshalJSON() ([]byte, error)

MarshalJSON implements Marshaler interface

func (*String) Scan

func (s *String) Scan(src interface{}) (err error)

Scan implements sql.Scanner for the String type

func (*String) UnmarshalJSON

func (s *String) UnmarshalJSON(data []byte) error

UnmarshalJSON implements Unmarshaler interface

func (String) Value

func (s String) Value() (driver.Value, error)

Value is the valuer for String type. The error is always nil.

type Strings

type Strings []string

Strings is a slice of strings for valuer interface

func (Strings) Contains

func (s Strings) Contains(val string) bool

Contains checks if s contains val

func (Strings) ContainsAll

func (s Strings) ContainsAll(vals []string) bool

ContainsAll checks if s contains all vals

func (Strings) ContainsAllSorted

func (s Strings) ContainsAllSorted(s2 Strings) bool

ContainsAllSorted checks if s contains all elements from `s2`. We assume that both slices are sorted.

func (Strings) Distinct

func (s Strings) Distinct() Strings

Distinct omits every non-first occurrence of a element

func (Strings) EmptyToNil

func (s Strings) EmptyToNil() Strings

EmptyToNil returns nil slice if source contains only empty elements. Otherwise original slice is returned

func (Strings) Equals

func (s Strings) Equals(s2 Strings) bool

Equals compares if two string slices are equal

func (Strings) Exclude

func (s Strings) Exclude(ss ...string) Strings

Exclude removes existing items from source

func (Strings) ForEachPair

func (s Strings) ForEachPair(f func(string, string))

ForEachPair executes function for each pair inside Strings. See tests for details

func (Strings) GetOrEmpty

func (s Strings) GetOrEmpty(i int) string

GetOrEmpty returns n-th element or empty string if i is out of range

func (Strings) Map

func (s Strings) Map(f func(string) string) Strings

Map using the specified function

func (*Strings) Scan

func (s *Strings) Scan(src interface{}) error

Scan implements sql.Scanner for the String slice type Scanners take the database value (in this case as a byte slice) and sets the value of the type. Here we cast to a string and do a regexp based parse

func (Strings) TrimMap

func (s Strings) TrimMap(m map[string]bool) Strings

TrimMap maps elements which are inside map to empty strings

func (Strings) TrimSpace

func (s Strings) TrimSpace() Strings

TrimSpace returns Strings instance with all elements transformed by strings.TrimSpace

func (Strings) Value

func (s Strings) Value() (driver.Value, error)

Value is the valuer for string slice which serializes it via comma Returns error if one of strings contain comma

type Time

type Time struct {
	time.Time
	Valid bool
}

Time overrides time.Time for two reasons: (1) json serialization (2) nullability.

On default time.Time serializes two json as string (ISO format). For communication with client we need serialize to UNIX timestamp.

As time.Time is as struct, we are unable to distinguish null value (best we can do is use time.Time.IsZero()). To overcome this issue, agtime.Time has `Valid` field which enables us to handle nulls property. Mainly replicates pq.NullTime behavior

func NewTime

func NewTime(t time.Time) Time

NewTime creates new valid UTC Time

func UTCNow

func UTCNow() Time

UTCNow is an utility method for creating Time representing "now" in UTC time zone

func (Time) Add

func (t Time) Add(d time.Duration) Time

Add is a proxy for the time:Time.Add method

func (Time) MarshalBinary

func (t Time) MarshalBinary() ([]byte, error)

MarshalBinary implements binary encoding for time This pair of methods are used if agtime.Time is msgpacked.

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON implements Marshaller interface

func (*Time) Scan

func (t *Time) Scan(value interface{}) error

Scan implements Scanner interface

func (*Time) UnmarshalBinary

func (t *Time) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements binary decoding for time

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements Unmarshaller interface

func (Time) Value

func (t Time) Value() (driver.Value, error)

Value implements Valuer interface

type TruncInterval

type TruncInterval string

TruncInterval is a type for postgres date_trunc first argument - `field`

const (
	Microseconds TruncInterval = "microseconds"
	Milliseconds TruncInterval = "milliseconds"
	Second       TruncInterval = "second"
	Minute       TruncInterval = "minute"
	Hour         TruncInterval = "hour"
	Day          TruncInterval = "day"
	Week         TruncInterval = "week"
	Month        TruncInterval = "month"
	Quarter      TruncInterval = "quarter"
	Year         TruncInterval = "year"
	Decade       TruncInterval = "decade"
	Century      TruncInterval = "century"
	Millennium   TruncInterval = "millennium"
)

Valid TruncInterval values

func ParseTruncInterval

func ParseTruncInterval(s string, required bool) (TruncInterval, error)

ParseTruncInterval converts s to valid TruncInternal or "" if s is empty and not required

func (*TruncInterval) Scan

func (tc *TruncInterval) Scan(src interface{}) error

Scan implements sql.Scanner for the TruncInterval type

func (TruncInterval) Value

func (tc TruncInterval) Value() (driver.Value, error)

Value is the valuer for TruncInterval type. The error is always nil.

type UUID

type UUID uuid.UUID

UUID type to wrap uuid package

func ExtractUUIDs

func ExtractUUIDs(i UUIDIterator, optLength ...int) []UUID

ExtractUUIDs returns a list of unique ids from given interator

func MustParseUUID

func MustParseUUID(s string, logger Logger) UUID

MustParseUUID parses UUID from string. The function will panic if `s` is not a proper UUID

func ParseUUID

func ParseUUID(s string) (UUID, errstack.E)

ParseUUID parses string into UUID value

func RandomUUID

func RandomUUID() UUID

RandomUUID returns a new random uuid.

func (UUID) Empty

func (u UUID) Empty() bool

Empty checks whether UUID holds any value

func (UUID) Equals

func (u UUID) Equals(u2 UUID) bool

Equals check if two UUIDs are equal

func (UUID) MarshalJSON

func (u UUID) MarshalJSON() ([]byte, error)

MarshalJSON implements Marshaller interface

func (UUID) MarshalYAML

func (u UUID) MarshalYAML() (interface{}, error)

MarshalYAML implements Marshaler interface of YAML

func (*UUID) Scan

func (u *UUID) Scan(value interface{}) error

Scan is the scanner for UUID

func (UUID) String

func (u UUID) String() string

String converts UUID into string

func (*UUID) UnmarshalJSON

func (u *UUID) UnmarshalJSON(data []byte) error

UnmarshalJSON implements Unmarshaller interface

func (*UUID) UnmarshalYAML

func (u *UUID) UnmarshalYAML(unmarshaler func(interface{}) error) error

UnmarshalYAML implements Unmarshaler interface of YAML

func (UUID) Value

func (u UUID) Value() (driver.Value, error)

Value is the valuer for UUID

type UUIDIterator

type UUIDIterator interface {
	Get() UUID
	Next() bool
}

UUIDIterator is an interface used by `ExtractUUIDs` function

type UUIDs

type UUIDs []UUID

UUIDs is a slice of UUID

func ParseUUIDArray

func ParseUUIDArray(src []byte) (UUIDs, error)

ParseUUIDArray parses UUID array

func (*UUIDs) Scan

func (ls *UUIDs) Scan(src interface{}) error

Scan implements sql Scanner interface

func (UUIDs) Value

func (ls UUIDs) Value() (driver.Value, error)

Value implements sql Valuer interface

Jump to

Keyboard shortcuts

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