forked from nakagami/firebirdsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows.go
153 lines (128 loc) · 4.25 KB
/
rows.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
/*******************************************************************************
The MIT License (MIT)
Copyright (c) 2013-2019 Hajime Nakagami
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/
package firebirdsql
import (
"bytes"
"container/list"
"context"
"database/sql/driver"
"io"
"reflect"
"strings"
)
type firebirdsqlRows struct {
ctx context.Context
stmt *firebirdsqlStmt
currentChunkRow *list.Element
moreData bool
result []driver.Value
}
func newFirebirdsqlRows(ctx context.Context, stmt *firebirdsqlStmt, result []driver.Value) *firebirdsqlRows {
rows := new(firebirdsqlRows)
rows.ctx = ctx
rows.stmt = stmt
rows.result = result
if stmt.stmtType == isc_info_sql_stmt_select {
rows.moreData = true
}
return rows
}
func (rows *firebirdsqlRows) Columns() []string {
columns := make([]string, len(rows.stmt.xsqlda))
for i, x := range rows.stmt.xsqlda {
columns[i] = x.aliasname
if rows.stmt.tx.fc.columnNameToLower {
columns[i] = strings.ToLower(columns[i])
}
}
return columns
}
func (rows *firebirdsqlRows) Close() (er error) {
rows.stmt.Close()
return
}
func (rows *firebirdsqlRows) Next(dest []driver.Value) (err error) {
if rows.ctx.Err() != nil {
rows.stmt.wp.opCancel(fb_cancel_raise)
return rows.ctx.Err()
}
if rows.stmt.stmtType == isc_info_sql_stmt_exec_procedure {
if rows.result != nil {
for i, v := range rows.result {
dest[i] = v
}
rows.result = nil
} else {
err = io.EOF
}
return
}
if rows.currentChunkRow != nil {
rows.currentChunkRow = rows.currentChunkRow.Next()
}
if rows.currentChunkRow == nil && rows.moreData == true {
// Get one chunk
var chunk *list.List
err = rows.stmt.wp.opFetch(rows.stmt.stmtHandle, rows.stmt.blr)
if err != nil {
return err
}
chunk, rows.moreData, err = rows.stmt.wp.opFetchResponse(rows.stmt.stmtHandle, rows.stmt.tx.transHandle, rows.stmt.xsqlda)
if err == nil {
rows.currentChunkRow = chunk.Front()
} else {
return
}
}
if rows.currentChunkRow == nil {
err = io.EOF
return
}
row, _ := rows.currentChunkRow.Value.([]driver.Value)
for i, v := range row {
if rows.stmt.xsqlda[i].sqltype == SQL_TYPE_BLOB && v != nil {
blobId := v.([]byte)
var blob []byte
blob, err = rows.stmt.wp.getBlobSegments(blobId, rows.stmt.tx.transHandle)
if rows.stmt.xsqlda[i].sqlsubtype == 1 {
dest[i] = bytes.NewBuffer(blob).String()
} else {
dest[i] = blob
}
} else {
dest[i] = v
}
}
return
}
func (rows *firebirdsqlRows) ColumnTypeDatabaseTypeName(index int) string {
return rows.stmt.xsqlda[index].typename()
}
func (rows *firebirdsqlRows) ColumnTypeLength(index int) (length int64, ok bool) {
return int64(rows.stmt.xsqlda[index].displayLength()), true
}
func (rows *firebirdsqlRows) ColumnTypeNullable(index int) (nullable bool, ok bool) {
return rows.stmt.xsqlda[index].null_ok, true
}
func (rows *firebirdsqlRows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) {
return int64(rows.stmt.xsqlda[index].displayLength()), int64(rows.stmt.xsqlda[index].sqlscale), rows.stmt.xsqlda[index].hasPrecisionScale()
}
func (rows *firebirdsqlRows) ColumnTypeScanType(index int) reflect.Type {
return rows.stmt.xsqlda[index].scantype()
}