forked from rakanalh/scheduler
/
sqlite3.go
170 lines (142 loc) · 3.65 KB
/
sqlite3.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// +build cgo
package storage
import (
"database/sql"
"fmt"
"log"
// Import the sqlite3 driver
_ "github.com/mattn/go-sqlite3"
)
// Sqlite3Config is the config structure holding information about sqlite db.
type Sqlite3Config struct {
DbName string
}
// Sqlite3Storage is the structure responsible for handling sqlite3 storage.
type Sqlite3Storage struct {
config Sqlite3Config
db *sql.DB
}
// NewSqlite3Storage returns a new instance of Sqlite3Storage.
func NewSqlite3Storage(config Sqlite3Config) Sqlite3Storage {
return Sqlite3Storage{
config: config,
}
}
// Connect creates the database file.
func (sqlite *Sqlite3Storage) Connect() error {
db, err := sql.Open("sqlite3", sqlite.config.DbName)
if err != nil {
return err
}
sqlite.db = db
return nil
}
// Close will close the open DB file.
func (sqlite Sqlite3Storage) Close() error {
return sqlite.db.Close()
}
// Initialize will run once to initialize the state of the database into the required one.
// In this case, it'll create the tables required to store task information.
func (sqlite *Sqlite3Storage) Initialize() error {
sqlStmt := `
CREATE TABLE IF NOT EXISTS task_store (
id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
name text,
params text,
duration integer,
last_run text,
next_run text,
is_recurring integer,
hash text
);
`
_, err := sqlite.db.Exec(sqlStmt)
if err != nil {
log.Printf("%q: %s\n", err, sqlStmt)
return err
}
return nil
}
// Add stores the task to sqlite.
func (sqlite Sqlite3Storage) Add(task TaskAttributes) error {
var count int
rows, err := sqlite.db.Query("SELECT count(*) FROM task_store WHERE hash=?", task.Hash)
if err == nil {
rows.Next()
_ = rows.Scan(&count)
}
_ = rows.Close()
if count == 0 {
return sqlite.insert(task)
}
return nil
}
// Remove will delete the task from storage.
func (sqlite Sqlite3Storage) Remove(task TaskAttributes) error {
stmt, err := sqlite.db.Prepare(`DELETE FROM task_store WHERE hash=?`)
if err != nil {
return fmt.Errorf("Error while pareparing delete task statement: %s", err)
}
defer stmt.Close()
_, err = stmt.Exec(
task.Hash,
)
if err != nil {
return fmt.Errorf("Error while deleting task: %s", err)
}
return nil
}
// Fetch will return the list of all stored tasks.
func (sqlite Sqlite3Storage) Fetch() ([]TaskAttributes, error) {
rows, err := sqlite.db.Query(`
SELECT name, params, duration, last_run, next_run, is_recurring
FROM task_store`)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var tasks []TaskAttributes
for rows.Next() {
var name, params, lastRun, nextRun, duration, isRecurring string
err = rows.Scan(&name, ¶ms, &duration, &lastRun, &nextRun, &isRecurring)
if err != nil {
return []TaskAttributes{}, err
}
task := TaskAttributes{
Name: name,
Params: params,
LastRun: lastRun,
NextRun: nextRun,
Duration: string(duration),
IsRecurring: string(isRecurring),
}
tasks = append(tasks, task)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
return tasks, nil
}
func (sqlite *Sqlite3Storage) insert(task TaskAttributes) error {
stmt, err := sqlite.db.Prepare(`
INSERT INTO task_store(name, params, duration, last_run, next_run, is_recurring, hash)
VALUES(?, ?, ?, ?, ?, ?, ?)`)
if err != nil {
return fmt.Errorf("Error while pareparing insert task statement: %s", err)
}
defer stmt.Close()
_, err = stmt.Exec(
task.Name,
task.Params,
task.Duration,
task.LastRun,
task.NextRun,
task.IsRecurring,
task.Hash,
)
if err != nil {
return fmt.Errorf("Error while inserting task: %s", err)
}
return nil
}