-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
111 lines (96 loc) · 2.62 KB
/
storage.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
package storage
import (
"database/sql"
"errors"
"fmt"
"log"
"os"
_ "github.com/mattn/go-sqlite3"
)
type Storage struct {
db *sql.DB
}
const (
tableSchema = ` CREATE TABLE IF NOT EXISTS test_%d (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
question TEXT,
rightanswer TEXT,
UNIQUE(question, rightanswer)
);`
)
var ErrNoSavedQA = errors.New("This question is not exists in databese")
// New creates new SQLite storage.
func New(path string) (*Storage, error) {
db, err := sql.Open("sqlite3", path)
if err != nil {
return nil, fmt.Errorf("Can't open database: %w", err)
}
if err := db.Ping(); err != nil {
return nil, fmt.Errorf("Can't connect to database: %w", err)
}
return &Storage{db: db}, nil
}
func (s *Storage) Close() {
s.db.Close()
}
// CreateSchemaByNum creates table with specific number.
func (s *Storage) CreateTableByNum(testNum int) error {
// Exec schema.
_, err := s.db.Exec(fmt.Sprintf(tableSchema, testNum))
if err != nil {
return fmt.Errorf("Can't create tables: %w", err)
}
return nil
}
// Put question and rightanswer to table test_<testNum>
func (s *Storage) Put(testNum int, question, rightanswer string) error {
q := `INSERT OR IGNORE INTO test_%d (question, rightanswer) VALUES ( ?, ?);`
_, err := s.db.Exec(fmt.Sprintf(q, testNum), question, rightanswer)
if err != nil {
return err
}
return nil
}
// PickRightanswer by question from storage.
func (s *Storage) PickRightanswer(testNum int, question string) (answer string, err error) {
q := `SELECT rightanswer FROM test_%d WHERE question = ? LIMIT 1;`
err = s.db.QueryRow(fmt.Sprintf(q, testNum), question).Scan(&answer)
//if err == sql.ErrNoRows {
//return "", ErrNoSavedQA
//}
if err != nil && err != sql.ErrNoRows {
return "", err
}
return answer, nil
}
func (s *Storage) ParseToFile(testNum int) error {
// Query the database for all rows in the "test_11" table.
rows, err := s.db.Query(fmt.Sprintf("SELECT question, rightanswer FROM test_%d", testNum))
if err != nil {
log.Fatal(err)
}
defer rows.Close()
//Create file
path := fmt.Sprintf("answers/test_%d.txt", testNum)
file, err := os.Create(path)
if err != nil {
return fmt.Errorf("Cant't create file: %w", err)
}
defer file.Close()
// Write each row to the output file.
var question, rightanswer string
for rows.Next() {
if err := rows.Scan(&question, &rightanswer); err != nil {
log.Fatal(err)
}
_, err := fmt.Fprintf(file, "Question: %s\nRightanswer: %s\n\n", question, rightanswer)
if err != nil {
log.Fatal(err)
}
}
// Check for any errors encountered while iterating over rows.
if err := rows.Err(); err != nil {
log.Fatal(err)
}
return nil
}