Repository navigation
Expand file tree
/
Copy pathcatalog.go
More file actions
207 lines (194 loc) · 6.1 KB
/
Copy pathcatalog.go
File metadata and controls
207 lines (194 loc) · 6.1 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package odbc
import (
"context"
"database/sql/driver"
"errors"
"fmt"
"io"
"runtime"
"unsafe"
)
// TableInfo is a table or a view that SQLTables lists.
type TableInfo struct {
Catalog string
Schema string
Name string
// Type is TABLE, VIEW, SYSTEM TABLE and so on, as the database driver names it.
Type string
Remarks string
}
// ColumnInfo is a column that SQLColumns lists.
type ColumnInfo struct {
Catalog string
Schema string
Table string
Name string
// DataType is the SQL type code of ODBC, such as 12 for SQL_VARCHAR.
DataType int
// TypeName is the name that the database gives the type.
TypeName string
// Size is the length of a text or the precision of a number.
Size int
// Digits is the number of digits after the decimal point.
Digits int
// Nullable is 0 for a column that holds no NULL, 1 for one that does, and 2
// when the database driver does not know.
Nullable int
Remarks string
Default string
// Ordinal is the position of the column in the table, from 1.
Ordinal int
}
// PrimaryKeyInfo is a column of the primary key that SQLPrimaryKeys lists.
type PrimaryKeyInfo struct {
Catalog string
Schema string
Table string
Column string
// Sequence is the position of the column in the key, from 1.
Sequence int
// Name is the name of the key, when the database has one.
Name string
}
// Tables lists the tables that match. An empty argument matches everything. The
// name arguments are patterns, in which % matches any text and _ matches one
// character. tableTypes is a comma separated list such as "TABLE,VIEW" (D24).
func (c *conn) Tables(ctx context.Context, catalog, schema, table, tableTypes string) ([]TableInfo, error) {
cat, sch, tab, typ := c.api.encodeOrNil(catalog), c.api.encodeOrNil(schema), c.api.encodeOrNil(table), c.api.encodeOrNil(tableTypes)
rows, err := c.catalogRows(ctx, "listing the tables", func(h uintptr) int16 {
return c.api.tables(h, ptr(cat), len0(cat), ptr(sch), len0(sch), ptr(tab), len0(tab), ptr(typ), len0(typ))
})
runtime.KeepAlive([][]byte{cat, sch, tab, typ})
if err != nil {
return nil, err
}
out := make([]TableInfo, len(rows))
for i, r := range rows {
out[i] = TableInfo{Catalog: str(r, 0), Schema: str(r, 1), Name: str(r, 2), Type: str(r, 3), Remarks: str(r, 4)}
}
return out, nil
}
// Columns lists the columns that match, in the order of the table. An empty
// argument matches everything, and the name arguments are patterns (D24).
func (c *conn) Columns(ctx context.Context, catalog, schema, table, column string) ([]ColumnInfo, error) {
cat, sch, tab, col := c.api.encodeOrNil(catalog), c.api.encodeOrNil(schema), c.api.encodeOrNil(table), c.api.encodeOrNil(column)
rows, err := c.catalogRows(ctx, "listing the columns", func(h uintptr) int16 {
return c.api.columns(h, ptr(cat), len0(cat), ptr(sch), len0(sch), ptr(tab), len0(tab), ptr(col), len0(col))
})
runtime.KeepAlive([][]byte{cat, sch, tab, col})
if err != nil {
return nil, err
}
out := make([]ColumnInfo, len(rows))
for i, r := range rows {
out[i] = ColumnInfo{
Catalog: str(r, 0), Schema: str(r, 1), Table: str(r, 2), Name: str(r, 3),
DataType: num(r, 4), TypeName: str(r, 5), Size: num(r, 6), Digits: num(r, 8),
Nullable: num(r, 10), Remarks: str(r, 11), Default: str(r, 12), Ordinal: num(r, 16),
}
}
return out, nil
}
// PrimaryKeys lists the columns of the primary key of one table. The table name
// is not a pattern, and an empty catalog or schema matches the current one
// (D24).
func (c *conn) PrimaryKeys(ctx context.Context, catalog, schema, table string) ([]PrimaryKeyInfo, error) {
if catalog == "" {
// Some database drivers list no key for a table when the catalog is not
// given, so name the current one. A table name alone means that one.
if cur, err := c.catalog(); err == nil {
catalog = cur
}
}
cat, sch, tab := c.api.encodeOrNil(catalog), c.api.encodeOrNil(schema), c.api.encodeOrNil(table)
rows, err := c.catalogRows(ctx, "listing the primary key", func(h uintptr) int16 {
return c.api.primaryKeys(h, ptr(cat), len0(cat), ptr(sch), len0(sch), ptr(tab), len0(tab))
})
runtime.KeepAlive([][]byte{cat, sch, tab})
if err != nil {
return nil, err
}
out := make([]PrimaryKeyInfo, len(rows))
for i, r := range rows {
out[i] = PrimaryKeyInfo{Catalog: str(r, 0), Schema: str(r, 1), Table: str(r, 2), Column: str(r, 3), Sequence: num(r, 4), Name: str(r, 5)}
}
return out, nil
}
// catalogRows runs a catalog function on a statement of its own and reads every
// row of the result.
func (c *conn) catalogRows(ctx context.Context, op string, call func(h uintptr) int16) ([][]driver.Value, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
s, err := c.newStmt("")
if err != nil {
return nil, err
}
defer func() { _ = s.free() }()
stop := c.watch(ctx, s.h)
ret := call(s.h)
stop()
if err := c.api.check(op, ret, handleStmt, s.h); err != nil {
return nil, wrapCtx(ctx, err)
}
r := &rows{ctx: ctx, s: s, own: true, undo: func() {}}
if err := r.describe(); err != nil {
return nil, err
}
var out [][]driver.Value
for {
err := r.NextRow()
if errors.Is(err, io.EOF) {
return out, nil
}
if err != nil {
return nil, fmt.Errorf("%s: %w", op, err)
}
out = append(out, append([]driver.Value(nil), r.row...))
}
}
// encodeOrNil encodes a string, and returns nil for an empty one, which ODBC
// reads as "not given".
func (a *api) encodeOrNil(s string) []byte {
if s == "" {
return nil
}
return a.encode(s)
}
// ptr returns the address of an encoded string, or nil.
func ptr(b []byte) unsafe.Pointer {
if len(b) == 0 {
return nil
}
return unsafe.Pointer(&b[0])
}
// len0 returns SQL_NTS for a string that is given, and 0 for one that is not.
func len0(b []byte) int16 {
if len(b) == 0 {
return 0
}
return nts
}
// str returns column i of a row as text.
func str(r []driver.Value, i int) string {
if i >= len(r) {
return ""
}
if s, ok := r[i].(string); ok {
return s
}
return ""
}
// num returns column i of a row as an integer.
func num(r []driver.Value, i int) int {
if i >= len(r) {
return 0
}
switch v := r[i].(type) {
case int64:
return int(v)
case uint64:
return int(v)
}
return 0
}