-
Notifications
You must be signed in to change notification settings - Fork 54
/
driver.go
121 lines (100 loc) · 3.72 KB
/
driver.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
113
114
115
116
117
118
119
120
121
package driver
import (
"context"
"database/sql"
"database/sql/driver"
"os"
"strconv"
"strings"
)
// DriverVersion is the version number of the hdb driver.
const DriverVersion = "1.1.1"
// DriverName is the driver name to use with sql.Open for hdb databases.
const DriverName = "hdb"
var clientID = func() string {
if hostname, err := os.Hostname(); err == nil {
return strings.Join([]string{strconv.Itoa(os.Getpid()), hostname}, "@")
}
return strconv.Itoa(os.Getpid())
}()
// clientType is the information provided to HDB identifying the driver.
// Previously the driver.DriverName "hdb" was used but we should be more specific in providing a unique client type to HANA backend.
const clientType = "go-hdb"
var defaultApplicationName, _ = os.Executable()
// driver singleton instance (do not use directly - use getDriver() instead)
var stdHdbDriver *hdbDriver
func init() {
// load stats configuration
if err := loadStatsCfg(); err != nil {
panic(err) // invalid configuration file
}
// create driver
stdHdbDriver = &hdbDriver{metrics: newMetrics(nil, statsCfg.TimeUpperBounds)}
// register driver
sql.Register(DriverName, stdHdbDriver)
}
// driver
// check if driver implements all required interfaces
var (
_ driver.Driver = (*hdbDriver)(nil)
_ driver.DriverContext = (*hdbDriver)(nil)
_ Driver = (*hdbDriver)(nil)
)
// Driver enhances a connection with go-hdb specific connection functions.
type Driver interface {
Name() string // Name returns the driver name.
Version() string // Version returns the driver version.
Stats() *Stats // Stats returns aggregated driver statistics.
}
// hdbDriver represents the go sql driver implementation for hdb.
type hdbDriver struct {
metrics *metrics
}
// Open implements the driver.Driver interface.
func (d *hdbDriver) Open(dsn string) (driver.Conn, error) {
connector, err := NewDSNConnector(dsn)
if err != nil {
return nil, err
}
return connector.Connect(context.Background())
}
// OpenConnector implements the driver.DriverContext interface.
func (d *hdbDriver) OpenConnector(dsn string) (driver.Connector, error) { return NewDSNConnector(dsn) }
// Name returns the driver name.
func (d *hdbDriver) Name() string { return DriverName }
// Version returns the driver version.
func (d *hdbDriver) Version() string { return DriverVersion }
// Stats returns aggregated driver statistics.
func (d *hdbDriver) Stats() *Stats { return d.metrics.stats() }
// DB represents a driver database and can be used as a replacement for sql.DB.
// It provides all of the sql.DB methods plus additional methods only available for driver.DB.
type DB struct {
// The embedded sql.DB instance. Please use only the methods of the wrapper (driver.DB).
// The field is exported to support use cases where a sql.DB object is requested, but please
// use with care as some of the sql.DB methods (e.g. Close) are redefined in driver.DB.
*sql.DB
metrics *metrics
}
// OpenDB opens and returns a database. It also calls the OpenDB method of the sql package and stores an embedded *sql.DB object.
func OpenDB(c *Connector) *DB {
metrics := newMetrics(stdHdbDriver.metrics, statsCfg.TimeUpperBounds)
nc := &Connector{
connAttrs: c.connAttrs,
authAttrs: c.authAttrs,
newConn: func(ctx context.Context, connAttrs *connAttrs, authAttrs *authAttrs) (driver.Conn, error) {
return newConn(ctx, metrics, connAttrs, authAttrs) // use db specific metrics
},
}
return &DB{
metrics: metrics,
DB: sql.OpenDB(nc),
}
}
// Close closes the DB. It also calls the Close method of the embedded sql.DB.
func (db *DB) Close() error {
err := db.DB.Close()
db.metrics.close()
return err
}
// ExStats returns the extended database statistics.
func (db *DB) ExStats() *Stats { return db.metrics.stats() }