diff --git a/local/.records.go.swp b/local/.records.go.swp new file mode 100644 index 0000000..63bee61 Binary files /dev/null and b/local/.records.go.swp differ diff --git a/local/go.mod b/local/go.mod new file mode 100644 index 0000000..9a3e15b --- /dev/null +++ b/local/go.mod @@ -0,0 +1,3 @@ +module git.rdx4.com/monqui/records + +go 1.18 diff --git a/local/records.go b/local/records.go new file mode 100644 index 0000000..c0a34c0 --- /dev/null +++ b/local/records.go @@ -0,0 +1,89 @@ +package records + +// User record management stuff. Eventually want to abstract +// this out to store any sort of record... +type User struct { + id int64 + name string +} + +func CreateUser(db *sql.DB, name string) (User) { + s := prep_query(db, "INSERT INTO users (name) VALUES (?)") + res, err := s.Exec(name) + check(err) + + var u User + + id, _ := res.LastInsertId() + u.id = id + u.name = name + + return u +} + +func GetUser(db *sql.DB, name string) (User) { + s := prep_query(db, "SELECT * FROM users WHERE name = ? LIMIT 1") + res, err := s.Query(name) + + check(err) + + var u User + + res.Next() + res.Scan(&u.id, &u.name) + res.Close() + + if u.id == 0 { + return create_user(db, name) + } else { + return u + } +} + +// Blerk. Start here with a get/create and see where +// that goes... +type Entity struct { + id int64 + user_id int64 + name string + spawn_line string +} + +func CreateEntity(db *sql.DB, user_id int64, name string, spawn_line string) (Entity) { + s := prep_query(db, "INSERT INTO entities (user_id, name, spawn_line) VALUES (?, ?, ?)") + res, err := s.Exec(user_id, name, spawn_line) + check(err) + + var ent Entity + + id, _ := res.LastInsertId() + ent.id = id + ent.user_id = user_id + ent.name = name + ent.spawn_line = spawn_line + + return ent +} + +func GetEntity(db *sql.DB, id int64) (Entity) { + s := prep_query(db, "SELECT * FROM entities WHERE id = ? LIMIT 1") + res, err := s.Query(id) + + check(err) + + var ent Entity + + res.Next() + err = res.Scan(&ent.id, &ent.user_id, &ent.name, &ent.spawn_line) + check(err) + + res.Close() + + + if ent.id == 0 { + log.Printf("Entity id %d doesn't exist!", id) + } + + return ent +} + diff --git a/main.go b/main.go index dd81aaa..a76ccb9 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "strings" "os" + "./local/git.rdx4.com/monqui/records" + "database/sql" _ "github.com/mattn/go-sqlite3" ) @@ -68,12 +70,13 @@ func get_user(db *sql.DB, name string) (User) { type Entity struct { id int64 user_id int64 + name string spawn_line string } -func create_entity(db *sql.DB, user_id int64, spawn_line string) (Entity) { - s := prep_query(db, "INSERT INTO entities (user_id, spawn_line) VALUES (?, ?)") - res, err := s.Exec(user_id, spawn_line) +func create_entity(db *sql.DB, user_id int64, name string, spawn_line string) (Entity) { + s := prep_query(db, "INSERT INTO entities (user_id, name, spawn_line) VALUES (?, ?, ?)") + res, err := s.Exec(user_id, name, spawn_line) check(err) var ent Entity @@ -81,6 +84,7 @@ func create_entity(db *sql.DB, user_id int64, spawn_line string) (Entity) { id, _ := res.LastInsertId() ent.id = id ent.user_id = user_id + ent.name = name ent.spawn_line = spawn_line return ent @@ -95,9 +99,9 @@ func get_entity(db *sql.DB, id int64) (Entity) { var ent Entity res.Next() - res.Scan(&ent.id, &ent.user_id, &ent.spawn_line) -log.Println("WHY DOESN'T THIS WORK") -log.Printf("ID: %d, user_id: %d, spawn_line: %s", ent.id, ent.user_id, ent.spawn_line) + err = res.Scan(&ent.id, &ent.user_id, &ent.name, &ent.spawn_line) + check(err) + res.Close() @@ -136,9 +140,9 @@ log.Printf("ID: %d, name: %s", u.id, u.name) // make some entities... log.Println("Adding entities...\n") - e := create_entity(db, u.id, "hello im monqui") + e := create_entity(db, u.id, "idk a demon", "hello im monqui") log.Printf("ID: %d, user_id: %d, spawn_line: %s", e.id, e.user_id, e.spawn_line) - e = create_entity(db, u.id, "here's a whole nother line!") + e = create_entity(db, u.id, "idk a monster", "here's a whole nother line!") log.Printf("ID: %d, user_id: %d, spawn_line: %s", e.id, e.user_id, e.spawn_line) log.Println("Trying to fetch some entities...\n") diff --git a/rpgqui.db b/rpgqui.db index b4e9701..ea2014e 100644 Binary files a/rpgqui.db and b/rpgqui.db differ