@@ -224,7 +224,7 @@ func parseDSNParams(cfg *config, params string) (err error) {
224
224
}
225
225
cfg .collation = collation
226
226
break
227
-
227
+
228
228
case "columnsWithAlias" :
229
229
var isBool bool
230
230
cfg .columnsWithAlias , isBool = readBool (value )
@@ -806,3 +806,72 @@ func appendLengthEncodedInteger(b []byte, n uint64) []byte {
806
806
return append (b , 0xfe , byte (n ), byte (n >> 8 ), byte (n >> 16 ), byte (n >> 24 ),
807
807
byte (n >> 32 ), byte (n >> 40 ), byte (n >> 48 ), byte (n >> 56 ))
808
808
}
809
+
810
+ // Escape string with backslashes (\)
811
+ // This escapes the contents of a string (provided as []byte) by adding backslashes before special
812
+ // characters, and turning others into specific escape sequences, such as
813
+ // turning newlines into \n and null bytes into \0.
814
+ // https://github.com/mysql/mysql-server/blob/mysql-5.7.5/mysys/charset.c#L823-L932
815
+ func EscapeString (v []byte ) []byte {
816
+ buf := make ([]byte , len (v )* 2 )
817
+ pos := 0
818
+ for _ , c := range v {
819
+ switch c {
820
+ case '\x00' :
821
+ buf [pos ] = '\\'
822
+ buf [pos + 1 ] = '0'
823
+ pos += 2
824
+ case '\n' :
825
+ buf [pos ] = '\\'
826
+ buf [pos + 1 ] = 'n'
827
+ pos += 2
828
+ case '\r' :
829
+ buf [pos ] = '\\'
830
+ buf [pos + 1 ] = 'r'
831
+ pos += 2
832
+ case '\x1a' :
833
+ buf [pos ] = '\\'
834
+ buf [pos + 1 ] = 'Z'
835
+ pos += 2
836
+ case '\'' :
837
+ buf [pos ] = '\\'
838
+ buf [pos + 1 ] = '\''
839
+ pos += 2
840
+ case '"' :
841
+ buf [pos ] = '\\'
842
+ buf [pos + 1 ] = '"'
843
+ pos += 2
844
+ case '\\' :
845
+ buf [pos ] = '\\'
846
+ buf [pos + 1 ] = '\\'
847
+ pos += 2
848
+ default :
849
+ buf [pos ] = c
850
+ pos += 1
851
+ }
852
+ }
853
+
854
+ return buf [:pos ]
855
+ }
856
+
857
+ // Escape apostrophes by doubling them up
858
+ // This escapes the contents of a string by doubling up any apostrophes that
859
+ // it contains. This is used when the NO_BACKSLASH_ESCAPES SQL_MODE is in
860
+ // effect on the server.
861
+ // https://github.com/mysql/mysql-server/blob/mysql-5.7.5/mysys/charset.c#L963-L1038
862
+ func EscapeQuotes (v []byte ) []byte {
863
+ buf := make ([]byte , len (v )* 2 )
864
+ pos := 0
865
+ for _ , c := range v {
866
+ if c == '\'' {
867
+ buf [pos ] = '\''
868
+ buf [pos + 1 ] = '\''
869
+ pos += 2
870
+ } else {
871
+ buf [pos ] = c
872
+ pos ++
873
+ }
874
+ }
875
+
876
+ return buf [:pos ]
877
+ }
0 commit comments