forked from alexbrainman/odbc
/
rows.go
140 lines (126 loc) · 2.82 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
// 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 odbc
import (
"database/sql/driver"
"io"
"github.com/Alucardinio/odbc/api"
)
type Rows struct {
os *ODBCStmt
}
func (r *Rows) Columns() []string {
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) Next(dest []driver.Value) error {
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
}
return nil
}
func (r *Rows) Close() error {
return r.os.closeByRows()
}
func (r *Rows) HasNextResultSet() bool {
return true
}
func (r *Rows) NextResultSet() error {
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
}
return nil
}
// ColumnTypeDatabaseTypeName return the database system type name.
func (r *Rows) ColumnTypeDatabaseTypeName(index int) string {
switch x := r.os.Cols[index].(type) {
case *BindableColumn:
return cTypeString(x.CType)
case *NonBindableColumn:
return cTypeString(x.CType)
}
return ""
}
// ColumnTypeNullable return nullable if drivers known
func (r *Rows) ColumnTypeNullable(index int) (nullable, ok bool) {
switch x := r.os.Cols[index].(type) {
case *BindableColumn:
return cTypeNullable(x.Nullable)
case *NonBindableColumn:
return cTypeNullable(x.Nullable)
}
return false, false
}
func cTypeString(ct api.SQLSMALLINT) string {
switch ct {
case api.SQL_C_CHAR:
return "SQL_C_CHAR"
case api.SQL_C_LONG:
return "SQL_C_LONG"
case api.SQL_C_SHORT:
return "SQL_C_SHORT"
case api.SQL_C_FLOAT:
return "SQL_C_FLOAT"
case api.SQL_C_DOUBLE:
return "SQL_C_DOUBLE"
case api.SQL_C_NUMERIC:
return "SQL_C_NUMERIC"
case api.SQL_C_DATE:
return "SQL_C_DATE"
case api.SQL_C_TIME:
return "SQL_C_TIME"
case api.SQL_C_TYPE_TIMESTAMP:
return "SQL_C_TYPE_TIMESTAMP"
case api.SQL_C_TIMESTAMP:
return "SQL_C_TIMESTAMP"
case api.SQL_C_BINARY:
return "SQL_C_BINARY"
case api.SQL_C_BIT:
return "SQL_C_BIT"
case api.SQL_C_WCHAR:
return "SQL_C_WCHAR"
case api.SQL_C_DEFAULT:
return "SQL_C_DEFAULT"
case api.SQL_C_SBIGINT:
return "SQL_C_SBIGINT"
case api.SQL_C_UBIGINT:
return "SQL_C_UBIGINT"
case api.SQL_C_GUID:
return "SQL_C_GUID"
}
return ""
}
func cTypeNullable(nl api.SQLSMALLINT) (nullable, ok bool) {
switch nl {
case api.SQL_NO_NULLS:
return false, true
case api.SQL_NULLABLE:
return true, true
case api.SQL_NULLABLE_UNKNOWN:
return false, false
}
return false, false
}