Merge pull request #123 from zhsj/clean-sql

Clean up SQL-related code.
This commit is contained in:
Qi Xiao 2016-02-13 15:35:01 +01:00
commit ac0c7b4561
2 changed files with 9 additions and 17 deletions

View File

@ -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.

View File

@ -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
}