From f78e1c60ee00920aa48d03a3d166f2b6e3947435 Mon Sep 17 00:00:00 2001 From: edalfon Date: Fri, 24 Sep 2021 11:18:42 +0200 Subject: [PATCH 1/3] Update engine.R also return data ("scalar numeric that specifies the number of rows affected") in case of an is_sql_update_query, and not just SELECT-like queries --- R/engine.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/engine.R b/R/engine.R index b662d85e3a..13299c7204 100644 --- a/R/engine.R +++ b/R/engine.R @@ -555,8 +555,8 @@ eng_sql = function(options) { data = tryCatch({ if (is_sql_update_query(query)) { - DBI::dbExecute(conn, query) - NULL + data = DBI::dbExecute(conn, query) + data } else if (is.null(varname) && max.print > 0) { # execute query -- when we are printing with an enforced max.print we # use dbFetch so as to only pull down the required number of records From d1f8cbf5255473b3ff4f711abb54ce2a4d036b82 Mon Sep 17 00:00:00 2001 From: edalfon Date: Fri, 24 Sep 2021 11:22:22 +0200 Subject: [PATCH 2/3] Update engine.R and print by default a message with number of rows affected --- R/engine.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/R/engine.R b/R/engine.R index 13299c7204..eda760304c 100644 --- a/R/engine.R +++ b/R/engine.R @@ -633,6 +633,11 @@ eng_sql = function(options) { } else print(display_data) # fallback to standard print }) + if (is.numeric(data) && length(data) == 1 && is.null(varname)) { + options$results = 'asis' + # format = "fg" instead of "d". Row counts on DB may be greater than integer max value + output = paste0("Number of affected rows: ", formatC(data, format = "fg", big.mark = ',')) + } if (options$results == 'hide') output = NULL # assign varname if requested From e45a2042ec2d49d168ceda850516c5d6e2961b07 Mon Sep 17 00:00:00 2001 From: edalfon Date: Sat, 25 Sep 2021 23:43:55 +0200 Subject: [PATCH 3/3] Update NEWS.md --- NEWS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index 782f0f1844..0093ed3bc9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +- The `sql` engine now produces an output for update-like SQL queries, indicating the number of rows affected (as returned by DBI::dbExecute). So far, `knitr` produces a nicely formatted output for queries returning a result set (typically SELECT...), but gives no feedback at all for queries making changes to the DB (e.g. INSERT|UPDATE|DELETE|CREATE|DROP; see not exported function knitr:::is_sql_update_query). For such queries, `knitr` now shows the number of affected rows. You can also set the chunk option `output.var` to assign the number of affected rows to a variable. + + + # CHANGES IN knitr VERSION 1.35 ## BUG FIXES