-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
94 lines (84 loc) · 1.59 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
type store struct {
db *sql.DB
helper
}
type storer interface {
createTable() error
deleteAll() error
deleteRow(title string) error
addRow(title, desc string) error
getRow() error
}
func newDB(db *sql.DB, msg helper) *store {
return &store{db, msg}
}
func connectDB() (*sql.DB, error) {
return sql.Open("sqlite3", "/app/db/helperbot.db")
}
func (d *store) createTable() error {
helperTableCreate := `
CREATE TABLE IF NOT EXISTS helper(
title TEXT NOT NULL,
description TEXT NOT NULL
);
`
_, err := d.db.Exec(helperTableCreate)
if err != nil {
return err
}
return nil
}
func (d *store) deleteAll() error {
helperTableDelete := `
DELETE FROM helper;
`
_, err := d.db.Exec(helperTableDelete)
if err != nil {
return err
}
return nil
}
func (d *store) deleteRow(title string) error {
stmt, err := d.db.Prepare("DELETE FROM helper where title=?")
if err != nil {
return err
}
_, err = stmt.Exec(title)
if err != nil {
return err
}
return nil
}
func (d *store) addRow(title, desc string) error {
stmt, err := d.db.Prepare("REPLACE INTO helper(title,description) values(?,?)")
if err != nil {
return err
}
_, err = stmt.Exec(title, desc)
if err != nil {
return err
}
return nil
}
func (d *store) getRow() error {
var title, desc string
r, err := d.db.Query("select * from helper;")
if err != nil {
return err
}
for r.Next() {
err := r.Scan(&title, &desc)
if err != nil {
return err
}
if _, ok := d.helper.message[title]; !ok {
d.helper.message[title] = desc
}
}
return nil
}