Support UTF-8 text
This commit is contained in:
@@ -74,7 +74,7 @@ value_to_sql_unsafe(X) ->
|
|||||||
_ when is_float(X) -> float_to_list(X);
|
_ when is_float(X) -> float_to_list(X);
|
||||||
?NULL_ATOM -> "NULL";
|
?NULL_ATOM -> "NULL";
|
||||||
{blob, Blob} -> ["x'", bin_to_hex(Blob), $'];
|
{blob, Blob} -> ["x'", bin_to_hex(Blob), $'];
|
||||||
_ -> [$', X, $'] %% assumes no $' inside strings!
|
_ -> [$', unicode:characters_to_binary(X), $'] %% assumes no $' inside strings!
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
@@ -94,7 +94,7 @@ value_to_sql(X) ->
|
|||||||
_ when is_float(X) -> float_to_list(X);
|
_ when is_float(X) -> float_to_list(X);
|
||||||
?NULL_ATOM -> "NULL";
|
?NULL_ATOM -> "NULL";
|
||||||
{blob, Blob} -> ["x'", bin_to_hex(Blob), $'];
|
{blob, Blob} -> ["x'", bin_to_hex(Blob), $'];
|
||||||
_ -> [$', escape(X), $']
|
_ -> [$', unicode:characters_to_binary(escape(X)), $']
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
@@ -147,7 +147,7 @@ write_col_sql(Cols) ->
|
|||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec escape(iodata()) -> iodata().
|
-spec escape(iodata()) -> iodata().
|
||||||
escape(IoData) -> re:replace(IoData, "'", "''", [global]).
|
escape(IoData) -> re:replace(IoData, "'", "''", [global, unicode]).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% @spec bin_to_hex(Binary :: binary()) -> binary()
|
%% @spec bin_to_hex(Binary :: binary()) -> binary()
|
||||||
@@ -357,7 +357,7 @@ sql_number(NumberStr) ->
|
|||||||
-spec sql_string(string()) -> binary().
|
-spec sql_string(string()) -> binary().
|
||||||
sql_string(StringWithEscapedQuotes) ->
|
sql_string(StringWithEscapedQuotes) ->
|
||||||
Res1 = re:replace(StringWithEscapedQuotes, "''", "'",
|
Res1 = re:replace(StringWithEscapedQuotes, "''", "'",
|
||||||
[global, {return, binary}]),
|
[global, {return, binary}, unicode]),
|
||||||
erlang:binary_part(Res1, 0, byte_size(Res1) - 1).
|
erlang:binary_part(Res1, 0, byte_size(Res1) - 1).
|
||||||
|
|
||||||
-spec sql_blob(string()) -> binary().
|
-spec sql_blob(string()) -> binary().
|
||||||
|
|||||||
@@ -46,7 +46,8 @@ all_test_() ->
|
|||||||
?FuncTest(escaping),
|
?FuncTest(escaping),
|
||||||
?FuncTest(select_many_records),
|
?FuncTest(select_many_records),
|
||||||
?FuncTest(nonexistent_table_info),
|
?FuncTest(nonexistent_table_info),
|
||||||
?FuncTest(large_number)]}.
|
?FuncTest(large_number),
|
||||||
|
?FuncTest(unicode)]}.
|
||||||
|
|
||||||
open_db() ->
|
open_db() ->
|
||||||
sqlite3:open(ct, [in_memory]).
|
sqlite3:open(ct, [in_memory]).
|
||||||
@@ -208,6 +209,13 @@ large_number() ->
|
|||||||
?assertEqual([{N1, N2}], rows(sqlite3:sql_exec(ct, Query2, [N1, N2]))),
|
?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]))).
|
?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() ->
|
prepared_test() ->
|
||||||
Columns = ["id", "name", "age", "wage"],
|
Columns = ["id", "name", "age", "wage"],
|
||||||
Abby = {1, <<"abby">>, 20, 2000},
|
Abby = {1, <<"abby">>, 20, 2000},
|
||||||
|
|||||||
Reference in New Issue
Block a user