-
-
Notifications
You must be signed in to change notification settings - Fork 174
/
main.go
165 lines (160 loc) · 3.56 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
package main
import (
"database/sql"
"fmt"
_ "github.com/sijms/go-ora/v2"
"os"
"strings"
"time"
)
func _print(prefix string, val interface{}) {
fmt.Println(prefix, val)
}
func createTable(conn *sql.DB) error {
t := time.Now()
sqlText := `CREATE TABLE TEMP_TABLE_343(
ID number(10) NOT NULL,
NAME VARCHAR(500),
VAL number(10,2),
LDATE date,
PRIMARY KEY(ID)
)`
_, err := conn.Exec(sqlText)
if err != nil {
return err
}
_print("finish create table: ", time.Now().Sub(t))
return nil
}
func dropTable(conn *sql.DB) error {
t := time.Now()
_, err := conn.Exec("drop table TEMP_TABLE_343 purge")
if err != nil {
return err
}
_print("finish drop table: ", time.Now().Sub(t))
return nil
}
func insert(conn *sql.DB) error {
t := time.Now()
sqlText := `INSERT INTO TEMP_TABLE_343(ID, NAME, VAL, LDATE) VALUES(:ID, :NAME, :VAL, :LDATE)`
length := 500
ids := make([]int, length)
names := make([]string, length+1)
vals := make([]float32, length+2)
dates := make([]time.Time, length)
for x := 0; x < length; x++ {
ids[x] = x + 1
names[x] = strings.Repeat("*", x+1)
vals[x] = float32(length) / float32(x+1)
dates[x] = time.Now()
}
_, err := conn.Exec(sqlText, sql.Named("ID", ids),
sql.Named("NAME", names),
sql.Named("VAL", vals),
sql.Named("LDATE", dates))
if err != nil {
return err
}
_print("finish insert: ", time.Now().Sub(t))
return nil
}
func merge(conn *sql.DB) error {
t := time.Now()
sqlText := `MERGE INTO TEMP_TABLE_343 t1 USING(select :ID ID,:NAME NAME,:VAL VAL,:LDATE LDATE from dual) tmp
ON (tmp.ID=t1.ID)
WHEN MATCHED THEN UPDATE SET NAME=:NAME, VAL=:VAL, LDATE=:LDATE
WHEN NOT MATCHED THEN INSERT (ID, NAME, VAL, LDATE) VALUES (:ID, :NAME, :VAL , :LDATE)`
length := 500
ids := make([]int, length)
names := make([]string, length+1)
vals := make([]float32, length+2)
dates := make([]time.Time, length)
for x := 0; x < length; x++ {
ids[x] = x + 1
names[x] = strings.Repeat("+", x+1)
vals[x] = float32(length) / float32(x+1)
dates[x] = time.Now()
}
_, err := conn.Exec(sqlText, sql.Named("ID", ids),
sql.Named("NAME", names),
sql.Named("VAL", vals),
sql.Named("LDATE", dates))
if err != nil {
return err
}
_print("finish merge: ", time.Now().Sub(t))
return nil
}
func query(conn *sql.DB) error {
t := time.Now()
rows, err := conn.Query(`SELECT ID, NAME, VAL, LDATE FROM TEMP_TABLE_343 WHERE ID <= 10 ORDER BY ID`)
if err != nil {
return err
}
defer func() {
err = rows.Close()
if err != nil {
fmt.Println("can't close rows: ", err)
}
}()
var (
id int
name string
val float32
date time.Time
)
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: ", time.Now().Sub(t))
return nil
}
func main() {
conn, err := sql.Open("oracle", os.Getenv("DSN"))
if err != nil {
_print("can't open connection: ", err)
}
defer func() {
err = conn.Close()
if err != nil {
_print("can't close connection: ", err)
return
}
}()
err = createTable(conn)
if err != nil {
_print("can't create table: ", err)
return
}
defer func() {
err = dropTable(conn)
if err != nil {
_print("can't drop table: ", err)
}
}()
err = insert(conn)
if err != nil {
_print("can't insert: ", err)
return
}
err = query(conn)
if err != nil {
fmt.Println("can't query: ", err)
return
}
err = merge(conn)
if err != nil {
_print("can't merge: ", err)
return
}
err = query(conn)
if err != nil {
fmt.Println("can't query: ", err)
return
}
}