From 866d839b06251d67dbb224fc9e66fb4e772422f2 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Sun, 2 Apr 2023 22:35:41 -0400 Subject: [PATCH] building a crude API for pulling data --- go.mod | 2 ++ go.sum | 2 ++ main.go | 24 ++++++++++++++++++++++-- models.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ notes.md | 12 ++++++++++++ 5 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 go.sum create mode 100644 models.go create mode 100644 notes.md diff --git a/go.mod b/go.mod index abbd483..8f62a76 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module code.ndumas.com/ndumas/aardmaps go 1.19 + +require github.com/mattn/go-sqlite3 v1.14.16 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7878efc --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= +github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= diff --git a/main.go b/main.go index 1a30a95..5652398 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,30 @@ package main import ( -// "github.com/goccy/go-graphviz" -// "github.com/mattn/go-sqlite3" + "flag" + "log" + // "github.com/goccy/go-graphviz" + _ "github.com/mattn/go-sqlite3" ) func main() { + // log.Println("entering main") + var ( + mapDB string + ) + + flag.StringVar(&mapDB, "map", "Aardwolf.db", "name of aardwolf map file") + + // log.Println("parsing flags?") + flag.Parse() + // log.Println("flags parsed") + + am, err := NewMapper(mapDB) + if err != nil { + log.Fatalf("error creating AardMapper: %s\n", err) + } + log.Printf("databse opened: %#+v\n", am) + areas, _ := am.Areas() + log.Printf("databse opened: %#+v\n", areas) } diff --git a/models.go b/models.go new file mode 100644 index 0000000..c382b1c --- /dev/null +++ b/models.go @@ -0,0 +1,46 @@ +package main + +import ( + "database/sql" + "fmt" + "log" +) + +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() ([]Area, error) { + areas := make([]Area, 0) + rows, err := am.db.Query("select uid, name, flags from areas;") + + if err != nil { + log.Fatalf("error querying areas: %s\n", err) + } + + for rows.Next() { + var a Area + err := rows.Scan(&a.Uid, &a.Name, &a.Flags) + areas = append(areas, a) + if err != nil { + return areas, fmt.Errorf("error scanning area into struct: %s\n", err) + } + } + return areas, nil +} + +type Area struct { + Uid, Name, Flags string +} diff --git a/notes.md b/notes.md new file mode 100644 index 0000000..f35b2e8 --- /dev/null +++ b/notes.md @@ -0,0 +1,12 @@ +# tables +areas +rooms +rooms_lookup_segments +bookmarks +rooms_lookup +storage +environments +rooms_lookup_content +terrain +exits +rooms_lookup_segdir