-
Notifications
You must be signed in to change notification settings - Fork 4
/
db.go
43 lines (36 loc) · 910 Bytes
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"log"
"github.com/jmoiron/sqlx"
)
func initSqliteDB() *sqlx.DB {
db, err := sqlx.Connect("sqlite3", "board.db")
if err != nil {
log.Fatalln(err)
}
return db
}
func initBoardSchema(db *sqlx.DB) {
schema := `
CREATE TABLE IF NOT EXISTS discussions (
id INTEGER PRIMARY KEY,
author TEXT NOT NULL,
message TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS replies (
id INTEGER PRIMARY KEY,
discussion_id INTEGER NOT NULL,
author TEXT NOT NULL,
message TEXT NOT NULL,
FOREIGN KEY (discussion_id) REFERENCES discussions(id) ON DELETE CASCADE
);
`
_, err := db.Exec(schema)
if err != nil {
log.Fatalln(err)
}
}
func createReply(db *sqlx.DB, postID int, authorHash string, replyBody string) error {
_, err := db.Exec("INSERT INTO replies (discussion_id, author, message) VALUES (?, ?, ?)", postID, authorHash, replyBody)
return err
}