Skip to content

Commit 803944a

Browse files
Extract BINARY column size schema details #480 (#481)
Co-authored-by: siddontang <[email protected]>
1 parent 3731d51 commit 803944a

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

schema/schema.go

+40-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package schema
77
import (
88
"database/sql"
99
"fmt"
10+
"strconv"
1011
"strings"
1112

1213
"github.com/pingcap/errors"
@@ -23,7 +24,7 @@ const (
2324
TYPE_FLOAT // float, double
2425
TYPE_ENUM // enum
2526
TYPE_SET // set
26-
TYPE_STRING // other
27+
TYPE_STRING // char, varchar, etc.
2728
TYPE_DATETIME // datetime
2829
TYPE_TIMESTAMP // timestamp
2930
TYPE_DATE // date
@@ -32,6 +33,7 @@ const (
3233
TYPE_JSON // json
3334
TYPE_DECIMAL // decimal
3435
TYPE_MEDIUM_INT
36+
TYPE_BINARY // binary, varbinary
3537
)
3638

3739
type TableColumn struct {
@@ -43,6 +45,8 @@ type TableColumn struct {
4345
IsUnsigned bool
4446
EnumValues []string
4547
SetValues []string
48+
FixedSize uint
49+
MaxSize uint
4650
}
4751

4852
type Index struct {
@@ -94,6 +98,14 @@ func (ta *Table) AddColumn(name string, columnType string, collation string, ext
9498
")"),
9599
"'", "", -1),
96100
",")
101+
} else if strings.HasPrefix(columnType, "binary") {
102+
ta.Columns[index].Type = TYPE_BINARY
103+
size := getSizeFromColumnType(columnType)
104+
ta.Columns[index].MaxSize = size
105+
ta.Columns[index].FixedSize = size
106+
} else if strings.HasPrefix(columnType, "varbinary") {
107+
ta.Columns[index].Type = TYPE_BINARY
108+
ta.Columns[index].MaxSize = getSizeFromColumnType(columnType)
97109
} else if strings.HasPrefix(columnType, "datetime") {
98110
ta.Columns[index].Type = TYPE_DATETIME
99111
} else if strings.HasPrefix(columnType, "timestamp") {
@@ -110,8 +122,14 @@ func (ta *Table) AddColumn(name string, columnType string, collation string, ext
110122
ta.Columns[index].Type = TYPE_MEDIUM_INT
111123
} else if strings.Contains(columnType, "int") || strings.HasPrefix(columnType, "year") {
112124
ta.Columns[index].Type = TYPE_NUMBER
125+
} else if strings.HasPrefix(columnType, "char") {
126+
ta.Columns[index].Type = TYPE_STRING
127+
size := getSizeFromColumnType(columnType)
128+
ta.Columns[index].FixedSize = size
129+
ta.Columns[index].MaxSize = size
113130
} else {
114131
ta.Columns[index].Type = TYPE_STRING
132+
ta.Columns[index].MaxSize = getSizeFromColumnType(columnType)
115133
}
116134

117135
if strings.Contains(columnType, "unsigned") || strings.Contains(columnType, "zerofill") {
@@ -124,6 +142,27 @@ func (ta *Table) AddColumn(name string, columnType string, collation string, ext
124142
}
125143
}
126144

145+
func getSizeFromColumnType(columnType string) uint {
146+
startIndex := strings.Index(columnType, "(")
147+
if startIndex < 0 {
148+
return 0
149+
}
150+
151+
// we are searching for the first () and there may not be any closing
152+
// brackets before the opening, so no need search at the offset from the
153+
// opening ones
154+
endIndex := strings.Index(columnType, ")")
155+
if startIndex < 0 || endIndex < 0 || startIndex > endIndex {
156+
return 0
157+
}
158+
159+
i, err := strconv.Atoi(columnType[startIndex+1:endIndex])
160+
if err != nil || i < 0 {
161+
return 0
162+
}
163+
return uint(i)
164+
}
165+
127166
func (ta *Table) FindColumn(name string) int {
128167
for i, col := range ta.Columns {
129168
if col.Name == name {

schema/schema_test.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func (s *schemaTestSuite) TestSchema(c *C) {
6262
zfint INT ZEROFILL,
6363
name_ucs VARCHAR(256) CHARACTER SET ucs2,
6464
name_utf8 VARCHAR(256) CHARACTER SET utf8,
65+
name_char CHAR(10),
66+
name_binary BINARY(11),
67+
name_varbinary VARBINARY(12),
6568
PRIMARY KEY(id2, id),
6669
UNIQUE (id1),
6770
INDEX name_idx (name)
@@ -74,20 +77,34 @@ func (s *schemaTestSuite) TestSchema(c *C) {
7477
ta, err := NewTable(s.conn, "test", "schema_test")
7578
c.Assert(err, IsNil)
7679

77-
c.Assert(ta.Columns, HasLen, 12)
80+
c.Assert(ta.Columns, HasLen, 15)
7881
c.Assert(ta.Indexes, HasLen, 3)
7982
c.Assert(ta.PKColumns, DeepEquals, []int{2, 0})
8083
c.Assert(ta.Indexes[0].Columns, HasLen, 2)
8184
c.Assert(ta.Indexes[0].Name, Equals, "PRIMARY")
8285
c.Assert(ta.Indexes[2].Name, Equals, "name_idx")
86+
c.Assert(ta.Columns[3].Type, Equals, TYPE_STRING)
87+
c.Assert(ta.Columns[3].MaxSize, Equals, uint(256))
88+
c.Assert(ta.Columns[3].FixedSize, Equals, uint(0))
8389
c.Assert(ta.Columns[4].EnumValues, DeepEquals, []string{"appointing", "serving", "abnormal", "stop", "noaftermarket", "finish", "financial_audit"})
8490
c.Assert(ta.Columns[5].SetValues, DeepEquals, []string{"a", "b", "c"})
8591
c.Assert(ta.Columns[7].Type, Equals, TYPE_DECIMAL)
8692
c.Assert(ta.Columns[0].IsUnsigned, IsFalse)
8793
c.Assert(ta.Columns[8].IsUnsigned, IsTrue)
8894
c.Assert(ta.Columns[9].IsUnsigned, IsTrue)
8995
c.Assert(ta.Columns[10].Collation, Matches, "^ucs2.*")
96+
c.Assert(ta.Columns[10].MaxSize, Equals, uint(256))
97+
c.Assert(ta.Columns[10].FixedSize, Equals, uint(0))
9098
c.Assert(ta.Columns[11].Collation, Matches, "^utf8.*")
99+
c.Assert(ta.Columns[12].Type, Equals, TYPE_STRING)
100+
c.Assert(ta.Columns[12].MaxSize, Equals, uint(10))
101+
c.Assert(ta.Columns[12].FixedSize, Equals, uint(10))
102+
c.Assert(ta.Columns[13].Type, Equals, TYPE_BINARY)
103+
c.Assert(ta.Columns[13].MaxSize, Equals, uint(11))
104+
c.Assert(ta.Columns[13].FixedSize, Equals, uint(11))
105+
c.Assert(ta.Columns[14].Type, Equals, TYPE_BINARY)
106+
c.Assert(ta.Columns[14].MaxSize, Equals, uint(12))
107+
c.Assert(ta.Columns[14].FixedSize, Equals, uint(0))
91108

92109
taSqlDb, err := NewTableFromSqlDB(s.sqlDB, "test", "schema_test")
93110
c.Assert(err, IsNil)

0 commit comments

Comments
 (0)