Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit 24bb9b6

Browse files
authored
Merge pull request #511 from kuba--/information_schema.columns
Add support for information_schema.columns
2 parents 7cdd432 + 3edc00d commit 24bb9b6

File tree

2 files changed

+109
-6
lines changed

2 files changed

+109
-6
lines changed

engine_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,29 @@ var queries = []struct {
531531
{"tabletest"},
532532
},
533533
},
534+
{
535+
`
536+
SELECT COLUMN_NAME, DATA_TYPE FROM information_schema.COLUMNS
537+
WHERE TABLE_SCHEMA='mydb' AND TABLE_NAME='mytable'
538+
`,
539+
[]sql.Row{
540+
{"s", "TEXT"},
541+
{"i", "INT64"},
542+
},
543+
},
544+
{
545+
`
546+
SELECT COLUMN_NAME FROM information_schema.COLUMNS
547+
WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME LIKE '%table'
548+
GROUP BY COLUMN_NAME
549+
`,
550+
[]sql.Row{
551+
{"s"},
552+
{"i"},
553+
{"s2"},
554+
{"i2"},
555+
},
556+
},
534557
{
535558
`SHOW CREATE DATABASE mydb`,
536559
[]sql.Row{{

sql/information_schema.go

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const (
1515
ColumnStatisticsTableName = "column_statistics"
1616
// TablesTableName is the name of tables table.
1717
TablesTableName = "tables"
18+
// ColumnsTableName is the name of columns table.
19+
ColumnsTableName = "columns"
1820
)
1921

2022
type (
@@ -118,9 +120,33 @@ var (
118120
&Column{Name: "table_comment", Type: Text, Default: "", Nullable: false, Source: TablesTableName},
119121
}
120122

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 {
122148
var rows []Row
123-
for _, db := range c.AllDatabases() {
149+
for _, db := range cat.AllDatabases() {
124150
tableType := "BASE TABLE"
125151
engine := "INNODB"
126152
rowFormat := "Dynamic"
@@ -158,29 +184,83 @@ var (
158184

159185
return RowsToRowIter(rows...)
160186
}
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+
}
161235
)
162236

163237
// NewInformationSchemaDatabase creates a new INFORMATION_SCHEMA Database.
164-
func NewInformationSchemaDatabase(c *Catalog) Database {
238+
func NewInformationSchemaDatabase(cat *Catalog) Database {
165239
return &informationSchemaDatabase{
166240
name: InformationSchemaDatabaseName,
167241
tables: map[string]Table{
168242
FilesTableName: &informationSchemaTable{
169243
name: FilesTableName,
170244
schema: filesSchema,
171-
catalog: c,
245+
catalog: cat,
172246
},
173247
ColumnStatisticsTableName: &informationSchemaTable{
174248
name: ColumnStatisticsTableName,
175249
schema: columnStatisticsSchema,
176-
catalog: c,
250+
catalog: cat,
177251
},
178252
TablesTableName: &informationSchemaTable{
179253
name: TablesTableName,
180254
schema: tablesSchema,
181-
catalog: c,
255+
catalog: cat,
182256
rowIter: tablesRowIter,
183257
},
258+
ColumnsTableName: &informationSchemaTable{
259+
name: ColumnsTableName,
260+
schema: columnsSchema,
261+
catalog: cat,
262+
rowIter: columnsRowIter,
263+
},
184264
},
185265
}
186266
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy