processone / ejabberd

Robust, Ubiquitous and Massively Scalable Messaging Platform (XMPP, MQTT, SIP Server)
https://www.process-one.net/en/ejabberd/
Other
6.01k stars 1.5k forks source link

Why ejabberd_sql does not support dynamic table name insertion #4084

Open skrleo opened 10 months ago

skrleo commented 10 months ago

I tried spool sub-table processing, and found that ejabberd_sql can't? SQL_INSERT, the following is my code

insert_into_spool({LUser, LServer, LStatus, XML}) ->
    {Year, Month, _} = erlang:timestamp(),
    TableName = lists:concat(["spool_", integer_to_list(Year), "_", integer_to_list(Month)]),
    ?INFO_MSG("TableName == ~s", [TableName]),
    case ejabberd_sql:sql_query(
        LServer,
        ?SQL_INSERT(
        TableName,
        ["username=%(LUser)s",
        "server_host=%(LServer)s",
            "status=%(LStatus)s",
        "xml=%(XML)s"])) of
    {updated, _} ->
        ok;
    _ ->
        {error, db_failure}
    end.
prefiks commented 10 months ago

Those ?SQL* macros are converted to erlang code, that's why they only accept literals - this needs to be expanded to code, before rest of that code is run. You will probably need to switch to using literal string in this case.