/
oracle.go
260 lines (226 loc) · 8.44 KB
/
oracle.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
Copyright © 2020 Marvin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package oracle
import (
"context"
"database/sql"
"fmt"
"github.com/godror/godror"
"github.com/godror/godror/dsn"
"github.com/wentaojin/transferdb/common"
"github.com/wentaojin/transferdb/config"
"runtime"
"strconv"
"strings"
)
type Oracle struct {
Ctx context.Context
OracleDB *sql.DB
}
// 创建 oracle 数据库引擎
func NewOracleDBEngine(ctx context.Context, oraCfg config.OracleConfig, currentSchema string) (*Oracle, error) {
// https://pkg.go.dev/github.com/godror/godror
// https://github.com/godror/godror/blob/db9cd12d89cdc1c60758aa3f36ece36cf5a61814/doc/connection.md
// https://godror.github.io/godror/doc/connection.html
// You can specify connection timeout seconds with "?connect_timeout=15" - Ping uses this timeout, NOT the Deadline in Context!
// For more connection options, see [Godor Connection Handling](https://godror.github.io/godror/doc/connection.html).
var (
connString string
oraDSN dsn.ConnectionParams
err error
)
//https://www.syntio.net/en/labs-musings/efficient-fetching-of-data-from-oracle-database-in-golang/
// https://github.com/godror/godror/pull/65
//connClass := fmt.Sprintf("pool_%v", xid.New().String())
//connString = fmt.Sprintf("oracle://@%s/%s?connectionClass=%s&%s",
// common.StringsBuilder(oraCfg.Host, ":", strconv.Itoa(oraCfg.Port)),
// oraCfg.ServiceName, "connClass", oraCfg.ConnectParams)
connString = fmt.Sprintf("oracle://@%s/%s?standaloneConnection=1&%s",
common.StringsBuilder(oraCfg.Host, ":", strconv.Itoa(oraCfg.Port)),
oraCfg.ServiceName, oraCfg.ConnectParams)
oraDSN, err = godror.ParseDSN(connString)
if err != nil {
return nil, err
}
oraDSN.Username, oraDSN.Password = oraCfg.Username, godror.NewPassword(oraCfg.Password)
if !strings.EqualFold(oraCfg.PDBName, "") {
oraCfg.SessionParams = append(oraCfg.SessionParams, fmt.Sprintf(`ALTER SESSION SET CONTAINER = %s`, oraCfg.PDBName))
}
if !strings.EqualFold(oraCfg.Username, currentSchema) && !strings.EqualFold(currentSchema, "") {
oraCfg.SessionParams = append(oraCfg.SessionParams, fmt.Sprintf(`ALTER SESSION SET CURRENT_SCHEMA = %s`, currentSchema))
}
// 关闭外部认证
oraDSN.ExternalAuth = false
oraDSN.OnInitStmts = oraCfg.SessionParams
// libDir won't have any effect on Linux for linking reasons to do with Oracle's libnnz library that are proving to be intractable.
// You must set LD_LIBRARY_PATH or run ldconfig before your process starts.
// This is documented in various places for other drivers that use ODPI-C. The parameter works on macOS and Windows.
if !strings.EqualFold(oraCfg.LibDir, "") {
switch runtime.GOOS {
case "windows", "darwin":
oraDSN.LibDir = oraCfg.LibDir
}
}
// charset 字符集
if !strings.EqualFold(oraCfg.Charset, "") {
oraDSN.CommonParams.Charset = oraCfg.Charset
}
// godror logger 日志输出
// godror.SetLogger(zapr.NewLogger(zap.L()))
sqlDB := sql.OpenDB(godror.NewConnector(oraDSN))
sqlDB.SetMaxIdleConns(0)
sqlDB.SetMaxOpenConns(0)
sqlDB.SetConnMaxLifetime(0)
err = sqlDB.Ping()
if err != nil {
return nil, fmt.Errorf("error on ping oracle database connection:%v", err)
}
return &Oracle{
Ctx: ctx,
OracleDB: sqlDB,
}, nil
}
// Only Used for ALL Mode
func NewOracleLogminerEngine(ctx context.Context, oraCfg config.OracleConfig) (*Oracle, error) {
// https://pkg.go.dev/github.com/godror/godror
// https://github.com/godror/godror/blob/db9cd12d89cdc1c60758aa3f36ece36cf5a61814/doc/connection.md
// https://godror.github.io/godror/doc/connection.html
// You can specify connection timeout seconds with "?connect_timeout=15" - Ping uses this timeout, NOT the Deadline in Context!
// For more connection options, see [Godor Connection Handling](https://godror.github.io/godror/doc/connection.html).
var (
connString string
oraDSN dsn.ConnectionParams
err error
)
// https://github.com/godror/godror/pull/65
//connClass := fmt.Sprintf("pool_%v", xid.New().String())
//connString = fmt.Sprintf("oracle://@%s/%s?connectionClass=%s&%s",
// common.StringsBuilder(oraCfg.Host, ":", strconv.Itoa(oraCfg.Port)),
// oraCfg.ServiceName, connClass, oraCfg.ConnectParams)
connString = fmt.Sprintf("oracle://@%s/%s?standaloneConnection=1&%s",
common.StringsBuilder(oraCfg.Host, ":", strconv.Itoa(oraCfg.Port)),
oraCfg.ServiceName, oraCfg.ConnectParams)
oraDSN, err = godror.ParseDSN(connString)
if err != nil {
return nil, err
}
oraDSN.Username, oraDSN.Password = oraCfg.Username, godror.NewPassword(oraCfg.Password)
// 关闭外部认证
oraDSN.ExternalAuth = false
oraDSN.OnInitStmts = oraCfg.SessionParams
// libDir won't have any effect on Linux for linking reasons to do with Oracle's libnnz library that are proving to be intractable.
// You must set LD_LIBRARY_PATH or run ldconfig before your process starts.
// This is documented in various places for other drivers that use ODPI-C. The parameter works on macOS and Windows.
if !strings.EqualFold(oraCfg.LibDir, "") {
switch runtime.GOOS {
case "windows", "darwin":
oraDSN.LibDir = oraCfg.LibDir
}
}
// charset 字符集
if !strings.EqualFold(oraCfg.Charset, "") {
oraDSN.Charset = oraCfg.Charset
}
// godror logger 日志输出
// godror.SetLogger(zapr.NewLogger(zap.L()))
sqlDB := sql.OpenDB(godror.NewConnector(oraDSN))
sqlDB.SetMaxIdleConns(0)
sqlDB.SetMaxOpenConns(0)
sqlDB.SetConnMaxLifetime(0)
err = sqlDB.Ping()
if err != nil {
return nil, fmt.Errorf("error on ping oracle database connection:%v", err)
}
return &Oracle{
Ctx: ctx,
OracleDB: sqlDB,
}, nil
}
func Query(ctx context.Context, db *sql.DB, querySQL string) ([]string, []map[string]string, error) {
var (
cols []string
res []map[string]string
)
rows, err := db.QueryContext(ctx, querySQL)
if err != nil {
return cols, res, fmt.Errorf("general sql [%v] query failed: [%v]", querySQL, err.Error())
}
defer rows.Close()
//不确定字段通用查询,自动获取字段名称
cols, err = rows.Columns()
if err != nil {
return cols, res, fmt.Errorf("general sql [%v] query rows.Columns failed: [%v]", querySQL, err.Error())
}
values := make([][]byte, len(cols))
scans := make([]interface{}, len(cols))
for i := range values {
scans[i] = &values[i]
}
for rows.Next() {
err = rows.Scan(scans...)
if err != nil {
return cols, res, fmt.Errorf("general sql [%v] query rows.Scan failed: [%v]", querySQL, err.Error())
}
row := make(map[string]string)
for k, v := range values {
// Oracle/Mysql 对于 'NULL' 统一字符 NULL 处理,查询出来转成 NULL,所以需要判断处理
// 查询字段值 NULL
// 如果字段值 = NULLABLE 则表示值是 NULL
// 如果字段值 = "" 则表示值是空字符串
// 如果字段值 = 'NULL' 则表示值是 NULL 字符串
// 如果字段值 = 'null' 则表示值是 null 字符串
if v == nil {
row[cols[k]] = "NULLABLE"
} else {
// 处理空字符串以及其他值情况
// 数据统一 string 格式显示
row[cols[k]] = string(v)
}
}
res = append(res, row)
}
if err = rows.Err(); err != nil {
return cols, res, fmt.Errorf("general sql [%v] query rows.Next failed: [%v]", querySQL, err.Error())
}
return cols, res, nil
}
func (o *Oracle) GetOracleSchemas() ([]string, error) {
var (
schemas []string
err error
)
cols, res, err := Query(o.Ctx, o.OracleDB, `SELECT DISTINCT username FROM DBA_USERS`)
if err != nil {
return schemas, err
}
for _, col := range cols {
for _, r := range res {
schemas = append(schemas, common.StringUPPER(r[col]))
}
}
return schemas, nil
}
func (o *Oracle) GetOracleSchemaTable(schemaName string) ([]string, error) {
var (
tables []string
err error
)
_, res, err := Query(o.Ctx, o.OracleDB, fmt.Sprintf(`SELECT table_name AS TABLE_NAME FROM DBA_TABLES WHERE UPPER(owner) = UPPER('%s') AND (IOT_TYPE IS NUll OR IOT_TYPE='IOT')`, schemaName))
if err != nil {
return tables, err
}
for _, r := range res {
tables = append(tables, strings.ToUpper(r["TABLE_NAME"]))
}
return tables, nil
}