Skip to content

Instantly share code, notes, and snippets.

@tonyhb
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonyhb/581114ae710dcf3a9369 to your computer and use it in GitHub Desktop.
Save tonyhb/581114ae710dcf3a9369 to your computer and use it in GitHub Desktop.
Exposing atomic and transactional database methods
package models
import (
"github.com/jmoiron/sqlx"
"github.com/tonyhb/govalidate"
)
type Page struct {
Id int64
Name string
}
func (p *Page) Create() (err error) {
var tx *sqlx.Tx
// DB() returns a *sqlx.DB
if tx, err = DB().Beginx(); err != nil {
return err
}
if err = p.CreateTx(tx); err != nil {
tx.Rollback()
return
}
return tx.Commit()
}
func (p *Page) CreateTx(tx *sqlx.Tx) (err error) {
if err = validate.Run(p); err != nil {
return
}
// We could also use Modl to wrap the transaction and insert this record.
res, err := tx.NamedExec(`INSERT INTO pages (name) VALUES (:name)`, p)
if err != nil {
return
}
p.Id, err = res.LastInsertId()
return
}
// Ideally all models will implement the same CreateTx format allowing us to
// create an interface:
//
// type Createable interface {
// CreateTx(*sqlx.Tx) error
// }
//
// We can then abstract the Create() call from each of our types into its own
// function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment