From 3d8f2a80d7f6089c2882729ca4de46c213aba99e Mon Sep 17 00:00:00 2001 From: Igor Manturov Jr Date: Sat, 15 Oct 2016 04:26:43 +0600 Subject: [PATCH 1/3] Support postgresql RETURNING clause Support postgresql RETURNING clause in INSERT, UPDATE, DELETE statements --- src/ejabberd_sql.erl | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/ejabberd_sql.erl b/src/ejabberd_sql.erl index b19f164141a..2c7a7b16c83 100644 --- a/src/ejabberd_sql.erl +++ b/src/ejabberd_sql.erl @@ -806,29 +806,43 @@ pgsql_to_odbc({ok, PGSQLResult}) -> end. pgsql_item_to_odbc({<<"SELECT", _/binary>>, Rows, - Recs}) -> - {selected, [element(1, Row) || Row <- Rows], Recs}; + Recs}) -> + {selected, [element(1, Row) || Row <- Rows], Recs}; pgsql_item_to_odbc({<<"FETCH", _/binary>>, Rows, - Recs}) -> - {selected, [element(1, Row) || Row <- Rows], Recs}; + Recs}) -> + {selected, [element(1, Row) || Row <- Rows], Recs}; pgsql_item_to_odbc(<<"INSERT ", OIDN/binary>>) -> - [_OID, N] = str:tokens(OIDN, <<" ">>), - {updated, jlib:binary_to_integer(N)}; + [_OID, N] = str:tokens(OIDN, <<" ">>), + {updated, jlib:binary_to_integer(N)}; +pgsql_item_to_odbc({<<"INSERT ", OIDN/binary>>, Rows, Recs}) -> + [_OID, N] = str:tokens(OIDN, <<" ">>), + {updated, jlib:binary_to_integer(N), [element(1, Row) || Row <- Rows], Recs}; pgsql_item_to_odbc(<<"DELETE ", N/binary>>) -> - {updated, jlib:binary_to_integer(N)}; + {updated, jlib:binary_to_integer(N)}; +pgsql_item_to_odbc({<<"DELETE ", N/binary>>, Rows, Recs}) -> + {updated, jlib:binary_to_integer(N), [element(1, Row) || Row <- Rows], Recs}; pgsql_item_to_odbc(<<"UPDATE ", N/binary>>) -> - {updated, jlib:binary_to_integer(N)}; + {updated, jlib:binary_to_integer(N)}; +pgsql_item_to_odbc({<<"UPDATE ", N/binary>>, Rows, Recs}) -> + {updated, jlib:binary_to_integer(N), [element(1, Row) || Row <- Rows], Recs}; pgsql_item_to_odbc({error, Error}) -> {error, Error}; -pgsql_item_to_odbc(_) -> {updated, undefined}. +pgsql_item_to_odbc(U) -> ?INFO_MSG(U, []), {updated, undefined}. pgsql_execute_to_odbc({ok, {<<"SELECT", _/binary>>, Rows}}) -> - {selected, [], [[Field || {_, Field} <- Row] || Row <- Rows]}; + {selected, [], [[Field || {_, Field} <- Row] || Row <- Rows]}; pgsql_execute_to_odbc({ok, {'INSERT', N}}) -> - {updated, N}; + {updated, N}; +pgsql_execute_to_odbc({ok, {<<"INSERT ", OIDN/binary>>, Rows}}) -> + [_OID, N] = str:tokens(OIDN, <<" ">>), + {updated, jlib:binary_to_integer(N), [[Field || {_, Field} <- Row] || Row <- Rows]}; pgsql_execute_to_odbc({ok, {'DELETE', N}}) -> - {updated, N}; + {updated, N}; +pgsql_execute_to_odbc({ok, {<<"DELETE ", N/binary>>, Rows}}) -> + {updated, jlib:binary_to_integer(N), [[Field || {_, Field} <- Row] || Row <- Rows]}; pgsql_execute_to_odbc({ok, {'UPDATE', N}}) -> - {updated, N}; + {updated, N}; +pgsql_execute_to_odbc({ok, {<<"UPDATE ", N/binary>>, Rows}}) -> + {updated, jlib:binary_to_integer(N), [[Field || {_, Field} <- Row] || Row <- Rows]}; pgsql_execute_to_odbc({error, Error}) -> {error, Error}; pgsql_execute_to_odbc(_) -> {updated, undefined}. From 6f07b0813b6b7e74966ff7bd458e2f544f3b2155 Mon Sep 17 00:00:00 2001 From: Igor Manturov Jr Date: Sat, 15 Oct 2016 04:40:56 +0600 Subject: [PATCH 2/3] Support postgresql RETURNING clause --- src/ejabberd_sql.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ejabberd_sql.erl b/src/ejabberd_sql.erl index 2c7a7b16c83..78b731325c0 100644 --- a/src/ejabberd_sql.erl +++ b/src/ejabberd_sql.erl @@ -826,7 +826,7 @@ pgsql_item_to_odbc(<<"UPDATE ", N/binary>>) -> pgsql_item_to_odbc({<<"UPDATE ", N/binary>>, Rows, Recs}) -> {updated, jlib:binary_to_integer(N), [element(1, Row) || Row <- Rows], Recs}; pgsql_item_to_odbc({error, Error}) -> {error, Error}; -pgsql_item_to_odbc(U) -> ?INFO_MSG(U, []), {updated, undefined}. +pgsql_item_to_odbc(U) -> {updated, undefined}. pgsql_execute_to_odbc({ok, {<<"SELECT", _/binary>>, Rows}}) -> {selected, [], [[Field || {_, Field} <- Row] || Row <- Rows]}; From 7f329f20f03c2db1a180c2f3342e15d3124135c7 Mon Sep 17 00:00:00 2001 From: Igor Manturov Jr Date: Sat, 15 Oct 2016 04:41:43 +0600 Subject: [PATCH 3/3] Support postgresql RETURNING clause --- src/ejabberd_sql.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ejabberd_sql.erl b/src/ejabberd_sql.erl index 78b731325c0..d0e4bef98fa 100644 --- a/src/ejabberd_sql.erl +++ b/src/ejabberd_sql.erl @@ -826,7 +826,7 @@ pgsql_item_to_odbc(<<"UPDATE ", N/binary>>) -> pgsql_item_to_odbc({<<"UPDATE ", N/binary>>, Rows, Recs}) -> {updated, jlib:binary_to_integer(N), [element(1, Row) || Row <- Rows], Recs}; pgsql_item_to_odbc({error, Error}) -> {error, Error}; -pgsql_item_to_odbc(U) -> {updated, undefined}. +pgsql_item_to_odbc(_) -> {updated, undefined}. pgsql_execute_to_odbc({ok, {<<"SELECT", _/binary>>, Rows}}) -> {selected, [], [[Field || {_, Field} <- Row] || Row <- Rows]};