Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added options immedate & replace to eng_SQL #2128

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions R/engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -608,13 +608,26 @@ eng_sql = function(options) {
max.print = -1
sql = one_string(options$code)
params = options$params
immediate = NULL
if(exists("sql.immediate", where = options) ) { immediate = options$sql.immediate }
Comment on lines +611 to +612
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need exist here ?

Suggested change
immediate = NULL
if(exists("sql.immediate", where = options) ) { immediate = options$sql.immediate }
immediate = options$sql.immediate

would be enough, wouldn't it ? If sql.immediate is not set then it will be NULL

options = list()
options$sql.immediate
#> NULL

Did I missed something ?

if(exists('sql.replace', where = options) ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. This is not a usual pattern. I am not sure I see why we can't retrieve the options and then check ?

immediate = options$sql.immediate
replace = options$sql.replace

if (isTRUE(replace) && isTRUE(immediate)) {
 do_somethings
}
... 

What did I miss ?

if(!isTRUE(immediate)) knitr:::stop2("To replace a temprary table, option sql.immediate has to be set to TRUE).")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(!isTRUE(immediate)) knitr:::stop2("To replace a temprary table, option sql.immediate has to be set to TRUE).")
if(!isTRUE(immediate)) knitr:::stop2("To replace a temporary table, option sql.immediate has to be set to TRUE).")

replace = options$sql.replace
if (isTRUE(immediate) && isTRUE(replace)) {
reptable = gsub('(^.*into[[:space:]]+)(#[0-9A-Za-z]+)(.+from.*$)', '\\2', sql, ignore.case = T)
if(!sub('(.).*.$', '\\1', reptable) == '#') knitr:::stop2(
"To replace a table, the table has to be a temporary table (tablename staring with # or ##)."
)
sql <- paste0("if object_id('tempdb.dbo.", reptable, "') is not null drop table ", reptable, " ;", sql)
}
cderv marked this conversation as resolved.
Show resolved Hide resolved
}

query = interpolate_from_env(conn, sql)
if (isFALSE(options$eval)) return(engine_output(options, query, ''))

data = tryCatch({
if (is_sql_update_query(query)) {
DBI::dbExecute(conn, query)
DBI::dbExecute(conn, query, immediate = immediate)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to take precaution here.
DBI::dbExecute() is a generic and immediate argument, is indeed among the specifications. However, immediate is not part of the generic argument to improve compatibility with the different backends.
https://dbi.r-dbi.org/reference/dbexecute#additional-arguments-1

The following arguments are not part of the dbExecute() generic (to improve compatibility across backends) but are part of the DBI specification:

  • params (default: NULL)
  • immediate (default: NULL)

They must be provided as named arguments. See the "Specification" sections for details on their usage

This is valid for other calls too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to take precaution here.
DBI::dbExecute() is a generic and immediate argument, is indeed among the specifications. However, immediate is not part of the generic argument to improve compatibility with the different backends.
https://dbi.r-dbi.org/reference/dbexecute#additional-arguments-1

The following arguments are not part of the dbExecute() generic (to improve compatibility across backends) but are part of the DBI specification:

  • params (default: NULL)
  • immediate (default: NULL)

They must be provided as named arguments. See the "Specification" sections for details on their usage

This is valid for other calls too.

NULL
} else if (is.null(varname) && max.print > 0) {
# execute query -- when we are printing with an enforced max.print we
Expand All @@ -623,13 +636,12 @@ eng_sql = function(options) {
data = DBI::dbFetch(res, n = max.print)
DBI::dbClearResult(res)
data

} else {
if (length(params) == 0) {
DBI::dbGetQuery(conn, query)
DBI::dbGetQuery(conn, query, immediate = immediate)
} else {
# If params option is provided, parameters are not interplolated
DBI::dbGetQuery(conn, sql, params = params)
DBI::dbGetQuery(conn, query, immediate = immediate, params = params)
}
}
}, error = function(e) {
Expand Down