-
Notifications
You must be signed in to change notification settings - Fork 0
/
k.go
267 lines (247 loc) · 6.3 KB
/
k.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
/*
* Copyright (c) 2000-2018, 达梦数据库有限公司.
* All rights reserved.
*/
package dm
import (
"io"
)
type DmBlob struct {
lob
data []byte
offset int64
}
func newDmBlob() *DmBlob {
return &DmBlob{
lob: lob{
inRow: true,
groupId: -1,
fileId: -1,
pageNo: -1,
readOver: false,
local: true,
updateable: true,
length: -1,
compatibleOracle: false,
fetchAll: false,
freed: false,
modify: false,
},
offset: 1,
}
}
func newBlobFromDB(value []byte, conn *Connection, column *column, fetchAll bool) *DmBlob {
var blob = newDmBlob()
blob.connection = conn
blob.lobFlag = LOB_FLAG_BYTE
blob.compatibleOracle = conn.CompatibleOracle()
blob.local = false
blob.updateable = !column.readonly
blob.tabId = column.lobTabId
blob.colId = column.lobColId
blob.inRow = Dm_build_1219.Dm_build_1312(value, NBLOB_HEAD_IN_ROW_FLAG) == LOB_IN_ROW
blob.blobId = Dm_build_1219.Dm_build_1326(value, NBLOB_HEAD_BLOBID)
if !blob.inRow {
blob.groupId = Dm_build_1219.Dm_build_1316(value, NBLOB_HEAD_OUTROW_GROUPID)
blob.fileId = Dm_build_1219.Dm_build_1316(value, NBLOB_HEAD_OUTROW_FILEID)
blob.pageNo = Dm_build_1219.Dm_build_1321(value, NBLOB_HEAD_OUTROW_PAGENO)
}
if conn.NewLobFlag {
blob.tabId = Dm_build_1219.Dm_build_1321(value, NBLOB_EX_HEAD_TABLE_ID)
blob.colId = Dm_build_1219.Dm_build_1316(value, NBLOB_EX_HEAD_COL_ID)
blob.rowId = Dm_build_1219.Dm_build_1326(value, NBLOB_EX_HEAD_ROW_ID)
blob.exGroupId = Dm_build_1219.Dm_build_1316(value, NBLOB_EX_HEAD_FPA_GRPID)
blob.exFileId = Dm_build_1219.Dm_build_1316(value, NBLOB_EX_HEAD_FPA_FILEID)
blob.exPageNo = Dm_build_1219.Dm_build_1321(value, NBLOB_EX_HEAD_FPA_PAGENO)
}
blob.resetCurrentInfo()
blob.length = blob.getLengthFromHead(value)
if blob.inRow {
blob.data = make([]byte, blob.length)
if conn.NewLobFlag {
Dm_build_1219.Dm_build_1275(blob.data, 0, value, NBLOB_EX_HEAD_SIZE, len(blob.data))
} else {
Dm_build_1219.Dm_build_1275(blob.data, 0, value, NBLOB_INROW_HEAD_SIZE, len(blob.data))
}
} else if fetchAll {
blob.loadAllData()
}
return blob
}
func newBlobOfLocal(value []byte, conn *Connection) *DmBlob {
var blob = newDmBlob()
blob.connection = conn
blob.lobFlag = LOB_FLAG_BYTE
blob.data = value
blob.length = int64(len(blob.data))
return blob
}
func NewBlob(value []byte) *DmBlob {
var blob = newDmBlob()
blob.lobFlag = LOB_FLAG_BYTE
blob.data = value
blob.length = int64(len(blob.data))
return blob
}
func (blob *DmBlob) Read(dest []byte) (n int, err error) {
result, err := blob.getBytes(blob.offset, int32(len(dest)))
if err != nil {
return 0, err
}
blob.offset += int64(len(result))
copy(dest, result)
if len(result) == 0 {
return 0, io.EOF
}
return len(result), nil
}
func (blob *DmBlob) ReadAt(pos int, dest []byte) (n int, err error) {
result, err := blob.getBytes(int64(pos), int32(len(dest)))
if err != nil {
return 0, err
}
if len(result) == 0 {
return 0, io.EOF
}
copy(dest[0:len(result)], result)
return len(result), nil
}
func (blob *DmBlob) Write(pos int, src []byte) (n int, err error) {
if err = blob.checkFreed(); err != nil {
return
}
if pos < 1 {
err = ECGO_INVALID_LENGTH_OR_OFFSET.throw()
return
}
if !blob.updateable {
err = ECGO_RESULTSET_IS_READ_ONLY.throw()
return
}
pos -= 1
if blob.local || blob.fetchAll {
if int64(pos) > blob.length {
err = ECGO_INVALID_LENGTH_OR_OFFSET.throw()
return
}
blob.setLocalData(pos, src)
n = len(src)
} else {
if err = blob.connection.checkClosed(); err != nil {
return -1, err
}
var writeLen, err = blob.connection.Access.dm_build_542(blob, pos, src)
if err != nil {
return -1, err
}
if blob.groupId == -1 {
blob.setLocalData(pos, src)
} else {
blob.inRow = false
blob.length = -1
}
n = writeLen
}
blob.modify = true
return
}
func (blob *DmBlob) Truncate(length int64) error {
var err error
if err = blob.checkFreed(); err != nil {
return err
}
if length < 0 {
return ECGO_INVALID_LENGTH_OR_OFFSET.throw()
}
if !blob.updateable {
return ECGO_RESULTSET_IS_READ_ONLY.throw()
}
if blob.local || blob.fetchAll {
if length >= int64(len(blob.data)) {
return nil
}
tmp := make([]byte, length)
Dm_build_1219.Dm_build_1275(tmp, 0, blob.data, 0, len(tmp))
blob.data = tmp
blob.length = int64(len(tmp))
} else {
if err = blob.connection.checkClosed(); err != nil {
return err
}
blob.length, err = blob.connection.Access.dm_build_556(&blob.lob, int(length))
if err != nil {
return err
}
if blob.groupId == -1 {
tmp := make([]byte, blob.length)
Dm_build_1219.Dm_build_1275(tmp, 0, blob.data, 0, int(blob.length))
blob.data = tmp
}
}
blob.modify = true
return nil
}
func (dest *DmBlob) Scan(src interface{}) error {
if dest == nil {
return ECGO_STORE_IN_NIL_POINTER.throw()
}
switch src := src.(type) {
case nil:
*dest = *new(DmBlob)
return nil
case []byte:
*dest = *NewBlob(src)
return nil
case *DmBlob:
*dest = *src
return nil
default:
return UNSUPPORTED_SCAN
}
}
func (blob *DmBlob) getBytes(pos int64, length int32) ([]byte, error) {
var err error
var leaveLength int64
if err = blob.checkFreed(); err != nil {
return nil, err
}
if pos < 1 || length < 0 {
return nil, ECGO_INVALID_LENGTH_OR_OFFSET.throw()
}
pos = pos - 1
if leaveLength, err = blob.GetLength(); err != nil {
return nil, err
}
leaveLength -= pos
if leaveLength < 0 {
return nil, ECGO_INVALID_LENGTH_OR_OFFSET.throw()
}
if int64(length) > leaveLength {
length = int32(leaveLength)
}
if blob.local || blob.inRow || blob.fetchAll {
return blob.data[pos : pos+int64(length)], nil
} else {
return blob.connection.Access.dm_build_505(blob, int32(pos), length)
}
}
func (blob *DmBlob) loadAllData() {
blob.checkFreed()
if blob.local || blob.inRow || blob.fetchAll {
return
}
len, _ := blob.GetLength()
blob.data, _ = blob.getBytes(1, int32(len))
blob.fetchAll = true
}
func (blob *DmBlob) setLocalData(pos int, p []byte) {
if pos+len(p) >= int(blob.length) {
var tmp = make([]byte, pos+len(p))
Dm_build_1219.Dm_build_1275(tmp, 0, blob.data, 0, pos)
Dm_build_1219.Dm_build_1275(tmp, pos, p, 0, len(p))
blob.data = tmp
} else {
Dm_build_1219.Dm_build_1275(blob.data, pos, p, 0, len(p))
}
blob.length = int64(len(blob.data))
}