diff --git a/activity.go b/activity.go index 4f20e1b..9788569 100644 --- a/activity.go +++ b/activity.go @@ -344,9 +344,9 @@ func getboxes(ident string) (*Box, error) { return b, nil } - row := stmtGetBoxes.QueryRow(ident) - b = &Box{} - err := row.Scan(&b.In, &b.Out, &b.Shared) + var info string + row := stmtGetXonker.QueryRow(ident, "boxes") + err := row.Scan(&info) if err != nil { j, err := GetJunk(ident) if err != nil { @@ -357,12 +357,17 @@ func getboxes(ident string) (*Box, error) { sbox, _ := jsonfindstring(j, []string{"endpoints", "sharedInbox"}) b = &Box{In: inbox, Out: outbox, Shared: sbox} if inbox != "" { - _, err = stmtSaveBoxes.Exec(ident, inbox, outbox, sbox, "") + m := strings.Join([]string{inbox, outbox, sbox}, " ") + _, err = stmtSaveXonker.Exec(ident, m, "boxes") if err != nil { log.Printf("error saving boxes: %s", err) } } + } else { + m := strings.Split(info, " ") + b = &Box{In: m[0], Out: m[1], Shared: m[2]} } + boxlock.Lock() boxofboxes[ident] = b boxlock.Unlock() @@ -889,8 +894,7 @@ func gofish(name string) string { if ok { return ref } - db := opendatabase() - row := db.QueryRow("select ibox from xonkers where xid = ?", name) + row := stmtGetXonker.QueryRow(name, "fishname") var href string err := row.Scan(&href) if err == nil { @@ -914,8 +918,7 @@ func gofish(name string) string { rel, _ := jsongetstring(l, "rel") t, _ := jsongetstring(l, "type") if rel == "self" && friendorfoe(t) { - db.Exec("insert into xonkers (xid, ibox, obox, sbox, pubkey) values (?, ?, ?, ?, ?)", - name, href, "", "", "") + stmtSaveXonker.Exec(name, href, "fishname") handlock.Lock() handfull[name] = href handlock.Unlock() diff --git a/fun.go b/fun.go index 5d2c7cf..2991304 100644 --- a/fun.go +++ b/fun.go @@ -394,8 +394,7 @@ func zaggy(keyname string) (key *rsa.PublicKey) { if key != nil { return } - db := opendatabase() - row := db.QueryRow("select pubkey from xonkers where xid = ?", keyname) + row := stmtGetXonker.QueryRow(keyname, "pubkey") var data string err := row.Scan(&data) if err != nil { @@ -421,7 +420,7 @@ func zaggy(keyname string) (key *rsa.PublicKey) { log.Printf("error decoding %s pubkey: %s", keyname, err) return } - _, err = stmtSaveBoxes.Exec(keyname, "", "", "", data) + _, err = stmtSaveXonker.Exec(keyname, data, "pubkey") if err != nil { log.Printf("error saving key: %s", err) } diff --git a/honk.go b/honk.go index 2b3c3e1..e89bb5c 100644 --- a/honk.go +++ b/honk.go @@ -1354,7 +1354,7 @@ var stmtHonksByHonker, stmtSaveHonk, stmtFileData, stmtWhatAbout *sql.Stmt var stmtFindXonk, stmtSaveDonk, stmtFindFile, stmtSaveFile *sql.Stmt var stmtAddDoover, stmtGetDoovers, stmtLoadDoover, stmtZapDoover *sql.Stmt var stmtHasHonker, stmtThumbBiters, stmtZonkIt, stmtZonkDonks, stmtSaveZonker *sql.Stmt -var stmtGetBoxes, stmtSaveBoxes *sql.Stmt +var stmtGetXonker, stmtSaveXonker *sql.Stmt func preparetodie(db *sql.DB, s string) *sql.Stmt { stmt, err := db.Prepare(s) @@ -1400,8 +1400,8 @@ func prepareStatements(db *sql.DB) { stmtZapDoover = preparetodie(db, "delete from doovers where dooverid = ?") stmtThumbBiters = preparetodie(db, "select userid, name, wherefore from zonkers where (wherefore = 'zonker' or wherefore = 'zurl' or wherefore = 'zword')") 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 (?, ?, ?, ?, ?)") + stmtGetXonker = preparetodie(db, "select info from xonkers where name = ? and flavor = ?") + stmtSaveXonker = preparetodie(db, "insert into xonkers (name, info, flavor) values (?, ?, ?)") } func ElaborateUnitTests() { diff --git a/schema.sql b/schema.sql index c899e16..f7639b5 100644 --- a/schema.sql +++ b/schema.sql @@ -3,7 +3,7 @@ create table honks (honkid integer primary key, userid integer, what text, honke create table donks (honkid integer, fileid integer); create table files(fileid integer primary key, xid text, name text, url text, media text, local integer, content blob); create table honkers (honkerid integer primary key, userid integer, name text, xid text, flavor text, combos text, pubkey text); -create table xonkers (xonkerid integer primary key, xid text, ibox text, obox text, sbox text, pubkey text); +create table xonkers (xonkerid integer primary key, name text, info text, flavor text); create table zonkers (zonkerid integer primary key, userid integer, name text, wherefore text); create table doovers(dooverid integer primary key, dt text, tries integer, username text, rcpt text, msg blob); @@ -11,7 +11,7 @@ create index idx_honksxid on honks(xid); create index idx_honksconvoy on honks(convoy); create index idx_honkshonker on honks(honker); create index idx_honkerxid on honkers(xid); -create index idx_xonkerxid on xonkers(xid); +create index idx_xonkername on xonkers(name); create index idx_zonkersname on zonkers(name); create index idx_filesxid on files(xid); create index idx_filesurl on files(url); diff --git a/upgradedb.go b/upgradedb.go index d580c9c..0ce1636 100644 --- a/upgradedb.go +++ b/upgradedb.go @@ -93,6 +93,11 @@ func upgradedb() { doordie(db, "update files set local = 1") doordie(db, "update config set value = 9 where key = 'dbversion'") case 9: + doordie(db, "drop table xonkers") + doordie(db, "create table xonkers (xonkerid integer primary key, name text, info text, flavor text)") + doordie(db, "create index idx_xonkername on xonkers(name)") + doordie(db, "update config set value = 10 where key = 'dbversion'") + case 10: default: log.Fatalf("can't upgrade unknown version %d", dbversion) } diff --git a/util.go b/util.go index aa67d51..23fb013 100644 --- a/util.go +++ b/util.go @@ -71,7 +71,7 @@ var dbtimeformat = "2006-01-02 15:04:05" var alreadyopendb *sql.DB var dbname = "honk.db" var stmtConfig *sql.Stmt -var myVersion = 9 +var myVersion = 10 func initdb() { schema, err := ioutil.ReadFile("schema.sql")