all repos — honk @ 2f24470a7751a2062381ef79e5f0a366abdb1f87

my fork of honk

combine more row scanning
Ted Unangst tedu@tedunangst.com
Mon, 16 Sep 2019 16:51:47 -0400
commit

2f24470a7751a2062381ef79e5f0a366abdb1f87

parent

379ea486c1cc0408b2c9602bb6fe60b1972103b0

1 files changed, 29 insertions(+), 37 deletions(-)

jump to
M database.godatabase.go

@@ -90,35 +90,14 @@ }

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 { row := stmtOneXonk.QueryRow(userid, xid) - return scanxonk(row) + return scanhonk(row) } func getbonk(userid int64, xid string) *Honk { row := stmtOneBonk.QueryRow(userid, xid) - return scanxonk(row) + return scanhonk(row) } func getpublichonks() []*Honk {

@@ -186,25 +165,38 @@ }

defer rows.Close() var honks []*Honk for rows.Next() { - var h Honk - var dt, aud, onts string - err = rows.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 { - log.Printf("error scanning honks: %s", err) - return nil + h := scanhonk(rows) + if h != nil { + honks = append(honks, h) } - 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, " ") - } - 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 + 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 honk: %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 donksforhonks(honks []*Honk) {