0
votes

I'm writing a module in ejabberd using hooks. In it, I fire a SQL query as follow:-

?INFO_MSG("Packet Type:- `~p` ~n ~n ~n", [sql_queries:get_privacy_list_names_t("praful")]);

sql_queries is another module in ejabberd and get_privacy_list_names_t is the function in it.

get_privacy_list_names_t(LUser) ->
    ejabberd_sql:sql_query_t(
      ?SQL("select @(name)s from privacy_list"
           " where username=%(LUser)s")).

It returns an error saying:-

11:50:34.790 [error] Internal error while processing SQL query: {error,{badrecord,state},[{ejabberd_sql,sql_query_internal,1,[{file,"src/ejabberd_sql.erl"},{line,526}]},{ejabberd_sql,sql_query_t,1,[{file,"src/ejabberd_sql.erl"},{line,180}]},{mod_sunshine,user_receive_packet,1,[{file,"src/mod_sunshine.erl"},{line,27}]},{ejabberd_hooks,safe_apply,4,[{file,"src/ejabberd_hooks.erl"},{line,380}]},{ejabberd_hooks,run_fold1,4,[{file,"src/ejabberd_hooks.erl"},{line,364}]},{ejabberd_c2s,process_info,2,[{file,"src/ejabberd_c2s.erl"},{line,231}]},{ejabberd_hooks,safe_apply,4,[{file,"src/ejabberd_hooks.erl"},{line,380}]},{ejabberd_hooks,run_fold1,4,[{file,"src/ejabberd_hooks.erl"},{line,364}]}]}

What exactly is the error?

It says, badrecord. What is this? I couldnt find such exception in erlang !!

1
On line 526 of ejabberd_sql.erl, the code tries to access a record field in a variable, but that variable doesn't actually contain a state record. (The error message doesn't say what it actually contained.) - legoscia
Sorry, didnt get you. Is this what you are referring? github.com/processone/ejabberd/blob/master/src/… - PythonEnthusiast
Doesn't look like the exact same version, so the line numbers are off. I guess the error happens on a line like case State#state.db_type of - it tries to access the db_type field in the state record contained in the variable State, but if State doesn't actually contain a state record, you'll get a badrecord error. (As for why State has the wrong value, I have no idea, I'm afraid.) - legoscia
It looks like you should call sql_queries:get_privacy_list_names instead of sql_queries:get_privacy_list_names_t, as the functions with the _t suffix should only be called from within an SQL transaction. - legoscia
What do you mean by within a transaction? Didnt get you? - PythonEnthusiast

1 Answers

1
votes

As mentioned by @legoscia, sql_queries:get_privacy_list_names instead of sql_queries:get_privacy_list_names_t, as the functions with the _t suffix should only be called from within an SQL transaction.

functionname(LServer, Param) ->
    ejabberd_sql:sql_query(
        LServer,
        ?SQL("select @(column)s from table "
             "where column=%(Param)s")).