Browse Source

Global replace in mysql:encode/2 fixes #15

Viktor Söderqvist 10 years ago
parent
commit
f5ec533595
2 changed files with 6 additions and 6 deletions
  1. 2 2
      src/mysql_encode.erl
  2. 4 4
      test/mysql_encode_tests.erl

+ 2 - 2
src/mysql_encode.erl

@@ -18,7 +18,7 @@ encode(Float) when is_float(Float) ->
     io_lib:format("~w", [Float]);
     io_lib:format("~w", [Float]);
 encode(String) when is_list(String); is_binary(String) ->
 encode(String) when is_list(String); is_binary(String) ->
     Bin = iolist_to_binary(String),
     Bin = iolist_to_binary(String),
-    Escaped = binary:replace(Bin, <<"'">>, <<"''">>),
+    Escaped = binary:replace(Bin, <<"'">>, <<"''">>, [global]),
     [$', Escaped, $'];
     [$', Escaped, $'];
 encode(Bitstring) when is_bitstring(Bitstring) ->
 encode(Bitstring) when is_bitstring(Bitstring) ->
     ["b'", [ case B of 0 -> $0; 1 -> $1 end || <<B:1>> <= Bitstring ], $'];
     ["b'", [ case B of 0 -> $0; 1 -> $1 end || <<B:1>> <= Bitstring ], $'];
@@ -64,4 +64,4 @@ encode({D, {H, M, S}}) when D < 0, is_float(S) ->
 %% backslash escapes are enabled in the session.
 %% backslash escapes are enabled in the session.
 backslash_escape(String) ->
 backslash_escape(String) ->
     Bin = iolist_to_binary(String),
     Bin = iolist_to_binary(String),
-    binary:replace(Bin, <<"\\">>, <<"\\\\">>).
+    binary:replace(Bin, <<"\\">>, <<"\\\\">>, [global]).

+ 4 - 4
test/mysql_encode_tests.erl

@@ -11,8 +11,8 @@ encode_test() ->
         [{null,    "NULL"},
         [{null,    "NULL"},
          {42,      "42"},
          {42,      "42"},
          {3.14,    "3.14"},
          {3.14,    "3.14"},
-         {"don't", "'don''t'"}, %% Escape single quote using single quote.
-         {"\\n",   "'\\n'"},    %% Don't escape backslash.
+         {"isn't didn't", "'isn''t didn''t'"}, %% Escape single quotes.
+         {"\\n",   "'\\n'"},                   %% Don't escape backslash.
          %% BIT(N)
          %% BIT(N)
          {<<255, 2:3>>,   "b'11111111010'"},
          {<<255, 2:3>>,   "b'11111111010'"},
          %% DATE
          %% DATE
@@ -42,5 +42,5 @@ encode_test() ->
     ).
     ).
 
 
 backslash_escape_test() ->
 backslash_escape_test() ->
-    ?assertEqual(<<"a'b\\\\c">>,
-                 iolist_to_binary(mysql_encode:backslash_escape("a'b\\c"))).
+    ?assertEqual(<<"a'b\\\\c'd\\\\e">>,
+                 iolist_to_binary(mysql_encode:backslash_escape("a'b\\c'd\\e"))).