-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathactivity.go
231 lines (175 loc) · 4.48 KB
/
activity.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package sqlquery
import (
"database/sql"
"fmt"
"github.com/project-flogo/contrib/activity/sqlquery/util"
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/data/metadata"
"github.com/project-flogo/core/support/log"
)
func init() {
_ = activity.Register(&Activity{}, New)
}
const (
ovResults = "results"
)
var activityMd = activity.ToMetadata(&Settings{}, &Input{}, &Output{})
func New(ctx activity.InitContext) (activity.Activity, error) {
s := &Settings{MaxIdleConns: 2}
err := metadata.MapToStruct(ctx.Settings(), s, true)
if err != nil {
return nil, err
}
dbHelper, err := util.GetDbHelper(s.DbType)
if err != nil {
return nil, err
}
ctx.Logger().Debugf("DB: '%s'", s.DbType)
// todo move this to a shared connection object
db, err := getConnection(s)
if err != nil {
return nil, err
}
sqlStatement, err := util.NewSQLStatement(dbHelper, s.Query)
if err != nil {
return nil, err
}
if sqlStatement.Type() != util.StSelect {
return nil, fmt.Errorf("only select statement is supported")
}
act := &Activity{db: db, dbHelper: dbHelper, sqlStatement: sqlStatement}
if !s.DisablePrepared {
ctx.Logger().Debugf("Using PreparedStatement: %s", sqlStatement.PreparedStatementSQL())
act.stmt, err = db.Prepare(sqlStatement.PreparedStatementSQL())
if err != nil {
return nil, err
}
}
return act, nil
}
// Activity is a Counter Activity implementation
type Activity struct {
dbHelper util.DbHelper
db *sql.DB
sqlStatement *util.SQLStatement
stmt *sql.Stmt
labeledResults bool
}
// Metadata implements activity.Activity.Metadata
func (a *Activity) Metadata() *activity.Metadata {
return activityMd
}
func (a *Activity) Cleanup() error {
if a.stmt != nil {
err := a.stmt.Close()
log.RootLogger().Warnf("error cleaning up SQL Query activity: %v", err)
}
log.RootLogger().Tracef("cleaning up SQL Query activity")
return a.db.Close()
}
// Eval implements activity.Activity.Eval
func (a *Activity) Eval(ctx activity.Context) (done bool, err error) {
in := &Input{}
err = ctx.GetInputObject(in)
if err != nil {
return false, err
}
results, err := a.doSelect(in.Params)
if err != nil {
return false, err
}
err = ctx.SetOutput(ovResults, results)
if err != nil {
return false, err
}
return true, nil
}
func (a *Activity) doSelect(params map[string]interface{}) (interface{}, error) {
var err error
var rows *sql.Rows
if a.stmt != nil {
args := a.sqlStatement.GetPreparedStatementArgs(params)
rows, err = a.stmt.Query(args...)
} else {
rows, err = a.db.Query(a.sqlStatement.ToStatementSQL(params))
}
if err != nil {
return nil, err
}
defer rows.Close()
var results interface{}
if a.labeledResults {
results, err = getLabeledResults(a.dbHelper, rows)
} else {
results, err = getResults(a.dbHelper, rows)
}
if err != nil {
return nil, err
}
return results, nil
}
func getLabeledResults(dbHelper util.DbHelper, rows *sql.Rows) ([]map[string]interface{}, error) {
columns, err := rows.Columns()
if err != nil {
return nil, err
}
columnTypes, err := rows.ColumnTypes()
if err != nil {
return nil, err
}
var results []map[string]interface{}
for rows.Next() {
values := make([]interface{}, len(columnTypes))
for i := range values {
values[i] = dbHelper.GetScanType(columnTypes[i])
}
err = rows.Scan(values...)
if err != nil {
return nil, err
}
err = rows.Scan(values...)
if err != nil {
return nil, err
}
resMap := make(map[string]interface{}, len(columns))
for i, column := range columns {
resMap[column] = *(values[i].(*interface{}))
}
//todo do we need to do column mapping
results = append(results, resMap)
}
return results, rows.Err()
}
func getResults(dbHelper util.DbHelper, rows *sql.Rows) ([][]interface{}, error) {
columnTypes, err := rows.ColumnTypes()
if err != nil {
return nil, err
}
var results [][]interface{}
for rows.Next() {
values := make([]interface{}, len(columnTypes))
for i := range values {
values[i] = dbHelper.GetScanType(columnTypes[i])
}
err = rows.Scan(values...)
if err != nil {
return nil, err
}
results = append(results, values)
}
return results, rows.Err()
}
//todo move to shared connection
func getConnection(s *Settings) (*sql.DB, error) {
db, err := sql.Open(s.DriverName, s.DataSourceName)
if err != nil {
return nil, err
}
if s.MaxOpenConns > 0 {
db.SetMaxOpenConns(s.MaxOpenConns)
}
if s.MaxIdleConns != 2 {
db.SetMaxIdleConns(s.MaxIdleConns)
}
return db, err
}