-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.go
60 lines (48 loc) · 1.11 KB
/
settings.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
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
)
type History interface {
IsExists(name string) bool
AddToHistory(name string)
Close()
}
type SqlHistory struct {
db *sql.DB
}
func NewHistory() *SqlHistory {
database, _ := sql.Open("sqlite3", "settings.db")
statement, _ := database.Prepare("CREATE TABLE IF NOT EXISTS history (id INTEGER PRIMARY KEY, filename TEXT UNIQUE)")
statement.Exec()
h := &SqlHistory{database}
return h
}
func (h *SqlHistory) IsExists(name string) bool {
rows, _ := h.db.Query("SELECT filename FROM history WHERE filename = $1", name)
var filename string = ""
for rows.Next() {
rows.Scan(&filename)
return name == filename
}
return false
}
func (h *SqlHistory) AddToHistory(name string) {
statement, _ :=
h.db.Prepare("INSERT INTO history (filename) VALUES (?)")
statement.Exec(name)
}
func (h *SqlHistory) Close() {
h.db.Close()
}
func ShowHistory() {
h := NewHistory()
defer h.Close()
rows, _ := h.db.Query("SELECT filename FROM history")
var filename string = ""
for rows.Next() {
rows.Scan(&filename)
fmt.Println(filename)
}
}