2017-03-29 15:54:07 -05:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2017-03-30 06:46:46 -05:00
|
|
|
"database/sql"
|
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"
|
2019-01-15 06:29:56 -06:00
|
|
|
|
2019-09-24 13:50:49 -05:00
|
|
|
"github.com/VividCortex/mysqlerr"
|
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"
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
2019-09-10 14:50:04 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/sqleng"
|
2020-04-01 08:57:21 -05:00
|
|
|
"xorm.io/core"
|
2017-03-29 15:54:07 -05:00
|
|
|
)
|
|
|
|
|
2017-03-30 06:46:46 -05:00
|
|
|
func init() {
|
2018-07-26 11:10:45 -05:00
|
|
|
tsdb.RegisterTsdbQueryEndpoint("mysql", newMysqlQueryEndpoint)
|
2017-03-30 06:46:46 -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
|
|
|
}
|
|
|
|
|
2018-07-26 11:10:45 -05:00
|
|
|
func newMysqlQueryEndpoint(datasource *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
|
|
|
|
logger := log.New("tsdb.mysql")
|
2017-03-29 15:54:07 -05:00
|
|
|
|
2018-10-17 12:45:14 -05:00
|
|
|
protocol := "tcp"
|
2018-10-17 04:45:06 -05:00
|
|
|
if strings.HasPrefix(datasource.Url, "/") {
|
|
|
|
protocol = "unix"
|
|
|
|
}
|
2019-11-06 02:54:53 -06:00
|
|
|
|
2017-11-21 06:21:19 -06:00
|
|
|
cnnstr := fmt.Sprintf("%s:%s@%s(%s)/%s?collation=utf8mb4_unicode_ci&parseTime=true&loc=UTC&allowNativePasswords=true",
|
2019-11-06 02:54:53 -06:00
|
|
|
characterEscape(datasource.User, ":"),
|
2019-11-14 04:16:38 -06:00
|
|
|
datasource.DecryptedPassword(),
|
2018-10-17 04:45:06 -05:00
|
|
|
protocol,
|
2019-11-06 02:54:53 -06:00
|
|
|
characterEscape(datasource.Url, ")"),
|
|
|
|
characterEscape(datasource.Database, "?"),
|
2017-10-10 08:19:14 -05:00
|
|
|
)
|
2019-01-15 06:29:56 -06:00
|
|
|
|
2019-01-28 12:38:56 -06:00
|
|
|
tlsConfig, err := datasource.GetTLSConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-01-15 06:29:56 -06:00
|
|
|
}
|
|
|
|
|
2019-01-28 12:38:56 -06:00
|
|
|
if tlsConfig.RootCAs != nil || len(tlsConfig.Certificates) > 0 {
|
2019-02-08 11:08:07 -06:00
|
|
|
tlsConfigString := fmt.Sprintf("ds%d", datasource.Id)
|
2019-10-22 09:16:53 -05:00
|
|
|
if err := mysql.RegisterTLSConfig(tlsConfigString, tlsConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-08 11:08:07 -06:00
|
|
|
cnnstr += "&tls=" + tlsConfigString
|
2019-01-15 06:29:56 -06:00
|
|
|
}
|
|
|
|
|
2020-10-02 08:45:45 -05:00
|
|
|
if setting.Env == setting.Dev {
|
2019-03-15 05:53:30 -05:00
|
|
|
logger.Debug("getEngine", "connection", cnnstr)
|
|
|
|
}
|
2017-03-30 06:46:46 -05:00
|
|
|
|
2019-09-10 14:50:04 -05:00
|
|
|
config := sqleng.SqlQueryEndpointConfiguration{
|
2018-07-26 11:10:45 -05:00
|
|
|
DriverName: "mysql",
|
|
|
|
ConnectionString: cnnstr,
|
|
|
|
Datasource: datasource,
|
|
|
|
TimeColumnNames: []string{"time", "time_sec"},
|
|
|
|
MetricColumnTypes: []string{"CHAR", "VARCHAR", "TINYTEXT", "TEXT", "MEDIUMTEXT", "LONGTEXT"},
|
2017-03-29 15:54:07 -05:00
|
|
|
}
|
|
|
|
|
2019-09-24 13:50:49 -05:00
|
|
|
rowTransformer := mysqlQueryResultTransformer{
|
2018-07-26 11:10:45 -05:00
|
|
|
log: logger,
|
2017-04-19 10:26:29 -05:00
|
|
|
}
|
2017-04-21 08:07:43 -05:00
|
|
|
|
2019-09-24 13:50:49 -05:00
|
|
|
return sqleng.NewSqlQueryEndpoint(&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) TransformQueryResult(columnTypes []*sql.ColumnType, rows *core.Rows) (tsdb.RowValues, error) {
|
2018-07-26 11:10:45 -05:00
|
|
|
values := make([]interface{}, len(columnTypes))
|
2017-04-21 08:07:43 -05:00
|
|
|
|
2018-01-18 04:44:36 -06:00
|
|
|
for i := range values {
|
2018-07-26 11:10:45 -05:00
|
|
|
scanType := columnTypes[i].ScanType()
|
2018-01-18 04:44:36 -06:00
|
|
|
values[i] = reflect.New(scanType).Interface()
|
|
|
|
|
2018-07-26 11:10:45 -05:00
|
|
|
if columnTypes[i].DatabaseTypeName() == "BIT" {
|
2017-08-08 05:00:39 -05:00
|
|
|
values[i] = new([]byte)
|
2017-04-21 08:07:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Scan(values...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-26 11:10:45 -05:00
|
|
|
for i := 0; i < len(columnTypes); i++ {
|
2018-01-18 04:44:36 -06:00
|
|
|
typeName := reflect.ValueOf(values[i]).Type().String()
|
|
|
|
|
|
|
|
switch typeName {
|
|
|
|
case "*sql.RawBytes":
|
|
|
|
values[i] = string(*values[i].(*sql.RawBytes))
|
|
|
|
case "*mysql.NullTime":
|
|
|
|
sqlTime := (*values[i].(*mysql.NullTime))
|
|
|
|
if sqlTime.Valid {
|
|
|
|
values[i] = sqlTime.Time
|
|
|
|
} else {
|
|
|
|
values[i] = nil
|
|
|
|
}
|
|
|
|
case "*sql.NullInt64":
|
|
|
|
nullInt64 := (*values[i].(*sql.NullInt64))
|
|
|
|
if nullInt64.Valid {
|
|
|
|
values[i] = nullInt64.Int64
|
|
|
|
} else {
|
|
|
|
values[i] = nil
|
|
|
|
}
|
|
|
|
case "*sql.NullFloat64":
|
|
|
|
nullFloat64 := (*values[i].(*sql.NullFloat64))
|
|
|
|
if nullFloat64.Valid {
|
|
|
|
values[i] = nullFloat64.Float64
|
|
|
|
} else {
|
|
|
|
values[i] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 11:10:45 -05:00
|
|
|
if columnTypes[i].DatabaseTypeName() == "DECIMAL" {
|
2018-01-18 04:44:36 -06:00
|
|
|
f, err := strconv.ParseFloat(values[i].(string), 64)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
values[i] = f
|
|
|
|
} else {
|
|
|
|
values[i] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 08:07:43 -05:00
|
|
|
return values, nil
|
2017-04-19 10:26:29 -05:00
|
|
|
}
|
2019-09-24 13:50:49 -05:00
|
|
|
|
|
|
|
func (t *mysqlQueryResultTransformer) TransformQueryError(err error) error {
|
|
|
|
if driverErr, ok := err.(*mysql.MySQLError); ok {
|
|
|
|
if driverErr.Number != mysqlerr.ER_PARSE_ERROR && driverErr.Number != mysqlerr.ER_BAD_FIELD_ERROR && driverErr.Number != mysqlerr.ER_NO_SUCH_TABLE {
|
|
|
|
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")
|