-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity.go
161 lines (123 loc) · 3.37 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
package mysql_activity
import (
"fmt"
"github.com/TIBCOSoftware/flogo-lib/core/data"
"strings"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/logger"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
// log is the default package logger
var log = logger.GetLogger("activity-mysql")
const (
ivDataSourceName = "dataSourceName"
ivQuery = "query"
ivParams = "params"
ivColumnTypes = "columnTypes"
ovResults = "results"
)
// MyActivity is a stub for your Activity implementation
type MyActivity struct {
metadata *activity.Metadata
}
// NewActivity creates a new AppActivity
func NewActivity(metadata *activity.Metadata) activity.Activity {
return &MyActivity{metadata: metadata}
}
// Metadata implements activity.Activity.Metadata
func (a *MyActivity) Metadata() *activity.Metadata {
return a.metadata
}
// Eval implements activity.Activity.Eval
func (a *MyActivity) Eval(context activity.Context) (done bool, err error) {
dsNameIn := context.GetInput(ivDataSourceName)
dataSourceName, ok := dsNameIn.(string)
if !ok || dataSourceName == "" {
return false, fmt.Errorf("dataSourceName not set")
}
log.Debugf("dataSourceName: %s", dataSourceName)
queryIn := context.GetInput(ivQuery)
query, ok := queryIn.(string)
if !ok || query == "" {
return false, fmt.Errorf("query not set")
}
query = strings.TrimSpace(query)
queryParts := strings.Fields(query)
if len(queryParts) == 0 {
return false, fmt.Errorf("invalid query '%s'", query)
}
log.Debugf("query: '%s'", query)
queryType := strings.ToLower(queryParts[0])
switch queryType {
case "select":
paramsIn := context.GetInput(ivParams)
var params map[string]interface{}
if paramsIn != nil {
params, ok = paramsIn.(map[string]interface{})
if !ok {
return false, fmt.Errorf("params is not valid: %v", params)
}
}
ctIn := context.GetInput(ivColumnTypes)
var columnTypes map[string]string
if ctIn != nil {
columnTypes, ok = ctIn.(map[string]string)
if !ok {
return false, fmt.Errorf("columnTypes is not valid: %v", params)
}
}
results, err := DoSelect(dataSourceName, query, params, columnTypes)
if err != nil {
return false, err
}
context.SetOutput(ovResults, results)
default:
return false, fmt.Errorf("queryType '%s' not supported", queryType)
}
return true, nil
}
func DoSelect(dataSourceName, query string, params map[string]interface{}, columnTypes map[string]string) ([]map[string]interface{}, error) {
db, err := sqlx.Connect("mysql", dataSourceName)
if err != nil {
return nil, err
}
defer db.Close()
var rows *sqlx.Rows
if len(params) > 0 {
rows, err = db.NamedQuery(query, params)
} else {
rows, err = db.Queryx(query)
}
if err != nil {
return nil, err
}
var results []map[string]interface{}
for rows.Next() {
row := make(map[string]interface{})
err := rows.MapScan(row)
if err != nil {
return nil, err
}
for k, encoded := range row {
switch t := encoded.(type) {
case []byte:
strVal := string(t)
dataType := data.TypeString
if columnTypes != nil {
if ct, ok := columnTypes[k]; ok {
if dt, ok := data.ToTypeEnum(ct); ok {
dataType = dt
}
}
}
row[k], err = data.CoerceToValue(strVal, dataType)
if err != nil {
return nil, err
}
}
}
results = append(results, row)
}
return results, nil
}