-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.go
70 lines (58 loc) · 1.58 KB
/
insert.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
package query
import "strings"
type BulkInsert struct {
sb *strings.Builder
first bool
valueQuery string
args []any
}
/*
NewBulkInsert
eg 1) INSERT INTO chair(id, name, description, thumbnail, price, height, width, depth, color, features, kind, popularity, stock) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)
table -> "chair"
columns -> "id, name, description, thumbnail, price, height, width, depth, color, features, kind, popularity, stock"
valueQuery -> "(?,?,?,?,?,?,?,?,?,?,?,?,?)"
eg 2) INSERT INTO chair VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)
table -> "chair"
columns -> ""
valueQuery -> "(?,?,?,?,?,?,?,?,?,?,?,?,?)"
*/
func NewBulkInsert(table, colNames, valueQuery string) *BulkInsert {
sb := &strings.Builder{}
sb.WriteString("INSERT INTO ")
sb.WriteString(table)
sb.WriteString(" (")
sb.WriteString(colNames)
sb.WriteString(") VALUES ")
return &BulkInsert{
sb: sb,
first: true,
valueQuery: valueQuery,
}
}
func NewBulkInsertWithArgNum(table, colNames, valueQuery string, argNum int) *BulkInsert {
sb := &strings.Builder{}
sb.WriteString("INSERT INTO ")
sb.WriteString(table)
sb.WriteString(" (")
sb.WriteString(colNames)
sb.WriteString(") VALUES ")
return &BulkInsert{
sb: sb,
first: true,
valueQuery: valueQuery,
args: make([]any, 0, argNum),
}
}
func (b *BulkInsert) Add(args ...any) {
if b.first {
b.first = false
} else {
b.sb.WriteString(", ")
}
b.sb.WriteString(b.valueQuery)
b.args = append(b.args, args...)
}
func (b *BulkInsert) Query() (string, []any) {
return b.sb.String(), b.args
}