package main
import (
"database/sql"
"fmt"
// "log"
_ "github.com/mattn/go-sqlite3"
)
func NewMapper ( fn string ) ( AardMapper , error ) {
var am AardMapper
db , err := sql . Open ( "sqlite3" , fn )
if err != nil {
return am , fmt . Errorf ( "error opening mapper sqlite3 database: %s\n" , err )
}
am . DB = db
return am , nil
}
type AardMapper struct {
DB * sql . DB
}
func ( am AardMapper ) Areas ( ) ( map [ string ] Area , error ) {
areas := make ( map [ string ] Area )
rows , err := am . DB . Query ( "select uid, name, texture, color, flags from areas;" )
defer rows . Close ( )
if err != nil {
return nil , fmt . Errorf ( "error selecting areas: %w" , err )
}
for rows . Next ( ) {
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
}
func ( am AardMapper ) Exits ( ) ( [ ] RoomExit , error ) {
exits := make ( [ ] RoomExit , 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;" )
// 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 err != nil {
return nil , fmt . Errorf ( "error selecting areas: %w" , err )
}
for rows . Next ( ) {
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 exits , nil
}