-
-
Notifications
You must be signed in to change notification settings - Fork 174
/
main.go
212 lines (196 loc) · 4.33 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
package main
import (
"database/sql"
"flag"
"fmt"
_ "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 insertData(conn *sql.DB) error {
t := time.Now()
index := 1
stmt, err := conn.Prepare(`INSERT INTO GOORA_TEMP_VISIT(VISIT_ID, NAME, VAL, VISIT_DATE)
VALUES(:1, :2, :3, :4)`)
if err != nil {
return err
}
defer func() {
_ = stmt.Close()
}()
nameText := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
val := 1.1
for index = 1; index <= 100; index++ {
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
}
fmt.Println("100 rows inserted: ", time.Now().Sub(t))
return nil
}
func queryData(conn *sql.DB) error {
t := time.Now()
rows, err := conn.Query("SELECT VISIT_ID, NAME, VAL, VISIT_DATE FROM GOORA_TEMP_VISIT")
if err != nil {
return err
}
defer func() {
err = rows.Close()
if err != nil {
fmt.Println("Can't close dataset: ", 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 rows: ", time.Now().Sub(t))
return nil
}
func updateData(conn *sql.DB) error {
t := time.Now()
updStmt, err := conn.Prepare(`UPDATE GOORA_TEMP_VISIT SET NAME = :1 WHERE VISIT_ID = :2`)
if err != nil {
return err
}
nameText := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
defer func() {
_ = updStmt.Close()
}()
for index := 1; index <= 100; index++ {
_, err = updStmt.Exec(nameText[:101-index], index)
if err != nil {
return err
}
}
fmt.Println("Finish update: ", time.Now().Sub(t))
return nil
}
func deleteData(conn *sql.DB) error {
t := time.Now()
_, err := conn.Exec("delete from GOORA_TEMP_VISIT")
if err != nil {
return err
}
fmt.Println("Finish delete: ", 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 usage() {
fmt.Println()
fmt.Println("crud")
fmt.Println(" a complete code of create table insert, update, query and delete then drop table.")
fmt.Println()
fmt.Println("Usage:")
fmt.Println(` curd -server server_url`)
flag.PrintDefaults()
fmt.Println()
fmt.Println("Example:")
fmt.Println(` crud -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", server)
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 = insertData(conn)
if err != nil {
fmt.Println("Can't insert data: ", err)
return
}
err = queryData(conn)
if err != nil {
fmt.Println("Can't query data: ", err)
return
}
err = updateData(conn)
if err != nil {
fmt.Println("Can't update data: ", err)
return
}
err = queryData(conn)
if err != nil {
fmt.Println("Can't query data: ", err)
return
}
err = deleteData(conn)
if err != nil {
fmt.Println("Can't delete data: ", err)
return
}
}