forked from sijms/go-ora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_set.go
141 lines (129 loc) · 3.37 KB
/
data_set.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
package go_ora
import (
"database/sql/driver"
"io"
"github.com/sijms/go-ora/trace"
"github.com/sijms/go-ora/network"
)
type Row []driver.Value
type DataSet struct {
ColumnCount int
RowCount int
UACBufferLength int
MaxRowSize int
Cols []ParameterInfo
Rows []Row
currentRow Row
index int
parent *Stmt
}
func (dataSet *DataSet) read(session *network.Session) error {
_, err := session.GetByte()
if err != nil {
return err
}
columnCount, err := session.GetInt(2, true, true)
if err != nil {
return err
}
num, err := session.GetInt(4, true, true)
if err != nil {
return err
}
columnCount += num * 0x100
if columnCount > dataSet.ColumnCount {
dataSet.ColumnCount = columnCount
}
if len(dataSet.currentRow) != dataSet.ColumnCount {
dataSet.currentRow = make(Row, dataSet.ColumnCount)
}
dataSet.RowCount, err = session.GetInt(4, true, true)
if err != nil {
return err
}
dataSet.UACBufferLength, err = session.GetInt(2, true, true)
if err != nil {
return err
}
bitVector, err := session.GetDlc()
if err != nil {
return err
}
dataSet.setBitVector(bitVector)
_, err = session.GetDlc()
return nil
}
func (dataSet *DataSet) setBitVector(bitVector []byte) {
index := dataSet.ColumnCount / 8
if dataSet.ColumnCount%8 > 0 {
index++
}
if len(bitVector) > 0 {
for x := 0; x < len(bitVector); x++ {
for i := 0; i < 8; i++ {
if (x*8)+i < dataSet.ColumnCount {
dataSet.Cols[(x*8)+i].getDataFromServer = bitVector[x]&(1<<i) > 0
}
}
}
} else {
for x := 0; x < len(dataSet.Cols); x++ {
dataSet.Cols[x].getDataFromServer = true
}
}
}
func (dataSet *DataSet) Close() error {
return nil
}
func (dataSet *DataSet) Next(dest []driver.Value) error {
//fmt.Println("has more row: ", dataSet.parent.hasMoreRows)
//fmt.Println("row length: ", len(dataSet.Rows))
//fmt.Println("cursor id: ", dataSet.parent.cursorID)
//if dataSet.parent.hasMoreRows && dataSet.index == len(dataSet.Rows) && len(dataSet.Rows) < dataSet.parent.noOfRowsToFetch {
// fmt.Println("inside first fetch")
// oldFetchCount := dataSet.parent.noOfRowsToFetch;
// dataSet.parent.noOfRowsToFetch = oldFetchCount - len(dataSet.Rows)
// err := dataSet.parent.fetch(dataSet)
// if err != nil {
// return err
// }
// dataSet.parent.noOfRowsToFetch = oldFetchCount
// fmt.Println("row count after first fetch: ", len(dataSet.Rows))
//}
if dataSet.parent.hasMoreRows && dataSet.index > 0 && dataSet.index%dataSet.parent.noOfRowsToFetch == 0 {
dataSet.Rows = make([]Row, 0, dataSet.parent.noOfRowsToFetch)
err := dataSet.parent.fetch(dataSet)
if err != nil {
return err
}
}
if dataSet.index%dataSet.parent.noOfRowsToFetch < len(dataSet.Rows) {
for x := 0; x < len(dataSet.Rows[dataSet.index%dataSet.parent.noOfRowsToFetch]); x++ {
dest[x] = driver.Value(dataSet.Rows[dataSet.index%dataSet.parent.noOfRowsToFetch][x])
}
dataSet.index++
return nil
}
return io.EOF
}
func (dataSet *DataSet) Columns() []string {
if len(dataSet.Cols) == 0 {
return nil
}
ret := make([]string, len(dataSet.Cols))
for x := 0; x < len(dataSet.Cols); x++ {
ret[x] = dataSet.Cols[x].Name
}
return ret
}
func (dataSet DataSet) Trace(t trace.Tracer) {
for r, row := range dataSet.Rows {
if r > 25 {
break
}
t.Printf("Row %d", r)
for c, col := range dataSet.Cols {
t.Printf(" %-20s: %v", col.Name, row[c])
}
}
}