forked from jsteenb2/mess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_sqlite.go
132 lines (109 loc) · 2.87 KB
/
db_sqlite.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
package allsrv
import (
"context"
"database/sql"
"sync"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/jmoiron/sqlx"
"github.com/jsteenb2/errors"
"github.com/mattn/go-sqlite3"
)
// NewSQLiteDB creates a new sqlite db.
func NewSQLiteDB(db *sqlx.DB) DB {
return &sqlDB{
db: db,
sq: sq.StatementBuilder.PlaceholderFormat(sq.Question),
}
}
type sqlDB struct {
db *sqlx.DB
sq sq.StatementBuilderType
mu sync.RWMutex
}
func (s *sqlDB) CreateFoo(ctx context.Context, f Foo) error {
sb := s.sq.
Insert("foos").
Columns("id", "name", "note", "created_at", "updated_at").
Values(f.ID, f.Name, f.Note, f.CreatedAt, f.UpdatedAt)
_, err := s.exec(ctx, sb)
return errors.Wrap(err)
}
func (s *sqlDB) ReadFoo(ctx context.Context, id string) (Foo, error) {
s.mu.RLock()
defer s.mu.RUnlock()
const query = `SELECT * FROM foos WHERE id=?`
var ent entFoo
err := s.db.GetContext(ctx, &ent, s.db.Rebind(query), id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return Foo{}, NotFoundErr("foo not found for id: " + id)
}
return Foo{}, errors.Wrap(err, errSQLiteFields(err))
}
out := Foo{
ID: ent.ID,
Name: ent.Name,
Note: ent.Note,
CreatedAt: ent.CreatedAt,
UpdatedAt: ent.UpdatedAt,
}
return out, nil
}
func (s *sqlDB) UpdateFoo(ctx context.Context, f Foo) error {
sb := s.sq.
Update("foos").
Set("name", f.Name).
Set("note", f.Note).
Set("updated_at", f.UpdatedAt).
Where(sq.Eq{"id": f.ID})
return errors.Wrap(s.update(ctx, sb))
}
func (s *sqlDB) DelFoo(ctx context.Context, id string) error {
err := s.update(ctx, s.sq.Delete("foos").Where(sq.Eq{"id": id}))
return errors.Wrap(err)
}
func (s *sqlDB) exec(ctx context.Context, sqlizer sq.Sqlizer) (sql.Result, error) {
query, args, err := sqlizer.ToSql()
if err != nil {
return nil, errors.Wrap(err)
}
s.mu.Lock()
defer s.mu.Unlock()
res, err := s.db.ExecContext(ctx, query, args...)
if sqErr := new(sqlite3.Error); errors.As(err, sqErr) {
switch sqErr.Code {
case sqlite3.ErrConstraint:
return nil, ExistsErr("foo exists", errSQLiteFields(err))
}
}
return res, errors.Wrap(err, errSQLiteFields(err))
}
func (s *sqlDB) update(ctx context.Context, sqlizer sq.Sqlizer) error {
res, err := s.exec(ctx, sqlizer)
if err != nil {
return errors.Wrap(err, errSQLiteFields(err))
}
n, err := res.RowsAffected()
if err == nil && n == 0 {
return NotFoundErr("foo not found")
}
return nil
}
type entFoo struct {
ID string `db:"id"`
Name string `db:"name"`
Note string `db:"note"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
func errSQLiteFields(err error) []errors.KV {
if sqErr := new(sqlite3.Error); errors.As(err, sqErr) {
return errors.KVs(
"sqlite_err_code", sqErr.Code,
"sqlite_err_extended_code", sqErr.ExtendedCode,
"sqlite_system_errno", sqErr.SystemErrno,
)
}
return nil
}