-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-repo-insert-logic.go
144 lines (124 loc) · 3.43 KB
/
db-repo-insert-logic.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
/*
* Copyright (c) 2024.
* Created by Andy Pangaribuan <https://github.com/apangaribuan>.
*
* This product is protected by copyright and distributed under
* licenses restricting copying, distribution and decompilation.
* All Rights Reserved.
*/
package db
import (
"errors"
"strings"
"github.com/andypangaribuan/gmod/fm"
"github.com/andypangaribuan/gmod/gm"
"github.com/andypangaribuan/gmod/ice"
"github.com/andypangaribuan/gmod/mol"
)
func (slf *stuRepo[T]) insert(tx ice.DbTx, rid bool, args []any) *stuRepoResult[T] { // (*int64, *stuReport, error) {
var (
report = &stuReport{
tableName: slf.tableName,
insertColumn: slf.insertColumn,
insertArgSign: slf.insertArgSign,
args: args,
query: `INSERT INTO ::tableName (
::insertColumn
) VALUES (
::insertArgSign
)`,
}
err error
id *int64
execReport *mol.DbExecReport
)
if rid {
report.query += `
RETURNING id`
}
err = report.transform()
if err != nil {
return slf.result(report, err, id, nil)
}
if tx != nil {
if rid {
id, execReport, err = slf.ins.TxExecuteRID(tx, report.query, report.args...)
} else {
execReport, err = slf.ins.TxExecute(tx, report.query, report.args...)
}
} else {
if rid {
id, execReport, err = slf.ins.ExecuteRID(report.query, report.args...)
} else {
execReport, err = slf.ins.Execute(report.query, report.args...)
}
}
report.execReport = execReport
return slf.result(report, err, id, nil)
}
func (slf *stuRepo[T]) bulkInsert(tx ice.DbTx, entities []*T, chunkSize ...int) *stuRepoResult[T] { // (*stuReport, error) {
if tx == nil {
return slf.result(nil, errors.New("db: bulk insert only available via transaction"), nil, nil)
}
var (
report = &stuReport{
tableName: slf.tableName,
insertColumn: slf.insertColumn,
insertArgSign: slf.insertArgSign,
query: `INSERT INTO ::tableName (
::insertColumn
) VALUES `,
execReport: &mol.DbExecReport{
StartedAt: gm.Util.Timenow(),
},
}
err error
insertChunkSize = *fm.GetFirst(chunkSize, 1000)
partSize = make([]int, 0)
partArgs = make([][]any, 0)
part = make([]any, 0)
count = 0
)
defer func() {
report.execReport.FinishedAt = gm.Util.Timenow()
report.execReport.DurationMs = report.execReport.FinishedAt.Sub(report.execReport.StartedAt).Milliseconds()
}()
for _, e := range entities {
count++
ar := slf.insertColumnFunc(e)
if len(ar) == 0 {
break
}
part = append(part, ar...)
if count == insertChunkSize {
count = 0
partSize = append(partSize, len(part))
partArgs = append(partArgs, part)
part = make([]any, 0)
}
}
if len(part) > 0 {
partSize = append(partSize, len(part))
partArgs = append(partArgs, part)
}
if len(partSize) == 0 {
return slf.result(nil, errors.New("db: no data to process"), nil, nil)
}
valFormat := `(
::insertArgSign
)`
for i, args := range partArgs {
size := partSize[i] / strings.Count(report.insertArgSign, "?")
query := strings.ReplaceAll(report.query, "::tableName", report.tableName)
query = strings.ReplaceAll(query, "::insertColumn", report.insertColumn)
valQuery := strings.ReplaceAll(valFormat, "::insertArgSign", report.insertArgSign)
for i := 0; i < size; i++ {
query += fm.Ternary(i == 0, valQuery, ", "+valQuery)
}
_, err = slf.ins.TxExecute(tx, query, args...)
if err != nil {
return slf.result(report, err, nil, nil)
}
}
return slf.result(report, nil, nil, nil)
}