Skip to content

Commit

Permalink
repair ODBC abstract layer
Browse files Browse the repository at this point in the history
  • Loading branch information
michalwski committed Aug 25, 2014
1 parent 0bdd128 commit d6146cf
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions apps/ejabberd/src/ejabberd_binary.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ string_to_binary(S) when is_list(S) ->
list_to_binary(S);
string_to_binary(B) when is_binary(B) ->
B.

37 changes: 31 additions & 6 deletions apps/ejabberd/src/ejabberd_odbc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,14 @@ outer_transaction(F, NRestarts, _Reason) ->
[T]),
erlang:exit(implementation_faulty)
end,
sql_query_internal(<<"begin;">>),
sql_query_internal([<<"begin;">>]),
put(?NESTING_KEY, PreviousNestingLevel + 1),
Result = (catch F()),
put(?NESTING_KEY, PreviousNestingLevel),
case Result of
{aborted, Reason} when NRestarts > 0 ->
%% Retry outer transaction upto NRestarts times.
sql_query_internal(<<"rollback;">>),
sql_query_internal([<<"rollback;">>]),
outer_transaction(F, NRestarts - 1, Reason);
{aborted, Reason} when NRestarts =:= 0 ->
%% Too many retries of outer transaction.
Expand All @@ -512,15 +512,15 @@ outer_transaction(F, NRestarts, _Reason) ->
"** When State == ~p",
[?MAX_TRANSACTION_RESTARTS, Reason,
erlang:get_stacktrace(), get(?STATE_KEY)]),
sql_query_internal(<<"rollback;">>),
sql_query_internal([<<"rollback;">>]),
{aborted, Reason};
{'EXIT', Reason} ->
%% Abort sql transaction on EXIT from outer txn only.
sql_query_internal(<<"rollback;">>),
sql_query_internal([<<"rollback;">>]),
{aborted, Reason};
Res ->
%% Commit successful outer txn
sql_query_internal(<<"commit;">>),
sql_query_internal([<<"commit;">>]),
{atomic, Res}
end.

Expand All @@ -541,7 +541,7 @@ sql_query_internal(Query) ->
State = get(?STATE_KEY),
Res = case State#state.db_type of
odbc ->
odbc:sql_query(State#state.db_ref, Query);
binaryze_odbc(odbc:sql_query(State#state.db_ref, Query));
pgsql ->
?DEBUG("Postres, Send query~n~p~n", [Query]),
pgsql_to_odbc(pgsql:squery(State#state.db_ref, Query));
Expand Down Expand Up @@ -585,6 +585,20 @@ odbc_connect(SQLServer) ->
application:start(odbc),
odbc:connect(SQLServer, [{scrollable_cursors, off}]).

binaryze_odbc(ODBCResults) when is_list(ODBCResults) ->
lists:map(fun binaryze_odbc/1, ODBCResults);
binaryze_odbc({selected, ColNames, Rows}) ->
ColNamesB = lists:map(fun ejabberd_binary:string_to_binary/1, ColNames),
RowsB = lists:map(fun binaryze_tuple/1, Rows),
{selected, ColNamesB, RowsB};
binaryze_odbc(ODBCResult) ->
ODBCResult.

binaryze_tuple(Tuple) when is_tuple(Tuple) ->
Binarized = lists:map(fun try_binaryze/1,
tuple_to_list(Tuple)),
list_to_tuple(Binarized).

%% == Native PostgreSQL code

%% part of init/1
Expand Down Expand Up @@ -736,3 +750,14 @@ fsm_limit_opts() ->
_ ->
[]
end.

-spec try_binaryze(term()) -> term().
try_binaryze(List) when is_list(List) ->
case io_lib:printable_unicode_list(List) of
true ->
ejabberd_binary:string_to_binary(List);
_ ->
List
end;
try_binaryze(Other) ->
Other.
2 changes: 1 addition & 1 deletion apps/ejabberd/src/odbc_queries.erl
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ escape_character($\n) -> "\\n";
escape_character($\t) -> "\\t";
escape_character($\b) -> "\\b";
escape_character($\r) -> "\\r";
escape_character($') -> "\\'";
escape_character($') -> "''";
escape_character($") -> "\\\"";
escape_character($\\) -> "\\\\";
escape_character(C) -> C.
Expand Down
1 change: 1 addition & 0 deletions rel/reltool.config
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
{app, runtime_tools, [{incl_cond, include}]},
{app, mysql, [{incl_cond, include}]},
{app, pgsql, [{incl_cond, include}]},
{app, odbc, [{incl_cond, include}]},
{app, redo, [{incl_cond, include}]},
{app, cuesport, [{incl_cond, include}]},
{app, inets, [{incl_cond, include}]},
Expand Down

0 comments on commit d6146cf

Please sign in to comment.