diff --git a/pkg/redshift/fake/datasource.go b/pkg/redshift/fake/datasource.go index 94530f7..735d3f3 100644 --- a/pkg/redshift/fake/datasource.go +++ b/pkg/redshift/fake/datasource.go @@ -35,8 +35,8 @@ func (s *RedshiftFakeDatasource) GetAsyncDB(ctx context.Context, config backend. return nil, nil } -func (s *RedshiftFakeDatasource) Macros() sqlds.Macros { - return sqlds.Macros{} +func (s *RedshiftFakeDatasource) Macros() sqlutil.Macros { + return sqlutil.Macros{} } func (s *RedshiftFakeDatasource) Regions(ctx context.Context) ([]string, error) { diff --git a/pkg/redshift/macros.go b/pkg/redshift/macros.go index cee5520..8ce97f0 100644 --- a/pkg/redshift/macros.go +++ b/pkg/redshift/macros.go @@ -6,11 +6,12 @@ import ( "time" "github.com/grafana/grafana-plugin-sdk-go/backend/gtime" + "github.com/grafana/grafana-plugin-sdk-go/data/sqlutil" "github.com/grafana/sqlds/v3" "github.com/pkg/errors" ) -func macroTimeEpoch(query *sqlds.Query, args []string) (string, error) { +func macroTimeEpoch(query *sqlutil.Query, args []string) (string, error) { if len(args) != 1 { return "", errors.WithMessagef(sqlds.ErrorBadArgumentCount, "expected 1 argument, received %d", len(args)) } @@ -18,7 +19,7 @@ func macroTimeEpoch(query *sqlds.Query, args []string) (string, error) { return fmt.Sprintf("extract(epoch from %s) as \"time\"", args[0]), nil } -func macroTimeFilter(query *sqlds.Query, args []string) (string, error) { +func macroTimeFilter(query *sqlutil.Query, args []string) (string, error) { if len(args) != 1 { return "", errors.WithMessagef(sqlds.ErrorBadArgumentCount, "expected 1 argument, received %d", len(args)) } @@ -32,16 +33,16 @@ func macroTimeFilter(query *sqlds.Query, args []string) (string, error) { return fmt.Sprintf("%s BETWEEN '%s' AND '%s'", column, from, to), nil } -func macroTimeFrom(query *sqlds.Query, args []string) (string, error) { +func macroTimeFrom(query *sqlutil.Query, args []string) (string, error) { return fmt.Sprintf("'%s'", query.TimeRange.From.UTC().Format(time.RFC3339)), nil } -func macroTimeTo(query *sqlds.Query, args []string) (string, error) { +func macroTimeTo(query *sqlutil.Query, args []string) (string, error) { return fmt.Sprintf("'%s'", query.TimeRange.To.UTC().Format(time.RFC3339)), nil } -func macroTimeGroup(query *sqlds.Query, args []string) (string, error) { +func macroTimeGroup(query *sqlutil.Query, args []string) (string, error) { if len(args) != 2 { return "", errors.WithMessagef(sqlds.ErrorBadArgumentCount, "macro $__timeGroup needs time column and interval") } @@ -54,19 +55,19 @@ func macroTimeGroup(query *sqlds.Query, args []string) (string, error) { return fmt.Sprintf("floor(extract(epoch from %s)/%v)*%v AS \"time\"", args[0], interval.Seconds(), interval.Seconds()), nil } -func macroSchema(query *sqlds.Query, args []string) (string, error) { +func macroSchema(query *sqlutil.Query, args []string) (string, error) { return query.Schema, nil } -func macroTable(query *sqlds.Query, args []string) (string, error) { +func macroTable(query *sqlutil.Query, args []string) (string, error) { return query.Table, nil } -func macroColumn(query *sqlds.Query, args []string) (string, error) { +func macroColumn(query *sqlutil.Query, args []string) (string, error) { return query.Column, nil } -func macroUnixEpochFilter(query *sqlds.Query, args []string) (string, error) { +func macroUnixEpochFilter(query *sqlutil.Query, args []string) (string, error) { if len(args) != 1 { return "", errors.WithMessagef(sqlds.ErrorBadArgumentCount, "expected 1 argument, received %d", len(args)) } @@ -80,7 +81,7 @@ func macroUnixEpochFilter(query *sqlds.Query, args []string) (string, error) { return fmt.Sprintf("%s >= %d AND %s <= %d", column, from, args[0], to), nil } -func macroUnixEpochGroup(query *sqlds.Query, args []string) (string, error) { +func macroUnixEpochGroup(query *sqlutil.Query, args []string) (string, error) { if len(args) != 2 { return "", errors.WithMessagef(sqlds.ErrorBadArgumentCount, "macro $__unixEpochGroup needs time column and interval") } @@ -93,7 +94,7 @@ func macroUnixEpochGroup(query *sqlds.Query, args []string) (string, error) { return fmt.Sprintf(`floor(%s/%v)*%v AS "time"`, args[0], interval.Seconds(), interval.Seconds()), nil } -var macros = map[string]sqlds.MacroFunc{ +var macros = map[string]sqlutil.MacroFunc{ "timeEpoch": macroTimeEpoch, "timeFilter": macroTimeFilter, "timeFrom": macroTimeFrom, @@ -106,6 +107,6 @@ var macros = map[string]sqlds.MacroFunc{ "unixEpochGroup": macroUnixEpochGroup, } -func (s *RedshiftDatasource) Macros() sqlds.Macros { +func (s *RedshiftDatasource) Macros() sqlutil.Macros { return macros } diff --git a/pkg/redshift/macros_test.go b/pkg/redshift/macros_test.go index b5a7ad9..24f716d 100644 --- a/pkg/redshift/macros_test.go +++ b/pkg/redshift/macros_test.go @@ -5,6 +5,7 @@ import ( "time" "github.com/grafana/grafana-plugin-sdk-go/backend" + "github.com/grafana/grafana-plugin-sdk-go/data/sqlutil" "github.com/grafana/sqlds/v3" "github.com/pkg/errors" ) @@ -13,7 +14,7 @@ func Test_macros(t *testing.T) { tests := []struct { description string macro string - query *sqlds.Query + query *sqlutil.Query args []string expected string expectedErr error @@ -21,7 +22,7 @@ func Test_macros(t *testing.T) { { "adds time as Unix", "timeEpoch", - &sqlds.Query{}, + &sqlutil.Query{}, []string{"starttime"}, `extract(epoch from starttime) as "time"`, nil, @@ -29,7 +30,7 @@ func Test_macros(t *testing.T) { { "creates time filter", "timeFilter", - &sqlds.Query{ + &sqlutil.Query{ TimeRange: backend.TimeRange{ From: time.Date(2021, 6, 23, 0, 0, 0, 0, &time.Location{}), To: time.Date(2021, 6, 23, 1, 0, 0, 0, &time.Location{}), @@ -42,7 +43,7 @@ func Test_macros(t *testing.T) { { "wrong args for time filter", "timeFilter", - &sqlds.Query{}, + &sqlutil.Query{}, []string{}, "", sqlds.ErrorBadArgumentCount, @@ -50,7 +51,7 @@ func Test_macros(t *testing.T) { { "creates time from filter", "timeFrom", - &sqlds.Query{ + &sqlutil.Query{ TimeRange: backend.TimeRange{ From: time.Date(2021, 6, 23, 0, 0, 0, 0, &time.Location{}), To: time.Date(2021, 6, 23, 1, 0, 0, 0, &time.Location{}), @@ -63,7 +64,7 @@ func Test_macros(t *testing.T) { { "creates time to filter", "timeTo", - &sqlds.Query{ + &sqlutil.Query{ TimeRange: backend.TimeRange{ From: time.Date(2021, 6, 23, 0, 0, 0, 0, &time.Location{}), To: time.Date(2021, 6, 23, 1, 0, 0, 0, &time.Location{}), @@ -76,7 +77,7 @@ func Test_macros(t *testing.T) { { "creates time group", "timeGroup", - &sqlds.Query{}, + &sqlutil.Query{}, []string{"starttime", "'1m'"}, `floor(extract(epoch from starttime)/60)*60 AS "time"`, nil, @@ -84,7 +85,7 @@ func Test_macros(t *testing.T) { { "wrong args for time group", "timeGroup", - &sqlds.Query{}, + &sqlutil.Query{}, []string{}, "", sqlds.ErrorBadArgumentCount, @@ -92,7 +93,7 @@ func Test_macros(t *testing.T) { { "adds a schema", "schema", - &sqlds.Query{Schema: "foo"}, + &sqlutil.Query{Schema: "foo"}, []string{}, `foo`, nil, @@ -100,7 +101,7 @@ func Test_macros(t *testing.T) { { "adds a table", "table", - &sqlds.Query{Table: "foo"}, + &sqlutil.Query{Table: "foo"}, []string{}, `foo`, nil, @@ -108,7 +109,7 @@ func Test_macros(t *testing.T) { { "adds a column", "column", - &sqlds.Query{Column: "foo"}, + &sqlutil.Query{Column: "foo"}, []string{}, `foo`, nil, @@ -116,7 +117,7 @@ func Test_macros(t *testing.T) { { "unix epoch filter", "unixEpochFilter", - &sqlds.Query{ + &sqlutil.Query{ TimeRange: backend.TimeRange{ From: time.Date(2021, 6, 23, 0, 0, 0, 0, &time.Location{}), To: time.Date(2021, 6, 23, 1, 0, 0, 0, &time.Location{}), @@ -129,7 +130,7 @@ func Test_macros(t *testing.T) { { "unix epoch time group", "unixEpochGroup", - &sqlds.Query{}, + &sqlutil.Query{}, []string{"starttime", "1h"}, `floor(starttime/3600)*3600 AS "time"`, nil,