Erlang和Ejabberd中的典型SQL查询

时间:2014-10-31 07:12:13

标签: erlang ejabberd

我想在Erlang中编写一个典型的SQL查询方面有所帮助。我只想根据此查询阻止记录离线消息。

想要将此查询放在此函数的开头。

store_offline_msg(Host, {User, _Server}, Msgs, Len, MaxOfflineMsgs, odbc) ->
    Count = if MaxOfflineMsgs =/= infinity ->
           Len + count_offline_messages(User, Host);
           true -> 0
        end,
    if Count > MaxOfflineMsgs -> discard_warn_sender(Msgs);

Mysql表

 ID  | user         |  touser          | starttime           | end
 586 | my_gmail.com | touser_gmail.com | 2014-10-29 14:03:16 |  60

我需要的查询是检查:

如果'user'和'touser'在此表中有记录,当前时间介于starttime和startime + end(60秒)之间

请帮助我,我很感激。

1 个答案:

答案 0 :(得分:1)

以下是我如何实施并解决了我的问题。

 Check = case catch ejabberd_odbc:sql_query(
            LServer,
            ["select id from archive_disable_user"
             "  where ( ( user ='", To, "' "
             "  and tuser='",  From , "' ) "
             "  or ( user ='", From, "' "
             "  and tuser='",  To , "' ) )"
             "  and ( ( UNIX_TIMESTAMP(start) + end ) > UNIX_TIMESTAMP()); "]) of
           {selected, ["id"], Rs} -> 
               Rcheck = len(Rs),
               if 
                Rcheck == 0 ->
                  false;
                true->                  
                  true
                end;         
           _-> false
          end,       
   Check.