-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.go
284 lines (213 loc) · 4.89 KB
/
todo.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"github.com/marcgwilson/todo/query"
"github.com/marcgwilson/todo/state"
_ "github.com/mattn/go-sqlite3"
"database/sql"
"encoding/json"
"fmt"
"log"
"time"
)
var CreateStmt = `CREATE TABLE IF NOT EXISTS todo (
desc TEXT,
due TIMESTAMP,
state TEXT
);`
type Todo struct {
ID int64 `db:"id" json:"id"`
Description string `db:"desc" json:"desc"`
Due time.Time `db:"due" json:"due"`
State state.State `db:"state" json:"state"`
}
func (r *Todo) Equal(t *Todo) bool {
if r.ID != t.ID {
return false
}
if r.Description != t.Description {
return false
}
if !r.Due.Equal(t.Due) {
return false
}
if r.State != t.State {
return false
}
return true
}
type TodoList []*Todo
func (r TodoList) Equal(t TodoList) bool {
if len(r) != len(t) {
return false
}
for i, elem := range r {
if !elem.Equal(t[i]) {
return false
}
}
return true
}
func (r *Todo) Marshal() ([]byte, error) {
return json.Marshal(r)
}
func UnmarshalTodo(b []byte) (*Todo, error) {
t := &Todo{}
if err := json.Unmarshal(b, t); err != nil {
return nil, err
}
return t, nil
}
func NewTodo(desc string, due time.Time, s state.State) *Todo {
return &Todo{-1, desc, due, s}
}
func UnmarshalTodoList(b []byte) (TodoList, error) {
t := TodoList{}
if err := json.Unmarshal(b, &t); err != nil {
return nil, err
}
return t, nil
}
type TodoManager struct {
Database *sql.DB
}
func NewManager(db *sql.DB) *TodoManager {
return &TodoManager{db}
}
func (r *TodoManager) Get(id int64) (*Todo, error) {
var stmt *sql.Stmt
var row *sql.Row
var err error
if stmt, err = r.Database.Prepare("SELECT rowid, desc, due, state FROM todo WHERE rowid = ?"); err != nil {
return nil, err
}
defer stmt.Close()
t := &Todo{}
row = stmt.QueryRow(id)
err = row.Scan(&t.ID, &t.Description, &t.Due, &t.State)
if err != nil {
return nil, err
}
return t, nil
}
func (r *TodoManager) Query(filter *query.Query) (TodoList, error) {
var stmt *sql.Stmt
var rows *sql.Rows
var err error
q := "SELECT rowid, desc, due, state FROM todo" + filter.Query()
log.Println(q)
log.Printf("%#v\n", filter.Values())
if stmt, err = r.Database.Prepare(q); err != nil {
return nil, err
}
defer stmt.Close()
if rows, err = stmt.Query(filter.Values()...); err != nil {
return nil, err
}
defer rows.Close()
results := []*Todo{}
for rows.Next() {
t := &Todo{}
if err = rows.Scan(&t.ID, &t.Description, &t.Due, &t.State); err != nil {
return nil, err
}
results = append(results, t)
}
if err = rows.Err(); err != nil {
return nil, err
}
return results, nil
}
func (r *TodoManager) Count(filter *query.Query) (int64, error) {
var stmt *sql.Stmt
var err error
var count int64
q := "SELECT COUNT(*) FROM todo" + filter.Query()
log.Println(q)
log.Printf("%#v\n", filter.Values())
if stmt, err = r.Database.Prepare(q); err != nil {
return 0, err
}
defer stmt.Close()
row := stmt.QueryRow(filter.Values()...)
err = row.Scan(&count)
return count, err
}
func (r *TodoManager) Create(data map[string]interface{}) (*Todo, error) {
var err error
var id int64
var result sql.Result
var tx *sql.Tx
var stmt *sql.Stmt
var d TodoMap
if d, err = Transform(data); err != nil {
return nil, err
}
insert := d.InsertVars()
sql := fmt.Sprintf("INSERT INTO todo(%s) VALUES(%s)", insert.Names, insert.Bindvars)
if tx, err = r.Database.Begin(); err != nil {
return nil, err
}
if stmt, err = tx.Prepare(sql); err != nil {
return nil, err
}
defer stmt.Close()
if result, err = stmt.Exec(insert.Values...); err != nil {
return nil, err
}
if id, err = result.LastInsertId(); err != nil {
return nil, err
}
if err = tx.Commit(); err != nil {
return nil, err
}
return &Todo{id, d.Description(), d.Due(), d.State()}, nil
}
func (r *TodoManager) Update(id int64, data map[string]interface{}) (*Todo, error) {
var err error
var todo *Todo
var tx *sql.Tx
var stmt *sql.Stmt
var d TodoMap
if d, err = Transform(data); err != nil {
return nil, err
}
update := d.UpdateVars()
query := fmt.Sprintf("UPDATE todo SET %s WHERE rowid = ?;", update.Bindvars)
if tx, err = r.Database.Begin(); err != nil {
return nil, err
}
if stmt, err = tx.Prepare(query); err != nil {
return nil, err
}
defer stmt.Close()
values := update.Values
values = append(values, id)
if _, err = stmt.Exec(values...); err != nil {
return nil, err
}
if err = tx.Commit(); err != nil {
return nil, err
}
if todo, err = r.Get(id); err != nil {
return nil, err
}
return todo, nil
}
func (r *TodoManager) Delete(id int64) error {
var err error
var tx *sql.Tx
var stmt *sql.Stmt
if tx, err = r.Database.Begin(); err != nil {
return err
}
if stmt, err = tx.Prepare("DELETE FROM todo WHERE rowid=?;"); err != nil {
return err
}
if _, err = stmt.Exec(id); err != nil {
return err
}
if err = tx.Commit(); err != nil {
return err
}
return nil
}