-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
66 lines (57 loc) · 1.76 KB
/
database.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package papaBot
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
// initDb initializes the bot's database.
func (bot *Bot) initDb() error {
db, err := sql.Open("sqlite3", "papabot.db")
if err != nil {
return err
}
// Create URLs tables and triggers, if needed.
query := `
-- Main URLs table.
CREATE TABLE IF NOT EXISTS "urls" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"transport" VARCHAR NOT NULL,
"channel" VARCHAR NOT NULL,
"nick" VARCHAR NOT NULL,
"link" VARCHAR NOT NULL,
"quote" VARCHAR NOT NULL,
"title" VARCHAR,
"timestamp" DATETIME DEFAULT (datetime('now','localtime'))
);
-- Virtual table for FTS.
CREATE VIRTUAL TABLE IF NOT EXISTS urls_search
USING fts4(transport, channel, nick, link, title, timestamp, search);
-- Triggers for FTS updating.
CREATE TRIGGER IF NOT EXISTS url_add AFTER INSERT ON urls BEGIN
INSERT INTO urls_search(transport, channel, nick, link, title, timestamp, search)
VALUES(new.transport, new.channel, new.nick, new.link, new.title, new.timestamp, new.link || ' ' || new.title);
END;
CREATE TRIGGER IF NOT EXISTS url_update AFTER UPDATE ON urls BEGIN
UPDATE urls_search SET title = new.title, search = new.link || ' ' || new.title
WHERE timestamp = new.timestamp;
END;
-- Users table.
CREATE TABLE IF NOT EXISTS "users" (
"nick" VARCHAR PRIMARY KEY NOT NULL UNIQUE,
"password" VARCHAR,
"alt_nicks" VARCHAR,
"owner" boolean DEFAULT 0,
"admin" boolean DEFAULT 0,
"joined" DATETIME DEFAULT (datetime('now','localtime'))
);
-- Custom variables.
CREATE TABLE IF NOT EXISTS "vars" (
"name" VARCHAR PRIMARY KEY NOT NULL UNIQUE,
"value" VARCHAR
);
`
if _, err := db.Exec(query); err != nil {
bot.Log.Panic(err)
}
bot.Db = db
return nil
}