2026-03-26 12:48:47 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
|
|
import "axolotl/models"
|
|
|
|
|
|
2026-03-27 02:11:46 +01:00
|
|
|
func (db *DB) AddRel(f, t string, r models.RelType) error {
|
|
|
|
|
_, err := db.Exec("INSERT OR IGNORE INTO rels (from_id, to_id, rel_type) VALUES (?, ?, ?)", f, t, r)
|
2026-03-26 12:48:47 +00:00
|
|
|
return err
|
|
|
|
|
}
|
2026-03-27 02:11:46 +01:00
|
|
|
func (db *DB) RemoveRel(f, t string, r models.RelType) error {
|
|
|
|
|
_, err := db.Exec("DELETE FROM rels WHERE from_id = ? AND to_id = ? AND rel_type = ?", f, t, r)
|
2026-03-26 12:48:47 +00:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 02:11:46 +01:00
|
|
|
func getIDs(db *DB, q, id string, r models.RelType) ([]string, error) {
|
|
|
|
|
rows, err := db.Query(q, id, r)
|
2026-03-26 12:48:47 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
var ids []string
|
|
|
|
|
for rows.Next() {
|
2026-03-27 02:11:46 +01:00
|
|
|
var i string
|
|
|
|
|
rows.Scan(&i)
|
|
|
|
|
ids = append(ids, i)
|
2026-03-26 12:48:47 +00:00
|
|
|
}
|
|
|
|
|
return ids, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 02:11:46 +01:00
|
|
|
func (db *DB) GetRelated(id string, r models.RelType) ([]string, error) {
|
|
|
|
|
return getIDs(db, "SELECT to_id FROM rels WHERE from_id = ? AND rel_type = ?", id, r)
|
|
|
|
|
}
|
|
|
|
|
func (db *DB) GetIncomingRels(id string, r models.RelType) ([]string, error) {
|
|
|
|
|
return getIDs(db, "SELECT from_id FROM rels WHERE to_id = ? AND rel_type = ?", id, r)
|
2026-03-26 12:48:47 +00:00
|
|
|
}
|