-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbulk.go
81 lines (73 loc) · 1.49 KB
/
bulk.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
package sqlz
import (
"context"
"strings"
)
type BulkInsert struct {
Prefix string
Suffix string
Row string
Sep string
ex ExecerContext
stmt string
size int
rows int
args []interface{}
}
func (bi *BulkInsert) Init(ex ExecerContext, size int) {
if len(bi.Sep) == 0 {
bi.Sep = ", "
}
if size < 1 {
size = 1
}
bi.ex = ex
bi.stmt = bi.sql(size)
bi.size = size
bi.rows = 0
bi.args = []interface{}{}
}
func (bi *BulkInsert) Next(ctx context.Context, args ...interface{}) (err error) {
bi.rows, bi.args = bi.rows+1, append(bi.args, args...)
if bi.rows < bi.size {
return nil
}
if bi.rows == bi.size {
_, err = bi.ex.ExecContext(ctx, bi.stmt, bi.args...)
} else {
_, err = bi.ex.ExecContext(ctx, bi.sql(bi.rows), bi.args...)
}
if err == nil {
bi.rows, bi.args = 0, bi.args[:0]
}
return
}
func (bi *BulkInsert) Done(ctx context.Context) (err error) {
if bi.rows == 0 {
return nil
}
_, err = bi.ex.ExecContext(ctx, bi.sql(bi.rows), bi.args...)
if err == nil {
bi.rows, bi.args = 0, bi.args[:0]
}
return
}
func (bi *BulkInsert) SQL() (string, []interface{}) {
return bi.sql(bi.rows), bi.args
}
func (bi *BulkInsert) sql(rows int) string {
if rows < 1 {
return ""
}
var buf strings.Builder
buf.Grow(len(bi.Prefix) + len(bi.Suffix) + len(bi.Row)*bi.rows + len(bi.Sep)*(bi.rows-1))
buf.WriteString(bi.Prefix)
for i := 0; i < rows; i++ {
if i > 0 {
buf.WriteString(bi.Sep)
}
buf.WriteString(bi.Row)
}
buf.WriteString(bi.Suffix)
return buf.String()
}