added decimal test

This commit is contained in:
Shlomi Noach 2016-11-29 15:47:39 +01:00
parent e7cf488818
commit 0a707688e0
2 changed files with 13 additions and 8 deletions

View File

@ -12,8 +12,8 @@ import (
) )
var ( var (
stripQuotesRegexp = regexp.MustCompile("('[^']*')") sanitizeQuotesRegexp = regexp.MustCompile("('[^']*')")
renameColumnRegexp = regexp.MustCompile(`(?i)\bchange\s+(column\s+|)([\S]+)\s+([\S]+)\s+`) renameColumnRegexp = regexp.MustCompile(`(?i)\bchange\s+(column\s+|)([\S]+)\s+([\S]+)\s+`)
) )
type Parser struct { type Parser struct {
@ -53,16 +53,16 @@ func (this *Parser) tokenizeAlterStatement(alterStatement string) (tokens []stri
return tokens, nil return tokens, nil
} }
func (this *Parser) stripQuotesFromAlterStatement(alterStatement string) (strippedStatement string) { func (this *Parser) sanitizeQuotesFromAlterStatement(alterStatement string) (strippedStatement string) {
strippedStatement = alterStatement strippedStatement = alterStatement
strippedStatement = stripQuotesRegexp.ReplaceAllString(strippedStatement, "''") strippedStatement = sanitizeQuotesRegexp.ReplaceAllString(strippedStatement, "''")
return strippedStatement return strippedStatement
} }
func (this *Parser) ParseAlterStatement(alterStatement string) (err error) { func (this *Parser) ParseAlterStatement(alterStatement string) (err error) {
alterTokens, _ := this.tokenizeAlterStatement(alterStatement) alterTokens, _ := this.tokenizeAlterStatement(alterStatement)
for _, alterToken := range alterTokens { for _, alterToken := range alterTokens {
alterToken = this.stripQuotesFromAlterStatement(alterToken) alterToken = this.sanitizeQuotesFromAlterStatement(alterToken)
allStringSubmatch := renameColumnRegexp.FindAllStringSubmatch(alterToken, -1) allStringSubmatch := renameColumnRegexp.FindAllStringSubmatch(alterToken, -1)
for _, submatch := range allStringSubmatch { for _, submatch := range allStringSubmatch {
if unquoted, err := strconv.Unquote(submatch[2]); err == nil { if unquoted, err := strconv.Unquote(submatch[2]); err == nil {

View File

@ -90,6 +90,11 @@ func TestTokenizeAlterStatement(t *testing.T) {
tokens, _ := parser.tokenizeAlterStatement(alterStatement) tokens, _ := parser.tokenizeAlterStatement(alterStatement)
test.S(t).ExpectTrue(reflect.DeepEqual(tokens, []string{"add column t int", "change column i int 'some comment, with comma'"})) test.S(t).ExpectTrue(reflect.DeepEqual(tokens, []string{"add column t int", "change column i int 'some comment, with comma'"}))
} }
{
alterStatement := "add column t int, add column d decimal(10,2)"
tokens, _ := parser.tokenizeAlterStatement(alterStatement)
test.S(t).ExpectTrue(reflect.DeepEqual(tokens, []string{"add column t int", "add column d decimal(10,2)"}))
}
{ {
alterStatement := "add column t int, add column e enum('a','b','c')" alterStatement := "add column t int, add column e enum('a','b','c')"
tokens, _ := parser.tokenizeAlterStatement(alterStatement) tokens, _ := parser.tokenizeAlterStatement(alterStatement)
@ -104,16 +109,16 @@ func TestTokenizeAlterStatement(t *testing.T) {
} }
} }
func TestStripQuotesFromAlterStatement(t *testing.T) { func TestSanitizeQuotesFromAlterStatement(t *testing.T) {
parser := NewParser() parser := NewParser()
{ {
alterStatement := "add column e enum('a','b','c')" alterStatement := "add column e enum('a','b','c')"
strippedStatement := parser.stripQuotesFromAlterStatement(alterStatement) strippedStatement := parser.sanitizeQuotesFromAlterStatement(alterStatement)
test.S(t).ExpectEquals(strippedStatement, "add column e enum('','','')") test.S(t).ExpectEquals(strippedStatement, "add column e enum('','','')")
} }
{ {
alterStatement := "change column i int 'some comment, with comma'" alterStatement := "change column i int 'some comment, with comma'"
strippedStatement := parser.stripQuotesFromAlterStatement(alterStatement) strippedStatement := parser.sanitizeQuotesFromAlterStatement(alterStatement)
test.S(t).ExpectEquals(strippedStatement, "change column i int ''") test.S(t).ExpectEquals(strippedStatement, "change column i int ''")
} }
} }