2017-12-02 05:40:12 -06:00
|
|
|
package mssql
|
|
|
|
|
|
|
|
import (
|
2021-09-07 02:35:37 -05:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2017-12-02 05:40:12 -06:00
|
|
|
"fmt"
|
2020-06-17 04:17:11 -05:00
|
|
|
"net/url"
|
2021-05-05 09:46:07 -05:00
|
|
|
"reflect"
|
2020-06-17 04:17:11 -05:00
|
|
|
"regexp"
|
2018-03-16 08:37:16 -05:00
|
|
|
"strconv"
|
2021-03-29 06:03:01 -05:00
|
|
|
"strings"
|
2017-12-02 05:40:12 -06:00
|
|
|
|
2021-10-07 09:33:50 -05:00
|
|
|
mssql "github.com/denisenkom/go-mssqldb"
|
2021-09-07 02:35:37 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
|
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"
|
2022-11-02 09:03:50 -05:00
|
|
|
|
2021-10-07 09:33:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2019-09-10 14:50:04 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb/sqleng"
|
2021-10-07 09:33:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2017-12-02 05:40:12 -06:00
|
|
|
)
|
|
|
|
|
2020-06-17 04:17:11 -05:00
|
|
|
var logger = log.New("tsdb.mssql")
|
2017-12-02 05:40:12 -06:00
|
|
|
|
2021-09-07 02:35:37 -05:00
|
|
|
type Service struct {
|
|
|
|
im instancemgmt.InstanceManager
|
|
|
|
}
|
|
|
|
|
2022-01-20 11:16:22 -06:00
|
|
|
func ProvideService(cfg *setting.Cfg) *Service {
|
|
|
|
return &Service{
|
2021-09-13 08:27:51 -05:00
|
|
|
im: datasource.NewInstanceManager(newInstanceSettings(cfg)),
|
2019-09-18 12:30:35 -05:00
|
|
|
}
|
2021-09-07 02:35:37 -05:00
|
|
|
}
|
2018-03-19 11:32:51 -05:00
|
|
|
|
2021-09-07 02:35:37 -05:00
|
|
|
func (s *Service) getDataSourceHandler(pluginCtx backend.PluginContext) (*sqleng.DataSourceHandler, error) {
|
|
|
|
i, err := s.im.Get(pluginCtx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-07-26 11:11:10 -05:00
|
|
|
}
|
2021-09-07 02:35:37 -05:00
|
|
|
instance := i.(*sqleng.DataSourceHandler)
|
|
|
|
return instance, nil
|
|
|
|
}
|
2018-07-26 11:11:10 -05:00
|
|
|
|
2021-09-07 02:35:37 -05:00
|
|
|
func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
|
|
|
dsHandler, err := s.getDataSourceHandler(req.PluginContext)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-03-19 11:32:51 -05:00
|
|
|
}
|
2021-09-07 02:35:37 -05:00
|
|
|
return dsHandler.QueryData(ctx, req)
|
|
|
|
}
|
2018-03-19 11:32:51 -05:00
|
|
|
|
2021-09-13 08:27:51 -05:00
|
|
|
func newInstanceSettings(cfg *setting.Cfg) datasource.InstanceFactoryFunc {
|
2021-09-07 02:35:37 -05:00
|
|
|
return func(settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
|
|
|
jsonData := sqleng.JsonData{
|
|
|
|
MaxOpenConns: 0,
|
|
|
|
MaxIdleConns: 2,
|
|
|
|
ConnMaxLifetime: 14400,
|
|
|
|
Encrypt: "false",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(settings.JSONData, &jsonData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading settings: %w", err)
|
|
|
|
}
|
|
|
|
dsInfo := sqleng.DataSourceInfo{
|
|
|
|
JsonData: jsonData,
|
|
|
|
URL: settings.URL,
|
|
|
|
User: settings.User,
|
|
|
|
Database: settings.Database,
|
|
|
|
ID: settings.ID,
|
|
|
|
Updated: settings.Updated,
|
|
|
|
UID: settings.UID,
|
|
|
|
DecryptedSecureJSONData: settings.DecryptedSecureJSONData,
|
|
|
|
}
|
|
|
|
cnnstr, err := generateConnectionString(dsInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-13 08:27:51 -05:00
|
|
|
|
|
|
|
if cfg.Env == setting.Dev {
|
2022-11-02 09:03:50 -05:00
|
|
|
logger.Debug("GetEngine", "connection", cnnstr)
|
2021-09-07 02:35:37 -05:00
|
|
|
}
|
|
|
|
config := sqleng.DataPluginConfiguration{
|
|
|
|
DriverName: "mssql",
|
|
|
|
ConnectionString: cnnstr,
|
|
|
|
DSInfo: dsInfo,
|
|
|
|
MetricColumnTypes: []string{"VARCHAR", "CHAR", "NVARCHAR", "NCHAR"},
|
2021-09-13 08:27:51 -05:00
|
|
|
RowLimit: cfg.DataProxyRowLimit,
|
2021-09-07 02:35:37 -05:00
|
|
|
}
|
|
|
|
|
2022-11-02 09:03:50 -05:00
|
|
|
queryResultTransformer := mssqlQueryResultTransformer{}
|
2021-09-07 02:35:37 -05:00
|
|
|
|
|
|
|
return sqleng.NewQueryDataHandler(config, &queryResultTransformer, newMssqlMacroEngine(), logger)
|
|
|
|
}
|
2018-03-19 11:32:51 -05:00
|
|
|
}
|
|
|
|
|
2020-06-17 04:17:11 -05:00
|
|
|
// ParseURL tries to parse an MSSQL URL string into a URL object.
|
|
|
|
func ParseURL(u string) (*url.URL, error) {
|
|
|
|
logger.Debug("Parsing MSSQL URL", "url", u)
|
|
|
|
|
|
|
|
// Recognize ODBC connection strings like host\instance:1234
|
2021-11-30 02:28:48 -06:00
|
|
|
reODBC := regexp.MustCompile(`^[^\\:]+(?:\\[^:]+)?(?::\d+)?(?:;.+)?$`)
|
2020-06-17 04:17:11 -05:00
|
|
|
var host string
|
|
|
|
switch {
|
|
|
|
case reODBC.MatchString(u):
|
|
|
|
logger.Debug("Recognized as ODBC URL format", "url", u)
|
|
|
|
host = u
|
|
|
|
default:
|
|
|
|
logger.Debug("Couldn't recognize as valid MSSQL URL", "url", u)
|
|
|
|
return nil, fmt.Errorf("unrecognized MSSQL URL format: %q", u)
|
|
|
|
}
|
|
|
|
return &url.URL{
|
|
|
|
Scheme: "sqlserver",
|
|
|
|
Host: host,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-09-07 02:35:37 -05:00
|
|
|
func generateConnectionString(dsInfo sqleng.DataSourceInfo) (string, error) {
|
2021-03-30 03:54:57 -05:00
|
|
|
const dfltPort = "0"
|
2020-06-17 04:17:11 -05:00
|
|
|
var addr util.NetworkAddress
|
2021-09-07 02:35:37 -05:00
|
|
|
if dsInfo.URL != "" {
|
|
|
|
u, err := ParseURL(dsInfo.URL)
|
2020-06-17 04:17:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-03-30 03:54:57 -05:00
|
|
|
addr, err = util.SplitHostPortDefault(u.Host, "localhost", dfltPort)
|
2020-06-17 04:17:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addr = util.NetworkAddress{
|
|
|
|
Host: "localhost",
|
2021-03-30 03:54:57 -05:00
|
|
|
Port: dfltPort,
|
2020-06-17 04:17:11 -05:00
|
|
|
}
|
2019-10-09 01:58:45 -05:00
|
|
|
}
|
2019-08-05 11:40:33 -05:00
|
|
|
|
2021-03-30 03:54:57 -05:00
|
|
|
args := []interface{}{
|
2021-09-07 02:35:37 -05:00
|
|
|
"url", dsInfo.URL, "host", addr.Host,
|
2021-03-30 03:54:57 -05:00
|
|
|
}
|
|
|
|
if addr.Port != "0" {
|
|
|
|
args = append(args, "port", addr.Port)
|
|
|
|
}
|
|
|
|
logger.Debug("Generating connection string", args...)
|
2022-01-26 08:00:18 -06:00
|
|
|
|
|
|
|
encrypt := dsInfo.JsonData.Encrypt
|
|
|
|
tlsSkipVerify := dsInfo.JsonData.TlsSkipVerify
|
|
|
|
hostNameInCertificate := dsInfo.JsonData.Servername
|
|
|
|
certificate := dsInfo.JsonData.RootCertFile
|
2021-03-30 03:54:57 -05:00
|
|
|
connStr := fmt.Sprintf("server=%s;database=%s;user id=%s;password=%s;",
|
2019-10-09 01:58:45 -05:00
|
|
|
addr.Host,
|
2021-09-07 02:35:37 -05:00
|
|
|
dsInfo.Database,
|
|
|
|
dsInfo.User,
|
|
|
|
dsInfo.DecryptedSecureJSONData["password"],
|
2019-09-18 12:30:35 -05:00
|
|
|
)
|
2021-03-30 03:54:57 -05:00
|
|
|
// Port number 0 means to determine the port automatically, so we can let the driver choose
|
|
|
|
if addr.Port != "0" {
|
|
|
|
connStr += fmt.Sprintf("port=%s;", addr.Port)
|
|
|
|
}
|
2022-01-26 08:00:18 -06:00
|
|
|
if encrypt == "true" {
|
|
|
|
connStr += fmt.Sprintf("encrypt=%s;TrustServerCertificate=%t;", encrypt, tlsSkipVerify)
|
|
|
|
if hostNameInCertificate != "" {
|
|
|
|
connStr += fmt.Sprintf("hostNameInCertificate=%s;", hostNameInCertificate)
|
|
|
|
}
|
2021-09-07 02:35:37 -05:00
|
|
|
|
2022-01-26 08:00:18 -06:00
|
|
|
if certificate != "" {
|
|
|
|
connStr += fmt.Sprintf("certificate=%s;", certificate)
|
|
|
|
}
|
2022-03-30 09:14:04 -05:00
|
|
|
} else if encrypt == "disable" {
|
|
|
|
connStr += fmt.Sprintf("encrypt=%s;", dsInfo.JsonData.Encrypt)
|
2018-10-16 11:14:22 -05:00
|
|
|
}
|
2019-09-18 12:30:35 -05:00
|
|
|
return connStr, nil
|
2017-12-02 05:40:12 -06:00
|
|
|
}
|
|
|
|
|
2022-11-02 09:03:50 -05:00
|
|
|
type mssqlQueryResultTransformer struct{}
|
2017-12-02 05:40:12 -06:00
|
|
|
|
2022-11-02 09:03:50 -05:00
|
|
|
func (t *mssqlQueryResultTransformer) TransformQueryError(logger log.Logger, err error) error {
|
2021-03-29 06:03:01 -05:00
|
|
|
// go-mssql overrides source error, so we currently match on string
|
|
|
|
// ref https://github.com/denisenkom/go-mssqldb/blob/045585d74f9069afe2e115b6235eb043c8047043/tds.go#L904
|
|
|
|
if strings.HasPrefix(strings.ToLower(err.Error()), "unable to open tcp connection with host") {
|
2022-11-02 09:03:50 -05:00
|
|
|
logger.Error("Query error", "error", err)
|
2021-03-29 06:03:01 -05:00
|
|
|
return sqleng.ErrConnectionFailed
|
|
|
|
}
|
|
|
|
|
2019-09-24 13:50:49 -05:00
|
|
|
return err
|
|
|
|
}
|
2021-05-05 09:46:07 -05:00
|
|
|
|
|
|
|
func (t *mssqlQueryResultTransformer) GetConverterList() []sqlutil.StringConverter {
|
|
|
|
return []sqlutil.StringConverter{
|
|
|
|
{
|
|
|
|
Name: "handle MONEY",
|
|
|
|
InputScanKind: reflect.Slice,
|
|
|
|
InputTypeName: "MONEY",
|
|
|
|
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 SMALLMONEY",
|
|
|
|
InputScanKind: reflect.Slice,
|
|
|
|
InputTypeName: "SMALLMONEY",
|
|
|
|
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 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 UNIQUEIDENTIFIER",
|
|
|
|
InputScanKind: reflect.Slice,
|
|
|
|
InputTypeName: "UNIQUEIDENTIFIER",
|
|
|
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
|
|
|
Replacer: &sqlutil.StringFieldReplacer{
|
|
|
|
OutputFieldType: data.FieldTypeNullableString,
|
|
|
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
uuid := &mssql.UniqueIdentifier{}
|
|
|
|
if err := uuid.Scan([]byte(*in)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
v := uuid.String()
|
|
|
|
return &v, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|