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

Commit 756e3bf

Browse files
authored
Merge pull request #704 from erizocosmico/fix/type-display-name
sql: use correct MySQL display name for types
2 parents dbd7fe9 + dee5051 commit 756e3bf

File tree

8 files changed

+84
-44
lines changed

8 files changed

+84
-44
lines changed

_integration/go/mysql_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func TestGrafana(t *testing.T) {
8282
{"name", "TEXT"},
8383
{"email", "TEXT"},
8484
{"phone_numbers", "JSON"},
85-
{"created_at", "TIMESTAMP"},
85+
{"created_at", "DATETIME"},
8686
},
8787
},
8888
{

engine_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ var queries = []struct {
694694
`,
695695
[]sql.Row{
696696
{"s", "TEXT"},
697-
{"i", "INT64"},
697+
{"i", "BIGINT"},
698698
},
699699
},
700700
{

sql/information_schema.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -214,27 +214,27 @@ func columnsRowIter(cat *Catalog) RowIter {
214214
collName = "utf8_bin"
215215
}
216216
rows = append(rows, Row{
217-
"def", // table_catalog
218-
db.Name(), // table_schema
219-
t.Name(), // table_name
220-
c.Name, // column_name
221-
uint64(i), // ordinal_position
222-
c.Default, // column_default
223-
nullable, // is_nullable
224-
c.Type.String(), // data_type
225-
nil, // character_maximum_length
226-
nil, // character_octet_length
227-
nil, // numeric_precision
228-
nil, // numeric_scale
229-
nil, // datetime_precision
230-
charName, // character_set_name
231-
collName, // collation_name
232-
c.Type.String(), // column_type
233-
"", // column_key
234-
"", // extra
235-
"select", // privileges
236-
"", // column_comment
237-
"", // generation_expression
217+
"def", // table_catalog
218+
db.Name(), // table_schema
219+
t.Name(), // table_name
220+
c.Name, // column_name
221+
uint64(i), // ordinal_position
222+
c.Default, // column_default
223+
nullable, // is_nullable
224+
MySQLTypeName(c.Type), // data_type
225+
nil, // character_maximum_length
226+
nil, // character_octet_length
227+
nil, // numeric_precision
228+
nil, // numeric_scale
229+
nil, // datetime_precision
230+
charName, // character_set_name
231+
collName, // collation_name
232+
MySQLTypeName(c.Type), // column_type
233+
"", // column_key
234+
"", // extra
235+
"select", // privileges
236+
"", // column_comment
237+
"", // generation_expression
238238
})
239239
}
240240
}

sql/plan/describe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (i *describeIter) Next() (sql.Row, error) {
7070

7171
f := i.schema[i.i]
7272
i.i++
73-
return sql.NewRow(f.Name, f.Type.Type().String()), nil
73+
return sql.NewRow(f.Name, sql.MySQLTypeName(f.Type)), nil
7474
}
7575

7676
func (i *describeIter) Close() error {

sql/plan/describe_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestDescribe(t *testing.T) {
3030

3131
n, err = iter.Next()
3232
require.NoError(err)
33-
require.Equal(sql.NewRow("c2", "INT32"), n)
33+
require.Equal(sql.NewRow("c2", "INTEGER"), n)
3434

3535
n, err = iter.Next()
3636
require.Equal(io.EOF, err)

sql/plan/show_create_table.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package plan
22

33
import (
44
"fmt"
5-
"gopkg.in/src-d/go-mysql-server.v0/internal/similartext"
65
"io"
76
"strings"
87

8+
"gopkg.in/src-d/go-mysql-server.v0/internal/similartext"
9+
910
"gopkg.in/src-d/go-mysql-server.v0/sql"
1011
)
1112

@@ -80,43 +81,42 @@ func (i *showCreateTablesIter) Next() (sql.Row, error) {
8081
composedCreateTableStatement := produceCreateStatement(table)
8182

8283
return sql.NewRow(
83-
i.table, // "Table" string
84+
i.table, // "Table" string
8485
composedCreateTableStatement, // "Create Table" string
8586
), nil
8687
}
8788

8889
func produceCreateStatement(table sql.Table) string {
8990
schema := table.Schema()
90-
colCreateStatements := make([]string, len(schema))
91+
colStmts := make([]string, len(schema))
9192

9293
// Statement creation parts for each column
93-
for indx, col := range schema {
94-
createStmtPart := fmt.Sprintf(" `%s` %s", col.Name,
95-
strings.ToLower(col.Type.Type().String()))
94+
for i, col := range schema {
95+
stmt := fmt.Sprintf(" `%s` %s", col.Name, sql.MySQLTypeName(col.Type))
9696

9797
if !col.Nullable {
98-
createStmtPart = fmt.Sprintf("%s NOT NULL", createStmtPart)
98+
stmt = fmt.Sprintf("%s NOT NULL", stmt)
9999
}
100100

101101
switch def := col.Default.(type) {
102102
case string:
103103
if def != "" {
104-
createStmtPart = fmt.Sprintf("%s DEFAULT %s", createStmtPart, def)
104+
stmt = fmt.Sprintf("%s DEFAULT %q", stmt, def)
105105
}
106106
default:
107107
if def != nil {
108-
createStmtPart = fmt.Sprintf("%s DEFAULT %v", createStmtPart, col.Default)
108+
stmt = fmt.Sprintf("%s DEFAULT %v", stmt, col.Default)
109109
}
110110
}
111111

112-
colCreateStatements[indx] = createStmtPart
112+
colStmts[i] = stmt
113113
}
114114

115-
prettyColCreateStmts := strings.Join(colCreateStatements, ",\n")
116-
composedCreateTableStatement :=
117-
fmt.Sprintf("CREATE TABLE `%s` (\n%s\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", table.Name(), prettyColCreateStmts)
118-
119-
return composedCreateTableStatement
115+
return fmt.Sprintf(
116+
"CREATE TABLE `%s` (\n%s\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
117+
table.Name(),
118+
strings.Join(colStmts, ",\n"),
119+
)
120120
}
121121

122122
func (i *showCreateTablesIter) Close() error {

sql/plan/show_create_table_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestShowCreateTable(t *testing.T) {
1818
sql.Schema{
1919
&sql.Column{Name: "baz", Type: sql.Text, Default: "", Nullable: false},
2020
&sql.Column{Name: "zab", Type: sql.Int32, Default: int32(0), Nullable: true},
21-
&sql.Column{Name: "bza", Type: sql.Int64, Default: int64(0), Nullable: true},
21+
&sql.Column{Name: "bza", Type: sql.Uint64, Default: uint64(0), Nullable: true},
2222
})
2323

2424
db.AddTable(table.Name(), table)
@@ -37,9 +37,9 @@ func TestShowCreateTable(t *testing.T) {
3737

3838
expected := sql.NewRow(
3939
table.Name(),
40-
"CREATE TABLE `test-table` (\n `baz` text NOT NULL,\n"+
41-
" `zab` int32 DEFAULT 0,\n"+
42-
" `bza` int64 DEFAULT 0\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
40+
"CREATE TABLE `test-table` (\n `baz` TEXT NOT NULL,\n"+
41+
" `zab` INTEGER DEFAULT 0,\n"+
42+
" `bza` BIGINT UNSIGNED DEFAULT 0\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
4343
)
4444

4545
require.Equal(expected, row)

sql/type.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,3 +875,43 @@ func NumColumns(t Type) int {
875875
}
876876
return len(v)
877877
}
878+
879+
// MySQLTypeName returns the MySQL display name for the given type.
880+
func MySQLTypeName(t Type) string {
881+
switch t.Type() {
882+
case sqltypes.Int8:
883+
return "TINYINT"
884+
case sqltypes.Uint8:
885+
return "TINYINT UNSIGNED"
886+
case sqltypes.Int16:
887+
return "SMALLINT"
888+
case sqltypes.Uint16:
889+
return "SMALLINT UNSIGNED"
890+
case sqltypes.Int32:
891+
return "INTEGER"
892+
case sqltypes.Int64:
893+
return "BIGINT"
894+
case sqltypes.Uint32:
895+
return "INTEGER UNSIGNED"
896+
case sqltypes.Uint64:
897+
return "BIGINT UNSIGNED"
898+
case sqltypes.Float32:
899+
return "FLOAT"
900+
case sqltypes.Float64:
901+
return "DOUBLE"
902+
case sqltypes.Timestamp:
903+
return "DATETIME"
904+
case sqltypes.Date:
905+
return "DATE"
906+
case sqltypes.Text, sqltypes.VarChar:
907+
return "TEXT"
908+
case sqltypes.Bit:
909+
return "BIT"
910+
case sqltypes.TypeJSON:
911+
return "JSON"
912+
case sqltypes.Blob:
913+
return "BLOB"
914+
default:
915+
return "UNKNOWN"
916+
}
917+
}

0 commit comments

Comments
 (0)