Doing packages wrong...
This commit is contained in:
parent
0083f2887e
commit
5811525c64
5 changed files with 104 additions and 8 deletions
BIN
local/.records.go.swp
Normal file
BIN
local/.records.go.swp
Normal file
Binary file not shown.
3
local/go.mod
Normal file
3
local/go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
module git.rdx4.com/monqui/records
|
||||||
|
|
||||||
|
go 1.18
|
||||||
89
local/records.go
Normal file
89
local/records.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
||||||
20
main.go
20
main.go
|
|
@ -5,6 +5,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"./local/git.rdx4.com/monqui/records"
|
||||||
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
@ -68,12 +70,13 @@ func get_user(db *sql.DB, name string) (User) {
|
||||||
type Entity struct {
|
type Entity struct {
|
||||||
id int64
|
id int64
|
||||||
user_id int64
|
user_id int64
|
||||||
|
name string
|
||||||
spawn_line string
|
spawn_line string
|
||||||
}
|
}
|
||||||
|
|
||||||
func create_entity(db *sql.DB, user_id int64, spawn_line string) (Entity) {
|
func create_entity(db *sql.DB, user_id int64, name string, spawn_line string) (Entity) {
|
||||||
s := prep_query(db, "INSERT INTO entities (user_id, spawn_line) VALUES (?, ?)")
|
s := prep_query(db, "INSERT INTO entities (user_id, name, spawn_line) VALUES (?, ?, ?)")
|
||||||
res, err := s.Exec(user_id, spawn_line)
|
res, err := s.Exec(user_id, name, spawn_line)
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
var ent Entity
|
var ent Entity
|
||||||
|
|
@ -81,6 +84,7 @@ func create_entity(db *sql.DB, user_id int64, spawn_line string) (Entity) {
|
||||||
id, _ := res.LastInsertId()
|
id, _ := res.LastInsertId()
|
||||||
ent.id = id
|
ent.id = id
|
||||||
ent.user_id = user_id
|
ent.user_id = user_id
|
||||||
|
ent.name = name
|
||||||
ent.spawn_line = spawn_line
|
ent.spawn_line = spawn_line
|
||||||
|
|
||||||
return ent
|
return ent
|
||||||
|
|
@ -95,9 +99,9 @@ func get_entity(db *sql.DB, id int64) (Entity) {
|
||||||
var ent Entity
|
var ent Entity
|
||||||
|
|
||||||
res.Next()
|
res.Next()
|
||||||
res.Scan(&ent.id, &ent.user_id, &ent.spawn_line)
|
err = res.Scan(&ent.id, &ent.user_id, &ent.name, &ent.spawn_line)
|
||||||
log.Println("WHY DOESN'T THIS WORK")
|
check(err)
|
||||||
log.Printf("ID: %d, user_id: %d, spawn_line: %s", ent.id, ent.user_id, ent.spawn_line)
|
|
||||||
res.Close()
|
res.Close()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -136,9 +140,9 @@ log.Printf("ID: %d, name: %s", u.id, u.name)
|
||||||
|
|
||||||
// make some entities...
|
// make some entities...
|
||||||
log.Println("Adding entities...\n")
|
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)
|
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.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")
|
log.Println("Trying to fetch some entities...\n")
|
||||||
|
|
|
||||||
BIN
rpgqui.db
BIN
rpgqui.db
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue