Skip to content

Commit

Permalink
sql-readx: Add support for custom query and renaming fields (#155)
Browse files Browse the repository at this point in the history
* sql-readx: Add support for custom query and renaming fields

* support query variables
  • Loading branch information
jbsmith7741 authored Apr 2, 2021
1 parent fb1caa8 commit 20f6c03
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 10 additions & 6 deletions apps/workers/sql-readx/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,18 @@ func (o *options) NewWorker(info string) task.Worker {
query = string(b)
}

if query != "" {
//replace templated values in query
for k, v := range iOpts.Fields {
query = strings.Replace(query, "{"+k+"}", v, -1)
}
iOpts.Fields = make(map[string]string) // deleting map prevents any renaming of fields when a query is provided.
}

if iOpts.Exec {
if query == "" {
return task.InvalidWorker("query in url or path required")
}
for k, v := range iOpts.Fields {
// wrap key in bracket to prevent injection {key}
query = strings.Replace(query, "{"+k+"}", v, -1)
}
return &executer{
db: o.db,
Query: query,
Expand All @@ -83,7 +87,7 @@ func (o *options) NewWorker(info string) task.Worker {
}

// generate query from fields
if len(iOpts.Fields) > 0 {
if len(iOpts.Fields) > 0 && query == "" {
if s := strings.Split(iOpts.Table, "."); len(s) != 2 {
return task.InvalidWorker("invalid table %s (schema.table)", iOpts.Table)
}
Expand Down Expand Up @@ -132,7 +136,7 @@ func (w *executer) DoTask(ctx context.Context) (task.Result, string) {
end := time.Now()
id, _ := r.LastInsertId()
rows, _ := r.RowsAffected()

if w.Meta == nil {
w.Meta = task.NewMeta()
}
Expand Down
4 changes: 4 additions & 0 deletions apps/workers/sql-readx/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func TestNewWorker(t *testing.T) {
Input: "?exec&query={time} and {table}&field=time:2020-01-01|table:test.table",
Expected: "2020-01-01 and test.table",
},
"query with fields": {
Input: "?query=select * from {table}&field=table:schema.data&dest=./output",
Expected: "select * from schema.data",
},
"missing query": {
Input: "?exec",
ShouldErr: true,
Expand Down

0 comments on commit 20f6c03

Please sign in to comment.