forked from sijms/go-ora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect_option.go
executable file
·210 lines (202 loc) · 4.88 KB
/
connect_option.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
package network
import (
"errors"
"fmt"
"net"
"regexp"
"strconv"
"strings"
"time"
"github.com/sijms/go-ora/trace"
)
type ServerAddr struct {
Protocol string
Addr string
Port int
}
type ClientInfo struct {
ProgramPath string
ProgramName string
UserName string
Password string
HostName string
DomainName string
DriverName string
PID int
}
type DatabaseInfo struct {
UserID string
Servers []ServerAddr
serverIndex int
SID string
ProxyClientName string
ServiceName string
InstanceName string
DBName string
AuthType int
connStr string
}
type SessionInfo struct {
SSLVersion string
Timeout time.Duration
UnixAddress string
TransportDataUnitSize uint32
SessionDataUnitSize uint32
Protocol string
SSL bool
SSLVerify bool
}
type AdvNegoSeviceInfo struct {
AuthService []string
}
type ConnectionOption struct {
ClientInfo
DatabaseInfo
SessionInfo
AdvNegoSeviceInfo
Tracer trace.Tracer
PrefetchRows int
}
func extractServers(connStr string) ([]ServerAddr, error) {
r, err := regexp.Compile(`(?i)\(\s*ADDRESS\s*=\s*(\(\s*(HOST)\s*=\s*([\w,\.,\-]+)\s*\)|\(\s*(PORT)\s*=\s*([0-9]+)\s*\)|\(\s*(COMMUNITY)\s*=\s*([\w,\.,\-]+)\s*\)|\(\s*(PORT)\s*=\s*([0-9]+)\s*\)|\(\s*(PROTOCOL)\s*=\s*(\w+)\s*\))+\)`)
if err != nil {
return nil, err
}
ret := make([]ServerAddr, 0, 5)
matchs := r.FindAllStringSubmatch(connStr, -1)
for _, match := range matchs {
server := ServerAddr{
Port: 1521,
}
for x := 2; x < len(match); x++ {
if strings.ToUpper(match[x]) == "PROTOCOL" {
x++
server.Protocol = match[x]
continue
}
if strings.ToUpper(match[x]) == "PORT" {
x++
server.Port, err = strconv.Atoi(match[x])
if err != nil {
return nil, err
}
continue
}
if strings.ToUpper(match[x]) == "HOST" {
x++
server.Addr = match[x]
}
}
if len(server.Addr) > 0 {
ret = append(ret, server)
}
}
return ret, nil
}
func (op *ConnectionOption) updateSSL(server *ServerAddr) error {
if server != nil {
if strings.ToLower(server.Protocol) == "tcps" {
op.SSL = true
return nil
} else if strings.ToLower(server.Protocol) == "tcp" {
op.SSL = false
return nil
}
}
if strings.ToLower(op.Protocol) == "tcp" {
op.SSL = false
} else if strings.ToLower(op.Protocol) == "tcps" {
op.SSL = true
} else {
return fmt.Errorf("unknown or missing protocol: %s", op.Protocol)
}
return nil
}
func (op *ConnectionOption) UpdateDatabaseInfo(connStr string) error {
op.connStr = connStr
var err error
op.Servers, err = extractServers(connStr)
if err != nil {
return err
}
if len(op.Servers) == 0 {
return errors.New("no address passed in connection string")
}
r, err := regexp.Compile(`(?i)\(\s*SERVICE_NAME\s*=\s*([\w,\.,\-]+)\s*\)`)
if err != nil {
return err
}
match := r.FindStringSubmatch(connStr)
if len(match) > 1 {
op.DatabaseInfo.ServiceName = match[1]
}
r, err = regexp.Compile(`(?i)\(\s*SID\s*=\s*([\w,\.,\-]+)\s*\)`)
if err != nil {
return err
}
match = r.FindStringSubmatch(connStr)
if len(match) > 1 {
op.DatabaseInfo.SID = match[1]
}
r, err = regexp.Compile(`(?i)\(\s*INSTANCE_NAME\s*=\s*([\w,\.,\-]+)\s*\)`)
if err != nil {
return err
}
match = r.FindStringSubmatch(connStr)
if len(match) > 1 {
op.DatabaseInfo.InstanceName = match[1]
}
return nil
}
func (op *ConnectionOption) AddServer(server ServerAddr) {
for i := 0; i < len(op.Servers); i++ {
if server.IsEqual(&op.Servers[i]) {
return
}
}
op.Servers = append(op.Servers, server)
}
func (serv *ServerAddr) IsEqual(input *ServerAddr) bool {
return strings.ToUpper(serv.Addr) == strings.ToUpper(input.Addr) &&
serv.Port == input.Port
}
func (serv *ServerAddr) networkAddr() string {
return net.JoinHostPort(serv.Addr, strconv.Itoa(serv.Port))
}
func (op *ConnectionOption) GetActiveServer(jump bool) *ServerAddr {
if jump {
op.serverIndex++
}
if op.serverIndex >= len(op.Servers) {
return nil
}
return &op.Servers[op.serverIndex]
}
func (op *ConnectionOption) ConnectionData() string {
if len(op.connStr) != 0 {
return op.connStr
}
host := op.GetActiveServer(false)
protocol := op.Protocol
if host.Protocol != "" {
protocol = host.Protocol
}
FulCid := "(CID=(PROGRAM=" + op.ProgramPath + ")(HOST=" + op.HostName + ")(USER=" + op.UserName + "))"
var address string
if len(op.UnixAddress) > 0 {
address = "(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1))"
} else {
address = "(ADDRESS=(PROTOCOL=" + protocol + ")(HOST=" + host.Addr + ")(PORT=" + strconv.Itoa(host.Port) + "))"
}
result := "(CONNECT_DATA="
if op.SID != "" {
result += "(SID=" + op.SID + ")"
} else {
result += "(SERVICE_NAME=" + op.ServiceName + ")"
}
if op.InstanceName != "" {
result += "(INSTANCE_NAME=" + op.InstanceName + ")"
}
result += FulCid
return "(DESCRIPTION=" + address + result + "))"
}