-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
148 lines (130 loc) · 3 KB
/
job.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
package sqlpipe
import (
"errors"
"fmt"
"log"
"strings"
"github.com/jmoiron/sqlx"
)
var ErrDimEmpty = errors.New("dim empty error")
type Job struct {
Name string
DBName string
Query string
SubJobs []*Job
Next *Job
affected int
}
func (j *Job) Run(dims []map[string]string, dbs map[string]*sqlx.DB) (cols []string, data []map[string]string, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("job name: %s, %v", j.Name, err)
}
}()
query := j.Query
placeholders := map[string]string{}
args := []interface{}{}
for _, sub := range j.SubJobs {
col, onecolrows, placeholder, err1 := sub.runSubJob(dbs)
if err1 != nil {
return nil, nil, err1
}
args = append(args, onecolrows...)
placeholders[col] = placeholder
}
db := dbs[j.DBName]
var n int
if len(dims) == 0 {
query, err = Tpl(query, placeholders)
log.Println("first query is: ", query, args)
cols, data, n, err = execute(db, query, args...)
if err != nil {
return
}
} else {
for i, dim := range dims {
newDim, err := MergeMapWithNoDupKey(dim, placeholders)
if err != nil {
return nil, nil, err
}
querytmp, err := Tpl(query, newDim)
if err != nil {
return nil, nil, err
}
log.Println("query is: ", querytmp, args, dim)
colstmp, datatmp, ntmp, err := execute(db, querytmp, args...)
if err != nil {
return nil, nil, err
}
if i == 0 {
cols = colstmp
}
data = append(data, datatmp...)
n += ntmp
}
}
if !j.isLastJob() {
if len(data) == 0 {
err = ErrDimEmpty
return
}
cols, data, err = j.Next.Run(data, dbs)
if err != nil {
return
}
}
j.affected = n
return
}
func (j *Job) runSubJob(dbs map[string]*sqlx.DB) (col string, args []any, placeholder string, err error) {
query := j.Query
db := dbs[j.DBName]
var cols []string
var data []map[string]string
log.Println("sub job query is: ", query)
cols, data, _, err = execute(db, query)
if err != nil {
err = fmt.Errorf("job name: %s, %v", j.Name, err)
return
}
col = cols[0]
args = getOneColFromRows(col, data)
placeholder = genPlaceholders(len(args))
return
}
func (j *Job) isLastJob() bool {
return j.Next == nil
}
func (j *Job) isLastJobSelect() bool {
last := j.lastJob()
query := strings.TrimSpace(last.Query)
query = strings.ToLower(query)
return strings.HasPrefix(query, "select")
}
func (j *Job) getAffected() int {
return j.lastJob().affected
}
func (j *Job) lastJob() *Job {
if j.isLastJob() {
return j
}
return j.Next.lastJob()
}
func execute(db *sqlx.DB, query string, args ...any) (cols []string, data []map[string]string, n int, err error) {
query2 := strings.TrimSpace(query)
query2 = strings.ToLower(query2)
if strings.HasPrefix(query2, "select") {
sqlrows, err := db.Query(query, args...)
if err != nil {
return nil, nil, 0, err
}
cols, data, err = Rows(sqlrows)
return cols, data, 0, err
}
affected, err := db.Exec(query, args...)
if err != nil {
return nil, nil, 0, err
}
n64, err := affected.RowsAffected()
return nil, nil, int(n64), err
}