forked from sijms/go-ora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
232 lines (224 loc) · 5.17 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
package main
import (
"flag"
"fmt"
go_ora "github.com/chenyb888/go-ora/v2"
"os"
)
var server string
var dirName string
var fileName string
/* before using this example do the following
* connect to oracle database as sys and create directory object
* put some file in the physical directory
* run the program:
* bfile -server serverUrl -dir dirObjectName -file fileName
*/
func createTable(conn *go_ora.Connection) error {
sqlText := `create table GOORA_TEST_BFILE (
FILE_ID NUMBER(10) NOT NULL,
FILE_DATA BFILE
)`
_, err := conn.Exec(sqlText)
return err
}
func dropTable(conn *go_ora.Connection) error {
_, err := conn.Exec(`drop table GOORA_TEST_BFILE purge`)
return err
}
func insertData(conn *go_ora.Connection) error {
file, err := go_ora.NewBFile(conn, dirName, fileName)
if err != nil {
return err
}
sqlText := `INSERT INTO GOORA_TEST_BFILE(FILE_ID, FILE_DATA) VALUES(:1, :2)`
_, err = conn.Exec(sqlText, 1, file)
return err
}
func workWithBFile(file *go_ora.BFile) error {
err := file.Open()
if err != nil {
return err
}
defer func() {
err = file.Close()
if err != nil {
fmt.Println("Can't close file: ", err)
}
}()
exists, err := file.Exists()
if err != nil {
return err
}
if exists {
length, err := file.GetLength()
if err != nil {
return err
}
fmt.Println("File length: ", length)
fmt.Println("Read all data: ")
data, err := file.Read()
if err != nil {
return err
}
fmt.Println(string(data))
if length > 2 {
fmt.Println("Read From Position 2 to the end of the file: ")
data, err = file.ReadFromPos(2)
if err != nil {
return err
}
fmt.Println(string(data))
}
if length > 5 {
fmt.Println("Read 5 bytes starting from position 2: ")
data, err = file.ReadBytesFromPos(2, 5)
if err != nil {
return err
}
fmt.Println(string(data))
}
}
return nil
}
func BFileOutputPar(conn *go_ora.Connection) error {
sqlText := `BEGIN SELECT FILE_ID, FILE_DATA INTO :1, :2 FROM GOORA_TEST_BFILE WHERE FILE_ID = 1; END;`
var (
id int64
file go_ora.BFile
)
_, err := conn.Exec(sqlText, go_ora.Out{Dest: &id}, go_ora.Out{Dest: &file})
if err != nil {
return err
}
fmt.Println("ID: ", id)
err = workWithBFile(&file)
if err != nil {
return err
}
return nil
}
func queryBFile(conn *go_ora.Connection) error {
sqlText := "SELECT FILE_ID, FILE_DATA FROM GOORA_TEST_BFILE WHERE FILE_ID = 1"
stmt := go_ora.NewStmt(sqlText, conn)
var err error
defer func() {
err = stmt.Close()
if err != nil {
fmt.Println("Can't close stmt: ", err)
}
}()
rows, err := stmt.Query_(nil)
if err != nil {
return err
}
var id int64
var file go_ora.BFile
for rows.Next_() {
err = rows.Scan(&id, &file)
if err != nil {
return err
}
fmt.Println("ID: ", id)
err = workWithBFile(&file)
if err != nil {
return err
}
}
return rows.Err()
}
func usage() {
fmt.Println()
fmt.Println("bfile")
fmt.Println(" a code for dealing with BFile data type")
fmt.Println(" before use the program create directory object in oracle that refer")
fmt.Println(" some physical directory that contains some text file")
fmt.Println()
fmt.Println("Usage:")
fmt.Println(` bfile -server server_url -dir dirName -file fileName`)
flag.PrintDefaults()
fmt.Println()
fmt.Println("Example:")
fmt.Println(` bfile -server "oracle://user:pass@server/service_name" -dir "GOORA_TEMP_DIR" -file "test.bin"`)
fmt.Println()
}
func main() {
flag.StringVar(&server, "server", "", "Server's URL, oracle://user:pass@server/service_name")
flag.StringVar(&dirName, "dir", "", "oracle directory object name")
flag.StringVar(&fileName, "file", "", "physical file name")
flag.Parse()
connStr := os.ExpandEnv(server)
if connStr == "" {
fmt.Println("Missing -server option")
usage()
os.Exit(1)
}
if dirName == "" {
fmt.Println("Missing -dir option")
usage()
os.Exit(1)
}
if fileName == "" {
fmt.Println("Missing -file option")
usage()
os.Exit(1)
}
fmt.Println("Connection string: ", connStr)
conn, err := go_ora.NewConnection(connStr)
if err != nil {
fmt.Println("Can't create connection: ", err)
return
}
err = conn.Open()
if err != nil {
fmt.Println("Can't open connection: ", err)
return
}
defer func() {
err = conn.Close()
if err != nil {
fmt.Println("Can't close connection", err)
}
}()
fmt.Println("Work with BFile object: ")
bfile, err := go_ora.NewBFile(conn, dirName, fileName)
if err != nil {
fmt.Println("Can't create BFile object: ", err)
return
}
err = workWithBFile(bfile)
if err != nil {
fmt.Println("BFile error: ", err)
}
err = createTable(conn)
if err != nil {
fmt.Println("Can't create table: ", err)
return
}
fmt.Println("Finish create table")
defer func() {
err = dropTable(conn)
if err != nil {
fmt.Println("Can't drop table: ", err)
}
fmt.Println("Finish drop table")
}()
err = insertData(conn)
if err != nil {
fmt.Println("Can't insert data: ", err)
return
}
fmt.Println("Finish insert one row: ")
fmt.Println("Query BFile: ")
err = queryBFile(conn)
if err != nil {
fmt.Println("Can't query: ", err)
return
}
fmt.Println("BFile as output parameter: ")
err = BFileOutputPar(conn)
if err != nil {
fmt.Println("Can't output: ", err)
return
}
}