2017-03-29 15:54:07 -05:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2019-09-24 13:50:49 -05:00
|
|
|
"errors"
|
2017-03-29 15:54:07 -05:00
|
|
|
"fmt"
|
2019-11-06 02:54:53 -06:00
|
|
|
"net/url"
|
2018-01-18 04:44:36 -06:00
|
|
|
"reflect"
|
2017-03-31 04:45:25 -05:00
|
|
|
"strconv"
|
2018-10-17 04:45:06 -05:00
|
|
|
"strings"
|
2021-05-05 09:46:07 -05:00
|
|
|
"time"
|
2019-01-15 06:29:56 -06:00
|
|
|
|
2019-09-24 13:50:49 -05:00
|
|
|
"github.com/VividCortex/mysqlerr"
|
2021-05-05 09:46:07 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
|
2021-05-19 16:53:41 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/httpclient"
|
2019-09-10 14:50:04 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
|
2017-04-21 08:07:43 -05:00
|
|
|
"github.com/go-sql-driver/mysql"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2017-03-29 15:54:07 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-03-08 00:02:49 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2019-09-10 14:50:04 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/sqleng"
|
2021-05-05 09:46:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dateFormat = "2006-01-02"
|
|
|
|
dateTimeFormat1 = "2006-01-02 15:04:05"
|
|
|
|
dateTimeFormat2 = "2006-01-02T15:04:05Z"
|
2017-03-29 15:54:07 -05:00
|
|
|
)
|
|
|
|
|
2019-11-06 02:54:53 -06:00
|
|
|
func characterEscape(s string, escapeChar string) string {
|
2020-09-22 09:22:19 -05:00
|
|
|
return strings.ReplaceAll(s, escapeChar, url.QueryEscape(escapeChar))
|
2019-11-06 02:54:53 -06:00
|
|
|
}
|
|
|
|
|
2021-04-22 20:03:11 -05:00
|
|
|
//nolint: staticcheck // plugins.DataPlugin deprecated
|
2021-05-19 16:53:41 -05:00
|
|
|
func New(httpClientProvider httpclient.Provider) func(datasource *models.DataSource) (plugins.DataPlugin, error) {
|
|
|
|
//nolint: staticcheck // plugins.DataPlugin deprecated
|
|
|
|
return func(datasource *models.DataSource) (plugins.DataPlugin, error) {
|
|
|
|
logger := log.New("tsdb.mysql")
|
|
|
|
|
|
|
|
protocol := "tcp"
|
|
|
|
if strings.HasPrefix(datasource.Url, "/") {
|
|
|
|
protocol = "unix"
|
|
|
|
}
|
2019-11-06 02:54:53 -06:00
|
|
|
|
2021-05-19 16:53:41 -05:00
|
|
|
cnnstr := fmt.Sprintf("%s:%s@%s(%s)/%s?collation=utf8mb4_unicode_ci&parseTime=true&loc=UTC&allowNativePasswords=true",
|
|
|
|
characterEscape(datasource.User, ":"),
|
|
|
|
datasource.DecryptedPassword(),
|
|
|
|
protocol,
|
|
|
|
characterEscape(datasource.Url, ")"),
|
|
|
|
characterEscape(datasource.Database, "?"),
|
|
|
|
)
|
2019-01-15 06:29:56 -06:00
|
|
|
|
2021-05-19 16:53:41 -05:00
|
|
|
tlsConfig, err := datasource.GetTLSConfig(httpClientProvider)
|
|
|
|
if err != nil {
|
2019-10-22 09:16:53 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-15 06:29:56 -06:00
|
|
|
|
2021-05-19 16:53:41 -05:00
|
|
|
if tlsConfig.RootCAs != nil || len(tlsConfig.Certificates) > 0 {
|
|
|
|
tlsConfigString := fmt.Sprintf("ds%d", datasource.Id)
|
|
|
|
if err := mysql.RegisterTLSConfig(tlsConfigString, tlsConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cnnstr += "&tls=" + tlsConfigString
|
|
|
|
}
|
2017-03-30 06:46:46 -05:00
|
|
|
|
2021-05-19 16:53:41 -05:00
|
|
|
if setting.Env == setting.Dev {
|
|
|
|
logger.Debug("getEngine", "connection", cnnstr)
|
|
|
|
}
|
2017-03-29 15:54:07 -05:00
|
|
|
|
2021-05-19 16:53:41 -05:00
|
|
|
config := sqleng.DataPluginConfiguration{
|
|
|
|
DriverName: "mysql",
|
|
|
|
ConnectionString: cnnstr,
|
|
|
|
Datasource: datasource,
|
|
|
|
TimeColumnNames: []string{"time", "time_sec"},
|
|
|
|
MetricColumnTypes: []string{"CHAR", "VARCHAR", "TINYTEXT", "TEXT", "MEDIUMTEXT", "LONGTEXT"},
|
|
|
|
}
|
2017-04-21 08:07:43 -05:00
|
|
|
|
2021-05-19 16:53:41 -05:00
|
|
|
rowTransformer := mysqlQueryResultTransformer{
|
|
|
|
log: logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
return sqleng.NewDataPlugin(config, &rowTransformer, newMysqlMacroEngine(logger), logger)
|
|
|
|
}
|
2017-04-21 08:07:43 -05:00
|
|
|
}
|
|
|
|
|
2019-09-24 13:50:49 -05:00
|
|
|
type mysqlQueryResultTransformer struct {
|
2018-07-26 11:10:45 -05:00
|
|
|
log log.Logger
|
|
|
|
}
|
2018-01-18 04:44:36 -06:00
|
|
|
|
2019-09-24 13:50:49 -05:00
|
|
|
func (t *mysqlQueryResultTransformer) TransformQueryError(err error) error {
|
2020-11-19 07:47:17 -06:00
|
|
|
var driverErr *mysql.MySQLError
|
|
|
|
if errors.As(err, &driverErr) {
|
|
|
|
if driverErr.Number != mysqlerr.ER_PARSE_ERROR && driverErr.Number != mysqlerr.ER_BAD_FIELD_ERROR &&
|
|
|
|
driverErr.Number != mysqlerr.ER_NO_SUCH_TABLE {
|
2019-09-24 13:50:49 -05:00
|
|
|
t.log.Error("query error", "err", err)
|
|
|
|
return errQueryFailed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-05 04:29:39 -06:00
|
|
|
var errQueryFailed = errors.New("query failed - please inspect Grafana server log for details")
|
2021-05-05 09:46:07 -05:00
|
|
|
|
|
|
|
func (t *mysqlQueryResultTransformer) GetConverterList() []sqlutil.StringConverter {
|
|
|
|
// For the MySQL driver , we have these possible data types:
|
|
|
|
// https://www.w3schools.com/sql/sql_datatypes.asp#:~:text=In%20MySQL%20there%20are%20three,numeric%2C%20and%20date%20and%20time.
|
|
|
|
// Since by default, we convert all into String, we need only to handle the Numeric data types
|
|
|
|
return []sqlutil.StringConverter{
|
|
|
|
{
|
|
|
|
Name: "handle DOUBLE",
|
|
|
|
InputScanKind: reflect.Struct,
|
|
|
|
InputTypeName: "DOUBLE",
|
|
|
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
|
|
|
Replacer: &sqlutil.StringFieldReplacer{
|
|
|
|
OutputFieldType: data.FieldTypeNullableFloat64,
|
|
|
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseFloat(*in, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &v, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "handle BIGINT",
|
|
|
|
InputScanKind: reflect.Struct,
|
|
|
|
InputTypeName: "BIGINT",
|
|
|
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
|
|
|
Replacer: &sqlutil.StringFieldReplacer{
|
|
|
|
OutputFieldType: data.FieldTypeNullableInt64,
|
|
|
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseInt(*in, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &v, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "handle DECIMAL",
|
|
|
|
InputScanKind: reflect.Slice,
|
|
|
|
InputTypeName: "DECIMAL",
|
|
|
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
|
|
|
Replacer: &sqlutil.StringFieldReplacer{
|
|
|
|
OutputFieldType: data.FieldTypeNullableFloat64,
|
|
|
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseFloat(*in, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &v, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "handle DATETIME",
|
|
|
|
InputScanKind: reflect.Struct,
|
|
|
|
InputTypeName: "DATETIME",
|
|
|
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
|
|
|
Replacer: &sqlutil.StringFieldReplacer{
|
|
|
|
OutputFieldType: data.FieldTypeNullableTime,
|
|
|
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
v, err := time.Parse(dateTimeFormat1, *in)
|
|
|
|
if err == nil {
|
|
|
|
return &v, nil
|
|
|
|
}
|
|
|
|
v, err = time.Parse(dateTimeFormat2, *in)
|
|
|
|
if err == nil {
|
|
|
|
return &v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "handle DATE",
|
|
|
|
InputScanKind: reflect.Struct,
|
|
|
|
InputTypeName: "DATE",
|
|
|
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
|
|
|
Replacer: &sqlutil.StringFieldReplacer{
|
|
|
|
OutputFieldType: data.FieldTypeNullableTime,
|
|
|
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
v, err := time.Parse(dateFormat, *in)
|
|
|
|
if err == nil {
|
|
|
|
return &v, nil
|
|
|
|
}
|
|
|
|
v, err = time.Parse(dateTimeFormat1, *in)
|
|
|
|
if err == nil {
|
|
|
|
return &v, nil
|
|
|
|
}
|
|
|
|
v, err = time.Parse(dateTimeFormat2, *in)
|
|
|
|
if err == nil {
|
|
|
|
return &v, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "handle TIMESTAMP",
|
|
|
|
InputScanKind: reflect.Struct,
|
|
|
|
InputTypeName: "TIMESTAMP",
|
|
|
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
|
|
|
Replacer: &sqlutil.StringFieldReplacer{
|
|
|
|
OutputFieldType: data.FieldTypeNullableTime,
|
|
|
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
v, err := time.Parse(dateTimeFormat1, *in)
|
|
|
|
if err == nil {
|
|
|
|
return &v, nil
|
|
|
|
}
|
|
|
|
v, err = time.Parse(dateTimeFormat2, *in)
|
|
|
|
if err == nil {
|
|
|
|
return &v, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "handle YEAR",
|
|
|
|
InputScanKind: reflect.Struct,
|
|
|
|
InputTypeName: "YEAR",
|
|
|
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
|
|
|
Replacer: &sqlutil.StringFieldReplacer{
|
|
|
|
OutputFieldType: data.FieldTypeNullableInt64,
|
|
|
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseInt(*in, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &v, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "handle INT",
|
|
|
|
InputScanKind: reflect.Struct,
|
|
|
|
InputTypeName: "INT",
|
|
|
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
|
|
|
Replacer: &sqlutil.StringFieldReplacer{
|
|
|
|
OutputFieldType: data.FieldTypeNullableInt64,
|
|
|
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseInt(*in, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &v, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "handle FLOAT",
|
|
|
|
InputScanKind: reflect.Struct,
|
|
|
|
InputTypeName: "FLOAT",
|
|
|
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
|
|
|
Replacer: &sqlutil.StringFieldReplacer{
|
|
|
|
OutputFieldType: data.FieldTypeNullableFloat64,
|
|
|
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseFloat(*in, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &v, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|