Skip to content

Commit

Permalink
Deprecating oracleTempSchema argument in various functions in favor…
Browse files Browse the repository at this point in the history
… of `tempEmulationSchema` schema, which can also be set globally using the `sqlRenderTempEmulationSchema` option.
  • Loading branch information
Schuemie authored and Schuemie committed Oct 21, 2020
1 parent 7ad0908 commit 8080559
Show file tree
Hide file tree
Showing 16 changed files with 372 additions and 255 deletions.
2 changes: 1 addition & 1 deletion .classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk6"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: SqlRender
Type: Package
Title: Rendering Parameterized SQL and Translation to Dialects
Version: 1.6.9
Date: 2020-09-26
Version: 1.7.0
Date: 2020-10-21
Authors@R: c(
person("Martijn", "Schuemie", , "[email protected]", role = c("aut", "cre")),
person("Marc", "Suchard", role = c("aut"))
Expand All @@ -16,7 +16,8 @@ VignetteBuilder: knitr
URL: https://ohdsi.github.io/SqlRender, https://github.com/OHDSI/SqlRender
BugReports: https://github.com/OHDSI/SqlRender/issues
Imports:
rJava
rJava,
rlang
Suggests:
testthat,
knitr,
Expand Down
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
SqlRender 1.6.9
SqlRender 1.7.0
===============

Changes:

1. Deprecating `oracleTempSchema` argument in various functions in favor of `tempEmulationSchema` schema, which can also be set globally using the `sqlRenderTempEmulationSchema` option.

Bugfixes:

1. Fixed translation of CTE without FROM or UNION in BigQuery.
Expand Down
34 changes: 25 additions & 9 deletions R/HelperFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,12 @@ renderSqlFile <- function(sourceFile, targetFile, warnOnMissingParameters = TRUE
#'
#' @param sourceFile The source SQL file
#' @param targetFile The target SQL file
#' @param targetDialect The target dialect. Currently 'oracle', 'postgresql', and 'redshift' are
#' supported
#' @param oracleTempSchema A schema that can be used to create temp tables in when using Oracle.
#' @param targetDialect The target dialect. Currently "oracle", "postgresql", "pdw", "impala", "sqlite", "netezza", "bigquery", and
#' "redshift" are supported.
#' @param oracleTempSchema DEPRECATED: use \code{tempEmulationSchema} instead.
#' @param tempEmulationSchema Some database platforms like Oracle and Impala do not truly support temp tables. To
#' emulate temp tables, provide a schema with write privileges where temp tables
#' can be created.
#'
#' @examples
#' \dontrun{
Expand All @@ -125,9 +128,14 @@ renderSqlFile <- function(sourceFile, targetFile, warnOnMissingParameters = TRUE
translateSqlFile <- function(sourceFile,
targetFile,
targetDialect,
tempEmulationSchema = getOption("sqlRenderTempEmulationSchema"),
oracleTempSchema = NULL) {
if (!is.null(oracleTempSchema) && oracleTempSchema != "") {
rlang::warn("The 'oracleTempSchema' argument is deprecated. Use 'tempEmulationSchema' instead.", .frequency = "regularly", .frequency_id = "oracleTempSchema")
tempEmulationSchema <- oracleTempSchema
}
sql <- readSql(sourceFile)
sql <- translate(sql = sql, targetDialect = targetDialect, oracleTempSchema = oracleTempSchema)
sql <- translate(sql = sql, targetDialect = targetDialect, tempEmulationSchema = tempEmulationSchema)
writeSql(sql, targetFile)
}

Expand All @@ -150,7 +158,10 @@ translateSqlFile <- function(sourceFile,
#' @param dbms The target dialect. Currently 'sql server', 'oracle', 'postgres', and
#' 'redshift' are supported
#' @param ... Parameter values used for \code{render}
#' @param oracleTempSchema A schema that can be used to create temp tables in when using Oracle.
#' @param oracleTempSchema DEPRECATED: use \code{tempEmulationSchema} instead.
#' @param tempEmulationSchema Some database platforms like Oracle and Impala do not truly support temp tables. To
#' emulate temp tables, provide a schema with write privileges where temp tables
#' can be created.
#' @param warnOnMissingParameters Should a warning be raised when parameters provided to this function
#' do not appear in the parameterized SQL that is being rendered? By default, this is TRUE.
#'
Expand All @@ -168,8 +179,13 @@ loadRenderTranslateSql <- function(sqlFilename,
packageName,
dbms = "sql server",
...,
tempEmulationSchema = getOption("sqlRenderTempEmulationSchema"),
oracleTempSchema = NULL,
warnOnMissingParameters = TRUE) {
if (!is.null(oracleTempSchema) && oracleTempSchema != "") {
rlang::warn("The 'oracleTempSchema' argument is deprecated. Use 'tempEmulationSchema' instead.", .frequency = "regularly", .frequency_id = "oracleTempSchema")
tempEmulationSchema <- oracleTempSchema
}
pathToSql <- system.file(paste("sql/", gsub(" ", "_", dbms), sep = ""),
sqlFilename,
package = packageName)
Expand All @@ -188,7 +204,7 @@ loadRenderTranslateSql <- function(sqlFilename,
renderedSql <- render(sql = parameterizedSql[1], warnOnMissingParameters = warnOnMissingParameters, ...)

if (mustTranslate)
renderedSql <- translate(sql = renderedSql, targetDialect = dbms, oracleTempSchema = oracleTempSchema)
renderedSql <- translate(sql = renderedSql, targetDialect = dbms, tempEmulationSchema = tempEmulationSchema)

renderedSql
}
Expand Down Expand Up @@ -354,7 +370,7 @@ createRWrapperForSql <- function(sqlFilename,
"#' @param connectionDetails\t\tAn R object of type \\code{ConnectionDetails} created using the function \\code{createConnectionDetails} in the \\code{DatabaseConnector} package.")
if (hasTempTables)
lines <- c(lines,
"#' @param oracleTempSchema\t\tA schema where temp tables can be created in Oracle.")
"#' @param tempEmulationSchema\t\tSome database platforms like Oracle and Impala do not truly support temp tables. To emulate temp tables, provide a schema with write privileges where temp tables can be created.")
for (i in 1:nrow(functionDefinitions)) {
lines <- c(lines, paste("#' @param", functionDefinitions$rParameter[i], "\t\t"))
}
Expand All @@ -364,7 +380,7 @@ createRWrapperForSql <- function(sqlFilename,
lines <- c(lines,
paste(gsub(".sql", "", sqlFilename), " <- function(connectionDetails,", sep = ""))
if (hasTempTables)
lines <- c(lines, " oracleTempSchema = NULL,")
lines <- c(lines, " tempEmulationSchema = getOption(\"sqlRenderTempEmulationSchema\"),")
for (i in 1:nrow(functionDefinitions)) {
if (i == nrow(functionDefinitions))
end <- ") {" else end <- ","
Expand All @@ -389,7 +405,7 @@ createRWrapperForSql <- function(sqlFilename,
lines <- c(lines, paste(" packageName = \"", packageName, "\",", sep = ""))
lines <- c(lines, " dbms = connectionDetails$dbms,")
if (hasTempTables)
lines <- c(lines, " oracleTempSchema = oracleTempSchema,")
lines <- c(lines, " tempEmulationSchema = tempEmulationSchema,")
for (i in 1:nrow(definitions)) {
if (i == nrow(definitions))
end <- ")" else end <- ","
Expand Down
44 changes: 30 additions & 14 deletions R/RenderSql.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,12 @@ renderSql <- function(sql = "", warnOnMissingParameters = TRUE, ...) {
#' \code{\link{splitSql}}.
#'
#' @param sql The SQL to be translated
#' @param targetDialect The target dialect. Currently "oracle", "postgresql", "pdw", "impala", "netezza", "bigquery", and
#' "redshift" are supported
#' @param oracleTempSchema A schema that can be used to create temp tables in when using Oracle or Impala.
#' @param targetDialect The target dialect. Currently "oracle", "postgresql", "pdw", "impala", "sqlite", "netezza", "bigquery", and
#' "redshift" are supported.
#' @param oracleTempSchema DEPRECATED: use \code{tempEmulationSchema} instead.
#' @param tempEmulationSchema Some database platforms like Oracle and Impala do not truly support temp tables. To
#' emulate temp tables, provide a schema with write privileges where temp tables
#' can be created.
#' @return
#' A character string containing the translated SQL.
#'
Expand All @@ -131,12 +134,17 @@ renderSql <- function(sql = "", warnOnMissingParameters = TRUE, ...) {
#' @export
translate <- function(sql = "",
targetDialect,
tempEmulationSchema = getOption("sqlRenderTempEmulationSchema"),
oracleTempSchema = NULL) {
if (!is.null(oracleTempSchema) && oracleTempSchema != "") {
rlang::warn("The 'oracleTempSchema' argument is deprecated. Use 'tempEmulationSchema' instead.", .frequency = "regularly", .frequency_id = "oracleTempSchema")
tempEmulationSchema <- oracleTempSchema
}
pathToReplacementPatterns <- system.file("csv", "replacementPatterns.csv", package = "SqlRender")
if (missing(oracleTempSchema) || is.null(oracleTempSchema)) {
oracleTempSchema <- rJava::.jnull()
if (missing(tempEmulationSchema) || is.null(tempEmulationSchema)) {
tempEmulationSchema <- rJava::.jnull()
} else {
oracleTempSchema <- as.character(oracleTempSchema)
tempEmulationSchema <- as.character(tempEmulationSchema)
}
messages <- rJava::J("org.ohdsi.sql.SqlTranslate")$check(as.character(sql),
as.character(targetDialect))
Expand All @@ -146,7 +154,7 @@ translate <- function(sql = "",
translatedSql <- rJava::J("org.ohdsi.sql.SqlTranslate")$translateSqlWithPath(as.character(sql),
as.character(targetDialect),
rJava::.jnull(),
oracleTempSchema,
tempEmulationSchema,
as.character(pathToReplacementPatterns))
return(translatedSql)
}
Expand Down Expand Up @@ -191,9 +199,12 @@ translateSql <- function(sql = "",
#' in the SQL.
#'
#' @param sql The SQL to be translated
#' @param targetDialect The target dialect. Currently "oracle", "postgresql", "pdw", "impala", "netezza", "bigquery", and
#' "redshift" are supported
#' @param oracleTempSchema A schema that can be used to create temp tables in when using Oracle or Impala.
#' @param targetDialect The target dialect. Currently "oracle", "postgresql", "pdw", "impala", "sqlite", "netezza", "bigquery", and
#' "redshift" are supported.
#' @param oracleTempSchema DEPRECATED: use \code{tempEmulationSchema} instead.
#' @param tempEmulationSchema Some database platforms like Oracle and Impala do not truly support temp tables. To
#' emulate temp tables, provide a schema with write privileges where temp tables
#' can be created.
#' @return
#' A character vector with the translated SQL.
#' @examples
Expand All @@ -202,12 +213,17 @@ translateSql <- function(sql = "",
#' @export
translateSingleStatement <- function(sql = "",
targetDialect,
tempEmulationSchema = getOption("sqlRenderTempEmulationSchema"),
oracleTempSchema = NULL) {
if (!is.null(oracleTempSchema) && oracleTempSchema != "") {
rlang::warn("The 'oracleTempSchema' argument is deprecated. Use 'tempEmulationSchema' instead.", .frequency = "regularly", .frequency_id = "oracleTempSchema")
tempEmulationSchema <- oracleTempSchema
}
pathToReplacementPatterns <- system.file("csv", "replacementPatterns.csv", package = "SqlRender")
if (missing(oracleTempSchema) || is.null(oracleTempSchema)) {
oracleTempSchema <- rJava::.jnull()
if (missing(tempEmulationSchema) || is.null(tempEmulationSchema)) {
tempEmulationSchema <- rJava::.jnull()
} else {
oracleTempSchema <- as.character(oracleTempSchema)
tempEmulationSchema <- as.character(tempEmulationSchema)
}
messages <- rJava::J("org.ohdsi.sql.SqlTranslate")$check(as.character(sql),
as.character(targetDialect))
Expand All @@ -217,7 +233,7 @@ translateSingleStatement <- function(sql = "",
translatedSql <- rJava::J("org.ohdsi.sql.SqlTranslate")$translateSingleStatementSqlWithPath(as.character(sql),
as.character(targetDialect),
rJava::.jnull(),
oracleTempSchema,
tempEmulationSchema,
as.character(pathToReplacementPatterns))
return(translatedSql)
}
Expand Down
Binary file modified inst/java/SqlRender.jar
Binary file not shown.
43 changes: 18 additions & 25 deletions inst/shinyApps/SqlDeveloper/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,25 @@ shinyServer(function(input, output, session) {
})

output$target <- renderText({
# if (!input$continuous && (input$renderTranslate == cache$clicks)) {
# return(cache$target)
# } else {
# print(paste("continuous: ", input$continuous, ", renderTranslate: ", input$renderTranslate))
parameterValues <- list()
for (param in parameters()) {
value <- input[[param]]
if (!is.null(value)) {
parameterValues[[param]] <- value
}
}
sql <- do.call("render", append(input$source, parameterValues))
warningString <- c()
handleWarning <- function(e) {
output$warnings <- e$message
parameterValues <- list()
for (param in parameters()) {
value <- input[[param]]
if (!is.null(value)) {
parameterValues[[param]] <- value
}
oracleTempSchema <- input$oracleTempSchema
if (oracleTempSchema == "")
oracleTempSchema <- NULL
sql <- withCallingHandlers(suppressWarnings(translate(sql, targetDialect = tolower(input$dialect), oracleTempSchema = oracleTempSchema)), warning = handleWarning)
if (!is.null(warningString))
output$warnings <- warningString
# cache$target <- sql
# cache$clicks <- input$renderTranslate
return(sql)
# }
}
sql <- do.call("render", append(input$source, parameterValues))
warningString <- c()
handleWarning <- function(e) {
output$warnings <- e$message
}
tempEmulationSchema <- input$tempEmulationSchema
if (tempEmulationSchema == "")
tempEmulationSchema <- NULL
sql <- withCallingHandlers(suppressWarnings(translate(sql, targetDialect = tolower(input$dialect), tempEmulationSchema = tempEmulationSchema)), warning = handleWarning)
if (!is.null(warningString))
output$warnings <- warningString
return(sql)
})

output$parameterInputs <- renderUI({
Expand Down
4 changes: 2 additions & 2 deletions inst/shinyApps/SqlDeveloper/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ dashboardPage(
box(background = "light-blue",
h4("Target dialect"), width = NULL,
selectInput("dialect", NULL, choices = c("BigQuery", "Impala", "Netezza", "Oracle", "PDW", "PostgreSQL", "RedShift", "SQL Server", "SQLite", "Hive"), selected = "SQL Server"),
h4("Oracle temp schema"),
textInput("oracleTempSchema", NULL),
h4("Temp emulation schema"),
textInput("tempEmulationSchema", NULL),
h4("Parameters"),
uiOutput("parameterInputs"),
textOutput("warnings")
Expand Down
12 changes: 6 additions & 6 deletions java/org/ohdsi/sql/MainClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void main(String[] args) {
List<String> parameters = new ArrayList<String>();
List<String> values = new ArrayList<String>();
for (int j = i + 1; j < args.length - 1; j += 2) {
if (args[j].equals("-translate") || args[j].equals("-oracle_temp_schema") || args[j].equals("-session_id"))
if (args[j].equals("-translate") || args[j].equals("-temp_emulation_schema") || args[j].equals("-session_id"))
break;
parameters.add(args[j]);
values.add(args[j + 1]);
Expand All @@ -52,10 +52,10 @@ public static void main(String[] args) {
}

// Translate
String oracleTempSchema = null;
String tempEmulationSchema = null;
for (int i = 2; i < args.length - 1; i++)
if (args[i].equals("-oracle_temp_schema")) {
oracleTempSchema = args[i + 1];
if (args[i].equals("-temp_emulation_schema")) {
tempEmulationSchema = args[i + 1];
break;
}
String sessionId = null;
Expand All @@ -68,7 +68,7 @@ public static void main(String[] args) {
for (int i = 2; i < args.length - 1; i++)
if (args[i].equals("-translate")) {
String targetDialect = args[i + 1];
sql = SqlTranslate.translateSql(sql, targetDialect, sessionId, oracleTempSchema);
sql = SqlTranslate.translateSql(sql, targetDialect, sessionId, tempEmulationSchema);
}
writeFile(sql, args[1]);
}
Expand All @@ -83,7 +83,7 @@ private static void printUsage() {
System.out.println("Options");
System.out.println(" -render {<name> <value>} ... Render the SQL with a list of parameter name-value pairs");
System.out.println(" -translate <target dialect> Translate the input SQL to the target dialect");
System.out.println(" -oracle_temp_schema <schema> When translating to Oracle SQL, use this schema to emulate temp tables");
System.out.println(" -temp_emulation_schema <schema> When translating to platforms that don't support tempt table, use this schema to emulate temp tables");
System.out.println(" -session_id <session id> When translating to Oracle SQL, use this ID to make emulated temp table names unique. Should be 8 chars long");
System.out.println("");
System.out.println("Examples");
Expand Down
Loading

0 comments on commit 8080559

Please sign in to comment.