package db import "tangled.org/core/deliberi/models" func PutRepoName(e Execer, repoDid, ownerDid, name string) error { _, err := e.Exec( `insert into repo_names (repo_did, name, owner_did) values (?, ?, ?) on conflict(repo_did) do update set name = excluded.name, owner_did = excluded.owner_did`, repoDid, name, ownerDid, ) return err } func GetRepoName(e Execer, repoDid string) string { var name string _ = e.QueryRow(`select name from repo_names where repo_did = ?`, repoDid).Scan(&name) return name } func GetRepoOwner(e Execer, repoDid string) string { var owner string _ = e.QueryRow(`select owner_did from repo_names where repo_did = ?`, repoDid).Scan(&owner) return owner } type Entity struct { Title string RepoDid string Kind models.EntityKind State string // who opened it. any non-empty value overwrites the stored one, so never pass the latest editor Author string Cid string } // empty fields keep the stored value, title always overwrites func PutEntity(e Execer, atUri string, ent Entity) error { _, err := e.Exec( `insert into entity_titles (at_uri, title, repo_did, kind, state, author_did, cid) values (?, ?, ?, ?, ?, ?, ?) on conflict(at_uri) do update set title = excluded.title, repo_did = case when excluded.repo_did != '' then excluded.repo_did else entity_titles.repo_did end, kind = case when excluded.kind != '' then excluded.kind else entity_titles.kind end, state = case when excluded.state != '' then excluded.state else entity_titles.state end, author_did = case when excluded.author_did != '' then excluded.author_did else entity_titles.author_did end, cid = case when excluded.cid != '' then excluded.cid else entity_titles.cid end`, atUri, ent.Title, ent.RepoDid, string(ent.Kind), ent.State, ent.Author, ent.Cid, ) return err } func GetEntity(e Execer, atUri string) Entity { var ent Entity var kind string _ = e.QueryRow(`select title, repo_did, kind, state, author_did, cid from entity_titles where at_uri = ?`, atUri). Scan(&ent.Title, &ent.RepoDid, &kind, &ent.State, &ent.Author, &ent.Cid) ent.Kind = models.EntityKind(kind) return ent } func DeleteEntity(e Execer, atUri string) error { _, err := e.Exec(`delete from entity_titles where at_uri = ?`, atUri) return err }