-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
331 lines (288 loc) · 8.65 KB
/
main.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"database/sql"
"flag"
"fmt"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"strconv"
"strings"
)
var cfg Config
type Config struct {
oracle struct {
databaseImport struct {
dsn string `yaml:"dsn"`
database string `yaml:"database"`
} `yaml:"database_import"`
fileImport struct {
fileLocation string `yaml:"file_location"`
} `yaml:"file_import"`
} `yaml:"oracle"`
clickhouse struct {
tablePrefix string `yaml:"table_prefix"`
fileLocation string `yaml:"file_location"`
} `yaml:"clickhouse"`
general struct {
debug bool `yaml:"debug"`
} `yaml:"general"`
}
var (
flagDebug bool
flagConfig string
)
// TODO: add more data types and find a solution for the precision problem
var ora2ChConversion = map[string]string{
"ANYDATA": "String",
"BINARY_DOUBLE": "String",
"BLOB": "String",
"CHAR": "String",
"CLOB": "String",
"COL_CLS_LIST": "String",
"DATE": "DateTime",
"DS_VARRAY_4_CLOB": "String",
"FLOAT": "Float128",
"LONG": "Int128",
"LONGRAW": "Int128",
"NUMBER": "Decimal256(30)",
"NVARCHAR2": "String",
"RAW": "String",
"ROWID": "Int128",
"SDO_DIM_ARRAY": "String",
"SDO_GEOMETRY": "String",
"SDO_NUMBER_ARRAY": "String",
"SDO_ORGSCL_TYPE": "String",
"SDO_STRING_ARRAY": "String",
"TIMESTAMP(0)": "DateTime",
"TIMESTAMP(3)": "DateTime",
"TIMESTAMP(3)WITHTIMEZONE": "DateTime",
"TIMESTAMP(6)": "DateTime",
"TIMESTAMP(6)WITHTIMEZONE": "DateTime",
"TIMESTAMP(9)": "DateTime",
"UNDEFINED": "String",
"VARCHAR2": "String",
"XMLTYPE": "String",
}
type oracleDbSchema struct {
owner string
tableName string
columnName string
dataType string
dataLength int
dataPrecision int
nullable bool
}
func init() {
flag.BoolVar(&flagDebug, "debug", false, "debug mode")
flag.StringVar(&flagConfig, "config", "config.yaml", "config file")
flag.Parse()
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.SetGlobalLevel(zerolog.InfoLevel)
f, err := os.Open(flagConfig)
if err != nil {
log.Panic().Err(err).Msg("Couldn't open config file")
}
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&cfg)
if err != nil {
log.Panic().Err(err).Msg("Error while parsing config file")
}
err = f.Close()
if err != nil {
log.Panic().Err(err).Msg("Error while closing file handler")
}
}
func main() {
var data []oracleDbSchema
flagDebug = true
if flagDebug {
cfg.oracle.fileImport.fileLocation = "output_example"
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
if cfg.oracle.fileImport.fileLocation != "" && cfg.oracle.databaseImport.dsn != "" {
log.Panic().Msg("Please provide at least 'database_import' and optionally 'file_import' in your config")
} else if cfg.oracle.fileImport.fileLocation != "" {
data = connectByFile(cfg.oracle.fileImport.fileLocation)
} else if cfg.oracle.fileImport.fileLocation == "" {
data = connectByODBC(cfg.oracle.databaseImport.dsn)
} else {
log.Panic().Msg("Please either provide at least 'database_import' in your config")
}
clickhouseQuery := generateClickhouseQuery(data)
saveClickhouseQuery(clickhouseQuery)
}
func connectByFile(filename string) []oracleDbSchema {
var fileContent string = openFile(filename)
var data []oracleDbSchema
log.Debug().Str("file content", fileContent)
if fileContent == "" {
log.Error().Msg("No file found...")
os.Exit(2)
}
data = parseFile(fileContent)
return data
}
func connectByODBC(dsn string) []oracleDbSchema {
// TODO: test if odbc connection works
var queryResult oracleDbSchema
var allQueryResult []oracleDbSchema
var err error
// Open Connection. Provide DSN, Username and Password
db, err := sql.Open("odbc", fmt.Sprintf("DSN=%v", dsn))
if err != nil {
log.Panic().Err(err).Msg("Couldn't connect to database")
} else {
log.Debug().Msg("Connection to DB successful")
}
// Provide the Query to execute
rows, err := db.Query("SELECT * from all_tab_columns")
if err != nil {
log.Panic().Err(err).Msg("Unable to query")
}
// Parse the Result set
for rows.Next() {
err = rows.Scan(&queryResult.owner, &queryResult.tableName, &queryResult.columnName, &queryResult.dataType, nil, nil, &queryResult.dataLength, &queryResult.dataPrecision, nil, &queryResult.nullable)
if err != nil {
log.Error().Err(err).Msg("Error while parsing result")
}
allQueryResult = append(allQueryResult, queryResult)
}
// Close the connection
err = rows.Close()
if err != nil {
log.Err(err)
}
err = db.Close()
if err != nil {
log.Err(err)
}
return allQueryResult
}
func saveClickhouseQuery(sqlQuery string) {
var err error
var f *os.File
f, err = os.OpenFile(cfg.clickhouse.fileLocation, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Error().Err(err).Msg("Couldn't create file")
}
_, err = f.WriteString(sqlQuery)
if err != nil {
log.Error().Err(err).Msg("Couldn't write into file")
}
err = f.Sync()
if err != nil {
log.Error().Err(err).Msg("Couldn't synchronize with disk")
}
err = f.Close()
if err != nil {
log.Error().Err(err).Msg("Couldn't close file")
}
}
func generateClickhouseQuery(oracleDbSchemas []oracleDbSchema) string {
var sqlTablePrefix string = "ora_"
var sqlColumns string
var sqlHead string
var sqlFoot string
var sqlDropTable string
var sqlQuery string
allTableNames := getAllTableNames(oracleDbSchemas)
log.Debug().Interface("allTableNames", allTableNames)
for _, allTableName := range allTableNames {
log.Debug().Str("allTableName", allTableName.tableName)
if allTableName.owner != "REPLACE" {
continue
}
for _, item := range oracleDbSchemas {
log.Debug().Str("oracleDbSchemas", item.tableName)
sqlDropTable = fmt.Sprintf("DROP TABLE IF EXISTS %v%v;", sqlTablePrefix, allTableName.tableName)
sqlHead = fmt.Sprintf("CREATE TABLE %v%v (", sqlTablePrefix, allTableName.tableName)
sqlFoot = fmt.Sprintf(") ENGINE = ODBC('DSN=%v', '%v', '%v');", cfg.oracle.databaseImport.dsn, item.owner, allTableName.tableName) // TODO: Config of REPLACE
if item.tableName == allTableName.tableName {
sqlColumns += fmt.Sprintf("`%v` %v,", item.columnName, ora2ChConversion[item.dataType])
log.Debug().Str("sqlHead", sqlHead).Str("sqlFoot", sqlFoot)
}
}
if sqlColumns != "" {
// cut off trailing comma in sql query
sqlColumns = sqlColumns[:len(sqlColumns)-1]
sqlQuery += sqlDropTable + "\n" + sqlHead + sqlColumns + sqlFoot + "\n\n"
log.Debug().Str("sqlQuery", sqlQuery)
sqlHead = ""
sqlColumns = ""
sqlFoot = ""
}
}
return sqlQuery
}
func getAllTableNames(oracleDbSchemas []oracleDbSchema) []oracleDbSchema {
var allTableNames []oracleDbSchema
m := make(map[string]int)
for _, item := range oracleDbSchemas {
if _, ok := m[item.tableName]; ok {
log.Debug().Msg("duplicate table key found")
} else {
log.Debug().Msg("new table key found")
m[item.tableName] = 1
allTableNames = append(allTableNames, item)
}
}
return allTableNames
}
func parseFile(content string) []oracleDbSchema {
var plus int = 0
var strippedString string = ""
var splittedString []string = []string{""}
var oracleTableData oracleDbSchema = oracleDbSchema{}
var allOracleTableData []oracleDbSchema
for _, line := range strings.Split(strings.TrimSuffix(content, "\n"), "\n") {
if string(line[0]) == "+" {
plus++
}
if plus == 4 {
if string(line[0]) == "+" {
continue
}
strippedString = strings.ReplaceAll(line, " ", "")
splittedString = strings.Split(strippedString, "|")
for i, item := range splittedString {
switch i {
case 0:
continue
case 1:
oracleTableData.owner = item
case 2:
oracleTableData.tableName = item
case 3:
oracleTableData.columnName = item
case 4:
oracleTableData.dataType = item
case 7:
oracleTableData.dataLength, _ = strconv.Atoi(item)
case 8:
oracleTableData.dataPrecision, _ = strconv.Atoi(item)
case 10:
if item == "Y" {
oracleTableData.nullable = true
} else {
oracleTableData.nullable = false
}
default:
continue
}
}
allOracleTableData = append(allOracleTableData, oracleTableData)
}
}
log.Debug().Interface("allOracleTableData", allOracleTableData)
return allOracleTableData
}
func openFile(filename string) string {
dat, err := ioutil.ReadFile(filename)
if err != nil {
log.Panic().Err(err).Msg("Couldn't read file")
}
return string(dat)
}