Browse Source

add unicode encoding to mysql:encode, update docs and add test

Sergej Jurecko 9 years ago
parent
commit
29edd23b38
3 changed files with 6 additions and 3 deletions
  1. 2 1
      doc/overview.edoc
  2. 3 2
      src/mysql_encode.erl
  3. 1 0
      test/mysql_encode_tests.erl

+ 2 - 1
doc/overview.edoc

@@ -95,7 +95,8 @@ Notes:
 
 <ol>
   <li id="vn1">When fetching VARCHAR, TEXT etc. they are returned as `binary()'.
-    When sending (insert or update) any `iodata()' is accepted.</li>
+    When sending (insert or update) any `iodata()' is accepted. Lists will be
+    interpreted as utf8 unicode, binaries will be left as is.</li>
   <li id="vn2">DECIMALs are returned as `integer()' or `float()' when the value
     can be represented without precision loss and as `binary()' for high
     precision DECIMAL values. This is similar to how the `odbc' OTP application

+ 3 - 2
src/mysql_encode.erl

@@ -16,10 +16,11 @@ encode(Int) when is_integer(Int) ->
 encode(Float) when is_float(Float) ->
     %% "floats are printed accurately as the shortest, correctly rounded string"
     io_lib:format("~w", [Float]);
-encode(String) when is_list(String); is_binary(String) ->
-    Bin = iolist_to_binary(String),
+encode(Bin) when is_binary(Bin) ->
     Escaped = binary:replace(Bin, <<"'">>, <<"''">>, [global]),
     [$', Escaped, $'];
+encode(String) when is_list(String) ->
+    encode(unicode:characters_to_binary(String));
 encode(Bitstring) when is_bitstring(Bitstring) ->
     ["b'", [ case B of 0 -> $0; 1 -> $1 end || <<B:1>> <= Bitstring ], $'];
 encode({Y, M, D}) ->

+ 1 - 0
test/mysql_encode_tests.erl

@@ -13,6 +13,7 @@ encode_test() ->
          {3.14,    "3.14"},
          {"isn't didn't", "'isn''t didn''t'"}, %% Escape single quotes.
          {"\\n",   "'\\n'"},                   %% Don't escape backslash.
+         {[<<"asdf">>,"ščžć€"], <<"'asdf",197,161,196,141,197,190,196,135,226,130,172,"'">>},
          %% BIT(N)
          {<<255, 2:3>>,   "b'11111111010'"},
          %% DATE