@@ -109,10 +109,12 @@ func parseValues(str string) ([]string, error) {
109109// read string until another single quote
110110j := i + 1
111111
112+ escaped := false
112113for j < len (str ) {
113114if str [j ] == '\\' {
114115// skip escaped character
115116j += 2
117+ escaped = true
116118continue
117119} else if str [j ] == '\'' {
118120break
@@ -125,7 +127,11 @@ func parseValues(str string) ([]string, error) {
125127return nil , fmt .Errorf ("parse quote values error" )
126128}
127129
128- values = append (values , str [i :j + 1 ])
130+ value := str [i : j + 1 ]
131+ if escaped {
132+ value = unescapeString (value )
133+ }
134+ values = append (values , value )
129135// skip ' and ,
130136i = j + 2
131137}
@@ -135,3 +141,48 @@ func parseValues(str string) ([]string, error) {
135141
136142return values , nil
137143}
144+
145+ // unescapeString un-escapes the string.
146+ // mysqldump will escape the string when dumps,
147+ // Refer http://dev.mysql.com/doc/refman/5.7/en/string-literals.html
148+ func unescapeString (s string ) string {
149+ i := 0
150+
151+ value := make ([]byte , 0 , len (s ))
152+ for i < len (s ) {
153+ if s [i ] == '\\' {
154+ j := i + 1
155+ if j == len (s ) {
156+ // The last char is \, remove
157+ break
158+ }
159+
160+ value = append (value , unescapeChar (s [j ]))
161+ i += 2
162+ } else {
163+ value = append (value , s [i ])
164+ i ++
165+ }
166+ }
167+
168+ return string (value )
169+ }
170+
171+ func unescapeChar (ch byte ) byte {
172+ // \" \' \\ \n \0 \b \Z \r \t ==> escape to one char
173+ switch ch {
174+ case 'n' :
175+ ch = '\n'
176+ case '0' :
177+ ch = 0
178+ case 'b' :
179+ ch = 8
180+ case 'Z' :
181+ ch = 26
182+ case 'r' :
183+ ch = '\r'
184+ case 't' :
185+ ch = '\t'
186+ }
187+ return ch
188+ }
0 commit comments