diff --git a/go/vt/vttablet/onlineddl/vrepl/encoding.go b/go/vt/vttablet/onlineddl/vrepl/encoding.go deleted file mode 100644 index 713c6925878..00000000000 --- a/go/vt/vttablet/onlineddl/vrepl/encoding.go +++ /dev/null @@ -1,23 +0,0 @@ -/* - Copyright 2016 GitHub Inc. - See https://github.com/github/gh-ost/blob/master/LICENSE -*/ - -package vrepl - -import ( - "golang.org/x/text/encoding" - "golang.org/x/text/encoding/charmap" - "golang.org/x/text/encoding/simplifiedchinese" -) - -type charsetEncoding map[string]encoding.Encoding - -var charsetEncodingMap charsetEncoding - -func init() { - charsetEncodingMap = make(map[string]encoding.Encoding) - // Begin mappings - charsetEncodingMap["latin1"] = charmap.Windows1252 - charsetEncodingMap["gbk"] = simplifiedchinese.GBK -} diff --git a/go/vt/vttablet/onlineddl/vrepl/parser.go b/go/vt/vttablet/onlineddl/vrepl/parser.go index 57689d64498..7c8a2209d3b 100644 --- a/go/vt/vttablet/onlineddl/vrepl/parser.go +++ b/go/vt/vttablet/onlineddl/vrepl/parser.go @@ -9,30 +9,16 @@ import ( "regexp" "strconv" "strings" + + "vitess.io/vitess/go/vt/schema" ) var ( - sanitizeQuotesRegexp = regexp.MustCompile("('[^']*')") - renameColumnRegexp = regexp.MustCompile(`(?i)\bchange\s+(column\s+|)([\S]+)\s+([\S]+)\s+`) - dropColumnRegexp = regexp.MustCompile(`(?i)\bdrop\s+(column\s+|)([\S]+)$`) - renameTableRegexp = regexp.MustCompile(`(?i)\brename\s+(to|as)\s+`) - autoIncrementRegexp = regexp.MustCompile(`(?i)\bauto_increment[\s]*=[\s]*([0-9]+)`) - alterTableExplicitSchemaTableRegexps = []*regexp.Regexp{ - // ALTER TABLE `scm`.`tbl` something - regexp.MustCompile(`(?i)\balter\s+table\s+` + "`" + `([^` + "`" + `]+)` + "`" + `[.]` + "`" + `([^` + "`" + `]+)` + "`" + `\s+(.*$)`), - // ALTER TABLE `scm`.tbl something - regexp.MustCompile(`(?i)\balter\s+table\s+` + "`" + `([^` + "`" + `]+)` + "`" + `[.]([\S]+)\s+(.*$)`), - // ALTER TABLE scm.`tbl` something - regexp.MustCompile(`(?i)\balter\s+table\s+([\S]+)[.]` + "`" + `([^` + "`" + `]+)` + "`" + `\s+(.*$)`), - // ALTER TABLE scm.tbl something - regexp.MustCompile(`(?i)\balter\s+table\s+([\S]+)[.]([\S]+)\s+(.*$)`), - } - alterTableExplicitTableRegexps = []*regexp.Regexp{ - // ALTER TABLE `tbl` something - regexp.MustCompile(`(?i)\balter\s+table\s+` + "`" + `([^` + "`" + `]+)` + "`" + `\s+(.*$)`), - // ALTER TABLE tbl something - regexp.MustCompile(`(?i)\balter\s+table\s+([\S]+)\s+(.*$)`), - } + sanitizeQuotesRegexp = regexp.MustCompile("('[^']*')") + renameColumnRegexp = regexp.MustCompile(`(?i)\bchange\s+(column\s+|)([\S]+)\s+([\S]+)\s+`) + dropColumnRegexp = regexp.MustCompile(`(?i)\bdrop\s+(column\s+|)([\S]+)$`) + renameTableRegexp = regexp.MustCompile(`(?i)\brename\s+(to|as)\s+`) + autoIncrementRegexp = regexp.MustCompile(`(?i)\bauto_increment[\s]*=[\s]*([0-9]+)`) ) // AlterTableParser is a parser tool for ALTER TABLE statements @@ -141,22 +127,8 @@ func (p *AlterTableParser) parseAlterToken(alterToken string) (err error) { // ParseAlterStatement is the main function of th eparser, and parses an ALTER TABLE statement func (p *AlterTableParser) ParseAlterStatement(alterStatement string) (err error) { - p.alterStatementOptions = alterStatement - for _, alterTableRegexp := range alterTableExplicitSchemaTableRegexps { - if submatch := alterTableRegexp.FindStringSubmatch(p.alterStatementOptions); len(submatch) > 0 { - p.explicitSchema = submatch[1] - p.explicitTable = submatch[2] - p.alterStatementOptions = submatch[3] - break - } - } - for _, alterTableRegexp := range alterTableExplicitTableRegexps { - if submatch := alterTableRegexp.FindStringSubmatch(p.alterStatementOptions); len(submatch) > 0 { - p.explicitTable = submatch[1] - p.alterStatementOptions = submatch[2] - break - } - } + p.explicitSchema, p.explicitTable, p.alterStatementOptions = schema.ParseAlterTableOptions(alterStatement) + alterTokens, _ := p.tokenizeAlterStatement(p.alterStatementOptions) for _, alterToken := range alterTokens { alterToken = p.sanitizeQuotesFromAlterStatement(alterToken) diff --git a/go/vt/vttablet/onlineddl/vrepl/types.go b/go/vt/vttablet/onlineddl/vrepl/types.go index fe7bcbcbd29..e0ef5b3aab2 100644 --- a/go/vt/vttablet/onlineddl/vrepl/types.go +++ b/go/vt/vttablet/onlineddl/vrepl/types.go @@ -26,74 +26,12 @@ package vrepl import ( "fmt" "reflect" - "strconv" "strings" ) -// ColumnType enumerates some important column types -type ColumnType int - -const ( - UnknownColumnType ColumnType = iota - TimestampColumnType - DateTimeColumnType - EnumColumnType - MediumIntColumnType - JSONColumnType - FloatColumnType -) - -const maxMediumintUnsigned int32 = 16777215 - -// TimezoneConversion indicates how to convert a timezone value -type TimezoneConversion struct { - ToTimezone string -} - // Column represents a table column type Column struct { - Name string - IsUnsigned bool - Charset string - Type ColumnType - timezoneConversion *TimezoneConversion -} - -func (c *Column) convertArg(arg interface{}) interface{} { - if s, ok := arg.(string); ok { - // string, charset conversion - if encoding, ok := charsetEncodingMap[c.Charset]; ok { - arg, _ = encoding.NewDecoder().String(s) - } - return arg - } - - if c.IsUnsigned { - if i, ok := arg.(int8); ok { - return uint8(i) - } - if i, ok := arg.(int16); ok { - return uint16(i) - } - if i, ok := arg.(int32); ok { - if c.Type == MediumIntColumnType { - // problem with mediumint is that it's a 3-byte type. There is no compatible golang type to match that. - // So to convert from negative to positive we'd need to convert the value manually - if i >= 0 { - return i - } - return uint32(maxMediumintUnsigned + i + 1) - } - return uint32(i) - } - if i, ok := arg.(int64); ok { - return strconv.FormatUint(uint64(i), 10) - } - if i, ok := arg.(int); ok { - return uint(i) - } - } - return arg + Name string } // NewColumns creates a new column array from non empty names @@ -178,46 +116,6 @@ func (l *ColumnList) GetColumn(columnName string) *Column { return nil } -// SetUnsigned toggles on the unsigned property -func (l *ColumnList) SetUnsigned(columnName string) { - l.GetColumn(columnName).IsUnsigned = true -} - -// IsUnsigned returns true when the column is an unsigned numeral -func (l *ColumnList) IsUnsigned(columnName string) bool { - return l.GetColumn(columnName).IsUnsigned -} - -// SetCharset sets the charset property -func (l *ColumnList) SetCharset(columnName string, charset string) { - l.GetColumn(columnName).Charset = charset -} - -// GetCharset returns the hcarset property -func (l *ColumnList) GetCharset(columnName string) string { - return l.GetColumn(columnName).Charset -} - -// SetColumnType sets the type of the column (for interesting types) -func (l *ColumnList) SetColumnType(columnName string, columnType ColumnType) { - l.GetColumn(columnName).Type = columnType -} - -// GetColumnType gets type of column, for interesting types -func (l *ColumnList) GetColumnType(columnName string) ColumnType { - return l.GetColumn(columnName).Type -} - -// SetConvertDatetimeToTimestamp sets the timezone conversion -func (l *ColumnList) SetConvertDatetimeToTimestamp(columnName string, toTimezone string) { - l.GetColumn(columnName).timezoneConversion = &TimezoneConversion{ToTimezone: toTimezone} -} - -// HasTimezoneConversion sees if there's timezone conversion defined (only applicable to temporal values) -func (l *ColumnList) HasTimezoneConversion(columnName string) bool { - return l.GetColumn(columnName).timezoneConversion != nil -} - // String returns a comma separated list of column names func (l *ColumnList) String() string { return strings.Join(l.Names(), ",")