-
-
Save jilieryuyi/d8e5cf059d49df8b1222274585de793d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Escape(sql string) string { | |
dest := make([]byte, 0, 2*len(sql)) | |
var escape byte | |
for i := 0; i < len(sql); i++ { | |
c := sql[i] | |
escape = 0 | |
switch c { | |
case 0: /* Must be escaped for 'mysql' */ | |
escape = '0' | |
break | |
case '\n': /* Must be escaped for logs */ | |
escape = 'n' | |
break | |
case '\r': | |
escape = 'r' | |
break | |
case '\\': | |
escape = '\\' | |
break | |
case '\'': | |
escape = '\'' | |
break | |
case '"': /* Better safe than sorry */ | |
escape = '"' | |
break | |
case '\032': /* This gives problems on Win32 */ | |
escape = 'Z' | |
} | |
if escape != 0 { | |
dest = append(dest, '\\', escape) | |
} else { | |
dest = append(dest, c) | |
} | |
} | |
return string(dest) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment