@@ -15,6 +15,8 @@ const (
15
15
ColumnStatisticsTableName = "column_statistics"
16
16
// TablesTableName is the name of tables table.
17
17
TablesTableName = "tables"
18
+ // ColumnsTableName is the name of columns table.
19
+ ColumnsTableName = "columns"
18
20
)
19
21
20
22
type (
@@ -118,9 +120,33 @@ var (
118
120
& Column {Name : "table_comment" , Type : Text , Default : "" , Nullable : false , Source : TablesTableName },
119
121
}
120
122
121
- tablesRowIter = func (c * Catalog ) RowIter {
123
+ columnsSchema = Schema {
124
+ & Column {Name : "table_catalog" , Type : Text , Default : "" , Nullable : false , Source : ColumnsTableName },
125
+ & Column {Name : "table_schema" , Type : Text , Default : "" , Nullable : false , Source : ColumnsTableName },
126
+ & Column {Name : "table_name" , Type : Text , Default : "" , Nullable : false , Source : ColumnsTableName },
127
+ & Column {Name : "column_name" , Type : Text , Default : "" , Nullable : false , Source : ColumnsTableName },
128
+ & Column {Name : "ordinal_position" , Type : Uint64 , Default : 0 , Nullable : false , Source : ColumnsTableName },
129
+ & Column {Name : "column_default" , Type : Text , Default : nil , Nullable : true , Source : ColumnsTableName },
130
+ & Column {Name : "is_nullable" , Type : Text , Default : "" , Nullable : false , Source : ColumnsTableName },
131
+ & Column {Name : "data_type" , Type : Text , Default : "" , Nullable : false , Source : ColumnsTableName },
132
+ & Column {Name : "character_maximum_length" , Type : Uint64 , Default : nil , Nullable : true , Source : ColumnsTableName },
133
+ & Column {Name : "character_octet_length" , Type : Uint64 , Default : nil , Nullable : true , Source : ColumnsTableName },
134
+ & Column {Name : "numeric_precision" , Type : Uint64 , Default : nil , Nullable : true , Source : ColumnsTableName },
135
+ & Column {Name : "numeric_scale" , Type : Uint64 , Default : nil , Nullable : true , Source : ColumnsTableName },
136
+ & Column {Name : "datetime_precision" , Type : Uint64 , Default : nil , Nullable : true , Source : ColumnsTableName },
137
+ & Column {Name : "character_set_name" , Type : Text , Default : nil , Nullable : true , Source : ColumnsTableName },
138
+ & Column {Name : "collation_name" , Type : Text , Default : nil , Nullable : true , Source : ColumnsTableName },
139
+ & Column {Name : "column_type" , Type : Text , Default : "" , Nullable : false , Source : ColumnsTableName },
140
+ & Column {Name : "column_key" , Type : Text , Default : "" , Nullable : false , Source : ColumnsTableName },
141
+ & Column {Name : "extra" , Type : Text , Default : "" , Nullable : false , Source : ColumnsTableName },
142
+ & Column {Name : "privileges" , Type : Text , Default : "" , Nullable : false , Source : ColumnsTableName },
143
+ & Column {Name : "column_comment" , Type : Text , Default : "" , Nullable : false , Source : ColumnsTableName },
144
+ & Column {Name : "generation_expression" , Type : Text , Default : "" , Nullable : false , Source : ColumnsTableName },
145
+ }
146
+
147
+ tablesRowIter = func (cat * Catalog ) RowIter {
122
148
var rows []Row
123
- for _ , db := range c .AllDatabases () {
149
+ for _ , db := range cat .AllDatabases () {
124
150
tableType := "BASE TABLE"
125
151
engine := "INNODB"
126
152
rowFormat := "Dynamic"
@@ -158,29 +184,83 @@ var (
158
184
159
185
return RowsToRowIter (rows ... )
160
186
}
187
+
188
+ columnsRowIter = func (cat * Catalog ) RowIter {
189
+ var rows []Row
190
+ for _ , db := range cat .AllDatabases () {
191
+ for _ , t := range db .Tables () {
192
+ for i , c := range t .Schema () {
193
+ var (
194
+ nullable string
195
+ charName interface {}
196
+ collName interface {}
197
+ )
198
+ if c .Nullable {
199
+ nullable = "YES"
200
+ } else {
201
+ nullable = "NO"
202
+ }
203
+ if IsText (c .Type ) {
204
+ charName = "utf-8"
205
+ collName = "utf8_bin"
206
+ }
207
+ rows = append (rows , Row {
208
+ "def" , // table_catalog
209
+ db .Name (), // table_schema
210
+ t .Name (), // table_name
211
+ c .Name , // column_name
212
+ uint64 (i ), // ordinal_position
213
+ c .Default , // column_default
214
+ nullable , // is_nullable
215
+ c .Type .String (), // data_type
216
+ nil , // character_maximum_length
217
+ nil , // character_octet_length
218
+ nil , // numeric_precision
219
+ nil , // numeric_scale
220
+ nil , // datetime_precision
221
+ charName , // character_set_name
222
+ collName , // collation_name
223
+ c .Type .String (), // column_type
224
+ "" , // column_key
225
+ "" , // extra
226
+ "select" , // privileges
227
+ "" , // column_comment
228
+ "" , // generation_expression
229
+ })
230
+ }
231
+ }
232
+ }
233
+ return RowsToRowIter (rows ... )
234
+ }
161
235
)
162
236
163
237
// NewInformationSchemaDatabase creates a new INFORMATION_SCHEMA Database.
164
- func NewInformationSchemaDatabase (c * Catalog ) Database {
238
+ func NewInformationSchemaDatabase (cat * Catalog ) Database {
165
239
return & informationSchemaDatabase {
166
240
name : InformationSchemaDatabaseName ,
167
241
tables : map [string ]Table {
168
242
FilesTableName : & informationSchemaTable {
169
243
name : FilesTableName ,
170
244
schema : filesSchema ,
171
- catalog : c ,
245
+ catalog : cat ,
172
246
},
173
247
ColumnStatisticsTableName : & informationSchemaTable {
174
248
name : ColumnStatisticsTableName ,
175
249
schema : columnStatisticsSchema ,
176
- catalog : c ,
250
+ catalog : cat ,
177
251
},
178
252
TablesTableName : & informationSchemaTable {
179
253
name : TablesTableName ,
180
254
schema : tablesSchema ,
181
- catalog : c ,
255
+ catalog : cat ,
182
256
rowIter : tablesRowIter ,
183
257
},
258
+ ColumnsTableName : & informationSchemaTable {
259
+ name : ColumnsTableName ,
260
+ schema : columnsSchema ,
261
+ catalog : cat ,
262
+ rowIter : columnsRowIter ,
263
+ },
184
264
},
185
265
}
186
266
}
0 commit comments