-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_handler.go
173 lines (161 loc) · 3.88 KB
/
db_handler.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
171
172
173
package main
import (
"context"
"database/sql"
"time"
_ "github.com/mattn/go-sqlite3"
)
type DBHandler struct {
dbPath string
contextTimeout time.Duration
}
func (d *DBHandler) Init(ctx context.Context) error {
db, err := sql.Open("sqlite3", d.dbPath)
if err != nil {
return err
}
defer db.Close()
ctx, cancel := context.WithTimeout(ctx, d.contextTimeout)
defer cancel()
_, err = db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS "processes" (
"id" INTEGER NOT NULL,
"name" TEXT NOT NULL,
"create_time" INTEGER NOT NULL,
"command" TEXT NOT NULL,
"status" INTEGER NOT NULL,
"order_id" INTEGER NOT NULL,
PRIMARY KEY("id" AUTOINCREMENT)
)`)
return err
}
func (d *DBHandler) InsertProcess(ctx context.Context, p *Process) (int64, error) {
db, err := sql.Open("sqlite3", d.dbPath)
if err != nil {
return 0, err
}
defer db.Close()
ctx, cancel := context.WithTimeout(ctx, d.contextTimeout)
defer cancel()
var id int64
db.QueryRowContext(
ctx,
"insert into processes(name,create_time,command,status,order_id) values(?,?,?,?,(SELECT ifnull(max(order_id), 0) + 1 from processes)) returning id",
p.Name,
p.CreateTime,
p.Command,
p.Status,
p.OrderId,
).Scan(&id)
return id, err
}
func (d *DBHandler) UpdateProcess(ctx context.Context, p *Process) error {
db, err := sql.Open("sqlite3", d.dbPath)
if err != nil {
return err
}
defer db.Close()
ctx, cancel := context.WithTimeout(ctx, d.contextTimeout)
defer cancel()
_, err = db.ExecContext(
ctx,
"update processes set name = ?, command = ?, status = ? where id = ?",
p.Name,
p.Command,
p.Status,
p.Id,
)
return err
}
func (d *DBHandler) UpdateProcessesOrderId(ctx context.Context, ids []int64) error {
db, err := sql.Open("sqlite3", d.dbPath)
if err != nil {
return err
}
defer db.Close()
ctx, cancel := context.WithTimeout(ctx, d.contextTimeout)
defer cancel()
tx, err := db.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
return err
}
defer tx.Rollback()
for i, v := range ids {
_, err = tx.ExecContext(ctx, "update processes set order_id = ? where id = ?", i+1, v)
if err != nil {
return err
}
}
return tx.Commit()
}
func (d *DBHandler) DeleteProcess(ctx context.Context, id int64) error {
db, err := sql.Open("sqlite3", d.dbPath)
if err != nil {
return err
}
defer db.Close()
ctx, cancel := context.WithTimeout(ctx, d.contextTimeout)
defer cancel()
_, err = db.ExecContext(
ctx,
"delete from processes where id = ?",
id,
)
return err
}
func (d *DBHandler) GetProcesses(ctx context.Context, onlyStatusOne bool) ([]Process, error) {
db, err := sql.Open("sqlite3", d.dbPath)
if err != nil {
return nil, err
}
defer db.Close()
ctx, cancel := context.WithTimeout(ctx, d.contextTimeout)
defer cancel()
query := "select id,name,create_time,command,status,order_id from processes"
if onlyStatusOne {
query = query + " where status = 1"
}
query += " order by order_id asc"
rows, err := db.QueryContext(
ctx,
query,
)
if err != nil {
return nil, err
}
ret := make([]Process, 0)
for rows.Next() {
p := Process{}
err = rows.Scan(&p.Id, &p.Name, &p.CreateTime, &p.Command, &p.Status, &p.OrderId)
if err != nil {
return nil, err
}
ret = append(ret, p)
}
return ret, nil
}
func (d *DBHandler) GetProcess(ctx context.Context, id int64) (*Process, error) {
db, err := sql.Open("sqlite3", d.dbPath)
if err != nil {
return nil, err
}
defer db.Close()
ctx, cancel := context.WithTimeout(ctx, d.contextTimeout)
defer cancel()
row := db.QueryRowContext(
ctx,
"select id,name,create_time,command,status,order_id from processes where id = ?",
id,
)
p := Process{}
err = row.Scan(&p.Id, &p.Name, &p.CreateTime, &p.Command, &p.Status, &p.OrderId)
if err != nil {
return nil, err
}
return &p, nil
}
func NewDBHandler(dbName string, contextTimeout time.Duration) *DBHandler {
return &DBHandler{
dbPath: dbName,
contextTimeout: contextTimeout,
}
}