upsert

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

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

Go to latest
Published: Dec 6, 2016 License: MIT Imports: 8 Imported by: 2

README

upsert

An obsolete Go library for upserting rows in PostgreSQL <= 9.4 via sqlx.

GoDoc Build Status

Requirements

Documentation

Index

Constants

View Source
const (
	Inserted = iota
	Updated
	NoChange
)

Variables

View Source
var (
	ErrNoIDReturned = errors.New("no id returned")

	// LongQuery will log long queries if set to a non-zero time
	LongQuery time.Duration
	Debug     = false
)

Functions

func Delete

func Delete(ext sqlx.Ext, u Upserter) (err error)

func Insert

func Insert(ext sqlx.Ext, u Upserter) (err error)

Insert takes either an sqlx.DB or sqlx.Tx as ext, along with a value that implements the Upserter() interface. We attempt to insert it and set its primary key id value.

func Update

func Update(ext sqlx.Ext, u Upserter) (status int, err error)

func Upsert

func Upsert(ext sqlx.Ext, u Upserter) (status int, err error)

Upsert takes either an sqlx.DB or sqlx.Tx as ext, along with a value that implements the Upserter() interface. We attempt to insert/update it and set the new primary key id if that succeeds. inserted returns true if a new row was inserted. The client is responsible for wrapping in a transaction when needed. This can be used when running a transaction at a higher level (upserting multiple items).

func UpsertTx

func UpsertTx(db *sqlx.DB, u Upserter) (status int, err error)

UpsertTx takes only an sqlx.DB and wraps the upsert attempt into a a transaction.

Types

type Upserter

type Upserter interface {
	// Table returns table name we should save to
	Table() string
}

Upserter is an interface specific to sqlx and PostgreSQL that can save a single row of data via Upsert(), Update() or Insert(). It doesn't try to know anything about relationships between tables. The behavior of Upserter depends on three struct tags.

  • db: As with sqlx, this tag is the database column name for the field. If db is not defined, the default is the lowercase value of the field name.

  • upsert: This may either be "key" or "omit". If it's "key", the field/column is part of the where clause when attempting to update an existing column. If it's "omit", the field is ignored completely. By default, the field is considered a non-key value that should be updated/set in the db.

  • upsert_value: This is the placeholder for the value of the field for use by sqlx.NamedExec(). By default, this is :column_name and typically doesn't need to be changed. However, if the field needs to be transformed by an SQL function before storing in the database, this tag can be set. For example, if you had "lat" and "lon" columns, you wouldn't want to store them in the db. Instead you'd want a "location" column tagged with `upsert_value:"ll_to_earth(:lat, :lon)`

Jump to

Keyboard shortcuts

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