diff --git a/src/mod_last.erl b/src/mod_last.erl index d76b60491..33f88e020 100644 --- a/src/mod_last.erl +++ b/src/mod_last.erl @@ -185,18 +185,12 @@ get_last(LUser, LServer, riak) -> Err end; get_last(LUser, LServer, odbc) -> - Username = ejabberd_odbc:escape(LUser), - case catch odbc_queries:get_last(LServer, Username) of - {selected, [<<"seconds">>, <<"state">>], []} -> - not_found; - {selected, [<<"seconds">>, <<"state">>], - [[STimeStamp, Status]]} -> - case catch jlib:binary_to_integer(STimeStamp) of - TimeStamp when is_integer(TimeStamp) -> - {ok, TimeStamp, Status}; - Reason -> {error, {invalid_timestamp, Reason}} - end; - Reason -> {error, {invalid_result, Reason}} + case catch odbc_queries:get_last(LServer, LUser) of + {selected, []} -> + not_found; + {selected, [{TimeStamp, Status}]} -> + {ok, TimeStamp, Status}; + Reason -> {error, {invalid_result, Reason}} end. get_last_iq(IQ, SubEl, LUser, LServer) -> @@ -260,12 +254,7 @@ store_last_info(LUser, LServer, TimeStamp, Status, last_activity_schema())}; store_last_info(LUser, LServer, TimeStamp, Status, odbc) -> - Username = ejabberd_odbc:escape(LUser), - Seconds = - ejabberd_odbc:escape(iolist_to_binary(integer_to_list(TimeStamp))), - State = ejabberd_odbc:escape(Status), - odbc_queries:set_last_t(LServer, Username, Seconds, - State). + odbc_queries:set_last_t(LServer, LUser, TimeStamp, Status). %% @spec (LUser::string(), LServer::string()) -> %% {ok, TimeStamp::integer(), Status::string()} | not_found @@ -286,8 +275,7 @@ remove_user(LUser, LServer, mnesia) -> F = fun () -> mnesia:delete({last_activity, US}) end, mnesia:transaction(F); remove_user(LUser, LServer, odbc) -> - Username = ejabberd_odbc:escape(LUser), - odbc_queries:del_last(LServer, Username); + odbc_queries:del_last(LServer, LUser); remove_user(LUser, LServer, riak) -> {atomic, ejabberd_riak:delete(last_activity, {LUser, LServer})}. diff --git a/src/odbc_queries.erl b/src/odbc_queries.erl index b6c9a36c0..283e33dfe 100644 --- a/src/odbc_queries.erl +++ b/src/odbc_queries.erl @@ -122,22 +122,22 @@ update(LServer, Table, Fields, Vals, Where) -> sql_transaction(LServer, F) -> ejabberd_odbc:sql_transaction(LServer, F). -get_last(LServer, Username) -> - ejabberd_odbc:sql_query(LServer, - [<<"select seconds, state from last where " - "username='">>, - Username, <<"'">>]). +get_last(LServer, LUser) -> + ejabberd_odbc:sql_query( + LServer, + ?SQL("select @(seconds)d, @(state)s from last" + " where username=%(LUser)s")). -set_last_t(LServer, Username, Seconds, State) -> - update(LServer, <<"last">>, - [<<"username">>, <<"seconds">>, <<"state">>], - [Username, Seconds, State], - [<<"username='">>, Username, <<"'">>]). +set_last_t(LServer, LUser, TimeStamp, Status) -> + ?SQL_UPSERT(LServer, "last", + ["!username=%(LUser)s", + "seconds=%(TimeStamp)d", + "state=%(Status)s"]). -del_last(LServer, Username) -> - ejabberd_odbc:sql_query(LServer, - [<<"delete from last where username='">>, Username, - <<"'">>]). +del_last(LServer, LUser) -> + ejabberd_odbc:sql_query( + LServer, + ?SQL("delete from last where username=%(LUser)s")). get_password(LServer, LUser) -> ejabberd_odbc:sql_query(