forked from grafana/sqlds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver-mock.go
72 lines (65 loc) · 1.79 KB
/
driver-mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package sqlds
import (
"context"
"database/sql"
"encoding/json"
"os"
"path/filepath"
"strings"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
_ "github.com/mithrandie/csvq-driver"
)
// SQLMock connects to a local folder with csv files
type SQLMock struct {
folder string
}
func (h *SQLMock) Settings(_ context.Context, _ backend.DataSourceInstanceSettings) DriverSettings {
return DriverSettings{
FillMode: &data.FillMissing{
Mode: data.FillModeNull,
},
Timeout: time.Second * time.Duration(30),
}
}
// Connect opens a sql.DB connection using datasource settings
func (h *SQLMock) Connect(_ context.Context, _ backend.DataSourceInstanceSettings, msg json.RawMessage) (*sql.DB, error) {
backend.Logger.Debug("connecting to mock data")
folder := h.folder
if folder == "" {
folder = MockDataFolder
}
if !strings.HasPrefix(folder, "/") {
folder = "/" + folder
}
err := CreateMockTable("users", folder)
if err != nil {
backend.Logger.Error("failed creating mock data: " + err.Error())
return nil, err
}
ex, err := os.Executable()
if err != nil {
backend.Logger.Error("failed accessing Mock path: " + err.Error())
}
exPath := filepath.Dir(ex)
db, err := sql.Open("csvq", exPath+folder)
if err != nil {
backend.Logger.Error("failed opening Mock sql: " + err.Error())
return nil, err
}
err = db.Ping()
if err != nil {
backend.Logger.Error("failed connecting to Mock: " + err.Error())
}
return db, nil
}
// Converters defines list of string convertors
func (h *SQLMock) Converters() []sqlutil.Converter {
return []sqlutil.Converter{}
}
// Macros returns list of macro functions convert the macros of raw query
func (h *SQLMock) Macros() Macros {
return Macros{}
}