-
Notifications
You must be signed in to change notification settings - Fork 2
/
sql-relational-like.go
99 lines (82 loc) · 2.52 KB
/
sql-relational-like.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
package storage
import (
"context"
"database/sql"
"encoding/json"
"log"
"time"
"github.com/golang-common-packages/hash"
)
// SQLLikeClient manage all SQL-Like actions
type SQLLikeClient struct {
Client *sql.DB
Config *LIKE
}
var (
// sqlLikeClientSessionMapping singleton pattern
sqlLikeClientSessionMapping = make(map[string]*SQLLikeClient)
)
// newSQLLike init new instance
// The sql package must be used in conjunction with a database driver. See https://golang.org/s/sqldrivers for a list of driverNames.
func newSQLLike(config *LIKE) *SQLLikeClient {
hasher := &hash.Client{}
configAsJSON, err := json.Marshal(config)
if err != nil {
log.Fatalln("Unable to marshal SQL-Like configuration: ", err)
}
configAsString := hasher.SHA1(string(configAsJSON))
currentSQLLikeSession := sqlLikeClientSessionMapping[configAsString]
if currentSQLLikeSession == nil {
currentSQLLikeSession = &SQLLikeClient{nil, nil}
client, err := sql.Open(config.DriverName, config.DataSourceName)
if err != nil {
log.Fatalln("Unable to connect to SQL-Like: ", err)
}
client.SetConnMaxLifetime(config.MaxConnectionLifetime)
client.SetMaxIdleConns(config.MaxConnectionIdle)
client.SetMaxOpenConns(config.MaxConnectionOpen)
if err := client.PingContext(context.TODO()); err != nil {
log.Fatalln("Unable to ping to SQL-Like: ", err)
}
currentSQLLikeSession.Client = client
currentSQLLikeSession.Config = config
sqlLikeClientSessionMapping[configAsString] = currentSQLLikeSession
log.Println("Connected to SQL-Like")
}
return currentSQLLikeSession
}
// Execute return results based on 'query' and 'dataModel'
func (c *SQLLikeClient) Execute(
query string,
dataModel interface{}) (interface{}, error) {
var results []interface{}
_, cancel := context.WithTimeout(context.TODO(), 60*time.Second)
defer cancel()
rows, err := c.Client.QueryContext(context.TODO(), query)
if err != nil {
log.Println("Unable to execute query: ", err)
return nil, err
}
// Go through each row to get the result
for rows.Next() {
err = rows.Scan(&dataModel)
if err != nil {
log.Println("Unable to scan rows data: ", err)
return nil, err
}
results = append(results, dataModel)
}
/*
Check for errors during rows "Close"
his may be more important if multiple statements are executed
in a single batch and rows were written as well as read.
*/
if err := rows.Close(); err != nil {
return nil, err
}
// Check for errors during row iteration.
if err := rows.Err(); err != nil {
return nil, err
}
return results, nil
}