-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
118 lines (96 loc) · 2.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"encoding/json"
"github.com/bwmarrin/lit"
tele "gopkg.in/telebot.v3"
)
const videoTable = "CREATE TABLE IF NOT EXISTS `video` ( `filename` VARCHAR(15) NOT NULL, `video` TEXT NOT NULL, PRIMARY KEY (`filename`) )"
const albumTable = "CREATE TABLE IF NOT EXISTS `album` ( `filename` VARCHAR(11) NOT NULL, `album` TEXT NOT NULL, PRIMARY KEY (`filename`) )"
const audioTable = "CREATE TABLE IF NOT EXISTS `audio` ( `filename` VARCHAR(15) NOT NULL, `audio` TEXT NOT NULL, PRIMARY KEY (`filename`) )"
func execQuery(query ...string) {
for _, q := range query {
_, err := db.Exec(q)
if err != nil {
lit.Error("Error executing query, %s", err)
return
}
}
}
func load() {
var (
filename string
bytes []byte
)
// Videos
rows, err := db.Query("SELECT filename, video FROM video")
if err != nil {
lit.Error("Error executing query, %s", err)
return
}
for rows.Next() {
var video []*tele.Video
err = rows.Scan(&filename, &bytes)
if err != nil {
lit.Error("Error scanning row, %s", err)
continue
}
_ = json.Unmarshal(bytes, &video)
cacheVideo[filename] = &video
}
// Photos
rows, err = db.Query("SELECT filename, album FROM album")
if err != nil {
lit.Error("Error executing query, %s", err)
return
}
for rows.Next() {
var photos []*tele.Photo
err = rows.Scan(&filename, &bytes)
if err != nil {
lit.Error("Error scanning row, %s", err)
continue
}
err = json.Unmarshal(bytes, &photos)
cacheAlbum[filename] = &photos
}
// Audio
rows, err = db.Query("SELECT filename, audio FROM audio")
if err != nil {
lit.Error("Error executing query, %s", err)
return
}
for rows.Next() {
var audio tele.Audio
err = rows.Scan(&filename, &bytes)
if err != nil {
lit.Error("Error scanning row, %s", err)
continue
}
err = json.Unmarshal(bytes, &audio)
cacheAudio[filename] = &audio
}
}
func saveVideo(video *[]*tele.Video) {
bytes, _ := json.Marshal(video)
_, err := db.Exec("INSERT INTO video (filename, video) VALUES (?, ?)", (*video)[0].FileName, bytes)
if err != nil {
lit.Error("Error executing query, %s", err)
return
}
}
func saveAlbum(album *[]*tele.Photo, filename string) {
bytes, _ := json.Marshal(album)
_, err := db.Exec("INSERT INTO album (filename, album) VALUES (?, ?)", filename, bytes)
if err != nil {
lit.Error("Error executing query, %s", err)
return
}
}
func saveAudio(audio *tele.Audio) {
bytes, _ := json.Marshal(audio)
_, err := db.Exec("INSERT INTO audio (filename, audio) VALUES (?, ?)", audio.FileName, bytes)
if err != nil {
lit.Error("Error executing query, %s", err)
return
}
}