consolidate write functions into one file. Add execute function.

This commit is contained in:
Adam Black 2021-08-18 23:32:31 -04:00
parent 867e9bb559
commit 08bf6ee17e
7 changed files with 190 additions and 129 deletions

View File

@ -1,4 +1,3 @@
# Copyright 2019 Observational Health Data Sciences and Informatics # Copyright 2019 Observational Health Data Sciences and Informatics
# #
# This file is part of CdmDdlBase # This file is part of CdmDdlBase
@ -46,7 +45,7 @@ createDdl <- function(cdmVersion){
tableList <- tableSpecs$cdmTableName tableList <- tableSpecs$cdmTableName
sql_result <- c() sql_result <- c()
sql_result <- c(paste0("--@targetdialect CDM DDL Specification for OMOP Common Data Model ", cdmVersion)) sql_result <- c(paste0("--@targetDialect CDM DDL Specification for OMOP Common Data Model ", cdmVersion))
for (tableName in tableList){ for (tableName in tableList){
fields <- subset(cdmSpecs, cdmTableName == tableName) fields <- subset(cdmSpecs, cdmTableName == tableName)
fieldNames <- fields$cdmFieldName fieldNames <- fields$cdmFieldName
@ -92,7 +91,7 @@ createDdl <- function(cdmVersion){
} }
#' @describeIn createDdl #' @describeIn createDdl createPrimaryKeys asdfladsfj
#' @export #' @export
createPrimaryKeys <- function(cdmVersion){ createPrimaryKeys <- function(cdmVersion){
@ -105,7 +104,7 @@ createPrimaryKeys <- function(cdmVersion){
primaryKeys <- subset(cdmSpecs, isPrimaryKey == "Yes") primaryKeys <- subset(cdmSpecs, isPrimaryKey == "Yes")
pkFields <- primaryKeys$cdmFieldName pkFields <- primaryKeys$cdmFieldName
sql_result <- c(paste0("--@targetdialect CDM Primary Key Constraints for OMOP Common Data Model ", cdmVersion, "\n")) sql_result <- c(paste0("--@targetDialect CDM Primary Key Constraints for OMOP Common Data Model ", cdmVersion, "\n"))
for (pkField in pkFields){ for (pkField in pkFields){
subquery <- subset(primaryKeys, cdmFieldName==pkField) subquery <- subset(primaryKeys, cdmFieldName==pkField)
@ -116,7 +115,7 @@ createPrimaryKeys <- function(cdmVersion){
return(paste0(sql_result, collapse = "")) return(paste0(sql_result, collapse = ""))
} }
#' @describeIn createDdl #' @describeIn createDdl createForeignKeys asdfladsf
#' @export #' @export
createForeignKeys <- function(cdmVersion){ createForeignKeys <- function(cdmVersion){
@ -129,7 +128,7 @@ createForeignKeys <- function(cdmVersion){
foreignKeys <- subset(cdmSpecs, isForeignKey == "Yes") foreignKeys <- subset(cdmSpecs, isForeignKey == "Yes")
foreignKeys$key <- paste0(foreignKeys$cdmTableName, "_", foreignKeys$cdmFieldName) foreignKeys$key <- paste0(foreignKeys$cdmTableName, "_", foreignKeys$cdmFieldName)
sql_result <- c(paste0("--@targetdialect CDM Foreign Key Constraints for OMOP Common Data Model ", cdmVersion, "\n")) sql_result <- c(paste0("--@targetDialect CDM Foreign Key Constraints for OMOP Common Data Model ", cdmVersion, "\n"))
for (foreignKey in foreignKeys$key){ for (foreignKey in foreignKeys$key){
subquery <- subset(foreignKeys, foreignKeys$key==foreignKey) subquery <- subset(foreignKeys, foreignKeys$key==foreignKey)

View File

@ -11,7 +11,8 @@
#' #'
#' @examples #' @examples
#' \dontrun{ #' \dontrun{
#' downloadCurrentDdl("OHDSI/CommonDataModel", pathToCsv="Sql%20Server/OMOP%20CDM%20sql%20server%20ddl.txt") #' downloadCurrentDdl("OHDSI/CommonDataModel",
#' pathToCsv="Sql%20Server/OMOP%20CDM%20sql%20server%20ddl.txt")
#' } #' }
#' #'
#' @export #' @export

86
R/executeDdl.R Normal file
View File

@ -0,0 +1,86 @@
# Copyright 2019 Observational Health Data Sciences and Informatics
#
# This file is part of CdmDdlBase
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#' Generate and execute the DDL on a database
#'
#' This function will generate the DDL for a specific dbms and CDM version and
#' then execute the DDL on a database.
#'
#' @param connectionDetails An object of class connectionDetails as created by the DatabaseConnector::createConnectionDetails function.
#' @param cdmVersion The version of the CDM you are creating, e.g. 5.3, 5.4
#' @param cdmDatabaseSchema The schema of the CDM instance where the DDL will be run. For example, this would be "ohdsi.dbo" when testing on sql server.
#' @param executeDdl Should the DDL be executed? TRUE or FALSE
#' @param executePrimaryKey Should the primary keys be added? TRUE or FALSE
#' @param executeForeignKey Should the foreign keys be added? TRUE or FALSE
#' @param ... Other arguments passed on to DatabaseConnector::executeSql. (This allows the user to set the path to errorReportFile.)
#' @export
#'
#' @examples
#' \dontrun{
#' executeDdl(connectionDetails = connectionDetails,
#' cdmVersion = "5.4",
#' cdmDatabaseSchema = "myCdm")
#'}
executeDdl <- function(connectionDetails,
cdmVersion,
cdmDatabaseSchema,
executeDdl = TRUE,
executePrimaryKey = TRUE,
executeForeignKey = TRUE,
...) {
outputpath <- tempdir(check = TRUE)
if(executeDdl) {
filename <- writeDdl(targetDialect = connectionDetails$dbms,
cdmVersion = cdmVersion,
cdmDatabaseSchema = cdmDatabaseSchema,
outputpath = outputpath)
sql <- readr::read_file(file.path(outputpath, filename))
} else {
sql <- ""
}
if(executePrimaryKey) {
filename <- writePrimaryKeys(targetDialect = connectionDetails$dbms,
cdmVersion = cdmVersion,
cdmDatabaseSchema = cdmDatabaseSchema,
outputpath = outputpath)
sql <- paste(sql, readr::read_file(file.path(outputpath, filename)), sep = "\n")
}
if(executeForeignKey) {
filename <- writeForeignKeys(targetDialect = connectionDetails$dbms,
cdmVersion = cdmVersion,
cdmDatabaseSchema = cdmDatabaseSchema,
outputpath = outputpath)
sql <- paste(sql, readr::read_file(file.path(outputpath, filename)), sep = "\n")
}
con <- DatabaseConnector::connect(connectionDetails = connectionDetails)
DatabaseConnector::executeSql(con, sql = sql, ...)
DatabaseConnector::disconnect(con)
}

View File

@ -16,22 +16,108 @@
#' Write DDL script #' Write DDL script
#' #'
#' @param targetdialect The dialect of the target database. Choices are "oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server" #' Write the DDL to a SQL file. The SQL will be rendered (parameters replaced) and translated to the target SQL
#' @param cdmVersion The version of the CDM for which you are creating the DDL. e.g. 5.3.1 #' dialect. By default the @cdmDatabaseSchema parameter is kept in the SQL file and needs to be replaced before
#' execution.
#'
#' @param targetDialect The dialect of the target database. Choices are "oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server"
#' @param cdmVersion The version of the CDM you are creating, e.g. 5.3, 5.4
#' @param outputpath The directory or folder where the SQL file should be saved.
#' @param cdmDatabaseSchema The schema of the CDM instance where the DDL will be run. For example, this would be "ohdsi.dbo" when testing on sql server. #' @param cdmDatabaseSchema The schema of the CDM instance where the DDL will be run. For example, this would be "ohdsi.dbo" when testing on sql server.
#' Defaults to "@cdmDatabaseSchema" #' Defaults to "@cdmDatabaseSchema"
#' #'
#' @export #' @export
writeDDL <- function(targetdialect, cdmVersion, cdmDatabaseSchema = "@cdmDatabaseSchema") { writeDdl <- function(targetDialect, cdmVersion, outputpath, cdmDatabaseSchema = "@cdmDatabaseSchema") {
outputpath <- file.path("ddl", cdmVersion, targetdialect)
dir.create(outputpath, showWarnings = FALSE, recursive = TRUE) # argument checks
stopifnot(targetDialect %in% c("oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server"))
stopifnot(cdmVersion %in% listSupportedVersions())
stopifnot(is.character(cdmDatabaseSchema))
if(missing(outputpath)) {
outputpath <- file.path("ddl", cdmVersion, gsub(" ", "_", targetDialect))
}
if(!dir.exists(outputpath)) dir.create(outputpath, showWarnings = FALSE, recursive = TRUE)
sql <- createDdl(cdmVersion) sql <- createDdl(cdmVersion)
sql <- SqlRender::render(sql = sql, cdmDatabaseSchema = cdmDatabaseSchema, targetdialect = targetdialect) sql <- SqlRender::render(sql = sql, cdmDatabaseSchema = cdmDatabaseSchema, targetDialect = targetDialect)
sql <- SqlRender::translate(sql, targetDialect = targetdialect) sql <- SqlRender::translate(sql, targetDialect = targetDialect)
filename <- paste("OMOPCDM", targetdialect, cdmVersion, "ddl.sql", sep = "_") filename <- paste("OMOPCDM", gsub(" ", "_", targetDialect), cdmVersion, "ddl.sql", sep = "_")
SqlRender::writeSql(sql = sql, SqlRender::writeSql(sql = sql, targetFile = file.path(outputpath, filename))
targetFile = file.path(outputpath, filename) invisible(filename)
) }
#' @describeIn writeDdl writePrimaryKeys Write the SQL code that creates the primary keys to a file.
#' @export
writePrimaryKeys <- function(targetDialect, cdmVersion, outputpath, cdmDatabaseSchema = "@cdmDatabaseSchema") {
# argument checks
stopifnot(targetDialect %in% c("oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server"))
stopifnot(cdmVersion %in% listSupportedVersions())
stopifnot(is.character(cdmDatabaseSchema))
if(missing(outputpath)) {
outputpath <- file.path("ddl", cdmVersion, gsub(" ", "_", targetDialect))
}
if(!dir.exists(outputpath)) dir.create(outputpath, showWarnings = FALSE, recursive = TRUE)
sql <- createPrimaryKeys(cdmVersion)
sql <- SqlRender::render(sql = sql, cdmDatabaseSchema = cdmDatabaseSchema, targetDialect = targetDialect)
sql <- SqlRender::translate(sql, targetDialect = targetDialect)
filename <- paste("OMOPCDM", gsub(" ", "_", targetDialect), cdmVersion, "primary", "keys.sql", sep = "_")
SqlRender::writeSql(sql = sql, targetFile = file.path(outputpath, filename))
invisible(filename)
}
#' @describeIn writeDdl writeForeignKeys Write the SQL code that creates the foreign keys to a file.
#' @export
writeForeignKeys <- function(targetDialect, cdmVersion, outputpath, cdmDatabaseSchema = "@cdmDatabaseSchema") {
# argument checks
stopifnot(targetDialect %in% c("oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server"))
stopifnot(cdmVersion %in% listSupportedVersions())
stopifnot(is.character(cdmDatabaseSchema))
if(missing(outputpath)) {
outputpath <- file.path("ddl", cdmVersion, gsub(" ", "_", targetDialect))
}
if(!dir.exists(outputpath)) dir.create(outputpath, showWarnings = FALSE, recursive = TRUE)
sql <- createForeignKeys(cdmVersion)
sql <- SqlRender::render(sql = sql, cdmDatabaseSchema = cdmDatabaseSchema, targetDialect = targetDialect)
sql <- SqlRender::translate(sql, targetDialect = targetDialect)
filename <- paste("OMOPCDM", gsub(" ", "_", targetDialect), cdmVersion, "constraints.sql", sep = "_")
SqlRender::writeSql(sql = sql, targetFile = file.path(outputpath, filename))
invisible(filename)
}
#' @describeIn writeDdl writeIndex Write the rendered and translated sql that creates recommended indexes to a file.
#' @export
writeIndex <- function(targetDialect, cdmVersion, outputpath, cdmDatabaseSchema = "@cdmDatabaseSchema") {
# argument checks
stopifnot(targetDialect %in% c("oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server"))
stopifnot(cdmVersion %in% listSupportedVersions())
stopifnot(is.character(cdmDatabaseSchema))
if(missing(outputpath)) {
outputpath <- file.path("ddl", cdmVersion, gsub(" ", "_", targetDialect))
}
if(!dir.exists(outputpath)) dir.create(outputpath, showWarnings = FALSE, recursive = TRUE)
sqlFilename <- paste0("OMOP_CDM_indices_v", cdmVersion, ".sql")
sql <- readr::read_file(system.file(file.path("sql", "sql_server", sqlFilename), package = "CommonDataModel"))
sql <- SqlRender::render(sql, targetDialect = targetDialect, cdmDatabaseSchema = cdmDatabaseSchema)
sql <- SqlRender::translate(sql, targetDialect = targetDialect)
filename <- paste("OMOPCDM", gsub(" ", "_", targetDialect), cdmVersion, "indices.sql", sep = "_")
SqlRender::writeSql(sql = sql, targetFile = file.path(outputpath, filename))
invisible(filename)
} }

View File

@ -1,36 +0,0 @@
# Copyright 2017 Observational Health Data Sciences and Informatics
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#' Write constraint script
#'
#' @param targetdialect The dialect of the target database. Choices are "oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server"
#' @param cdmVersion The version of the CDM that you are creating the primary keys for
#' @param cdmDatabaseSchema The name of the schema where the cdm sits. Defaults to "@cdmDatabaseSchema"
#'
#' @export
writeForeignKeys <- function(targetdialect, cdmVersion, cdmDatabaseSchema = "@cdmDatabaseSchema") {
outputpath <- file.path("ddl", cdmVersion, targetdialect)
dir.create(outputpath, showWarnings = FALSE, recursive = TRUE)
sql <- createForeignKeys(cdmVersion)
sql <- SqlRender::render(sql = sql, cdmDatabaseSchema = cdmDatabaseSchema, targetdialect = targetdialect)
sql <- SqlRender::translate(sql, targetDialect = targetdialect)
filename <- paste("OMOPCDM", targetdialect, cdmVersion, "constraints.sql", sep = "_")
SqlRender::writeSql(sql = sql,
targetFile = file.path(outputpath, filename)
)
}

View File

@ -1,38 +0,0 @@
# Copyright 2017 Observational Health Data Sciences and Informatics
#
# This file is part of DDLGeneratr
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#' Write Index script
#'
#' @param targetdialect The dialect of the target database. Choices are "oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server"
#' @param cdmVersion The version of the CDM that you are creating the indices for. e.g. 5.3.1
#' @param cdmDatabaseSchema The name of the schema where the cdm sits. Defaults to "@cdmDatabaseSchema"
#'
#' @export
writeIndex <- function(targetdialect, cdmVersion, cdmDatabaseSchema = "@cdmDatabaseSchema") {
outputpath <- file.path("ddl", cdmVersion, targetdialect)
dir.create(outputpath, showWarnings = FALSE, recursive = TRUE)
sqlFilename <- paste0("OMOP CDM indices v", cdmVersion, ".sql")
sql <- SqlRender::loadRenderTranslateSql(sqlFilename = sqlFilename,
packageName = "CdmDdlBase",
dbms = targetdialect,
targetdialect = targetdialect,
cdmDatabaseSchema = cdmDatabaseSchema)
filename <- paste("OMOPCDM", targetdialect, cdmVersion, "indices.sql", sep = "_")
SqlRender::writeSql(sql = sql,
targetFile = file.path(outputpath, filename))
}

View File

@ -1,37 +0,0 @@
# Copyright 2017 Observational Health Data Sciences and Informatics
#
# This file is part of DDLGeneratr
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#' Write primary key script
#'
#' @param targetdialect The dialect of the target database. Choices are "oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server"
#' @param cdmVersion The version of the CDM that you are creating the primary keys for
#' @param cdmDatabaseSchema The name of the schema where the cdm sits. Defaults to "@cdmDatabaseSchema"
#'
#' @export
writePrimaryKeys <- function(targetdialect, cdmVersion, cdmDatabaseSchema = "@cdmDatabaseSchema") {
outputpath <- file.path("ddl", cdmVersion, targetdialect)
dir.create(outputpath, showWarnings = FALSE, recursive = TRUE)
sql <- createPrimaryKeys(cdmVersion)
sql <- SqlRender::render(sql = sql, cdmDatabaseSchema = cdmDatabaseSchema, targetdialect = targetdialect)
sql <- SqlRender::translate(sql, targetDialect = targetdialect)
filename <- paste("OMOPCDM", targetdialect, cdmVersion, "primary", "keys.sql", sep = "_")
SqlRender::writeSql(sql = sql,
targetFile = file.path(outputpath, filename)
)
}