Support for unicode strings as prepared statement parameters
@@ -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
@@ -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}) ->
@@ -736,6 +736,8 @@ encode_param(null) ->
encode_param(Value) when is_binary(Value) ->
EncLength = lenenc_int_encode(byte_size(Value)),
{<<?TYPE_VAR_STRING, 0>>, <<EncLength/binary, Value/binary>>};
+encode_param(Value) when is_list(Value) ->
+ encode_param(unicode:characters_to_binary(Value));
encode_param(Value) when is_integer(Value), Value >= 0 ->
%% We send positive integers with the 'unsigned' flag set.
if
@@ -1,3 +1,4 @@
+%% coding: utf-8
%% @doc This test suite does not require an actual MySQL connection.
-module(mysql_encode_tests).
-include_lib("eunit/include/eunit.hrl").
@@ -13,6 +14,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