-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.xo.go
199 lines (162 loc) · 4.25 KB
/
index.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.
// Index represents an index.
type Index struct {
IndexName string // index_name
IsUnique bool // is_unique
IsPrimary bool // is_primary
SeqNo int // seq_no
Origin string // origin
IsPartial bool // is_partial
}
// PgTableIndexes runs a custom query, returning results as Index.
func PgTableIndexes(db XODB, schema string, table string) ([]*Index, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`DISTINCT ic.relname, ` + // ::varchar AS index_name
`i.indisunique, ` + // ::boolean AS is_unique
`i.indisprimary, ` + // ::boolean AS is_primary
`0, ` + // ::integer AS seq_no
`'', ` + // ::varchar AS origin
`false ` + // ::boolean AS is_partial
`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 ` +
`WHERE 0 != any(i.indkey) AND n.nspname = $1 AND c.relname = $2`
// run query
XOLog(sqlstr, schema, table)
q, err := db.Query(sqlstr, schema, table)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*Index{}
for q.Next() {
i := Index{}
// scan
err = q.Scan(&i.IndexName, &i.IsUnique, &i.IsPrimary, &i.SeqNo, &i.Origin, &i.IsPartial)
if err != nil {
return nil, err
}
res = append(res, &i)
}
return res, nil
}
// MyTableIndexes runs a custom query, returning results as Index.
func MyTableIndexes(db XODB, schema string, table string) ([]*Index, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`DISTINCT index_name, ` +
`NOT non_unique AS is_unique ` +
`FROM information_schema.statistics ` +
`WHERE index_name <> 'PRIMARY' AND index_schema = ? AND table_name = ?`
// run query
XOLog(sqlstr, schema, table)
q, err := db.Query(sqlstr, schema, table)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*Index{}
for q.Next() {
i := Index{}
// scan
err = q.Scan(&i.IndexName, &i.IsUnique)
if err != nil {
return nil, err
}
res = append(res, &i)
}
return res, nil
}
// SqTableIndexes runs a custom query, returning results as Index.
func SqTableIndexes(db XODB, table string) ([]*Index, error) {
var err error
// sql query
var sqlstr = `PRAGMA index_list(` + table + `)`
// run query
XOLog(sqlstr)
q, err := db.Query(sqlstr, table)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*Index{}
for q.Next() {
i := Index{}
// scan
err = q.Scan(&i.SeqNo, &i.IndexName, &i.IsUnique, &i.Origin, &i.IsPartial)
if err != nil {
return nil, err
}
res = append(res, &i)
}
return res, nil
}
// MsTableIndexes runs a custom query, returning results as Index.
func MsTableIndexes(db XODB, schema string, table string) ([]*Index, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`i.name AS index_name, ` +
`i.is_primary_key AS is_primary, ` +
`i.is_unique ` +
`FROM sys.indexes i ` +
`INNER JOIN sysobjects o ON i.object_id = o.id ` +
`WHERE i.name IS NOT NULL AND o.type = 'U' AND SCHEMA_NAME(o.uid) = $1 AND o.name = $2`
// run query
XOLog(sqlstr, schema, table)
q, err := db.Query(sqlstr, schema, table)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*Index{}
for q.Next() {
i := Index{}
// scan
err = q.Scan(&i.IndexName, &i.IsPrimary, &i.IsUnique)
if err != nil {
return nil, err
}
res = append(res, &i)
}
return res, nil
}
// OrTableIndexes runs a custom query, returning results as Index.
func OrTableIndexes(db XODB, schema string, table string) ([]*Index, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`LOWER(index_name) AS index_name, ` +
`CASE WHEN uniqueness = 'UNIQUE' THEN '1' ELSE '0' END AS is_unique ` +
`FROM all_indexes ` +
`WHERE owner = UPPER(:1) AND table_name = UPPER(:2)`
// run query
XOLog(sqlstr, schema, table)
q, err := db.Query(sqlstr, schema, table)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*Index{}
for q.Next() {
i := Index{}
// scan
err = q.Scan(&i.IndexName, &i.IsUnique)
if err != nil {
return nil, err
}
res = append(res, &i)
}
return res, nil
}