Documentation ¶
Index ¶
- Constants
- Variables
- func Delete(ext sqlx.Ext, u Upserter) (err error)
- func Insert(ext sqlx.Ext, u Upserter) (err error)
- func Update(ext sqlx.Ext, u Upserter) (status int, err error)
- func Upsert(ext sqlx.Ext, u Upserter) (status int, err error)
- func UpsertTx(db *sqlx.DB, u Upserter) (status int, err error)
- type Upserter
Constants ¶
const ( Inserted = iota Updated NoChange )
Variables ¶
Functions ¶
func Insert ¶
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 Upsert ¶
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).
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)`