building a crude API for pulling data
parent
f7c7ef976a
commit
866d839b06
@ -1,3 +1,5 @@
|
||||
module code.ndumas.com/ndumas/aardmaps
|
||||
|
||||
go 1.19
|
||||
|
||||
require github.com/mattn/go-sqlite3 v1.14.16
|
||||
|
@ -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=
|
@ -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)
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue