combine more row scanning

This commit is contained in:
Ted Unangst 2019-09-16 16:51:47 -04:00
parent 6a68d43997
commit 11721b9ec2
1 changed files with 29 additions and 37 deletions

View File

@ -90,35 +90,14 @@ func allusers() []login.UserInfo {
return users return users
} }
func scanxonk(row *sql.Row) *Honk {
h := new(Honk)
var dt, aud, onts string
err := row.Scan(&h.ID, &h.UserID, &h.Username, &h.What, &h.Honker, &h.Oonker, &h.XID, &h.RID,
&dt, &h.URL, &aud, &h.Noise, &h.Precis, &h.Convoy, &h.Whofore, &h.Flags, &onts)
if err != nil {
if err != sql.ErrNoRows {
log.Printf("error scanning xonk: %s", err)
}
return nil
}
h.Date, _ = time.Parse(dbtimeformat, dt)
h.Audience = strings.Split(aud, " ")
h.Public = !keepitquiet(h.Audience)
if len(onts) > 0 {
h.Onts = strings.Split(onts, " ")
}
return h
}
func getxonk(userid int64, xid string) *Honk { func getxonk(userid int64, xid string) *Honk {
row := stmtOneXonk.QueryRow(userid, xid) row := stmtOneXonk.QueryRow(userid, xid)
return scanxonk(row) return scanhonk(row)
} }
func getbonk(userid int64, xid string) *Honk { func getbonk(userid int64, xid string) *Honk {
row := stmtOneBonk.QueryRow(userid, xid) row := stmtOneBonk.QueryRow(userid, xid)
return scanxonk(row) return scanhonk(row)
} }
func getpublichonks() []*Honk { func getpublichonks() []*Honk {
@ -186,12 +165,29 @@ func getsomehonks(rows *sql.Rows, err error) []*Honk {
defer rows.Close() defer rows.Close()
var honks []*Honk var honks []*Honk
for rows.Next() { for rows.Next() {
var h Honk h := scanhonk(rows)
if h != nil {
honks = append(honks, h)
}
}
rows.Close()
donksforhonks(honks)
return honks
}
type RowLike interface {
Scan(dest ...interface{}) error
}
func scanhonk(row RowLike) *Honk {
h := new(Honk)
var dt, aud, onts string var dt, aud, onts string
err = rows.Scan(&h.ID, &h.UserID, &h.Username, &h.What, &h.Honker, &h.Oonker, &h.XID, err := row.Scan(&h.ID, &h.UserID, &h.Username, &h.What, &h.Honker, &h.Oonker, &h.XID, &h.RID,
&h.RID, &dt, &h.URL, &aud, &h.Noise, &h.Precis, &h.Convoy, &h.Whofore, &h.Flags, &onts) &dt, &h.URL, &aud, &h.Noise, &h.Precis, &h.Convoy, &h.Whofore, &h.Flags, &onts)
if err != nil { if err != nil {
log.Printf("error scanning honks: %s", err) if err != sql.ErrNoRows {
log.Printf("error scanning honk: %s", err)
}
return nil return nil
} }
h.Date, _ = time.Parse(dbtimeformat, dt) h.Date, _ = time.Parse(dbtimeformat, dt)
@ -200,11 +196,7 @@ func getsomehonks(rows *sql.Rows, err error) []*Honk {
if len(onts) > 0 { if len(onts) > 0 {
h.Onts = strings.Split(onts, " ") h.Onts = strings.Split(onts, " ")
} }
honks = append(honks, &h) return h
}
rows.Close()
donksforhonks(honks)
return honks
} }
func donksforhonks(honks []*Honk) { func donksforhonks(honks []*Honk) {