forked from sijms/go-ora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lob.go
268 lines (254 loc) · 5.27 KB
/
lob.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
package go_ora
import (
"bytes"
"errors"
"github.com/sijms/go-ora/network"
)
type Lob struct {
sourceLocator []byte
destLocator []byte
scn []byte
sourceOffset int
destOffset int
charsetID int
size int64
data bytes.Buffer
}
func (lob *Lob) variableWidthChar() bool {
if len(lob.sourceLocator) > 6 && lob.sourceLocator[6]&128 == 128 {
return true
}
return false
}
func (lob *Lob) littleEndianClob() bool {
if len(lob.sourceLocator) > 7 && lob.sourceLocator[7]&64 > 0 {
return true
}
return false
}
func (lob *Lob) getSize(conn *Connection) (size int64, err error) {
session := conn.session
tracer := conn.connOption.Tracer
tracer.Print("Read Lob Size")
err = lob.write(session, 1)
if err != nil {
return
}
err = lob.read(session)
if err != nil {
return
}
size = lob.size
tracer.Print("Lob Size: ", size)
return
}
func (lob *Lob) getData(conn *Connection) (data []byte, err error) {
session := conn.session
tracer := conn.connOption.Tracer
tracer.Print("Read Lob Data")
lob.sourceOffset = 1
err = lob.write(session, 2)
if err != nil {
return
}
err = lob.read(session)
if err != nil {
return
}
data = lob.data.Bytes()
return
}
func (lob *Lob) write(session *network.Session, operationID int) error {
session.ResetBuffer()
session.PutBytes(3, 0x60, 0)
if len(lob.sourceLocator) == 0 {
session.PutBytes(0)
} else {
session.PutBytes(1)
}
session.PutUint(len(lob.sourceLocator), 4, true, true)
if len(lob.destLocator) == 0 {
session.PutBytes(0)
} else {
session.PutBytes(1)
}
session.PutUint(len(lob.destLocator), 4, true, true)
// put offsets
if session.TTCVersion < 3 {
session.PutUint(lob.sourceOffset, 4, true, true)
session.PutUint(lob.destOffset, 4, true, true)
} else {
session.PutBytes(0, 0)
}
if lob.charsetID != 0 {
session.PutBytes(1)
} else {
session.PutBytes(0)
}
if session.TTCVersion < 3 {
session.PutBytes(1)
} else {
session.PutBytes(0)
}
// if bNullO2U (false) {
// session.PutBytes(1)
//} else {
session.PutBytes(0)
session.PutInt(operationID, 4, true, true)
if len(lob.scn) == 0 {
session.PutBytes(0)
} else {
session.PutBytes(1)
}
session.PutUint(len(lob.scn), 4, true, true)
if session.TTCVersion >= 3 {
session.PutUint(lob.sourceOffset, 8, true, true)
session.PutInt(lob.destOffset, 8, true, true)
// sendAmount
session.PutBytes(1)
}
if session.TTCVersion >= 4 {
session.PutBytes(0, 0, 0, 0, 0, 0)
}
if len(lob.sourceLocator) > 0 {
session.PutBytes(lob.sourceLocator...)
}
if len(lob.destLocator) > 0 {
session.PutBytes(lob.destLocator...)
}
if lob.charsetID != 0 {
session.PutUint(lob.charsetID, 2, true, true)
}
if session.TTCVersion < 3 {
session.PutUint(lob.size, 4, true, true)
}
for x := 0; x < len(lob.scn); x++ {
session.PutUint(lob.scn[x], 4, true, true)
}
if session.TTCVersion >= 3 {
session.PutUint(lob.size, 8, true, true)
}
return session.Write()
}
func (lob *Lob) read(session *network.Session) error {
loop := true
for loop {
msg, err := session.GetByte()
if err != nil {
return err
}
switch msg {
case 4:
session.Summary, err = network.NewSummary(session)
if err != nil {
return err
}
if session.HasError() {
if session.Summary.RetCode == 1403 {
session.Summary = nil
} else {
return errors.New(session.GetError())
}
}
loop = false
case 8:
// read rpa message
if len(lob.sourceLocator) != 0 {
_, err = session.GetBytes(len(lob.sourceLocator))
if err != nil {
return err
}
}
if len(lob.destLocator) != 0 {
_, err = session.GetBytes(len(lob.destLocator))
if err != nil {
return err
}
}
if lob.charsetID != 0 {
lob.charsetID, err = session.GetInt(2, true, true)
if err != nil {
return err
}
}
// get datasize
if session.TTCVersion < 3 {
lob.size, err = session.GetInt64(4, true, true)
if err != nil {
return err
}
} else {
lob.size, err = session.GetInt64(8, true, true)
if err != nil {
return err
}
}
case 9:
if session.HasEOSCapability {
session.Summary.EndOfCallStatus, err = session.GetInt(4, true, true)
if err != nil {
return err
}
}
loop = false
case 14:
// get the data
err = lob.readData(session)
if err != nil {
return err
}
default:
return errors.New("TTC error")
}
}
return nil
}
func (lob *Lob) readData(session *network.Session) error {
num1 := 0 // data readed in the call of this function
var chunkSize byte = 0
var err error
//num3 := offset // the data readed from the start of read operation
num4 := 0
for num4 != 4 {
switch num4 {
case 0:
chunkSize, err = session.GetByte()
if err != nil {
return err
}
if chunkSize == 0xFE {
num4 = 2
} else {
num4 = 1
}
case 1:
chunk, err := session.GetBytes(int(chunkSize))
if err != nil {
return err
}
lob.data.Write(chunk)
num1 += int(chunkSize)
num4 = 4
case 2:
chunkSize, err = session.GetByte()
if err != nil {
return err
}
if chunkSize <= 0 {
num4 = 4
} else {
num4 = 3
}
case 3:
chunk, err := session.GetBytes(int(chunkSize))
if err != nil {
return err
}
lob.data.Write(chunk)
num1 += int(chunkSize)
//num3 += chunkSize
num4 = 2
}
}
return nil
}