forked from sijms/go-ora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_set.go
176 lines (157 loc) · 4.48 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
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
package go_ora
import (
"database/sql/driver"
"io"
"github.com/sijms/go-ora/trace"
"github.com/sijms/go-ora/network"
)
// Compile time Sentinels for implemented Interfaces.
var _ = driver.Rows((*DataSet)(nil))
var _ = driver.RowsColumnTypeDatabaseTypeName((*DataSet)(nil))
var _ = driver.RowsColumnTypeLength((*DataSet)(nil))
var _ = driver.RowsColumnTypeNullable((*DataSet)(nil))
// var _ = driver.RowsColumnTypePrecisionScale((*DataSet)(nil))
// var _ = driver.RowsColumnTypeScanType((*DataSet)(nil))
// var _ = driver.RowsNextResultSet((*DataSet)(nil))
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.parent.hasMoreRows && (dataSet.parent.hasBLOB || dataSet.parent.hasLONG) && dataSet.index == 0 {
if err := dataSet.parent.fetch(dataSet); 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] = 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])
}
}
}
func (dataSet DataSet) ColumnTypeDatabaseTypeName(index int) string {
return dataSet.Cols[index].DataType.String()
}
func (dataSet DataSet) ColumnTypeLength(index int) (length int64, ok bool) {
switch dataSet.Cols[index].DataType {
case NCHAR, CHAR:
return int64(dataSet.Cols[index].MaxCharLen), true
case NUMBER:
return int64(dataSet.Cols[index].Precision), true
}
return int64(0), false
}
func (dataSet DataSet) ColumnTypeNullable(index int) (nullable, ok bool) {
return dataSet.Cols[index].AllowNull, true
}