go full cte for the thread query
Ted Unangst tedu@tedunangst.com
Mon, 11 Mar 2024 21:51:00 -0400
1 files changed,
11 insertions(+),
31 deletions(-)
jump to
M
database.go
→
database.go
@@ -300,36 +300,8 @@ rows, err := stmtHonksByCombo.Query(wanted, userid, userid, combo, userid, wanted, userid, combo, userid)
return getsomehonks(rows, err) } func gethonksbyconvoy(userid UserID, convoy string, wanted int64) []*Honk { - rows, err := stmtHonksByConvoy.Query(wanted, userid, userid, convoy) - honks := getsomehonks(rows, err) - seen := make(map[string]bool, len(honks)) - for _, h := range honks { - seen[h.XID] = true - } - var missing []string - for _, h := range honks { - if h.RID != "" && !seen[h.RID] { - missing = append(missing, h.RID) - } - } - for len(missing) > 0 { - dlog.Printf("missing honks: %v", missing) - nextround := missing - missing = nil - for _, xid := range nextround { - seen[xid] = true - } - for _, xid := range nextround { - h := getxonk(userid, xid) - if h != nil { - honks = append(honks, h) - if h.RID != "" && !seen[h.RID] { - missing = append(missing, h.RID) - } - } - } - } - return honks + rows, err := stmtHonksByConvoy.Query(convoy, wanted, userid) + return getsomehonks(rows, err) } func gethonksbysearch(userid UserID, q string, wanted int64) []*Honk { var queries []string@@ -1306,7 +1278,15 @@ stmtHonksISaved = preparetodie(db, selecthonks+"where honks.honkid > ? and honks.userid = ? and flags & 4 order by honks.honkid desc")
stmtHonksByHonker = preparetodie(db, selecthonks+"join honkers on (honkers.xid = honks.honker or honkers.xid = honks.oonker) where honks.honkid > ? and honks.userid = ? and honkers.name = ?"+butnotthose+limit) stmtHonksByXonker = preparetodie(db, selecthonks+" where honks.honkid > ? and honks.userid = ? and (honker = ? or oonker = ?)"+butnotthose+limit) stmtHonksByCombo = preparetodie(db, selecthonks+" where honks.honkid > ? and honks.userid = ? and honks.honker in (select xid from honkers where honkers.userid = ? and honkers.combos like ?) "+butnotthose+" union "+selecthonks+"join onts on honks.honkid = onts.honkid where honks.honkid > ? and honks.userid = ? and onts.ontology in (select xid from honkers where combos like ?)"+butnotthose+limit) - stmtHonksByConvoy = preparetodie(db, selecthonks+"where honks.honkid > ? and (honks.userid = ? or (? = -1 and whofore = 2)) and convoy = ?"+limit) + stmtHonksByConvoy = preparetodie(db, `with recursive getthread(x, c) as ( + values('', ?) + union + select xid, convoy from honks, getthread where honks.convoy = getthread.c + union + select xid, convoy from honks, getthread where honks.rid <> '' and honks.rid = getthread.x + union + select rid, convoy from honks, getthread where honks.xid = getthread.x and rid <> '' + ) `+selecthonks+"where honks.honkid > ? and honks.userid = ? and xid in (select x from getthread)"+limit) stmtHonksByOntology = preparetodie(db, selecthonks+"join onts on honks.honkid = onts.honkid where honks.honkid > ? and onts.ontology = ? and (honks.userid = ? or (? = -1 and honks.whofore = 2))"+limit) stmtSaveMeta = preparetodie(db, "insert into honkmeta (honkid, genus, json) values (?, ?, ?)")