From abbbc3f6e3112c52efa41e324086ba02be7c6614 Mon Sep 17 00:00:00 2001 From: Shengjing Zhu Date: Sat, 13 Feb 2016 20:51:36 +0800 Subject: [PATCH] clean up sql code --- store/cmd_hist.go | 7 +++++-- store/sqlutil.go | 19 ++++--------------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/store/cmd_hist.go b/store/cmd_hist.go index d62cae66..c1754a3c 100644 --- a/store/cmd_hist.go +++ b/store/cmd_hist.go @@ -21,14 +21,17 @@ func init() { func addLastAmongDup(tx *sql.Tx) error { // Upgrade from early version where lastAmongDup is missing. - rows, err := tx.Query("pragma table_info(cmd)") + rows, err := tx.Query("select * from cmd limit 1") if err != nil { return err } + defer rows.Close() + hasLastAmongDup, err := hasColumn(rows, "lastAmongDup") if err != nil { return err } + if !hasLastAmongDup { _, err := tx.Exec("alter table cmd add column lastAmongDup bool") if err != nil { @@ -39,7 +42,7 @@ func addLastAmongDup(tx *sql.Tx) error { return err } } - return tx.Commit() + return nil } // NextCmdSeq returns the next sequence number of the command history. diff --git a/store/sqlutil.go b/store/sqlutil.go index ff9a934f..e612ba0d 100644 --- a/store/sqlutil.go +++ b/store/sqlutil.go @@ -7,19 +7,8 @@ func hasColumn(rows *sql.Rows, colname string) (bool, error) { if err != nil { return false, err } - dests := make([]interface{}, len(cols)) - var name string - for i, col := range cols { - if col == "name" { - dests[i] = &name - } else { - dests[i] = new(interface{}) - } - } - defer rows.Close() - for rows.Next() { - rows.Scan(dests...) - if name == colname { + for _, col := range cols { + if col == colname { return true, nil } } @@ -35,9 +24,9 @@ func transaction(db *sql.DB, f func(*sql.Tx) error) error { } err = f(tx) if err != nil { - tx.Rollback() + return tx.Rollback() } else { - tx.Commit() + return tx.Commit() } return err }