forked from grafana/xk6-sql
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sql.go
112 lines (93 loc) · 2.64 KB
/
sql.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package sql
import (
dbsql "database/sql"
"fmt"
_ "github.com/denisenkom/go-mssqldb"
_ "github.com/go-sql-driver/mysql"
_ "github.com/godror/godror"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"go.k6.io/k6/js/modules"
)
func init() {
modules.Register("k6/x/sql", new(RootModule))
}
// RootModule is the global module object type. It is instantiated once per test
// run and will be used to create `k6/x/sql` module instances for each VU.
type RootModule struct{}
// SQL represents an instance of the SQL module for every VU.
type SQL struct {
vu modules.VU
}
// Ensure the interfaces are implemented correctly.
var (
_ modules.Module = &RootModule{}
_ modules.Instance = &SQL{}
)
// NewModuleInstance implements the modules.Module interface to return
// a new instance for each VU.
func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
return &SQL{vu: vu}
}
// Exports implements the modules.Instance interface and returns the exports
// of the JS module.
func (sql *SQL) Exports() modules.Exports {
return modules.Exports{Default: sql}
}
// keyValue is a simple key-value pair.
type keyValue map[string]interface{}
func contains(array []string, element string) bool {
for _, item := range array {
if item == element {
return true
}
}
return false
}
// Open establishes a connection to the specified database type using
// the provided connection string.
func (*SQL) Open(database string, connectionString string) (*dbsql.DB, error) {
supportedDatabases := []string{"mysql", "postgres", "sqlite3", "sqlserver", "oracle"}
if !contains(supportedDatabases, database) {
return nil, fmt.Errorf("database %s is not supported", database)
}
if database == "oracle" {
database = "godror"
}
db, err := dbsql.Open(database, connectionString)
if err != nil {
return nil, err
}
return db, nil
}
// Query executes the provided query string against the database, while
// providing results as a slice of KeyValue instance(s) if available.
func (*SQL) Query(db *dbsql.DB, query string, args ...interface{}) ([]keyValue, error) {
rows, err := db.Query(query, args...)
if err != nil {
return nil, err
}
cols, err := rows.Columns()
if err != nil {
return nil, err
}
values := make([]interface{}, len(cols))
valuePtrs := make([]interface{}, len(cols))
result := make([]keyValue, 0)
for rows.Next() {
for i := range values {
valuePtrs[i] = &values[i]
}
err := rows.Scan(valuePtrs...)
if err != nil {
return nil, err
}
data := make(keyValue, len(cols))
for i, colName := range cols {
data[colName] = *valuePtrs[i].(*interface{})
}
result = append(result, data)
}
rows.Close()
return result, nil
}