mirror of
https://github.com/grafana/grafana.git
synced 2024-11-28 19:54:10 -06:00
Fix MSSQL queries failing because of bad interpolation (#63167)
fix failing mssql queries
This commit is contained in:
parent
804bd08f11
commit
62b078e4e4
@ -49,9 +49,13 @@ func (m *msSQLMacroEngine) Interpolate(query *backend.DataQuery, timeRange backe
|
|||||||
// TODO: Return any error
|
// TODO: Return any error
|
||||||
rExp, _ := regexp.Compile(sExpr)
|
rExp, _ := regexp.Compile(sExpr)
|
||||||
var macroError error
|
var macroError error
|
||||||
formattedSql, fctCallArgsMap := replaceFunctionCallArgsWithPlaceholders(sql)
|
var fctCallArgsMap map[string]string
|
||||||
|
|
||||||
sql = m.ReplaceAllStringSubmatchFunc(rExp, formattedSql, func(groups []string) string {
|
if rExp.FindAllSubmatchIndex([]byte(sql), -1) != nil {
|
||||||
|
sql, fctCallArgsMap = replaceFunctionCallArgsWithPlaceholders(sql)
|
||||||
|
}
|
||||||
|
|
||||||
|
sql = m.ReplaceAllStringSubmatchFunc(rExp, sql, func(groups []string) string {
|
||||||
args := strings.Split(groups[2], ",")
|
args := strings.Split(groups[2], ",")
|
||||||
for i, arg := range args {
|
for i, arg := range args {
|
||||||
args[i] = strings.Trim(arg, " ")
|
args[i] = strings.Trim(arg, " ")
|
||||||
|
@ -349,6 +349,20 @@ func TestMacroEngine(t *testing.T) {
|
|||||||
require.NotNil(t, err)
|
require.NotNil(t, err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("should return unmodified sql if there are no macros present", func(t *testing.T) {
|
||||||
|
sqls := []string{
|
||||||
|
"select * from table",
|
||||||
|
"select count(val) from table",
|
||||||
|
"select col1, col2,col3, col4 from table where col1 = 'val1' and col2 = 'val2' order by col1 asc",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, sql := range sqls {
|
||||||
|
actual, err := engine.Interpolate(query, timeRange, sql)
|
||||||
|
require.Nil(t, err)
|
||||||
|
require.Equal(t, sql, actual)
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user