mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-04 10:57:50 +08:00
33 lines
616 B
Go
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
|
|
}
|