convert a few one off db queries to statements

This commit is contained in:
Ted Unangst 2019-04-28 22:48:46 -04:00
parent cd7626a515
commit 370268399d
2 changed files with 34 additions and 17 deletions

View File

@ -327,9 +327,7 @@ func getboxes(ident string) (*Box, error) {
return b, nil return b, nil
} }
db := opendatabase() row := stmtGetBoxes.QueryRow(ident)
row := db.QueryRow("select ibox, obox, sbox from xonkers where xid = ?", ident)
b = &Box{} b = &Box{}
err := row.Scan(&b.In, &b.Out, &b.Shared) err := row.Scan(&b.In, &b.Out, &b.Shared)
if err != nil { if err != nil {
@ -342,8 +340,10 @@ func getboxes(ident string) (*Box, error) {
sbox, _ := jsonfindstring(j, []string{"endpoints", "sharedInbox"}) sbox, _ := jsonfindstring(j, []string{"endpoints", "sharedInbox"})
b = &Box{In: inbox, Out: outbox, Shared: sbox} b = &Box{In: inbox, Out: outbox, Shared: sbox}
if inbox != "" { if inbox != "" {
db.Exec("insert into xonkers (xid, ibox, obox, sbox, pubkey) values (?, ?, ?, ?, ?)", _, err = stmtSaveBoxes.Exec(ident, inbox, outbox, sbox, "")
ident, inbox, outbox, sbox, "") if err != nil {
log.Printf("error saving boxes: %s", err)
}
} }
} }
boxlock.Lock() boxlock.Lock()

41
honk.go
View File

@ -344,9 +344,12 @@ func inbox(w http.ResponseWriter, r *http.Request) {
log.Printf("can't follow %s", obj) log.Printf("can't follow %s", obj)
} }
case "Accept": case "Accept":
db := opendatabase()
log.Printf("updating honker accept: %s", who) log.Printf("updating honker accept: %s", who)
db.Exec("update honkers set flavor = 'sub' where userid = ? and xid = ? and flavor = 'presub'", user.ID, who) _, err = stmtUpdateFlavor.Exec("sub", user.ID, who, "presub")
if err != nil {
log.Printf("error updating honker: %s", err)
return
}
case "Undo": case "Undo":
obj, ok := jsongetmap(j, "object") obj, ok := jsongetmap(j, "object")
if !ok { if !ok {
@ -356,8 +359,11 @@ func inbox(w http.ResponseWriter, r *http.Request) {
switch what { switch what {
case "Follow": case "Follow":
log.Printf("updating honker undo: %s", who) log.Printf("updating honker undo: %s", who)
db := opendatabase() _, err = stmtUpdateFlavor.Exec("undub", user.ID, who, "dub")
db.Exec("update honkers set flavor = 'undub' where userid = ? and xid = ? and flavor = 'dub'", user.ID, who) if err != nil {
log.Printf("error updating honker: %s", err)
return
}
case "Like": case "Like":
default: default:
log.Printf("unknown undo: %s", what) log.Printf("unknown undo: %s", what)
@ -809,9 +815,11 @@ func zonkit(w http.ResponseWriter, r *http.Request) {
if wherefore == "zonk" { if wherefore == "zonk" {
stmtZonkIt.Exec(userinfo.UserID, what) stmtZonkIt.Exec(userinfo.UserID, what)
} else { } else {
db := opendatabase() _, err := stmtSaveZonker.Exec(userinfo.UserID, what, wherefore)
db.Exec("insert into zonkers (userid, name, wherefore) values (?, ?, ?)", if err != nil {
userinfo.UserID, what, wherefore) log.Printf("error saving zonker: %s", err)
return
}
} }
} }
@ -1032,14 +1040,18 @@ func savehonker(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
log.Printf("can't take it back: %s", err) log.Printf("can't take it back: %s", err)
} else { } else {
db.Exec("update honkers set flavor = 'unsub' where honkerid = ?", honkerid) _, err = stmtUpdateFlavor.Exec("unsub", u.UserID, xid, "sub")
if err != nil {
log.Printf("error updating honker: %s", err)
return
}
} }
http.Redirect(w, r, "/honkers", http.StatusSeeOther) http.Redirect(w, r, "/honkers", http.StatusSeeOther)
return return
} }
combos = " " + strings.TrimSpace(combos) + " " combos = " " + strings.TrimSpace(combos) + " "
_, err := stmtUpdateHonker.Exec(combos, honkerid, u.UserID) _, err := stmtUpdateCombos.Exec(combos, honkerid, u.UserID)
if err != nil { if err != nil {
log.Printf("update honker err: %s", err) log.Printf("update honker err: %s", err)
return return
@ -1258,13 +1270,14 @@ func serve() {
} }
} }
var stmtHonkers, stmtDubbers, stmtSaveHonker, stmtUpdateHonker *sql.Stmt var stmtHonkers, stmtDubbers, stmtSaveHonker, stmtUpdateFlavor, stmtUpdateCombos *sql.Stmt
var stmtOneXonk, stmtPublicHonks, stmtUserHonks, stmtHonksByCombo, stmtHonksByConvoy *sql.Stmt var stmtOneXonk, stmtPublicHonks, stmtUserHonks, stmtHonksByCombo, stmtHonksByConvoy *sql.Stmt
var stmtHonksForUser, stmtHonksForMe, stmtDeleteHonk, stmtSaveDub *sql.Stmt var stmtHonksForUser, stmtHonksForMe, stmtDeleteHonk, stmtSaveDub *sql.Stmt
var stmtHonksByHonker, stmtSaveHonk, stmtFileData, stmtWhatAbout *sql.Stmt var stmtHonksByHonker, stmtSaveHonk, stmtFileData, stmtWhatAbout *sql.Stmt
var stmtFindXonk, stmtSaveDonk, stmtFindFile, stmtSaveFile *sql.Stmt var stmtFindXonk, stmtSaveDonk, stmtFindFile, stmtSaveFile *sql.Stmt
var stmtAddDoover, stmtGetDoovers, stmtLoadDoover, stmtZapDoover *sql.Stmt var stmtAddDoover, stmtGetDoovers, stmtLoadDoover, stmtZapDoover *sql.Stmt
var stmtHasHonker, stmtThumbBiters, stmtZonkIt *sql.Stmt var stmtHasHonker, stmtThumbBiters, stmtZonkIt, stmtSaveZonker *sql.Stmt
var stmtGetBoxes, stmtSaveBoxes *sql.Stmt
func preparetodie(db *sql.DB, s string) *sql.Stmt { func preparetodie(db *sql.DB, s string) *sql.Stmt {
stmt, err := db.Prepare(s) stmt, err := db.Prepare(s)
@ -1277,7 +1290,8 @@ func preparetodie(db *sql.DB, s string) *sql.Stmt {
func prepareStatements(db *sql.DB) { func prepareStatements(db *sql.DB) {
stmtHonkers = preparetodie(db, "select honkerid, userid, name, xid, flavor, combos from honkers where userid = ? and (flavor = 'sub' or flavor = 'peep' or flavor = 'unsub') order by name") stmtHonkers = preparetodie(db, "select honkerid, userid, name, xid, flavor, combos from honkers where userid = ? and (flavor = 'sub' or flavor = 'peep' or flavor = 'unsub') order by name")
stmtSaveHonker = preparetodie(db, "insert into honkers (userid, name, xid, flavor, combos) values (?, ?, ?, ?, ?)") stmtSaveHonker = preparetodie(db, "insert into honkers (userid, name, xid, flavor, combos) values (?, ?, ?, ?, ?)")
stmtUpdateHonker = preparetodie(db, "update honkers set combos = ? where honkerid = ? and userid = ?") stmtUpdateFlavor = preparetodie(db, "update honkers set flavor = ? where userid = ? and xid = ? and flavor = ?")
stmtUpdateCombos = preparetodie(db, "update honkers set combos = ? where honkerid = ? and userid = ?")
stmtHasHonker = preparetodie(db, "select honkerid from honkers where xid = ? and userid = ?") stmtHasHonker = preparetodie(db, "select honkerid from honkers where xid = ? and userid = ?")
stmtDubbers = preparetodie(db, "select honkerid, userid, name, xid, flavor from honkers where userid = ? and flavor = 'dub'") stmtDubbers = preparetodie(db, "select honkerid, userid, name, xid, flavor from honkers where userid = ? and flavor = 'dub'")
@ -1308,6 +1322,9 @@ func prepareStatements(db *sql.DB) {
stmtZapDoover = preparetodie(db, "delete from doovers where dooverid = ?") stmtZapDoover = preparetodie(db, "delete from doovers where dooverid = ?")
stmtZonkIt = preparetodie(db, "update honks set what = 'zonk' where userid = ? and xid = ?") stmtZonkIt = preparetodie(db, "update honks set what = 'zonk' where userid = ? and xid = ?")
stmtThumbBiters = preparetodie(db, "select userid, name, wherefore from zonkers where (wherefore = 'zonker' or wherefore = 'zurl')") stmtThumbBiters = preparetodie(db, "select userid, name, wherefore from zonkers where (wherefore = 'zonker' or wherefore = 'zurl')")
stmtSaveZonker = preparetodie(db, "insert into zonkers (userid, name, wherefore) values (?, ?, ?)")
stmtGetBoxes = preparetodie(db, "select ibox, obox, sbox from xonkers where xid = ?")
stmtSaveBoxes = preparetodie(db, "insert into xonkers (xid, ibox, obox, sbox, pubkey) values (?, ?, ?, ?, ?)")
} }
func ElaborateUnitTests() { func ElaborateUnitTests() {