ejabberd icon indicating copy to clipboard operation
ejabberd copied to clipboard

Why ejabberd_sql does not support dynamic table name insertion

Open skrleo opened this issue 2 years ago • 1 comments

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.

skrleo avatar Aug 31 '23 05:08 skrleo

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.

prefiks avatar Aug 31 '23 10:08 prefiks