-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathmain.go
executable file
·284 lines (271 loc) · 6.45 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
package main
import (
"database/sql"
"fmt"
"os"
go_ora "github.com/sijms/go-ora/v2"
)
type Product struct {
Id int `udt:"ID"`
Name string `udt:"NAME"`
Desc string `udt:"DESCR"`
}
type Customer struct {
Id int `udt:"ID"`
Name string `udt:"NAME"`
Products []Product `udt:"PRODUCTS"`
FavProduct *Product `udt:"FAV_PRODUCT"`
}
func execCmd(db *sql.DB, stmts ...string) error {
for _, stmt := range stmts {
if _, err := db.Exec(stmt); err != nil {
if len(stmts) > 1 {
return fmt.Errorf("error: %v in execuation of stmt: %s", err, stmt)
} else {
return err
}
}
}
return nil
}
func createTypes(db *sql.DB) error {
return execCmd(db, `
create or replace type PRODUCT as object(
ID number,
NAME varchar2(100),
DESCR varchar2(100)
)`, `create or replace type ProductCol as table of product`, `
create or replace type CUSTOMER as object(
ID number,
FAV_PRODUCT PRODUCT,
NAME varchar2(100),
PRODUCTS ProductCol
)`)
}
func dropTypes(db *sql.DB) error {
return execCmd(db, "DROP TYPE CUSTOMER", "DROP TYPE ProductCol", "DROP TYPE PRODUCT")
}
func fullInputFullOutput(db *sql.DB) error {
input := Customer{
Id: 1,
Name: "customer_",
FavProduct: &Product{
1, "product_1", "description of product_1",
},
Products: []Product{
{1, "prduct_1", "description of product_1"},
{2, "prduct_2", "description of product_2"},
},
}
var output Customer
_, err := db.Exec(`
DECLARE
l_customer customer;
BEGIN
l_customer := :1;
l_customer.id := l_customer.id + 2;
l_customer.name := l_customer.name || '3';
l_customer.FAV_PRODUCT := PRODUCT(3, 'product_3', 'description of product_3');
for x in 3..5 loop
l_customer.products.extend;
l_customer.products(x) := product(x, 'product_' || x, 'description of product_' || x);
end loop;
:2 := l_customer;
END;`, go_ora.Object{Name: "Customer", Value: input},
go_ora.Object{Name: "Customer", Value: &output})
if err != nil {
return err
}
fmt.Println("full input full output: ", output)
if output.FavProduct != nil {
fmt.Println("fav product: ", output.FavProduct)
}
return nil
}
func fullInputNullOutput(db *sql.DB) error {
input := Customer{
Id: 1,
Name: "customer_",
FavProduct: &Product{
1, "product_1", "description of product_1",
},
Products: []Product{
{1, "prduct_1", "description of product_1"},
{2, "prduct_2", "description of product_2"},
},
}
var output Customer
_, err := db.Exec(`
DECLARE
l_customer customer;
BEGIN
l_customer := :1;
l_customer.id := l_customer.id + 2;
l_customer.name := l_customer.name || '3';
l_customer.FAV_PRODUCT := null;
l_customer.products := null;
:2 := l_customer;
END;`, go_ora.Object{Name: "Customer", Value: input},
go_ora.Object{Name: "Customer", Value: &output})
if err != nil {
return err
}
fmt.Println("full input null output: ", output)
if output.FavProduct != nil {
fmt.Println("fav product: ", output.FavProduct)
}
return nil
}
func nullInputFullOutput(db *sql.DB) error {
input := Customer{
Id: 1,
Name: "customer_",
}
var output Customer
_, err := db.Exec(`
DECLARE
l_customer customer;
BEGIN
l_customer := :1;
l_customer.id := l_customer.id + 2;
l_customer.name := l_customer.name || '3';
l_customer.FAV_PRODUCT := PRODUCT(1, 'product_1', 'description of product_1');
l_customer.Products := productCol();
for x in 1..5 loop
l_customer.products.extend;
l_customer.products(x) := product(x, 'product_' || x, 'description of product_' || x);
end loop;
:2 := l_customer;
END;`, go_ora.Object{Name: "Customer", Value: input},
go_ora.Object{Name: "customer", Value: &output})
if err != nil {
return err
}
fmt.Println("null input full output: ", output)
if output.FavProduct != nil {
fmt.Println("fav product: ", output.FavProduct)
}
return nil
}
func nullObject(db *sql.DB) error {
var output *Product
var output2 string
_, err := db.Exec(`
DECLARE
l_product Product := null;
BEGIN
:1 := l_product;
:2 := 'this is a test';
END;`, go_ora.Object{Name: "Product", Value: &output}, go_ora.Out{Dest: &output2, Size: 20})
fmt.Println(output)
fmt.Println(output2)
return err
}
func nullArray(db *sql.DB) error {
var output []Product
// output = append(output, Product{3, "product_3", "desc_3"})
var output2 string
_, err := db.Exec(`
DECLARE
l_products productCol := null;
BEGIN
:1 := l_products;
:2 := 'this is a test';
END;`, go_ora.Object{Name: "ProductCol", Value: &output}, go_ora.Out{Dest: &output2, Size: 30})
fmt.Println(output)
fmt.Println(output2)
return err
}
// passing nil nested type and receiving it
// passing nil nested array and receving it
func nullInputNullOutput(db *sql.DB) error {
input := Customer{
Id: 1,
Name: "customer_",
}
var output Customer
_, err := db.Exec(`
DECLARE
l_customer customer;
BEGIN
l_customer := :1;
l_customer.id := l_customer.id + 2;
l_customer.name := l_customer.name || '3';
:2 := l_customer;
END;`, go_ora.Object{Name: "Customer", Value: input},
go_ora.Object{Name: "customer", Value: &output})
if err != nil {
return err
}
fmt.Println("null input null output: ", output)
return nil
}
func main() {
db, err := sql.Open("oracle", os.Getenv("DSN"))
if err != nil {
fmt.Println("can't open database: ", err)
return
}
defer func() {
err = db.Close()
if err != nil {
fmt.Println("can't close database: ", err)
}
}()
err = createTypes(db)
if err != nil {
fmt.Println("can't create types: ", err)
return
}
defer func() {
err = dropTypes(db)
if err != nil {
fmt.Println("can't drop types: ", err)
}
}()
err = go_ora.RegisterType(db, "product", "ProductCol", Product{})
if err != nil {
fmt.Println("can't register product: ", err)
return
}
err = go_ora.RegisterType(db, "customer", "", Customer{})
if err != nil {
fmt.Println("can't register customer: ", err)
return
}
err = nullInputNullOutput(db)
if err != nil {
fmt.Println("can't send null input null output: ", err)
return
}
fmt.Println()
err = nullInputFullOutput(db)
if err != nil {
fmt.Println("can't send null input full output: ", err)
return
}
fmt.Println()
err = fullInputFullOutput(db)
if err != nil {
fmt.Println("can't send full input full output: ", err)
return
}
fmt.Println()
err = fullInputNullOutput(db)
if err != nil {
fmt.Println("can't send full input null output: ", err)
return
}
fmt.Println()
err = nullArray(db)
if err != nil {
fmt.Println("can't get null array: ", err)
return
}
fmt.Println()
err = nullObject(db)
if err != nil {
fmt.Println("can't get null object: ", err)
return
}
}