-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqliter.go
162 lines (141 loc) · 3.68 KB
/
sqliter.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
package sqliter
import (
"database/sql"
"fmt"
"sync"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
const InMemory = ":memory:"
type Sqliter struct {
db *sqlx.DB
mutex sync.RWMutex
}
func Open(filename string) (*Sqliter, error) {
if db, err := sqlx.Connect("sqlite3", filename); err == nil {
return &Sqliter{db: db}, nil
} else {
return nil, err
}
}
func (s *Sqliter) Close() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.db.Close()
}
func (s *Sqliter) Exec(q string, args ...interface{}) (sql.Result, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.db.Exec(q, args)
}
func (s *Sqliter) CreateTable(sample interface{}) error {
s.mutex.Lock()
defer s.mutex.Unlock()
name, fields, err := decomposeStruct(sample)
if err != nil {
return err
}
q, err := fieldsListToCreateTable(name, fields)
if err != nil {
return err
}
_, err = s.db.Exec(q)
return err
}
func (s *Sqliter) Insert(obj interface{}) (int64, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
name, fields, err := decomposeStruct(obj)
if err != nil {
return 0, err
}
q, vals, err := fieldsListToInsertRecord(name, fields)
if err != nil {
return 0, err
}
r, err := s.db.Exec(q, vals...)
if err != nil {
return 0, err
}
return r.LastInsertId()
}
func (s *Sqliter) ReadOne(outPtr interface{}, where string, args ...interface{}) error {
s.mutex.RLock()
defer s.mutex.RUnlock()
name, fields, err := decomposeStruct(outPtr)
if err != nil {
return err
}
q, err := fieldListToReadRecord(name, fields, where)
if err != nil {
return err
}
if err = s.db.Get(outPtr, q, args...); err != nil {
return fmt.Errorf("get %s not found where %s, %s, %v", name, where, args, err.Error())
}
return nil
}
func (s *Sqliter) ReadMany(outPtr interface{}, where string, args ...interface{}) error {
s.mutex.RLock()
defer s.mutex.RUnlock()
name, fields, err := decomposeStruct(outPtr)
if err != nil {
return err
}
q, err := fieldListToReadRecord(name, fields, where)
if err != nil {
return err
}
if err = s.db.Select(outPtr, q, args...); err != nil {
return fmt.Errorf("select %s err where %s, %s", name, where, err.Error())
}
return nil
}
func (s *Sqliter) Update(obj interface{}, where string, args ...interface{}) error {
s.mutex.Lock()
defer s.mutex.Unlock()
name, fields, err := decomposeStruct(obj)
if err != nil {
return err
}
q, vals, err := fieldsListToUpdateRecord(name, fields)
if err != nil {
return err
}
vals = append(vals, args...)
_, err = s.db.Exec(q, vals...)
return err
}
func (s *Sqliter) Upsert(obj interface{}, where string, args ...interface{}) (int64,error) {
name, _, err := decomposeStruct(obj)
if err != nil {
return 0, err
}
count := 0
q := "SELECT COUNT(1) FROM " + name + " WHERE " + where
fmt.Printf("%s, %v\n", q, args)
if err = s.db.Get(&count, "SELECT COUNT(1) FROM " + name + " WHERE " + where, args...); err != nil {
return 0, fmt.Errorf("count %s where %s, %s", name, where, err.Error())
}
switch count {
case 0: return s.Insert(obj)
case 1: return 0, s.Update(obj, where, args...) // TODO return the updated record id
default: return 0, fmt.Errorf("upsert err: %d existing records", count)
}
}
func (s *Sqliter) Delete(sample interface{}, where string, args ...interface{}) error {
name, _, err := decomposeStruct(sample)
if err != nil {
return fmt.Errorf("Delete decompose struct err:%w", err)
}
return s.DeleteFrom(name, where, args...)
}
func (s *Sqliter) DeleteFrom(table, where string, args ...interface{}) error {
s.mutex.Lock()
defer s.mutex.Unlock()
q := fmt.Sprintf("DELETE FROM %s WHERE %s", table, where)
if _, err := s.db.Exec(q, args...); err != nil {
return fmt.Errorf("DeleteFrom('%s'):%w", q, err)
}
return nil
}