Compare commits
No commits in common. "a3fa5d6645d77f46fa192cd2be76f916f9e4174e" and "040633b7be1dbf7b5273462477683e9742901f96" have entirely different histories.
a3fa5d6645
...
040633b7be
3 changed files with 93 additions and 60 deletions
138
main.go
138
main.go
|
|
@ -3,47 +3,23 @@ package main
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"log"
|
"log"
|
||||||
"os/exec"
|
|
||||||
// "reflect"
|
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type active_record interface {
|
// Book is a placeholder for book
|
||||||
init(string, int) bool
|
type Word struct {
|
||||||
create() bool
|
word_id int
|
||||||
read() bool
|
source_id int
|
||||||
update() bool
|
previous string
|
||||||
delete() bool
|
current string
|
||||||
|
next string
|
||||||
|
count int
|
||||||
}
|
}
|
||||||
|
|
||||||
type ActiveSqliteRow struct {
|
type Source struct {
|
||||||
id int
|
source_id int
|
||||||
table_name string
|
name string
|
||||||
columns []string
|
|
||||||
// values map[string]
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
func (r ActiveSqliteRow) init(t string, i int) bool{
|
|
||||||
log.Print("In init with" + t)
|
|
||||||
log.Print(i)
|
|
||||||
|
|
||||||
r.id = i
|
|
||||||
r.table_name = t
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
type Entity struct {
|
|
||||||
id int
|
|
||||||
user_id int
|
|
||||||
name string
|
|
||||||
spawn_line string
|
|
||||||
}
|
|
||||||
|
|
||||||
type User struct {
|
|
||||||
id int
|
|
||||||
name string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func prep_query(db *sql.DB, q string) (*sql.Stmt){
|
func prep_query(db *sql.DB, q string) (*sql.Stmt){
|
||||||
|
|
@ -58,15 +34,87 @@ func prep_query(db *sql.DB, q string) (*sql.Stmt){
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// exec setup.sql to init
|
db, _ := sql.Open("sqlite3", "words.db")
|
||||||
cmd := exec.Command("sqlite3", "rpgqui.db < sedfsftup.sql")
|
|
||||||
out, err := cmd.Output()
|
|
||||||
|
|
||||||
if err != nil {
|
log.Println("Nuking tables...")
|
||||||
log.Print(err)
|
prep_query(db, "DROP TABLE IF EXISTS words").Exec()
|
||||||
} else {
|
prep_query(db, "DROP TABLE IF EXISTS sources").Exec()
|
||||||
log.Print(out)
|
log.Println("Done nuking tables!")
|
||||||
}
|
|
||||||
|
|
||||||
// db, _ := sql.Open("sqlite3", "words.db")
|
log.Println("Creating tables...")
|
||||||
|
prep_query(db, `
|
||||||
|
CREATE TABLE IF NOT EXISTS sources (
|
||||||
|
source_id INTEGER PRIMARY KEY,
|
||||||
|
name VARCHAR(64)
|
||||||
|
);
|
||||||
|
`).Exec()
|
||||||
|
|
||||||
|
prep_query(db, `
|
||||||
|
CREATE TABLE IF NOT EXISTS words (
|
||||||
|
word_id INTEGER PRIMARY KEY,
|
||||||
|
source_id INTEGER,
|
||||||
|
previous VARCHAR(64) DEFAULT NULL,
|
||||||
|
current VARCHAR(64),
|
||||||
|
next VARCHAR(64) DEFAULT NULL,
|
||||||
|
count INTEGER DEFAULT 0,
|
||||||
|
FOREIGN KEY(source_id) REFERENCES sources(source_id)
|
||||||
|
);
|
||||||
|
`).Exec()
|
||||||
|
log.Println("Done making tables!")
|
||||||
|
|
||||||
|
log.Println("Making indexes...")
|
||||||
|
prep_query(db, "CREATE INDEX words_sources_index ON words(source_id)").Exec()
|
||||||
|
log.Println("Done making indexes!")
|
||||||
|
|
||||||
|
log.Println("Inserting source/word...")
|
||||||
|
prep_query(db, `
|
||||||
|
INSERT INTO sources
|
||||||
|
(name)
|
||||||
|
VALUES
|
||||||
|
(?)
|
||||||
|
`).Exec("Monqui")
|
||||||
|
|
||||||
|
prep_query(db, `
|
||||||
|
INSERT INTO words
|
||||||
|
(current, next, source_id)
|
||||||
|
VALUES
|
||||||
|
(?, ?, ?)
|
||||||
|
`).Exec("My", "name", 1)
|
||||||
|
|
||||||
|
log.Println("Inserted the source/word into database!")
|
||||||
|
|
||||||
|
rows, _ := db.Query("SELECT source_id, name FROM sources")
|
||||||
|
var tempSource Source
|
||||||
|
for rows.Next() {
|
||||||
|
rows.Scan(&tempSource.source_id, &tempSource.name)
|
||||||
|
log.Printf("source_id:%d, name:%s\n",
|
||||||
|
tempSource.source_id, tempSource.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, _ = db.Query("SELECT word_id, current, next, source_id FROM words")
|
||||||
|
var tempWord Word
|
||||||
|
for rows.Next() {
|
||||||
|
rows.Scan(&tempWord.word_id, &tempWord.current, &tempWord.next, &tempWord.source_id)
|
||||||
|
log.Printf(
|
||||||
|
"word_id:%d, current:%s, next:%s, source_id:%d\n",
|
||||||
|
tempWord.word_id, tempWord.current, tempWord.next, tempWord.source_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update
|
||||||
|
prep_query(db, "UPDATE sources SET name=? WHERE source_id=?").Exec("Monqu2", 1)
|
||||||
|
|
||||||
|
rows, _ = db.Query("SELECT source_id, name FROM sources")
|
||||||
|
for rows.Next() {
|
||||||
|
rows.Scan(&tempSource.source_id, &tempSource.name)
|
||||||
|
log.Printf("source_id:%d, name:%s\n",
|
||||||
|
tempSource.source_id, tempSource.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
/*
|
||||||
|
// Delete
|
||||||
|
statement, _ = db.Prepare("delete from books where id=?")
|
||||||
|
statement.Exec(1)
|
||||||
|
log.Println("Successfully deleted the book in database!")
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
|
||||||
15
setup.sql
15
setup.sql
|
|
@ -1,15 +0,0 @@
|
||||||
DROP TABLE IF EXISTS users;
|
|
||||||
DROP TABLE IF EXISTS entities;
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
|
||||||
id INTEGER PRIMARY KEY,
|
|
||||||
name VARCHAR(64)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS entities (
|
|
||||||
id INTEGER PRIMARY KEY,
|
|
||||||
user_id INTEGER,
|
|
||||||
name VARCHAR(64),
|
|
||||||
spawn_line VARCHAR(64),
|
|
||||||
FOREIGN KEY(user_id) REFERENCES users(id)
|
|
||||||
);
|
|
||||||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue