forked from sijms/go-ora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udt.go
258 lines (248 loc) · 7.53 KB
/
udt.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
package go_ora
import (
"reflect"
"strings"
)
type customType struct {
owner string
name string
//arrayTypeName string
attribs []ParameterInfo
typ reflect.Type
toid []byte // type oid
//arrayTOID []byte
isArray bool
fieldMap map[string]int
}
// RegisterType register user defined type with owner equal to user id
//func (conn *Connection) RegisterType(typeName, arrayTypeName string, typeObj interface{}) error {
// return conn.RegisterTypeWithOwner(conn.connOption.UserID, typeName, arrayTypeName, typeObj)
//}
// RegisterTypeWithOwner take typename, owner and go type object and make an information
// structure that used to create a new type during query and store values in it
//
// DataType of UDT field that can be manipulated by this function are: NUMBER,
// VARCHAR2, NVARCHAR2, TIMESTAMP, DATE AND RAW
//func (conn *Connection) RegisterTypeWithOwner(owner, typeName, arrayTypeName string, typeObj interface{}) error {
// if typeObj == nil {
// return errors.New("type object cannot be nil")
// }
// typ := reflect.TypeOf(typeObj)
// switch typ.Kind() {
// case reflect.Ptr:
// return errors.New("unsupported type object: Ptr")
// case reflect.Array:
// return errors.New("unsupported type object: Array")
// case reflect.Chan:
// return errors.New("unsupported type object: Chan")
// case reflect.Map:
// return errors.New("unsupported type object: Map")
// case reflect.Slice:
// return errors.New("unsupported type object: Slice")
// }
// if typ.Kind() != reflect.Struct {
// return errors.New("type object should be of structure type")
// }
// cust := customType{
// owner: owner,
// name: typeName,
// arrayTypeName: arrayTypeName,
// typ: typ,
// fieldMap: map[string]int{},
// }
// var err error
// cust.toid, err = getTOID(conn, cust.owner, cust.name)
// if err != nil {
// return err
// }
// if len(cust.arrayTypeName) > 0 {
// cust.arrayTOID, err = getTOID(conn, cust.owner, cust.arrayTypeName)
// if err != nil {
// return err
// }
// }
// sqlText := `SELECT ATTR_NAME, ATTR_TYPE_NAME, LENGTH, ATTR_NO
//FROM ALL_TYPE_ATTRS WHERE UPPER(OWNER)=:1 AND UPPER(TYPE_NAME)=:2`
//
// stmt := NewStmt(sqlText, conn)
// defer func(stmt *Stmt) {
// _ = stmt.Close()
// }(stmt)
// rows, err := stmt.Query_([]driver.NamedValue{{Value: strings.ToUpper(owner)}, {Value: strings.ToUpper(typeName)}})
// if err != nil {
// return err
// }
// var (
// attName sql.NullString
// attOrder int64
// attTypeName sql.NullString
// length sql.NullInt64
// )
// for rows.Next_() {
// err = rows.Scan(&attName, &attTypeName, &length, &attOrder)
// if err != nil {
// return err
// }
//
// for int(attOrder) > len(cust.attribs) {
// cust.attribs = append(cust.attribs, ParameterInfo{
// Direction: Input,
// Flag: 3,
// })
// }
// param := &cust.attribs[attOrder-1]
// param.Name = attName.String
// param.TypeName = attTypeName.String
// switch strings.ToUpper(attTypeName.String) {
// case "NUMBER":
// param.DataType = NUMBER
// param.MaxLen = converters.MAX_LEN_NUMBER
// case "VARCHAR2":
// param.DataType = NCHAR
// param.CharsetForm = 1
// param.ContFlag = 16
// param.MaxCharLen = int(length.Int64)
// param.CharsetID = conn.tcpNego.ServerCharset
// param.MaxLen = int(length.Int64) * converters.MaxBytePerChar(param.CharsetID)
// case "NVARCHAR2":
// param.DataType = NCHAR
// param.CharsetForm = 2
// param.ContFlag = 16
// param.MaxCharLen = int(length.Int64)
// param.CharsetID = conn.tcpNego.ServernCharset
// param.MaxLen = int(length.Int64) * converters.MaxBytePerChar(param.CharsetID)
// case "TIMESTAMP":
// fallthrough
// case "DATE":
// param.DataType = DATE
// param.MaxLen = 11
// case "RAW":
// param.DataType = RAW
// param.MaxLen = int(length.Int64)
// case "BLOB":
// param.DataType = OCIBlobLocator
// param.MaxLen = int(length.Int64)
// case "CLOB":
// param.DataType = OCIClobLocator
// param.CharsetForm = 1
// param.ContFlag = 16
// param.CharsetID = conn.tcpNego.ServerCharset
// param.MaxCharLen = int(length.Int64)
// param.MaxLen = int(length.Int64) * converters.MaxBytePerChar(param.CharsetID)
// case "NCLOB":
// param.DataType = OCIClobLocator
// param.CharsetForm = 2
// param.ContFlag = 16
// param.CharsetID = conn.tcpNego.ServernCharset
// param.MaxCharLen = int(length.Int64)
// param.MaxLen = int(length.Int64) * converters.MaxBytePerChar(param.CharsetID)
// default:
// // search for type in registered types
// found := false
// for name, value := range conn.cusTyp {
// if name == strings.ToUpper(attTypeName.String) {
// found = true
// param.cusType = new(customType)
// *param.cusType = value
// param.ToID = value.toid
// break
// }
// if value.arrayTypeName == strings.ToUpper(attTypeName.String) {
// found = true
// param.cusType = new(customType)
// *param.cusType = value
// param.ToID = value.toid
// break
// }
// }
// if !found {
// return fmt.Errorf("unsupported attribute type: %s", attTypeName.String)
// }
// }
// }
//for {
// err = rows.Next(values)
// if err != nil {
// if errors.Is(err, io.EOF) {
// break
// }
// return err
// }
// if attName, ok = values[0].(string); !ok {
// return errors.New(fmt.Sprint("error reading attribute properties for type: ", typeName))
// }
// if attTypeName, ok = values[1].(string); !ok {
// return errors.New(fmt.Sprint("error reading attribute properties for type: ", typeName))
// }
// if values[2] == nil {
// length = 0
// } else {
// if length, ok = values[2].(int64); !ok {
// return fmt.Errorf("error reading attribute properties for type: %s", typeName)
// }
// }
// if attOrder, ok = values[3].(int64); !ok {
// return fmt.Errorf("error reading attribute properties for type: %s", typeName)
// }
//
//}
// if len(cust.attribs) == 0 {
// return fmt.Errorf("unknown or empty type: %s", typeName)
// }
// cust.loadFieldMap()
// conn.cusTyp[strings.ToUpper(typeName)] = cust
// return nil
//}
//func (cust *customType) getFieldIndex(name string) int {
// for x := 0; x < cust.typ.NumField(); x++ {
// fieldID, _, _, _ := extractTag(cust.typ.Field(x).Tag.Get("udt"))
// if strings.ToUpper(fieldID) == strings.ToUpper(name) {
// return x
// }
// }
// return -1
//}
// loadFieldMap read struct tag that supplied with golang type object passed in RegisterType
// function
func (cust *customType) loadFieldMap() {
typ := cust.typ
for x := 0; x < typ.NumField(); x++ {
f := typ.Field(x)
fieldID, _, _, _ := extractTag(f.Tag.Get("udt"))
if len(fieldID) == 0 {
continue
}
fieldID = strings.ToUpper(fieldID)
cust.fieldMap[fieldID] = x
}
}
//func (cust *customType) isRegularArray() bool {
// return cust.isArray && len(cust.attribs) > 0 && cust.attribs[0].DataType != XMLType
//}
type Object struct {
Owner string
Name string
Value interface{}
itemMaxSize int
}
// NewObject call this function to wrap oracle object types or arrayso
func NewObject(owner, name string, value interface{}) *Object {
return &Object{
Owner: owner,
Name: name,
Value: value,
itemMaxSize: 0,
}
}
// NewArrayObject call this function for creation of array of regular type
// for example sql: create or replace TYPE SLICE AS TABLE OF varchar2(500)
// in this case itemMaxSize will be 500.
// if you use this function to define array of object type itemMaxSize is not used
//func NewArrayObject(owner, name string, itemMaxSize int, value interface{}) *Object {
// return &Object{
// Owner: owner,
// Name: name,
// Value: value,
// itemMaxSize: itemMaxSize,
// }
//}