-
-
Notifications
You must be signed in to change notification settings - Fork 174
/
main.go
303 lines (284 loc) · 7.59 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
package main
import (
"database/sql"
"database/sql/driver"
"flag"
"fmt"
go_ora "github.com/sijms/go-ora/v2"
"os"
"time"
)
func createTable(conn *sql.DB) error {
t := time.Now()
sqlText := `CREATE TABLE GOORA_TEMP_VISIT(
VISIT_ID number(10) NOT NULL,
NAME VARCHAR(200),
VAL number(10,2),
VISIT_DATE date,
PRIMARY KEY(VISIT_ID)
)`
_, err := conn.Exec(sqlText)
if err != nil {
return err
}
fmt.Println("Finish create table GOORA_TEMP_VISIT :", time.Now().Sub(t))
return nil
}
func dropTable(conn *sql.DB) error {
t := time.Now()
_, err := conn.Exec("drop table GOORA_TEMP_VISIT purge")
if err != nil {
return err
}
fmt.Println("Finish drop table: ", time.Now().Sub(t))
return nil
}
func bulkInsert(databaseUrl string) error {
conn, err := go_ora.NewConnection(databaseUrl)
if err != nil {
return err
}
err = conn.Open()
if err != nil {
return err
}
defer func() {
err = conn.Close()
if err != nil {
fmt.Println("Can't close connection: ", err)
}
}()
t := time.Now()
sqlText := `INSERT INTO GOORA_TEMP_VISIT(VISIT_ID, NAME, VAL, VISIT_DATE) VALUES(:1, :2, :3, :4)`
rowNum := 100
visitID := make([]driver.Value, rowNum)
nameText := make([]driver.Value, rowNum)
val := make([]driver.Value, rowNum)
date := make([]driver.Value, rowNum)
initalVal := 1.1
for index := 0; index < rowNum; index++ {
visitID[index] = index + 1
nameText[index] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
val[index] = initalVal
date[index] = time.Now().AddDate(0, index, 0)
initalVal += 1.1
//if index%5 == 0 {
// _, err = stmt.Exec(index, nameText, val, nil)
//} else {
// _, err = stmt.Exec(index, nameText, val, time.Now())
//}
//if err != nil {
// return err
//}
//val += 1.1
}
result, err := conn.BulkInsert(sqlText, rowNum, visitID, nameText, val, date)
if err != nil {
return err
}
rowsAffected, _ := result.RowsAffected()
fmt.Printf("%d rows inserted: %v\n", rowsAffected, time.Now().Sub(t))
return nil
}
func dropPackage(conn *sql.DB) error {
t := time.Now()
_, err := conn.Exec(`drop package GOORA_TEMP_PKG`)
if err != nil {
return err
}
fmt.Println("Drop package: ", time.Now().Sub(t))
return nil
}
func createPackage(conn *sql.DB) error {
t := time.Now()
sqlText := `create or replace package GOORA_TEMP_PKG as
type t_visit_id is table of GOORA_TEMP_VISIT.visit_id%type index by binary_integer;
type t_visit_name is table of GOORA_TEMP_VISIT.name%type index by binary_integer;
type t_visit_val is table of GOORA_TEMP_VISIT.val%type index by binary_integer;
type t_visit_date is table of GOORA_TEMP_VISIT.visit_date%type index by binary_integer;
procedure test_get1(p_visit_id t_visit_id, l_cursor out SYS_REFCURSOR);
procedure test_get2(p_visit_id t_visit_id, p_visit_name out t_visit_name,
p_visit_val out t_visit_val, p_visit_date out t_visit_date);
end GOORA_TEMP_PKG;
`
_, err := conn.Exec(sqlText)
if err != nil {
return err
}
sqlText = `create or replace PACKAGE BODY GOORA_TEMP_PKG as
procedure test_get1(p_visit_id t_visit_id, l_cursor out SYS_REFCURSOR) as
temp t_visit_id := p_visit_id;
begin
OPEN l_cursor for select visit_id, name, val, visit_date from GOORA_TEMP_VISIT
where visit_id in (select column_value from table(temp));
end test_get1;
procedure test_get2(p_visit_id t_visit_id, p_visit_name out t_visit_name,
p_visit_val out t_visit_val, p_visit_date out t_visit_date) as
temp t_visit_id := p_visit_id;
cursor tempCur is select visit_id, name, val, visit_date from GOORA_TEMP_VISIT
where visit_id in (select column_value from table(temp));
tempRow tempCur%rowtype;
idx number := 1;
begin
for tempRow in tempCur loop
p_visit_name(idx) := tempRow.name;
p_visit_val(idx) := tempRow.val;
p_visit_date(idx) := tempRow.visit_date;
idx := idx + 1;
end loop;
end test_get2;
end GOORA_TEMP_PKG;
`
_, err = conn.Exec(sqlText)
if err != nil {
return err
}
fmt.Println("Finish create package: ", time.Now().Sub(t))
return nil
}
// function do something like "SELECT col1, col2, ... FROM table WHERE col1 in (values)"
func query1(conn *sql.DB) error {
t := time.Now()
var cursor go_ora.RefCursor
// sql code take input array of integer and return a cursor that can be queried for result
_, err := conn.Exec(`BEGIN GOORA_TEMP_PKG.TEST_GET1(:1, :2); END;`, []int64{1, 3, 5}, sql.Out{Dest: &cursor})
if err != nil {
return err
}
defer func() {
err = cursor.Close()
if err != nil {
fmt.Println("Can't close RefCursor", err)
}
}()
err = queryCursor(&cursor)
if err != nil {
return err
}
fmt.Println("Finish Query1: ", time.Now().Sub(t))
return nil
}
// this function take one input array and return 3 output arraies of different types
func query2(conn *sql.DB) error {
t := time.Now()
var (
nameArray []string
valArray []float32
dateArray []sql.NullTime
)
// note size here is important and equal to max number of items that array can accommodate
_, err := conn.Exec(`BEGIN GOORA_TEMP_PKG.TEST_GET2(:1, :2, :3, :4); END;`,
[]int{1, 3, 5}, go_ora.Out{Dest: &nameArray, Size: 10},
go_ora.Out{Dest: &valArray, Size: 10},
go_ora.Out{Dest: &dateArray, Size: 10})
if err != nil {
return err
}
fmt.Println(nameArray)
fmt.Println(valArray)
fmt.Println(dateArray)
fmt.Println("Finish Query2: ", time.Now().Sub(t))
return nil
}
func queryCursor(cursor *go_ora.RefCursor) error {
t := time.Now()
rows, err := cursor.Query()
if err != nil {
return err
}
var (
id int64
name string
val float32
date sql.NullTime
)
for rows.Next_() {
err = rows.Scan(&id, &name, &val, &date)
if err != nil {
return err
}
fmt.Println("ID: ", id, "\tName: ", name, "\tval: ", val, "\tDate: ", date)
}
fmt.Println("Finish query RefCursor: ", time.Now().Sub(t))
return rows.Err()
}
func usage() {
fmt.Println()
fmt.Println("array")
fmt.Println(" a complete code dealing with array.")
fmt.Println()
fmt.Println("Usage:")
fmt.Println(` array -server server_url`)
flag.PrintDefaults()
fmt.Println()
fmt.Println("Example:")
fmt.Println(` array -server "oracle://user:pass@server/service_name"`)
fmt.Println()
}
func main() {
var (
server string
)
flag.StringVar(&server, "server", "", "Server's URL, oracle://user:pass@server/service_name")
flag.Parse()
connStr := os.ExpandEnv(server)
if connStr == "" {
fmt.Println("Missing -server option")
usage()
os.Exit(1)
}
fmt.Println("Connection string: ", connStr)
conn, err := sql.Open("oracle", connStr)
if err != nil {
fmt.Println("Can't open the driver: ", err)
return
}
defer func() {
err = conn.Close()
if err != nil {
fmt.Println("Can't close connection: ", err)
}
}()
err = conn.Ping()
if err != nil {
fmt.Println("Can't ping connection: ", err)
return
}
err = createTable(conn)
if err != nil {
fmt.Println("Can't create table: ", err)
return
}
defer func() {
err = dropTable(conn)
if err != nil {
fmt.Println("Can't drop table: ", err)
}
}()
err = bulkInsert(connStr)
if err != nil {
fmt.Println("Can't bulkInsert: ", err)
return
}
// oracle assocuative array need type table indexed by binary_integer which can
// only defined inside the package
err = createPackage(conn)
if err != nil {
fmt.Println("Can't create package: ", err)
}
defer func() {
err = dropPackage(conn)
if err != nil {
fmt.Println("Can't drop package: ", err)
}
}()
err = query1(conn)
if err != nil {
fmt.Println("Can't call query1: ", err)
return
}
err = query2(conn)
if err != nil {
fmt.Println("Can't call query2: ", err)
}
}