No more GORM.

Too much magic. I'll just write a JOIN and create small, composable data
types to represent my domain objects.

grug brain smile, complexity demon no visit today
main
Nick Dumas 2 years ago
parent 4fec3f685b
commit d9671822b7

@ -8,42 +8,27 @@ import (
) )
func main() { func main() {
// log.Println("entering main")
var ( var (
mapDB string mapDB string
) )
flag.StringVar(&mapDB, "map", "Aardwolf.db", "name of aardwolf map file") flag.StringVar(&mapDB, "map", "Aardwolf.db", "name of aardwolf map file")
// log.Println("parsing flags?")
flag.Parse() flag.Parse()
// log.Println("flags parsed")
am, err := NewMapper(mapDB) am, err := NewMapper(mapDB)
if err != nil { if err != nil {
log.Fatalf("error creating AardMapper: %s\n", err) log.Fatalf("error creating AardMapper: %s\n", err)
} }
log.Printf("databse opened: %#+v\n", am) // log.Printf("databse opened: %#+v\n", am)
areas, err := am.Areas() exits, err := am.Exits()
if err != nil {
log.Fatalf("error fetching Areas: %s\n", err)
}
log.Printf("found %d areas\n", len(areas))
for k, v := range areas {
log.Printf("found Area: %s\n", k)
log.Printf("%#+v\n", v)
break
}
rooms, err := am.Rooms()
if err != nil { if err != nil {
log.Fatalf("error fetching Rooms: %s\n", err) log.Fatalf("error fetching Rooms: %s\n", err)
} }
log.Printf("found %d rooms\n", len(rooms)) for _, v := range exits {
for k, v := range rooms { // log.Printf("Area: %#+v\n", v)
log.Printf("found Room: (%s,%s)[%s]\n", v.Name, v.Area, k) log.Printf("RoomExit found: %s\n", v)
log.Printf("%#+v\n", v)
break break
} }

@ -1,19 +1,17 @@
package main package main
import ( import (
"database/sql"
"fmt" "fmt"
// "log"
"gorm.io/driver/sqlite" _ "github.com/mattn/go-sqlite3"
"gorm.io/gorm"
) )
func NewMapper(fn string) (AardMapper, error) { func NewMapper(fn string) (AardMapper, error) {
var am AardMapper var am AardMapper
db, err := gorm.Open(sqlite.Open(fn), &gorm.Config{}) db, err := sql.Open("sqlite3", fn)
db.AutoMigrate(&Area{})
db.AutoMigrate(&Room{})
db.AutoMigrate(&Exit{})
if err != nil { if err != nil {
return am, fmt.Errorf("error opening mapper sqlite3 database: %s\n", err) return am, fmt.Errorf("error opening mapper sqlite3 database: %s\n", err)
@ -23,33 +21,47 @@ func NewMapper(fn string) (AardMapper, error) {
} }
type AardMapper struct { type AardMapper struct {
DB *gorm.DB DB *sql.DB
} }
func (am AardMapper) Areas() (map[string]Area, error) { func (am AardMapper) Areas() (map[string]Area, error) {
areas := make(map[string]Area) areas := make(map[string]Area)
x := make([]Area, 0) rows, err := am.DB.Query("select uid, name, texture, color, flags from areas;")
tx := am.DB.Find(&x) defer rows.Close()
if tx.Error != nil { if err != nil {
return nil, fmt.Errorf("error fetching areas: %w", tx.Error) return nil, fmt.Errorf("error selecting areas: %w", err)
} }
for _, a := range x { for rows.Next() {
areas[a.Uid] = a a := Area{}
err := rows.Scan(&a.Uid, &a.Name, &a.Texture, &a.Color, &a.Flags)
if err != nil {
return nil, fmt.Errorf("error scanning Areas: %d", err)
}
areas[a.Uid.String] = a
} }
return areas, nil return areas, nil
} }
func (am AardMapper) Rooms() (map[string]Room, error) { func (am AardMapper) Exits() ([]RoomExit, error) {
rooms := make(map[string]Room) exits := make([]RoomExit, 0)
x := make([]Room, 0) rows, err := am.DB.Query("select r.uid,r.name,r.building,r.info,r.notes,r.flags,r.area, r.norecall,r.noportal,e.dir,e.fromuid,e.touid,e.level from rooms r INNER JOIN exits e ON r.uid = e.fromuid;")
tx := am.DB.Find(&x) // rows, err := am.DB.Query("select e.dir,e.fromuid,e.touid,e.level from rooms r INNER JOIN exits e ON r.uid = e.fromuid;")
defer rows.Close()
if tx.Error != nil { if err != nil {
return nil, fmt.Errorf("error fetching areas: %w", tx.Error) return nil, fmt.Errorf("error selecting areas: %w", err)
} }
for _, a := range x { for rows.Next() {
rooms[a.Uid] = a e := RoomExit{}
// err := rows.Scan(&e.Dir, &e.Fromuid, &e.Touid, &e.Level)
err := rows.Scan(&e.Uid, &e.Name, &e.Building, &e.Info, &e.Notes, &e.Flags, &e.AreaName, &e.Norecall, &e.Noportal, &e.Dir, &e.Fromuid, &e.Touid, &e.Level)
if err != nil {
return nil, fmt.Errorf("error scanning Areas: %d", err)
}
exits = append(exits, e)
} }
return rooms, nil return exits, nil
} }

@ -1,27 +1,34 @@
package main package main
import ( import (
"gorm.io/gorm" "database/sql"
"fmt"
) )
type Area struct { type Area struct {
gorm.Model Uid, Name, Flags, Color, Texture sql.NullString
Uid string
Name, Flags string
} }
type Room struct { type Room struct {
gorm.Model Uid, Name, Building, Info, Notes, Flags sql.NullString
Uid string Area Area
Name, Area string AreaName sql.NullString
Building string Norecall, Noportal sql.NullInt64
Info, Notes, Flags string Exits []Exit
X, Y, Z int
Norecall, Noportal int
Exits []Exit `gorm:"foreignKey:Fromuid"`
} }
type Exit struct { type Exit struct {
gorm.Model Dir sql.NullString
Dir, Fromuid, Touid, Level string Fromuid, Touid sql.NullString
Level int
}
type RoomExit struct {
Room
Exit
}
func (re RoomExit) String() string {
return fmt.Sprintf("[%s(%s) |%s> %s]", re.Name.String, re.Uid.String, re.Dir.String, re.Touid.String)
} }

@ -6,7 +6,7 @@ import (
) )
func walk(r Room, parent *cgraph.Node, g *cgraph.Graph) error { func walk(r Room, parent *cgraph.Node, g *cgraph.Graph) error {
_, err := g.CreateNode(r.Uid) _, err := g.CreateNode(r.Uid.String)
if err != nil { if err != nil {
return err return err
} }

Loading…
Cancel
Save