elvish/store/sqlutil.go
2016-02-13 22:30:48 +08:00

33 lines
616 B
Go

package store
import "database/sql"
func hasColumn(rows *sql.Rows, colname string) (bool, error) {
cols, err := rows.Columns()
if err != nil {
return false, err
}
for _, col := range cols {
if col == colname {
return true, nil
}
}
return false, rows.Err()
}
// transaction creates a Tx and calls f on it. It commits or rollbacks the
// transaction depending on whether f suceeded.
func transaction(db *sql.DB, f func(*sql.Tx) error) error {
tx, err := db.Begin()
if err != nil {
return err
}
err = f(tx)
if err != nil {
return tx.Rollback()
} else {
return tx.Commit()
}
return err
}