-
-
Notifications
You must be signed in to change notification settings - Fork 878
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 } | ||||||
if(exists('sql.replace', where = options) ) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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).") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to take precaution here.
This is valid for other calls too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to take precaution here.
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 | ||||||
|
@@ -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) { | ||||||
|
There was a problem hiding this comment.
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 ?would be enough, wouldn't it ? If
sql.immediate
is not set then it will be NULLDid I missed something ?