This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
generated from mostafa/k6-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsql.go
72 lines (58 loc) · 1.3 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
package main
import (
dbsql "database/sql"
"log"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
type sql struct{}
type keyValue map[string]interface{}
func New() *sql {
return &sql{}
}
func contains(array []string, element string) bool {
for _, item := range array {
if item == element {
return true
}
}
return false
}
func (*sql) Open(database string, connectionString string) *dbsql.DB {
supportedDatabases := []string{"mysql", "postgres", "sqlite3"}
if !contains(supportedDatabases, database) {
log.Fatal("Database is not supported")
return nil
}
db, err := dbsql.Open(database, connectionString)
if err == nil {
return db
}
log.Fatal(err)
return nil
}
func (*sql) Query(db *dbsql.DB, query string) []keyValue {
rows, _ := db.Query(query)
cols, _ := rows.Columns()
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 {
log.Fatal(err)
return nil
}
data := make(keyValue, len(cols))
for i, colName := range cols {
data[colName] = *valuePtrs[i].(*interface{})
}
result = append(result, data)
}
rows.Close()
return result
}