| 
									
										
										
										
											2017-12-02 19:40:12 +08:00
										 |  |  | package mssql | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"database/sql" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2020-06-17 17:17:11 +08:00
										 |  |  | 	"net/url" | 
					
						
							|  |  |  | 	"regexp" | 
					
						
							| 
									
										
										
										
											2018-03-16 21:37:16 +08:00
										 |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2021-03-29 19:03:01 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2017-12-02 19:40:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 03:50:04 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2020-06-17 17:17:11 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2019-09-11 03:50:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-26 16:11:43 +08:00
										 |  |  | 	mssql "github.com/denisenkom/go-mssqldb" | 
					
						
							| 
									
										
										
										
											2019-05-13 14:45:54 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/log" | 
					
						
							| 
									
										
										
										
											2017-12-02 19:40:12 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins" | 
					
						
							| 
									
										
										
										
											2019-09-11 03:50:04 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/tsdb/sqleng" | 
					
						
							| 
									
										
										
										
											2020-04-01 21:57:21 +08:00
										 |  |  | 	"xorm.io/core" | 
					
						
							| 
									
										
										
										
											2017-12-02 19:40:12 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-17 17:17:11 +08:00
										 |  |  | var logger = log.New("tsdb.mssql") | 
					
						
							| 
									
										
										
										
											2017-12-02 19:40:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | func NewExecutor(datasource *models.DataSource) (plugins.DataPlugin, error) { | 
					
						
							| 
									
										
										
										
											2019-09-19 01:30:35 +08:00
										 |  |  | 	cnnstr, err := generateConnectionString(datasource) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 	// TODO: Don't use global
 | 
					
						
							| 
									
										
										
										
											2020-10-02 21:45:45 +08:00
										 |  |  | 	if setting.Env == setting.Dev { | 
					
						
							| 
									
										
										
										
											2019-03-15 18:53:30 +08:00
										 |  |  | 		logger.Debug("getEngine", "connection", cnnstr) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-20 00:32:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 	config := sqleng.DataPluginConfiguration{ | 
					
						
							| 
									
										
										
										
											2018-07-27 00:11:10 +08:00
										 |  |  | 		DriverName:        "mssql", | 
					
						
							|  |  |  | 		ConnectionString:  cnnstr, | 
					
						
							|  |  |  | 		Datasource:        datasource, | 
					
						
							|  |  |  | 		MetricColumnTypes: []string{"VARCHAR", "CHAR", "NVARCHAR", "NCHAR"}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 02:50:49 +08:00
										 |  |  | 	queryResultTransformer := mssqlQueryResultTransformer{ | 
					
						
							| 
									
										
										
										
											2018-07-27 00:11:10 +08:00
										 |  |  | 		log: logger, | 
					
						
							| 
									
										
										
										
											2018-03-20 00:32:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 	return sqleng.NewDataPlugin(config, &queryResultTransformer, newMssqlMacroEngine(), logger) | 
					
						
							| 
									
										
										
										
											2018-03-20 00:32:51 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-17 17:17:11 +08: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
 | 
					
						
							|  |  |  | 	reODBC := regexp.MustCompile(`^[^\\:]+(?:\\[^:]+)?(?::\d+)?$`) | 
					
						
							|  |  |  | 	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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-19 01:30:35 +08:00
										 |  |  | func generateConnectionString(datasource *models.DataSource) (string, error) { | 
					
						
							| 
									
										
										
										
											2020-06-17 17:17:11 +08:00
										 |  |  | 	var addr util.NetworkAddress | 
					
						
							|  |  |  | 	if datasource.Url != "" { | 
					
						
							|  |  |  | 		u, err := ParseURL(datasource.Url) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return "", err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		addr, err = util.SplitHostPortDefault(u.Host, "localhost", "1433") | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return "", err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		addr = util.NetworkAddress{ | 
					
						
							|  |  |  | 			Host: "localhost", | 
					
						
							|  |  |  | 			Port: "1433", | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-10-09 14:58:45 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-06 00:40:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-17 17:17:11 +08:00
										 |  |  | 	logger.Debug("Generating connection string", "url", datasource.Url, "host", addr.Host, "port", addr.Port) | 
					
						
							| 
									
										
										
										
											2019-09-19 01:30:35 +08:00
										 |  |  | 	encrypt := datasource.JsonData.Get("encrypt").MustString("false") | 
					
						
							|  |  |  | 	connStr := fmt.Sprintf("server=%s;port=%s;database=%s;user id=%s;password=%s;", | 
					
						
							| 
									
										
										
										
											2019-10-09 14:58:45 +08:00
										 |  |  | 		addr.Host, | 
					
						
							|  |  |  | 		addr.Port, | 
					
						
							| 
									
										
										
										
											2019-09-19 01:30:35 +08:00
										 |  |  | 		datasource.Database, | 
					
						
							|  |  |  | 		datasource.User, | 
					
						
							|  |  |  | 		datasource.DecryptedPassword(), | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if encrypt != "false" { | 
					
						
							|  |  |  | 		connStr += fmt.Sprintf("encrypt=%s;", encrypt) | 
					
						
							| 
									
										
										
										
											2018-10-17 00:14:22 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-09-19 01:30:35 +08:00
										 |  |  | 	return connStr, nil | 
					
						
							| 
									
										
										
										
											2017-12-02 19:40:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 02:50:49 +08:00
										 |  |  | type mssqlQueryResultTransformer struct { | 
					
						
							| 
									
										
										
										
											2018-07-27 00:11:10 +08:00
										 |  |  | 	log log.Logger | 
					
						
							| 
									
										
										
										
											2017-12-02 19:40:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | func (t *mssqlQueryResultTransformer) TransformQueryResult(columnTypes []*sql.ColumnType, rows *core.Rows) ( | 
					
						
							|  |  |  | 	plugins.DataRowValues, error) { | 
					
						
							| 
									
										
										
										
											2018-07-27 00:11:10 +08:00
										 |  |  | 	values := make([]interface{}, len(columnTypes)) | 
					
						
							|  |  |  | 	valuePtrs := make([]interface{}, len(columnTypes)) | 
					
						
							| 
									
										
										
										
											2017-12-02 19:40:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-13 00:44:31 +08:00
										 |  |  | 	for i := range columnTypes { | 
					
						
							|  |  |  | 		// debug output on large tables causes high memory utilization/leak
 | 
					
						
							|  |  |  | 		// t.log.Debug("type", "type", stype)
 | 
					
						
							| 
									
										
										
										
											2017-12-06 15:32:20 +08:00
										 |  |  | 		valuePtrs[i] = &values[i] | 
					
						
							| 
									
										
										
										
											2017-12-02 19:40:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 15:32:20 +08:00
										 |  |  | 	if err := rows.Scan(valuePtrs...); err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-02 19:40:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-16 21:37:16 +08:00
										 |  |  | 	// convert types not handled by denisenkom/go-mssqldb
 | 
					
						
							|  |  |  | 	// unhandled types are returned as []byte
 | 
					
						
							| 
									
										
										
										
											2018-07-27 00:11:10 +08:00
										 |  |  | 	for i := 0; i < len(columnTypes); i++ { | 
					
						
							| 
									
										
											  
											
												Simplify comparison to bool constant (gosimple)
This fixes:
build.go:553:6: should omit comparison to bool constant, can be simplified to !strings.Contains(path, ".sha256") (S1002)
pkg/cmd/grafana-cli/commands/ls_command.go:27:5: should omit comparison to bool constant, can be simplified to !pluginDirInfo.IsDir() (S1002)
pkg/components/dynmap/dynmap_test.go:24:5: should omit comparison to bool constant, can be simplified to !value (S1002)
pkg/components/dynmap/dynmap_test.go:122:14: should omit comparison to bool constant, can be simplified to b (S1002)
pkg/components/dynmap/dynmap_test.go:125:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/components/dynmap/dynmap_test.go:128:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/models/org_user.go:51:5: should omit comparison to bool constant, can be simplified to !(*r).IsValid() (S1002)
pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go:77:12: should omit comparison to bool constant, can be simplified to !haveBool (S1002)
pkg/services/alerting/conditions/evaluator.go:23:9: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:48:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:91:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/query.go:56:6: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/extractor.go:107:20: should omit comparison to bool constant, can be simplified to !enabled.MustBool() (S1002)
pkg/services/alerting/notifiers/telegram.go:222:41: should omit comparison to bool constant, can be simplified to this.UploadImage (S1002)
pkg/services/sqlstore/apikey.go:58:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/apikey.go:72:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:66:33: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard.go:175:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:311:13: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:444:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:472:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:554:32: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard_snapshot.go:83:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/plugin_setting.go:39:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:34:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:111:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:136:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:213:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/temp_user.go:129:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:157:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:182:5: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:191:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:212:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:307:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/social/generic_oauth.go:185:5: should omit comparison to bool constant, can be simplified to !s.extractToken(&data, token) (S1002)
pkg/tsdb/mssql/mssql.go:148:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:212:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mssql/mssql.go:247:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:274:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mssql/mssql.go:282:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:221:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mysql/mysql.go:256:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mysql/mysql.go:283:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:291:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:134:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:201:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/postgres/postgres.go:236:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:263:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:271:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
											
										 
											2018-04-17 02:12:59 +08:00
										 |  |  | 		if value, ok := values[i].([]byte); ok { | 
					
						
							| 
									
										
										
										
											2018-07-27 00:11:10 +08:00
										 |  |  | 			switch columnTypes[i].DatabaseTypeName() { | 
					
						
							| 
									
										
										
										
											2018-03-16 21:37:16 +08:00
										 |  |  | 			case "MONEY", "SMALLMONEY", "DECIMAL": | 
					
						
							|  |  |  | 				if v, err := strconv.ParseFloat(string(value), 64); err == nil { | 
					
						
							|  |  |  | 					values[i] = v | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2018-07-27 00:11:10 +08:00
										 |  |  | 					t.log.Debug("Rows", "Error converting numeric to float", value) | 
					
						
							| 
									
										
										
										
											2018-03-16 21:37:16 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2020-06-26 16:11:43 +08:00
										 |  |  | 			case "UNIQUEIDENTIFIER": | 
					
						
							|  |  |  | 				uuid := &mssql.UniqueIdentifier{} | 
					
						
							|  |  |  | 				if err := uuid.Scan(value); err == nil { | 
					
						
							|  |  |  | 					values[i] = uuid.String() | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					t.log.Debug("Rows", "Error converting uniqueidentifier to string", value) | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-03-16 21:37:16 +08:00
										 |  |  | 			default: | 
					
						
							| 
									
										
										
										
											2018-07-27 00:11:10 +08:00
										 |  |  | 				t.log.Debug("Rows", "Unknown database type", columnTypes[i].DatabaseTypeName(), "value", value) | 
					
						
							| 
									
										
										
										
											2018-03-16 21:37:16 +08:00
										 |  |  | 				values[i] = string(value) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-02 19:40:12 +08:00
										 |  |  | 	return values, nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-09-25 02:50:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (t *mssqlQueryResultTransformer) TransformQueryError(err error) error { | 
					
						
							| 
									
										
										
										
											2021-03-29 19:03:01 +08: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") { | 
					
						
							|  |  |  | 		t.log.Error("query error", "err", err) | 
					
						
							|  |  |  | 		return sqleng.ErrConnectionFailed | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 02:50:49 +08:00
										 |  |  | 	return err | 
					
						
							|  |  |  | } |