-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.xo.go
195 lines (158 loc) · 3.77 KB
/
table.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
// Package models contains the types for schema 'public'.
package models
// Code generated by xo. DO NOT EDIT.
// Table represents table info.
type Table struct {
Type string // type
TableName string // table_name
ManualPk bool // manual_pk
}
// PgTables runs a custom query, returning results as Table.
func PgTables(db XODB, schema string, relkind string) ([]*Table, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`c.relkind, ` + // ::varchar AS type
`c.relname, ` + // ::varchar AS table_name
`false ` + // ::boolean AS manual_pk
`FROM pg_class c ` +
`JOIN ONLY pg_namespace n ON n.oid = c.relnamespace ` +
`WHERE n.nspname = $1 AND c.relkind = $2`
// run query
XOLog(sqlstr, schema, relkind)
q, err := db.Query(sqlstr, schema, relkind)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*Table{}
for q.Next() {
t := Table{}
// scan
err = q.Scan(&t.Type, &t.TableName, &t.ManualPk)
if err != nil {
return nil, err
}
res = append(res, &t)
}
return res, nil
}
// MyTables runs a custom query, returning results as Table.
func MyTables(db XODB, schema string, relkind string) ([]*Table, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`table_name ` +
`FROM information_schema.tables ` +
`WHERE table_schema = ? AND table_type = ?`
// run query
XOLog(sqlstr, schema, relkind)
q, err := db.Query(sqlstr, schema, relkind)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*Table{}
for q.Next() {
t := Table{}
// scan
err = q.Scan(&t.TableName)
if err != nil {
return nil, err
}
res = append(res, &t)
}
return res, nil
}
// SqTables runs a custom query, returning results as Table.
func SqTables(db XODB, relkind string) ([]*Table, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`tbl_name AS table_name ` +
`FROM sqlite_master ` +
`WHERE tbl_name NOT LIKE 'sqlite_%' AND type = ?`
// run query
XOLog(sqlstr, relkind)
q, err := db.Query(sqlstr, relkind)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*Table{}
for q.Next() {
t := Table{}
// scan
err = q.Scan(&t.TableName)
if err != nil {
return nil, err
}
res = append(res, &t)
}
return res, nil
}
// MsTables runs a custom query, returning results as Table.
func MsTables(db XODB, schema string, relkind string) ([]*Table, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`xtype AS type, ` +
`name AS table_name ` +
`FROM sysobjects ` +
`WHERE SCHEMA_NAME(uid) = $1 AND xtype = $2`
// run query
XOLog(sqlstr, schema, relkind)
q, err := db.Query(sqlstr, schema, relkind)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*Table{}
for q.Next() {
t := Table{}
// scan
err = q.Scan(&t.Type, &t.TableName)
if err != nil {
return nil, err
}
res = append(res, &t)
}
return res, nil
}
// OrTables runs a custom query, returning results as Table.
func OrTables(db XODB, schema string, relkind string) ([]*Table, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`LOWER(object_name) AS table_name ` +
`FROM all_objects ` +
`WHERE owner = UPPER(:1) AND object_type = UPPER(:2) ` +
`AND object_name NOT LIKE '%$%' ` +
`AND object_name NOT LIKE 'LOGMNR%_%' ` +
`AND object_name NOT LIKE 'REDO_%' ` +
`AND object_name NOT LIKE 'SCHEDULER_%_TBL' ` +
`AND object_name NOT LIKE 'SQLPLUS_%'`
// run query
XOLog(sqlstr, schema, relkind)
q, err := db.Query(sqlstr, schema, relkind)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*Table{}
for q.Next() {
t := Table{}
// scan
err = q.Scan(&t.TableName)
if err != nil {
return nil, err
}
res = append(res, &t)
}
return res, nil
}