-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrites.go
More file actions
104 lines (83 loc) · 2.19 KB
/
writes.go
File metadata and controls
104 lines (83 loc) · 2.19 KB
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
package dbcl
const (
batchSize = 1000
)
func ExecInsert(q Querier, table string, cols []string, object interface{}) (uint64, error) {
query := prepareNamedInsert(table, cols)
res, err := q.NamedExec(query, object)
if err != nil {
return 0, err
}
insertID, err := res.LastInsertId()
if err != nil {
return 0, err
}
return uint64(insertID), nil
}
func ExecInsertNoID(q Querier, table string, cols []string, object interface{}) error {
query := prepareNamedInsert(table, cols)
_, err := q.NamedExec(query, object)
return err
}
func execBulkInsertFromQuery(q Querier, query string, objects []interface{}) error {
if len(objects) == 0 {
return nil
}
for i := 0; i <= len(objects)/batchSize; i++ {
startOffset := i * batchSize
endOffset := (i + 1) * batchSize
if endOffset >= len(objects) {
endOffset = len(objects)
}
if startOffset >= endOffset {
continue
}
_, err := q.NamedExec(query, objects[startOffset:endOffset])
if err != nil {
return err
}
}
return nil
}
func ExecBulkInsert(q Querier, table string, cols []string, objects []interface{}) error {
query := prepareNamedInsert(table, cols)
return execBulkInsertFromQuery(q, query, objects)
}
func ExecBulkInsertUpdateAdd(
q Querier,
table string,
insertCols, updateCols []string,
objects []interface{},
) error {
query := prepareNamedInsertUpdateWithOperator(table, insertCols, updateCols, "+")
return execBulkInsertFromQuery(q, query, objects)
}
func ExecBulkInsertUpdateSubtract(
q Querier,
table string,
insertCols, updateCols []string,
objects []interface{},
) error {
query := prepareNamedInsertUpdateWithOperator(table, insertCols, updateCols, "-")
return execBulkInsertFromQuery(q, query, objects)
}
func ExecBulkInsertUpdateOverwrite(
q Querier,
table string,
insertCols, updateCols []string,
objects []interface{},
) error {
query := prepareNamedInsertUpdateOverwrite(table, insertCols, updateCols)
return execBulkInsertFromQuery(q, query, objects)
}
func ExecUpdate(
q Querier,
table string,
updateCols, whereCols []string,
updatedAt bool,
obj interface{},
) error {
query := prepareNamedUpdate(table, updateCols, whereCols, updatedAt)
_, err := q.NamedExec(query, obj)
return err
}