forked from SAP/cgo-ase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
228 lines (194 loc) · 6.91 KB
/
connection.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// 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
//#include <stdlib.h>
//#include "ctlib.h"
import "C"
import (
"context"
"database/sql/driver"
"fmt"
"io"
"unsafe"
"github.com/SAP/go-dblib"
"github.com/SAP/go-dblib/asetypes"
)
// Interface satisfaction checks.
var (
_ driver.Conn = (*Connection)(nil)
_ driver.ConnBeginTx = (*Connection)(nil)
_ driver.ConnPrepareContext = (*Connection)(nil)
_ driver.Execer = (*Connection)(nil)
_ driver.ExecerContext = (*Connection)(nil)
_ driver.Pinger = (*Connection)(nil)
_ driver.Queryer = (*Connection)(nil)
_ driver.QueryerContext = (*Connection)(nil)
_ driver.NamedValueChecker = (*Connection)(nil)
)
// Connection implements the driver.Conn interface.
type Connection struct {
conn *C.CS_CONNECTION
driverCtx *csContext
}
// NewConnection allocates a new connection based on the
// options in the dsn.
//
// If driverCtx is nil a new csContext will be initialized.
func NewConnection(driverCtx *csContext, info *Info) (*Connection, error) {
if driverCtx == nil {
var err error
driverCtx, err = newCsContext(info)
if err != nil {
return nil, fmt.Errorf("Failed to initialize context for conn: %w", err)
}
}
if err := driverCtx.newConn(); err != nil {
return nil, fmt.Errorf("Failed to ensure context: %w", err)
}
conn := &Connection{
driverCtx: driverCtx,
}
if retval := C.ct_con_alloc(driverCtx.ctx, &conn.conn); retval != C.CS_SUCCEED {
conn.Close()
return nil, makeError(retval, "C.ct_con_alloc failed")
}
// Set password encryption
cTrue := C.CS_TRUE
if retval := C.ct_con_props(conn.conn, C.CS_SET, C.CS_SEC_EXTENDED_ENCRYPTION, unsafe.Pointer(&cTrue), C.CS_UNUSED, nil); retval != C.CS_SUCCEED {
conn.Close()
return nil, makeError(retval, "C.ct_con_props failed for CS_SEC_EXTENDED_ENCRYPTION")
}
cFalse := C.CS_FALSE
if retval := C.ct_con_props(conn.conn, C.CS_SET, C.CS_SEC_NON_ENCRYPTION_RETRY, unsafe.Pointer(&cFalse), C.CS_UNUSED, nil); retval != C.CS_SUCCEED {
conn.Close()
return nil, makeError(retval, "C.ct_con_props failed for CS_SEC_NON_ENCRYPTION_RETRY")
}
// Give preference to the user store key
if len(info.Userstorekey) > 0 {
// Set userstorekey
userstorekey := unsafe.Pointer(C.CString(info.Userstorekey))
defer C.free(userstorekey)
if retval := C.ct_con_props(conn.conn, C.CS_SET, C.CS_SECSTOREKEY, userstorekey, C.CS_NULLTERM, nil); retval != C.CS_SUCCEED {
conn.Close()
return nil, makeError(retval, "C.ct_con_props failed for C.CS_SECSTOREKEY")
}
} else {
// Set username.
username := unsafe.Pointer(C.CString(info.Username))
defer C.free(username)
if retval := C.ct_con_props(conn.conn, C.CS_SET, C.CS_USERNAME, username, C.CS_NULLTERM, nil); retval != C.CS_SUCCEED {
conn.Close()
return nil, makeError(retval, "C.ct_con_props failed for CS_USERNAME")
}
// Set password.
password := unsafe.Pointer(C.CString(info.Password))
defer C.free(password)
if retval := C.ct_con_props(conn.conn, C.CS_SET, C.CS_PASSWORD, password, C.CS_NULLTERM, nil); retval != C.CS_SUCCEED {
conn.Close()
return nil, makeError(retval, "C.ct_con_props failed for CS_PASSWORD")
}
}
if info.Host != "" && info.Port != "" {
// Set hostname and port as string, since it is modified if
// '-o ssl' is set.
strHostport := info.Host + " " + info.Port
// If '-o ssl='-option is set, add it to strHostport
if info.TLSHostname != "" {
strHostport += fmt.Sprintf("ssl=\"%s\"", info.TLSHostname)
}
// Create pointer
ptrHostport := unsafe.Pointer(C.CString(strHostport))
defer C.free(ptrHostport)
if retval := C.ct_con_props(conn.conn, C.CS_SET, C.CS_SERVERADDR, ptrHostport, C.CS_NULLTERM, nil); retval != C.CS_SUCCEED {
conn.Close()
return nil, makeError(retval, "C.ct_con_props failed for CS_SERVERADDR")
}
}
if info.AppName != "" {
ptrAppName := unsafe.Pointer(C.CString(info.AppName))
defer C.free(ptrAppName)
if retval := C.ct_con_props(conn.conn, C.CS_SET, C.CS_APPNAME, ptrAppName, C.CS_NULLTERM, nil); retval != C.CS_SUCCEED {
return nil, makeError(retval, "C.ct_con_props failed for CS_APPNAME")
}
}
if retval := C.ct_connect(conn.conn, nil, 0); retval != C.CS_SUCCEED {
conn.Close()
return nil, makeError(retval, "C.ct_connect failed")
}
// Set database
if info.Database != "" {
if _, err := conn.Exec("use "+info.Database, nil); err != nil {
conn.Close()
return nil, fmt.Errorf("Failed to connect to database %s: %w", info.Database, err)
}
}
return conn, nil
}
// Close implements the driver.Conn interface. It closes and deallocates
// a connection.
func (conn *Connection) Close() error {
// Call context.drop when exiting this function to decrease the
// connection counter and potentially deallocate the context.
defer conn.driverCtx.dropConn()
retval := C.ct_close(conn.conn, C.CS_UNUSED)
if retval != C.CS_SUCCEED {
return makeError(retval, "C.ct_close failed, connection has results pending")
}
retval = C.ct_con_drop(conn.conn)
if retval != C.CS_SUCCEED {
return makeError(retval, "C.ct_con_drop failed")
}
conn.conn = nil
return nil
}
// Ping implements the driver.Pinger interface.
func (conn *Connection) Ping(ctx context.Context) error {
rows, err := conn.QueryContext(ctx, "SELECT 'PING'", nil)
if err != nil {
return driver.ErrBadConn
}
defer rows.Close()
cols := rows.Columns()
cellRefs := make([]driver.Value, len(cols))
for {
err := rows.Next(cellRefs)
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("Error occurred while exhausting result set: %w", err)
}
}
return nil
}
// Exec implements the driver.Execer interface.
func (conn *Connection) Exec(query string, args []driver.Value) (driver.Result, error) {
return conn.ExecContext(context.Background(), query, dblib.ValuesToNamedValues(args))
}
// ExecContext implements the driver.ExecerContext interface.
func (conn *Connection) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
_, result, err := conn.GenericExec(ctx, query, args)
return result, err
}
// Query implements the driver.Queryer interface.
func (conn *Connection) Query(query string, args []driver.Value) (driver.Rows, error) {
return conn.QueryContext(context.Background(), query, dblib.ValuesToNamedValues(args))
}
// QueryContext implements the driver.QueryerContext interface.
func (conn *Connection) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
rows, _, err := conn.GenericExec(ctx, query, args)
return rows, err
}
// CheckNamedValue implements the driver.NamedValueChecker interface.
func (conn *Connection) CheckNamedValue(nv *driver.NamedValue) error {
v, err := asetypes.DefaultValueConverter.ConvertValue(nv.Value)
if err != nil {
return err
}
nv.Value = v
return nil
}