-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdb.go
233 lines (204 loc) · 4.86 KB
/
db.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
package xsql
import (
"database/sql"
"encoding/json"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"reflect"
"strings"
)
type TimeFunc func(placeholder string) string
type DB struct {
Options *sqlOptions
raw *sql.DB
executor executor
query query
}
func New(db *sql.DB, opts ...SqlOption) *DB {
return &DB{
Options: mergeOptions(opts),
raw: db,
executor: executor{
Executor: db,
},
query: query{
Query: db,
},
}
}
func (t *DB) mergeOptions(opts []SqlOption) *sqlOptions {
cp := *t.Options // copy
for _, o := range opts {
o.apply(&cp)
}
return &cp
}
func (t *DB) Insert(data interface{}, opts ...SqlOption) (sql.Result, error) {
return t.executor.Insert(data, t.mergeOptions(opts))
}
func (t *DB) BatchInsert(data interface{}, opts ...SqlOption) (sql.Result, error) {
return t.executor.BatchInsert(data, t.mergeOptions(opts))
}
func (t *DB) Update(data interface{}, expr string, args ...interface{}) (sql.Result, error) {
return t.executor.Update(data, expr, args, t.Options)
}
func (t *DB) Model(s interface{}) *ModelExecutor {
return t.executor.model(s, t.Options)
}
func (t *DB) Exec(query string, args ...interface{}) (sql.Result, error) {
return t.executor.Exec(query, args, t.Options)
}
func (t *DB) Begin() (*Tx, error) {
tx, err := t.raw.Begin()
if err != nil {
return nil, err
}
return &Tx{
raw: tx,
DB: &DB{
Options: t.Options,
executor: executor{
Executor: tx,
},
query: query{
Query: tx,
},
},
}, nil
}
func (t *DB) Query(query string, args ...interface{}) ([]*Row, error) {
f, err := t.query.Fetch(query, args, t.Options)
if err != nil {
return nil, err
}
r, err := f.Rows()
if err != nil {
return nil, err
}
return r, nil
}
func (t *DB) QueryFirst(query string, args ...interface{}) (*Row, error) {
rows, err := t.Query(query, args...)
if err != nil {
return nil, err
}
if len(rows) == 0 {
return nil, sql.ErrNoRows
}
return rows[0], nil
}
func (t *DB) Find(i interface{}, query string, args ...interface{}) error {
query = tableReplace(i, query, t.Options)
f, err := t.query.Fetch(query, args, t.Options)
if err != nil {
return err
}
if err := f.Find(i); err != nil {
return err
}
return nil
}
func (t *DB) First(i interface{}, query string, args ...interface{}) error {
query = tableReplace(i, query, t.Options)
f, err := t.query.Fetch(query, args, t.Options)
if err != nil {
return err
}
if err := f.First(i); err != nil {
return err
}
return nil
}
func tableReplace(i interface{}, query string, opts *sqlOptions) string {
var table string
value := reflect.ValueOf(i)
switch value.Kind() {
case reflect.Ptr:
if value.Elem().IsValid() {
// *Test > Test
if value.Elem().Kind() == reflect.Struct {
// 先尝试*Test能不能找到
if tab, ok := value.Interface().(Table); ok {
table = tab.TableName()
break
}
}
// **Test > *Test
return tableReplace(value.Elem().Interface(), query, opts)
}
if tab, ok := value.Interface().(Table); ok {
table = tab.TableName()
break
}
table = getTypeName(i)
case reflect.Struct:
if tab, ok := value.Interface().(Table); ok {
table = tab.TableName()
break
}
// 也去尝试*Test能不能找到
valuePtr := reflect.New(value.Type())
if tab, ok := valuePtr.Interface().(Table); ok {
table = tab.TableName()
break
}
table = getTypeName(i)
case reflect.Array, reflect.Slice:
elemType := value.Type().Elem()
if elemType.Kind() == reflect.Ptr {
elemType = elemType.Elem()
}
if elemType.Kind() == reflect.Struct {
// 创建该类型的新实例
// Test
elemValue := reflect.New(elemType)
elemInstance := elemValue.Interface()
if tab, ok := elemInstance.(Table); ok {
table = tab.TableName()
break
}
// Test > *Test
if elemValue.CanAddr() {
elemPtrInstance := elemValue.Addr().Interface()
if tab, ok := elemPtrInstance.(Table); ok {
table = tab.TableName()
break
}
}
table = getTypeName(elemInstance)
} else {
return query // 如果元素不是结构体或其指针,返回原始查询
}
default:
return query // 如果不是结构体、数组或切片,返回原始查询
}
return strings.Replace(query, opts.TableKey, table, 1)
}
func getTypeName(i interface{}) string {
t := reflect.TypeOf(i)
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
return t.Name()
}
var ProtoMarshalOptions = protojson.MarshalOptions{
UseProtoNames: true,
UseEnumNumbers: true,
}
func marshal(v any) ([]byte, error) {
if m, ok := v.(proto.Message); ok {
return ProtoMarshalOptions.Marshal(m)
} else {
return json.Marshal(v)
}
}
var ProtoUnmarshalOptions = protojson.UnmarshalOptions{
DiscardUnknown: true,
}
func unmarshal(b []byte, v any) error {
if m, ok := v.(proto.Message); ok {
return ProtoUnmarshalOptions.Unmarshal(b, m)
} else {
return json.Unmarshal(b, v)
}
}