forked from turnkey-commerce/gendal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexcolumn.xo.go
199 lines (162 loc) · 4.48 KB
/
indexcolumn.xo.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Package models contains the types for schema 'public'.
package models
// Code generated by xo. DO NOT EDIT.
// IndexColumn represents index column info.
type IndexColumn struct {
SeqNo int // seq_no
Cid int // cid
ColumnName string // column_name
}
// PgIndexColumns runs a custom query, returning results as IndexColumn.
func PgIndexColumns(db XODB, schema string, index string) ([]*IndexColumn, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`(row_number() over()), ` + // ::integer AS seq_no
`a.attnum, ` + // ::integer AS cid
`a.attname ` + // ::varchar AS column_name
`FROM pg_index i ` +
`JOIN ONLY pg_class c ON c.oid = i.indrelid ` +
`JOIN ONLY pg_namespace n ON n.oid = c.relnamespace ` +
`JOIN ONLY pg_class ic ON ic.oid = i.indexrelid ` +
`LEFT JOIN pg_attribute a ON i.indrelid = a.attrelid AND a.attnum = ANY(i.indkey) AND a.attisdropped = false ` +
`WHERE 0 != any(i.indkey) AND n.nspname = $1 AND ic.relname = $2`
// run query
XOLog(sqlstr, schema, index)
q, err := db.Query(sqlstr, schema, index)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*IndexColumn{}
for q.Next() {
ic := IndexColumn{}
// scan
err = q.Scan(&ic.SeqNo, &ic.Cid, &ic.ColumnName)
if err != nil {
return nil, err
}
res = append(res, &ic)
}
return res, nil
}
// MyIndexColumns runs a custom query, returning results as IndexColumn.
func MyIndexColumns(db XODB, schema string, table string, index string) ([]*IndexColumn, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`seq_in_index AS seq_no, ` +
`column_name ` +
`FROM information_schema.statistics ` +
`WHERE index_schema = ? AND table_name = ? AND index_name = ? ` +
`ORDER BY seq_in_index`
// run query
XOLog(sqlstr, schema, table, index)
q, err := db.Query(sqlstr, schema, table, index)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*IndexColumn{}
for q.Next() {
ic := IndexColumn{}
// scan
err = q.Scan(&ic.SeqNo, &ic.ColumnName)
if err != nil {
return nil, err
}
res = append(res, &ic)
}
return res, nil
}
// SqIndexColumns runs a custom query, returning results as IndexColumn.
func SqIndexColumns(db XODB, index string) ([]*IndexColumn, error) {
var err error
// sql query
var sqlstr = `PRAGMA index_info(` + index + `)`
// run query
XOLog(sqlstr)
q, err := db.Query(sqlstr, index)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*IndexColumn{}
for q.Next() {
ic := IndexColumn{}
// scan
err = q.Scan(&ic.SeqNo, &ic.Cid, &ic.ColumnName)
if err != nil {
return nil, err
}
res = append(res, &ic)
}
return res, nil
}
// MsIndexColumns runs a custom query, returning results as IndexColumn.
func MsIndexColumns(db XODB, schema string, table string, index string) ([]*IndexColumn, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`k.keyno AS seq_no, ` +
`k.colid AS cid, ` +
`c.name AS column_name ` +
`FROM sysindexes i ` +
`INNER JOIN sysobjects o ON i.id = o.id ` +
`INNER JOIN sysindexkeys k ON k.id = o.id AND k.indid = i.indid ` +
`INNER JOIN syscolumns c ON c.id = o.id AND c.colid = k.colid ` +
`WHERE o.type = 'U' AND SCHEMA_NAME(o.uid) = $1 AND o.name = $2 AND i.name = $3 ` +
`ORDER BY k.keyno`
// run query
XOLog(sqlstr, schema, table, index)
q, err := db.Query(sqlstr, schema, table, index)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*IndexColumn{}
for q.Next() {
ic := IndexColumn{}
// scan
err = q.Scan(&ic.SeqNo, &ic.Cid, &ic.ColumnName)
if err != nil {
return nil, err
}
res = append(res, &ic)
}
return res, nil
}
// OrIndexColumns runs a custom query, returning results as IndexColumn.
func OrIndexColumns(db XODB, schema string, table string, index string) ([]*IndexColumn, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`column_position AS seq_no, ` +
`LOWER(column_name) AS column_name ` +
`FROM all_ind_columns ` +
`WHERE index_owner = UPPER(:1) AND table_name = UPPER(:2) AND index_name = UPPER(:3) ` +
`ORDER BY column_position`
// run query
XOLog(sqlstr, schema, table, index)
q, err := db.Query(sqlstr, schema, table, index)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*IndexColumn{}
for q.Next() {
ic := IndexColumn{}
// scan
err = q.Scan(&ic.SeqNo, &ic.ColumnName)
if err != nil {
return nil, err
}
res = append(res, &ic)
}
return res, nil
}