本文整理匯總了Golang中github.com/coopernurse/gorp.DbMap.Begin方法的典型用法代碼示例。如果您正苦於以下問題:Golang DbMap.Begin方法的具體用法?Golang DbMap.Begin怎麽用?Golang DbMap.Begin使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/coopernurse/gorp.DbMap
的用法示例。
在下文中一共展示了DbMap.Begin方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Transact
func Transact(dbm *gorp.DbMap, f func(gorp.SqlExecutor) error) error {
txn, err := dbm.Begin()
if err != nil {
return err
}
defer func() {
if txn == nil {
return
}
if err := txn.Rollback(); err != nil && err != sql.ErrTxDone {
panic(err)
}
}()
err = f(txn)
if err != nil {
return err
}
err = txn.Commit()
if err != nil && err != sql.ErrTxDone {
return err
}
txn = nil
return nil
}
示例2: AddImages
func (n New) AddImages(db *gorp.DbMap, iids []int64) error {
n.GetImages(db)
for _, v := range n.Images {
for _, j := range iids {
if v == j {
return errors.New("Image already taken")
}
}
}
tx, err := db.Begin()
if err != nil {
return err
}
for _, v := range iids {
NewImage := NewImages{
NewId: n.Id,
ImageId: v,
}
err := tx.Insert(&NewImage)
if err != nil {
tx.Rollback()
return err
}
}
tx.Commit()
return nil
}
示例3: CreateOrder
func CreateOrder(db *gorp.DbMap, customer, storeid, deliveryid int64, phone,
address string, products []OrderProduct, price float64) (Order, error) {
t := time.Now().UnixNano()
o := Order{
CustomerId: customer,
StoreId: storeid,
DeliveryId: deliveryid,
Phone: phone,
Price: price,
Address: address,
Created: t,
Updated: t,
}
tx, err := db.Begin()
if err != nil {
return o, err
}
err = tx.Insert(&o)
if err != nil {
tx.Rollback()
return o, err
}
for _, v := range products {
v.OrderId = o.Id
err := tx.Insert(&v)
if err != nil {
tx.Rollback()
return o, err
}
}
tx.Commit()
o.Products = products
return o, err
}