forked from ibmdb/go_ibm_db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrows.go
181 lines (153 loc) · 5.12 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
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
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package go_ibm_db
import (
"database/sql/driver"
"fmt"
"io"
"reflect"
"unsafe"
"github.com/ibmdb/go_ibm_db/api"
trc "github.com/ibmdb/go_ibm_db/log2"
)
type Rows struct {
os *ODBCStmt
}
func (r *Rows) Columns() []string {
trc.Trace1("rows.go: Columns()")
names := make([]string, len(r.os.Cols))
for i := 0; i < len(names); i++ {
names[i] = r.os.Cols[i].Name()
}
return names
}
func (r *Rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) {
//TODO(Akhil):This functions retuns the precision and scale of column.
trc.Trace1("rows.go: ColumnTypePrecisionScale() - ENTRY")
trc.Trace1(fmt.Sprintf("index=%d", index))
ok = false;
var namelen api.SQLSMALLINT
namebuf := make([]byte, api.MAX_FIELD_SIZE)
ret := api.SQLColAttribute(r.os.h, api.SQLUSMALLINT(index+1), api.SQL_DESC_TYPE_NAME, api.SQLPOINTER(unsafe.Pointer(&namebuf[0])), (api.MAX_FIELD_SIZE), (*api.SQLSMALLINT)(&namelen), (api.SQLPOINTER)(unsafe.Pointer(nil)))
if IsError(ret) {
fmt.Println(ret)
trc.Trace1(fmt.Sprintf("Error: %s",ret))
return 0, 0, false
}
dbtype := string(namebuf[:namelen])
ret = api.SQLColAttribute(r.os.h, api.SQLUSMALLINT(index+1), api.SQL_DESC_PRECISION, api.SQLPOINTER(unsafe.Pointer(nil)), 0, (*api.SQLSMALLINT)(nil), (api.SQLPOINTER)(unsafe.Pointer(&precision)))
if IsError(ret) {
fmt.Println(ret)
trc.Trace1(fmt.Sprintf("Error: %s",ret))
return 0, 0, false
}
ret = api.SQLColAttribute(r.os.h, api.SQLUSMALLINT(index+1), api.SQL_DESC_SCALE, api.SQLPOINTER(unsafe.Pointer(nil)), 0, (*api.SQLSMALLINT)(nil), (api.SQLPOINTER)(unsafe.Pointer(&scale)))
if IsError(ret) {
fmt.Println(ret)
trc.Trace1(fmt.Sprintf("Error: %s",ret))
return 0, 0, false
}
if dbtype == "DECIMAL" {
ok = true;
} else if dbtype == "NUMERIC" {
ok = true;
} else if dbtype == "TIMESTAMP" {
ok = true;
}
trc.Trace1(fmt.Sprintf("precision=%d, scale=%d", precision, scale))
trc.Trace1("rows.go: ColumnTypePrecisionScale() - EXIT")
return precision, scale, ok
}
func (r *Rows) ColumnTypeLength(index int) (length int64, ok bool) {
//ToDo(Akhil):This functions retuns the length of column.
trc.Trace1("rows.go: ColumnTypeLength() - ENTRY")
ret := api.SQLColAttribute(r.os.h, api.SQLUSMALLINT(index+1), api.SQL_DESC_LENGTH, api.SQLPOINTER(unsafe.Pointer(nil)), 0, (*api.SQLSMALLINT)(nil), (api.SQLPOINTER)(unsafe.Pointer(&length)))
if IsError(ret) {
fmt.Println(ret)
return 0, false
}
trc.Trace1("rows.go: ColumnTypeLength() - EXIT")
return length, true
}
func (r *Rows) ColumnTypeNullable(index int) (nullable, ok bool) {
//TODO(Akhil):This functions retuns whether the column is nullable or not
trc.Trace1("rows.go: ColumnTypeNullable() - ENTRY")
var null int64
ret := api.SQLColAttribute(r.os.h, api.SQLUSMALLINT(index+1), api.SQL_DESC_NULLABLE, api.SQLPOINTER(unsafe.Pointer(nil)), 0, (*api.SQLSMALLINT)(nil), (api.SQLPOINTER)(unsafe.Pointer(&null)))
if IsError(ret) {
fmt.Println(ret)
return false, false
}
if null == api.SQL_NULLABLE {
return true, true
}
trc.Trace1("rows.go: ColumnTypeNullable() - EXIT")
return false, true
}
func (r *Rows) ColumnTypeScanType(index int) reflect.Type {
//TODO(AKHIL):This function will return the scantype that can be used to scan
//the data to the golang variable.
trc.Trace1("rows.go: ColumnTypeScanType()")
trc.Trace1(fmt.Sprintf("index=%d", index))
a := r.os.Cols[index].TypeScan()
return (a)
}
func (r *Rows) ColumnTypeDatabaseTypeName(index int) string {
//TODO(AKHIL):This functions retuns the dbtype(VARCHAR,DECIMAL etc..) of column.
//namebuf can be of uint8 or byte
trc.Trace1("rows.go: ColumnTypeDatabaseTypeName() - ENTRY")
var namelen api.SQLSMALLINT
namebuf := make([]byte, api.MAX_FIELD_SIZE)
ret := api.SQLColAttribute(r.os.h, api.SQLUSMALLINT(index+1), api.SQL_DESC_TYPE_NAME, api.SQLPOINTER(unsafe.Pointer(&namebuf[0])), (api.MAX_FIELD_SIZE), (*api.SQLSMALLINT)(&namelen), (api.SQLPOINTER)(unsafe.Pointer(nil)))
if IsError(ret) {
fmt.Println(ret)
return ""
}
dbtype := string(namebuf[:namelen])
trc.Trace1("rows.go: ColumnTypeDatabaseTypeName() - EXIT")
return dbtype
}
func (r *Rows) Next(dest []driver.Value) error {
trc.Trace1("rows.go: Next() - ENTRY")
ret := api.SQLFetch(r.os.h)
if ret == api.SQL_NO_DATA {
return io.EOF
}
if IsError(ret) {
return NewError("SQLFetch", r.os.h)
}
for i := range dest {
v, err := r.os.Cols[i].Value(r.os.h, i)
if err != nil {
return err
}
dest[i] = v
}
trc.Trace1("rows.go: Next() - EXIT")
return nil
}
func (r *Rows) HasNextResultSet() bool {
trc.Trace1("rows.go: HasVextResultSet()")
return true
}
func (r *Rows) NextResultSet() error {
trc.Trace1("rows.go: NextResultSet() - ENTRY")
ret := api.SQLMoreResults(r.os.h)
if ret == api.SQL_NO_DATA {
return io.EOF
}
if IsError(ret) {
return NewError("SQLMoreResults", r.os.h)
}
err := r.os.BindColumns()
if err != nil {
return err
}
trc.Trace1("rows.go: NextResultSet() - EXIT")
return nil
}
func (r *Rows) Close() error {
trc.Trace1("rows.go: Close()")
return r.os.closeByRows()
}