From 6b9523d7d348f5be8d42741e5c82474752dd0b02 Mon Sep 17 00:00:00 2001 From: Alexey Romanov Date: Fri, 11 Mar 2011 18:16:03 +0300 Subject: [PATCH] Support UTF-8 text --- src/sqlite3_lib.erl | 8 ++++---- test/sqlite3_test.erl | 10 +++++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/sqlite3_lib.erl b/src/sqlite3_lib.erl index 5fa8343..0966639 100644 --- a/src/sqlite3_lib.erl +++ b/src/sqlite3_lib.erl @@ -74,7 +74,7 @@ value_to_sql_unsafe(X) -> _ when is_float(X) -> float_to_list(X); ?NULL_ATOM -> "NULL"; {blob, Blob} -> ["x'", bin_to_hex(Blob), $']; - _ -> [$', X, $'] %% assumes no $' inside strings! + _ -> [$', unicode:characters_to_binary(X), $'] %% assumes no $' inside strings! end. %%-------------------------------------------------------------------- @@ -94,7 +94,7 @@ value_to_sql(X) -> _ when is_float(X) -> float_to_list(X); ?NULL_ATOM -> "NULL"; {blob, Blob} -> ["x'", bin_to_hex(Blob), $']; - _ -> [$', escape(X), $'] + _ -> [$', unicode:characters_to_binary(escape(X)), $'] end. %%-------------------------------------------------------------------- @@ -147,7 +147,7 @@ write_col_sql(Cols) -> %% @end %%-------------------------------------------------------------------- -spec escape(iodata()) -> iodata(). -escape(IoData) -> re:replace(IoData, "'", "''", [global]). +escape(IoData) -> re:replace(IoData, "'", "''", [global, unicode]). %%-------------------------------------------------------------------- %% @spec bin_to_hex(Binary :: binary()) -> binary() @@ -357,7 +357,7 @@ sql_number(NumberStr) -> -spec sql_string(string()) -> binary(). sql_string(StringWithEscapedQuotes) -> Res1 = re:replace(StringWithEscapedQuotes, "''", "'", - [global, {return, binary}]), + [global, {return, binary}, unicode]), erlang:binary_part(Res1, 0, byte_size(Res1) - 1). -spec sql_blob(string()) -> binary(). diff --git a/test/sqlite3_test.erl b/test/sqlite3_test.erl index 790e20d..545c875 100644 --- a/test/sqlite3_test.erl +++ b/test/sqlite3_test.erl @@ -46,7 +46,8 @@ all_test_() -> ?FuncTest(escaping), ?FuncTest(select_many_records), ?FuncTest(nonexistent_table_info), - ?FuncTest(large_number)]}. + ?FuncTest(large_number), + ?FuncTest(unicode)]}. open_db() -> sqlite3:open(ct, [in_memory]). @@ -208,6 +209,13 @@ large_number() -> ?assertEqual([{N1, N2}], rows(sqlite3:sql_exec(ct, Query2, [N1, N2]))), ?assertNot([{N1 + 1, N2 - 1}] == rows(sqlite3:sql_exec(ct, Query2, [N1 + 1, N2 - 1]))). +unicode() -> + UnicodeString = [1102,1085,1080,1082,1086,1076], %% "Unicode" in Russian, in UTF-8 + drop_table_if_exists(ct, unicode), + sqlite3:create_table(ct, unicode, [{str, text}]), + sqlite3:write(ct, unicode, [{str, UnicodeString}]), + ?assertEqual([{unicode:characters_to_binary(UnicodeString)}], rows(sqlite3:read_all(ct, unicode))). + prepared_test() -> Columns = ["id", "name", "age", "wage"], Abby = {1, <<"abby">>, 20, 2000},