From 446cb485acee5364287d3dd121940b9e205927ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chmielowski?= Date: Mon, 11 Jan 2021 14:20:17 +0100 Subject: [PATCH] Don't use REPLACE for upsert when there are "-" fields. This should fix problem reported in #3476 --- src/ejabberd_sql_pt.erl | 46 +++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src/ejabberd_sql_pt.erl b/src/ejabberd_sql_pt.erl index 66b2e9c59..47f980d05 100644 --- a/src/ejabberd_sql_pt.erl +++ b/src/ejabberd_sql_pt.erl @@ -549,25 +549,35 @@ parse_upsert_field1([C | S], Acc, ParamPos, Loc) -> make_sql_upsert(Table, ParseRes, Pos) -> check_upsert(ParseRes, Pos), + HasInsertOnlyFields = lists:any( + fun({_, {false}, _}) -> true; + (_) -> false + end, ParseRes), + MySqlReplace = case HasInsertOnlyFields of + false -> + [erl_syntax:clause( + [erl_syntax:atom(mysql), erl_syntax:underscore()], + [], + [make_sql_upsert_mysql(Table, ParseRes), + erl_syntax:atom(ok)])]; + _ -> + [] + end, erl_syntax:fun_expr( - [erl_syntax:clause( - [erl_syntax:atom(pgsql), erl_syntax:variable("__Version")], - [erl_syntax:infix_expr( - erl_syntax:variable("__Version"), - erl_syntax:operator('>='), - erl_syntax:integer(90100))], - [make_sql_upsert_pgsql901(Table, ParseRes), - erl_syntax:atom(ok)]), - erl_syntax:clause( - [erl_syntax:atom(mysql), erl_syntax:underscore()], - [], - [make_sql_upsert_mysql(Table, ParseRes), - erl_syntax:atom(ok)]), - erl_syntax:clause( - [erl_syntax:underscore(), erl_syntax:underscore()], - none, - [make_sql_upsert_generic(Table, ParseRes)]) - ]). + [erl_syntax:clause( + [erl_syntax:atom(pgsql), erl_syntax:variable("__Version")], + [erl_syntax:infix_expr( + erl_syntax:variable("__Version"), + erl_syntax:operator('>='), + erl_syntax:integer(90100))], + [make_sql_upsert_pgsql901(Table, ParseRes), + erl_syntax:atom(ok)])] ++ + MySqlReplace ++ + [erl_syntax:clause( + [erl_syntax:underscore(), erl_syntax:underscore()], + none, + [make_sql_upsert_generic(Table, ParseRes)]) + ]). make_sql_upsert_generic(Table, ParseRes) -> Update = make_sql_query(make_sql_upsert_update(Table, ParseRes)),