mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-05 03:17:50 +08:00
clean up sql code
This commit is contained in:
parent
d7cbcffdbf
commit
abbbc3f6e3
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user