forked from SAP/cgo-ase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.go
84 lines (72 loc) · 1.81 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// 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"
)
// Interface satisfaction checks.
var _ driver.Connector = (*connector)(nil)
// connector implements the driver.Connector interface.
type connector struct {
driverCtx *csContext
info *Info
}
// NewConnector returns a new connector with the passed configuration.
func NewConnector(info *Info) (driver.Connector, error) {
driverCtx, err := newCsContext(info)
if err != nil {
return nil, fmt.Errorf("Failed to initialize context: %w", err)
}
c := &connector{
driverCtx: driverCtx,
info: info,
}
conn, err := c.Connect(context.Background())
if err != nil {
driverCtx.drop()
return nil, fmt.Errorf("Failed to open connection: %w", err)
}
defer func() {
// In- and decrease connections count before and after closing
// connection to prevent the context being deallocated.
driverCtx.connections++
conn.Close()
driverCtx.connections--
}()
return c, nil
}
// Connect implements the driver.Connector interface.
func (connector *connector) Connect(ctx context.Context) (driver.Conn, error) {
connChan := make(chan driver.Conn, 1)
errChan := make(chan error, 1)
go func() {
conn, err := NewConnection(connector.driverCtx, connector.info)
connChan <- conn
close(connChan)
errChan <- err
close(errChan)
}()
select {
case <-ctx.Done():
defer func() {
conn := <-connChan
if conn != nil {
conn.Close()
}
}()
return nil, ctx.Err()
case err := <-errChan:
conn := <-connChan
return conn, err
}
}
// Driver implements the driver.Connector interface.
func (connector connector) Driver() driver.Driver {
return drv
}