postgres

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const QueryOptionTypeLimit = "limit"

QueryOptionTypeLimit describes a limit option type

View Source
const QueryOptionTypeOffset = "offset"

QueryOptionTypeOffset describes an offset option type

View Source
const QueryOptionTypeOrderBy = "orderBy"

QueryOptionTypeOrderBy describes an order by option type

Variables

This section is empty.

Functions

func BulkInsert

func BulkInsert(ctx context.Context, tx CRUD, itemsToInsert Bulker) (rowsInserted int, insertErr error)

BulkInsert is a general bulk insert func that can be used to insert any valid Bulker type.

func Commit

func Commit(tx *sqlx.Tx, resource string) error

Commit will attept to commit the provided resource. Upon a failure to commit, the transaction will be rolled back.

func Err

func Err(resource string, err error) error

Err creates a new PG error from a provided error.

func NewTx

func NewTx(ctx context.Context, db *sqlx.DB) (*sqlx.Tx, error)

NewTx creates a new database transaction with default isolation levels and passes a context to roll back a transaction in the case of a context cancellation. Should clean up any zombie connections that could be if we did not control for this.

func Rollback

func Rollback(tx *sqlx.Tx, resource string, err error) error

Rollback undoes the supplied transaction.

func SanitizeString

func SanitizeString(s string) string

SanitizeString removes \u0000 (null byte) characters, as PG cannot handle them.

Relevant Thread: https://www.postgresql.org/message-id/CAE37PpOn%3DMcGeokmny4tm4FTHmXSG4KydgUJemKqT9XxkrrTmg%40mail.gmail.com TLDR: Postgres is written in C and uses null bytes to terminate strings. It would be too much work to change across the whole codebase.

func ToNullFloat64

func ToNullFloat64(f float64) sql.NullFloat64

ToNullFloat64 is a utility method to convert a float64 into an sql null float64. This func never sets the valid field to false.

func ToNullInt64

func ToNullInt64(i int64) sql.NullInt64

ToNullInt64 is a utility method to convert a int64 into an sql null int64. This func never sets the valid field to false.

func ToNullString

func ToNullString(s string) sql.NullString

ToNullString is a utility method to convert a string into a sql null string. If the input string is len 0 it will set a sql.NullString with Valid false.

func ToWhereClause

func ToWhereClause(rebinder Rebinder, wherable Whereable) (clause string, args []interface{}, err error)

ToWhereClause adds WHERE prefix to a whereable's clause output if the whereable is not nil

Types

type Bulker

type Bulker interface {
	Len() int
	PrepareStatement() string
	KeyedArgsAtIndex(index int) (key string, arguments []interface{})
	TableName() string
}

Bulker is an interface that defines the behavior a type needs to implement to be bulk insert/updated into PG.

type CRUD

CRUD is a ready to go type that implements most of the basic methods we use for 90%+ of our database calls.

type Cube

type Cube []float64

Cube is a type that is used to work with the postgresql cube extension which enables the use of euclidian operators on a matrix of sorts. As of writing this, the only supported use case is a 1xn matrix, since that is all we support in faces at the moment. For more information on cube see: https://www.postgresql.org/docs/9.6/cube.html.

func (*Cube) Scan

func (c *Cube) Scan(src interface{}) error

Scan converts the postgesql cube bytes into a valid cube type. This again was inspired by github.com/lib/pq.FloatArray type.

func (Cube) Value

func (c Cube) Value() (driver.Value, error)

Value is the cube value to convert the cube into the postgreql wire protocol. It is was inspired by the github.com/lib/pq.FloatArray type.

type DatabaseErr

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

DatabaseErr provides meaningful behavior to the postgres error.

func (*DatabaseErr) Conflict

func (e *DatabaseErr) Conflict() bool

Conflict returns where this error refers to the behavior of a resource that conflicts. At this time the conflict is determiend from a foreign key violation, but can be expanded in the future.

func (*DatabaseErr) Error

func (e *DatabaseErr) Error() string

Error returns the error string.

func (*DatabaseErr) Exists

func (e *DatabaseErr) Exists() bool

Exists returns whether this error refers to the behavior of a resource that already exists in the datastore and a violation is thrown. This will be true for a unique key violation in the PG store, but can be expanded in the future.

func (*DatabaseErr) NotFound

func (e *DatabaseErr) NotFound() bool

NotFound returns whether this error refers to the behavior of a resource that was not found.

type Equals

type Equals struct {
	Key string
	Val interface{}
}

Equals is a struct used to represent a part of a where clause

func (Equals) Clause

func (e Equals) Clause(rb Rebinder) (string, []interface{}, error)

Clause prints out a sql ready statement for the Rebinder to repackage.

type ExecRebinder

type ExecRebinder interface {
	Execer
	Rebinder
}

ExecRebinder is an interface that is agnostic for database transactions for the sql type, an execer can be a sqlx.DB, transactions for something custom.

type Execer

type Execer interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}

Execer provides the exec behavior.

type GetRebinder

type GetRebinder interface {
	Getter
	Rebinder
}

GetRebinder provides the get and rebinding functionality.

type Getter

type Getter interface {
	GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
}

Getter provides get functionality.

type In

type In struct {
	Key  string
	Vals []interface{}
}

In is a struct used to represent an IN clause

func (In) Clause

func (i In) Clause(rb Rebinder) (string, []interface{}, error)

Clause prints out a sql ready statement for the Rebinder to repackage.

type NameBinder

type NameBinder interface {
	BindNamed(query string, v interface{}) (bindedQuery string, args []interface{}, err error)
}

NameBinder preforms an operating that returns a pointer sql.Result.

type NamedExecer

type NamedExecer interface {
	NamedExecContext(ctx context.Context, query string, args interface{}) (sql.Result, error)
}

NamedExecer preforms an operating that returns an sql.Result and error.

type NamedGetBinder

type NamedGetBinder interface {
	Getter
	Rebinder
	NameBinder
}

NamedGetBinder allows you to use the GetContext query.

type NamedQuerier

type NamedQuerier interface {
	QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
	NameBinder
}

NamedQuerier allows you to use the named query arguments with a query.

type NamedRowQuerier

type NamedRowQuerier interface {
	QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row
	NameBinder
}

NamedRowQuerier allows you to use the named query arguments with a row query.

type OrderPair

type OrderPair struct {
	Col  string
	Desc bool
}

OrderPair is a type to declare a column to be ordered by and if it should be ASC or DESC.

type Preparer

type Preparer interface {
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

Preparer provides prepared statement behaviors.

type Querier

type Querier interface {
	QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
}

Querier preforms an operating that returns a pointer sql.Rows and possible error.

type QueryBinder

type QueryBinder interface {
	QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
	Rebinder
}

QueryBinder preforms an operating that returns a pointer sql.Result.

type QueryOption

type QueryOption interface {
	Clause() string
	Type() string
	// contains filtered or unexported methods
}

QueryOption is an interface around query options that can be of and order By, Limit or Offset.

func Limit

func Limit(lim int64) QueryOption

Limit returns a QueryOption that applies a LIMIT to the query.

func Offset

func Offset(o int64) QueryOption

Offset returns a QueryOption that applies an OFFSET to the query.

func OrderBy

func OrderBy(cols ...OrderPair) QueryOption

OrderBy returns a QueryOption that applies an ORDER BY to the query.

type QueryOptions

type QueryOptions []QueryOption

QueryOptions provides a means to turn a collection of Query Options into a sql safe clause.

func (QueryOptions) Clause

func (q QueryOptions) Clause() string

Clause prints out a sql ready statement for the Rebinder to repackage.

type Rebinder

type Rebinder interface {
	Rebind(string) string
}

Rebinder preforms a strings altering operation.

type RowQueryBinder

type RowQueryBinder interface {
	QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row
	Rebinder
}

RowQueryBinder preforms an operating that returns a pointer sql.Result.

type SelectBinder

type SelectBinder interface {
	Selecter
	Rebinder
}

SelectBinder preforms a query with a context.

type SelectGetter

type SelectGetter interface {
	Selecter
	Getter
}

SelectGetter combines Selecter and Getter interfaces.

type Selecter

type Selecter interface {
	SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
}

Selecter performs select behavior with contexts.

type Whereable

type Whereable interface {
	Clause(rebinder Rebinder) (query string, args []interface{}, err error)
}

Whereable is a way of creating a functional query statement for different lookups.

func All

func All() Whereable

All provides a nil that indicates no Where clause is provided.

func And

func And(first Whereable, second Whereable, rest ...Whereable) Whereable

And joins multiple Whereable clauses with AND operations.

func ByFieldEquals

func ByFieldEquals(field string, value interface{}) Whereable

ByFieldEquals creates a `{field} = ?` argument for a Where clause.

func ByFieldIn

func ByFieldIn(field string, values ...interface{}) Whereable

ByFieldIn creates a `{field} = (?, ?, ...)` argument for a Where clause.

func ByFieldNotEquals

func ByFieldNotEquals(field string, value interface{}) Whereable

ByFieldNotEquals creates a `{field} = ?` argument for a Where clause.

func ByID

func ByID(id interface{}) Whereable

ByID creates a `id = ?` argument for a Where clause.

func ByIDsIn

func ByIDsIn(ids ...string) Whereable

ByIDsIn provides a where clause with the IN syntax, matching many rows potentially.

func ByItemID

func ByItemID(id interface{}) Whereable

ByItemID creates a `id = ?` argument for a Where clause.

func ByItemIDsIn

func ByItemIDsIn(ids ...string) Whereable

ByItemIDsIn provides a where clause with the IN syntax, matching many rows potentially.

func ByLocationID

func ByLocationID(id interface{}) Whereable

ByLocationID creates a `location_id = ?` arugment for a Where clause.

func IDsNotNull

func IDsNotNull() Whereable

IDsNotNull returns all any non Null row by id.

func Like

func Like(column string, val string) Whereable

Like creates a where clause that uses a like matcher.

func Or

func Or(first Whereable, second Whereable, rest ...Whereable) Whereable

Or joins multiple Whereable clauses with OR operations.

Jump to

Keyboard shortcuts

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