Skip to content

Commit

Permalink
add other default macros (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
bossinc authored Dec 9, 2021
1 parent c93210a commit 7af303a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
22 changes: 22 additions & 0 deletions pkg/macros/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ func ToTimeFilter(query *sqlds.Query, args []string) (string, error) {
return newTimeFilter(timeQueryTypeTo, query)
}

func TimeFilter(query *sqlds.Query, args []string) (string, error) {
if len(args) != 1 {
return "", fmt.Errorf("%w: expected 1 argument, received %d", sqlds.ErrorBadArgumentCount, len(args))
}

var (
column = args[0]
from = query.TimeRange.From.UTC().UnixMilli()
to = query.TimeRange.To.UTC().UnixMilli()
)

return fmt.Sprintf("%s >= '%d' AND %s <= '%d'", column, from, column, to), nil
}

func Table(query *sqlds.Query, args []string) (string, error) {
return query.Table, nil
}

func Column(query *sqlds.Query, args []string) (string, error) {
return query.Column, nil
}

// RemoveQuotesInArgs remove all quotes from macro arguments and return
func RemoveQuotesInArgs(args []string) []string {
updatedArgs := []string{}
Expand Down
7 changes: 5 additions & 2 deletions pkg/plugin/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ func (h *Clickhouse) Converters() []sqlutil.Converter {
// Macros returns list of macro functions convert the macros of raw query
func (h *Clickhouse) Macros() sqlds.Macros {
return map[string]sqlds.MacroFunc{
"from": macros.FromTimeFilter,
"to": macros.ToTimeFilter,
"from": macros.FromTimeFilter,
"to": macros.ToTimeFilter,
"timeFilter": macros.TimeFilter,
"table": macros.Table,
"column": macros.Column,
}
}

0 comments on commit 7af303a

Please sign in to comment.