From 9eb6f9ed1f7bac39bc4944e93c5eaa8ec03a2fa6 Mon Sep 17 00:00:00 2001 From: Alexey Romanov Date: Tue, 26 Apr 2011 11:25:05 +0400 Subject: [PATCH] Fixed a term encoding problem See http://www.erlang.org/doc/man/ei.html#ei_decode_string --- c_src/sqlite3_drv.c | 17 +++++++++++++++-- test/sqlite3_test.erl | 6 +++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/c_src/sqlite3_drv.c b/c_src/sqlite3_drv.c index 808d877..4d6ab38 100644 --- a/c_src/sqlite3_drv.c +++ b/c_src/sqlite3_drv.c @@ -403,10 +403,23 @@ static int bind_parameters( int i, cur_list_size = -1, param_index = 1, param_indices_are_explicit = 0, result = 0; long param_index_long; char param_name[MAXATOMLEN + 1]; // parameter names shouldn't be longer than 256! + char *acc_string; result = ei_decode_list_header(buffer, p_index, &cur_list_size); if (result) { - return output_error(drv, SQLITE_ERROR, - "error while binding parameters"); + // probably all parameters are integers between 0 and 255 + // and the list was encoded as string (see ei documentation) + ei_get_type(buffer, p_index, p_type, p_size); + if (*p_type != ERL_STRING_EXT) { + return output_error(drv, SQLITE_ERROR, + "error while binding parameters"); + } + acc_string = driver_alloc(sizeof(char*) * (*p_size + 1)); + ei_decode_string(buffer, p_index, acc_string); + for (param_index = 1; param_index <= *p_size; param_index++) { + sqlite3_bind_int(statement, param_index, (int) acc_string[param_index - 1]); + } + driver_free(acc_string); + return 0; } for (i = 0; i < cur_list_size; i++) { if (*p_index >= buffer_size) { diff --git a/test/sqlite3_test.erl b/test/sqlite3_test.erl index a207bc9..1748087 100644 --- a/test/sqlite3_test.erl +++ b/test/sqlite3_test.erl @@ -47,7 +47,8 @@ all_test_() -> ?FuncTest(select_many_records), ?FuncTest(nonexistent_table_info), ?FuncTest(large_number), - ?FuncTest(unicode)]}. + ?FuncTest(unicode), + ?FuncTest(acc_string_encoding)]}. open_db() -> sqlite3:open(ct, [in_memory]). @@ -216,6 +217,9 @@ unicode() -> sqlite3:write(ct, unicode, [{str, UnicodeString}]), ?assertEqual([{unicode:characters_to_binary(UnicodeString)}], rows(sqlite3:read_all(ct, unicode))). +acc_string_encoding() -> + ?assertEqual([{62}], rows(sqlite3:sql_exec(ct, "SELECT ? + ?", [30,32]))). + prepared_test() -> Columns = ["id", "name", "age", "wage"], Abby = {1, <<"abby">>, 20, 2000},