Browse Source

Edit docs and tests for unicode chardata as input

Viktor Söderqvist 9 years ago
parent
commit
f1e32adea3
3 changed files with 23 additions and 9 deletions
  1. 13 3
      doc/overview.edoc
  2. 5 1
      test/mysql_encode_tests.erl
  3. 5 5
      test/mysql_tests.erl

+ 13 - 3
doc/overview.edoc

@@ -47,10 +47,15 @@ For the reference manual see the <a href="mysql.html">mysql</a> module.
     </tr>
     </tr>
     <tr>
     <tr>
       <td>VARCHAR, TEXT, etc.</td>
       <td>VARCHAR, TEXT, etc.</td>
-      <td>`iodata()' [<a href="#vn1">1</a>]</td>
+      <td>`unicode:chardata()' [<a href="#vn1">1</a>]</td>
       <td>`<<"foo">>', `"bar"'</td>
       <td>`<<"foo">>', `"bar"'</td>
     </tr>
     </tr>
     <tr>
     <tr>
+      <td>VARBINARY, BLOB, etc.</td>
+      <td>`binary()'</td>
+      <td>`<<1, 2, 3, 4>>'</td>
+    </tr>
+    <tr>
       <td>BIT(N)</td>
       <td>BIT(N)</td>
       <td>`<<_:N/bitstring>>'</td>
       <td>`<<_:N/bitstring>>'</td>
       <td>`<<255, 6:3>>'</td>
       <td>`<<255, 6:3>>'</td>
@@ -95,8 +100,13 @@ Notes:
 
 
 <ol>
 <ol>
   <li id="vn1">When fetching VARCHAR, TEXT etc. they are returned as `binary()'.
   <li id="vn1">When fetching VARCHAR, TEXT etc. they are returned as `binary()'.
-    When sending (insert or update) any `iodata()' is accepted. Lists will be
-    interpreted as utf8 unicode, binaries will be left as is.</li>
+    When sending (insert or update) any `unicode:chardata()' is accepted as
+    input. In a (possibly deep) list of integers and binaries, the integers are
+    treated as Unicode codepoints while binaries are treated as UTF-8 encoded
+    Unicode data. For lists, an error occurs if you try to send invalid Unicode
+    data, but if the input is a pure binary, no validation will be done. This is
+    to allow sending binary non-Unicode data for MySQL's binary strings (BLOB,
+    VARBINARY, etc.).</li>
   <li id="vn2">DECIMALs are returned as `integer()' or `float()' when the value
   <li id="vn2">DECIMALs are returned as `integer()' or `float()' when the value
     can be represented without precision loss and as `binary()' for high
     can be represented without precision loss and as `binary()' for high
     precision DECIMAL values. This is similar to how the `odbc' OTP application
     precision DECIMAL values. This is similar to how the `odbc' OTP application

+ 5 - 1
test/mysql_encode_tests.erl

@@ -14,7 +14,11 @@ encode_test() ->
          {3.14,    "3.14"},
          {3.14,    "3.14"},
          {"isn't didn't", "'isn''t didn''t'"}, %% Escape single quotes.
          {"isn't didn't", "'isn''t didn''t'"}, %% Escape single quotes.
          {"\\n",   "'\\n'"},                   %% Don't escape backslash.
          {"\\n",   "'\\n'"},                   %% Don't escape backslash.
-         {[<<"asdf">>,"ščžć€"], <<"'asdf",197,161,196,141,197,190,196,135,226,130,172,"'">>},
+         %% Unicode codepoints gets encoded as UTF-8
+         {[<<"asdf">>, "ščžć€"],
+          <<"'asdf",197,161,196,141,197,190,196,135,226,130,172,"'">>},
+         %% Non-Unicode binary
+         {<<255, 0, 255, 0>>, <<"'", 255, 0, 255, 0, "'">>},
          %% BIT(N)
          %% BIT(N)
          {<<255, 2:3>>,   "b'11111111010'"},
          {<<255, 2:3>>,   "b'11111111010'"},
          %% DATE
          %% DATE

+ 5 - 5
test/mysql_tests.erl

@@ -253,11 +253,11 @@ text_protocol(Pid) ->
 binary_protocol(Pid) ->
 binary_protocol(Pid) ->
     ok = mysql:query(Pid, ?create_table_t),
     ok = mysql:query(Pid, ?create_table_t),
     %% The same queries as in the text protocol. Expect the same results.
     %% The same queries as in the text protocol. Expect the same results.
-    {ok, Ins} = mysql:prepare(Pid, <<"INSERT INTO t (bl, f, d, dc, y, ti,"
+    {ok, Ins} = mysql:prepare(Pid, <<"INSERT INTO t (bl, tx, f, d, dc, y, ti,"
                                      " ts, da, c)"
                                      " ts, da, c)"
-                                     " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)">>),
-
-    ok = mysql:execute(Pid, Ins, [<<"blob">>, 3.14, 3.14, 3.14,
+                                     " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)">>),
+    %% 16#161 is the codepoint for "s with caron"; <<197, 161>> in UTF-8.
+    ok = mysql:execute(Pid, Ins, [<<"blob">>, [16#161], 3.14, 3.14, 3.14,
                                   2014, {0, {0, 22, 11}}, 
                                   2014, {0, {0, 22, 11}}, 
                                   {{2014, 11, 03}, {0, 22, 24}},
                                   {{2014, 11, 03}, {0, 22, 24}},
                                   {2014, 11, 03}, null]),
                                   {2014, 11, 03}, null]),
@@ -267,7 +267,7 @@ binary_protocol(Pid) ->
     ?assertEqual([<<"id">>, <<"bl">>, <<"tx">>, <<"f">>, <<"d">>, <<"dc">>,
     ?assertEqual([<<"id">>, <<"bl">>, <<"tx">>, <<"f">>, <<"d">>, <<"dc">>,
                   <<"y">>, <<"ti">>,
                   <<"y">>, <<"ti">>,
                   <<"ts">>, <<"da">>, <<"c">>], Columns),
                   <<"ts">>, <<"da">>, <<"c">>], Columns),
-    ?assertEqual([[1, <<"blob">>, <<>>, 3.14, 3.14, 3.14,
+    ?assertEqual([[1, <<"blob">>, <<197, 161>>, 3.14, 3.14, 3.14,
                    2014, {0, {0, 22, 11}},
                    2014, {0, {0, 22, 11}},
                    {{2014, 11, 03}, {00, 22, 24}}, {2014, 11, 03}, null]],
                    {{2014, 11, 03}, {00, 22, 24}}, {2014, 11, 03}, null]],
                  Rows),
                  Rows),