-
Notifications
You must be signed in to change notification settings - Fork 15
/
connector.go
67 lines (54 loc) · 1.72 KB
/
connector.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
// SPDX-FileCopyrightText: 2020 SAP SE
// SPDX-FileCopyrightText: 2021 SAP SE
// SPDX-FileCopyrightText: 2022 SAP SE
// SPDX-FileCopyrightText: 2023 SAP SE
//
// SPDX-License-Identifier: Apache-2.0
package ase
import (
"context"
"database/sql/driver"
"fmt"
"github.com/SAP/go-dblib/tds"
)
// Interface satisfaction checks
var (
_ driver.Connector = (*Connector)(nil)
)
// Connector implements the driver.Connector interface.
type Connector struct {
Info *Info
EnvChangeHooks []tds.EnvChangeHook
EEDHooks []tds.EEDHook
}
// NewConnector returns a new connector with the passed configuration.
func NewConnector(info *Info) (driver.Connector, error) {
return NewConnectorWithHooks(info, nil, nil)
}
// NewConnectorWithHooks returns a new connector with the passed
// configuration.
func NewConnectorWithHooks(info *Info, envChangeHooks []tds.EnvChangeHook, eedHooks []tds.EEDHook) (driver.Connector, error) {
connector := &Connector{
Info: info,
}
conn, err := connector.Connect(context.Background())
if err != nil {
return nil, fmt.Errorf("error opening test connection: %w", err)
}
if err := conn.Close(); err != nil {
return nil, fmt.Errorf("error closing test connection: %w", err)
}
// Set the hooks after validating the connection otherwise hooks
// would get called during the test connection.
connector.EnvChangeHooks = envChangeHooks
connector.EEDHooks = eedHooks
return connector, nil
}
// Driver implements the driver.Connector interface.
func (c Connector) Driver() driver.Driver {
return drv
}
// Connect implements the driver.Connector interface.
func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
return NewConnWithHooks(ctx, c.Info, c.EnvChangeHooks, c.EEDHooks)
}