OMOP/R/createDdl.R

142 lines
5.7 KiB
R
Raw Normal View History

# 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.
#' Create the OHDSI-SQL Common Data Model DDL code
#'
#' The createDdl, createForeignKeys, and createPrimaryKeys functions each return a character string
#' containing their respective DDL SQL code in OHDSQL dialect for a specific CDM version.
#' The SQL they generate needs to be rendered and translated before it can be executed.
#'
#' The DDL SQL code is created from a two csv files that detail the OMOP CDM Specifications.
#' These files also form the basis of the CDM documentation and the Data Quality
#' Dashboard.
#'
#' @param cdmVersion The version of the CDM you are creating, e.g. 5.3, 5.4
#' @return A character string containing the OHDSQL DDL
Add unit tests for all databases and DDLs (#431) * Add github actions workflow to build package and run tests. * update Description file * rename .Rproj file. * Consolidate 'create' functions into one file. * Add tests for create functions. * update description * removed spaces in file and folder names. Regenerated ddl output. Tried to fix Field_Level.csv file. * consolidate write functions into one file. Add execute function. * update docs * add tests for write and execute functions * update documentation * Add windows and linux runners in github actions. * update github actions * download drivers before running tests * fix small error in setup test file. * debug github actions * debug github actions * debug github actions * debug github actions * fix tiny bug * comment out execute ddl test * fix bug in test * Add execute test back in * revert accidental change in description * add print statement for debugging schema error on github actions. * Fix schema environment variable name * Add comment to github actions workflow file. * remove placeholder text in function documentation. * Rename createdDdl.R to createDdl.R * Hack-a-thon updates Closes #81, #387, #239, #412, #391, #330, #408, #365, #306, #264 * Changed bigint to integer for consistency * Updated DDLs * Add tests for redshift. Clean up test setup file. * Foreign key fixes * Add imports and update docs. * Fix bug in setup test script. * update setup file * Add tests for oracle and sql server. Move setup.R file. * fix bug in setup * debug tests on github * debug github actions * debug actions. * debug actions * debug actions. * Add missing secrets to yaml!! * debug actions * test connection on all platforms * add ddl execution * add windows and linux runners Co-authored-by: Adam Black <adam.black@odysseusinc.com> Co-authored-by: Clair Blacketer <mblacke@its.jnj.com>
2021-08-20 11:59:29 +00:00
#' @importFrom utils read.csv
#' @export
#' @examples
#' ddl <- createDdl("5.4")
#' pk <- createPrimaryKeys("5.4")
#' fk <- createForeignKeys("5.4")
createDdl <- function(cdmVersion){
# argument checks
stopifnot(is.character(cdmVersion), length(cdmVersion) == 1, cdmVersion %in% listSupportedVersions())
cdmTableCsvLoc <- system.file(file.path("csv", paste0("OMOP_CDMv", cdmVersion, "_Table_Level.csv")), package = "CommonDataModel", mustWork = TRUE)
cdmFieldCsvLoc <- system.file(file.path("csv", paste0("OMOP_CDMv", cdmVersion, "_Field_Level.csv")), package = "CommonDataModel", mustWork = TRUE)
tableSpecs <- read.csv(cdmTableCsvLoc, stringsAsFactors = FALSE)
cdmSpecs <- read.csv(cdmFieldCsvLoc, stringsAsFactors = FALSE)
tableList <- tableSpecs$cdmTableName
2021-08-18 17:02:22 +00:00
sql_result <- c()
sql_result <- c(paste0("--@targetDialect CDM DDL Specification for OMOP Common Data Model ", cdmVersion))
2021-08-18 17:02:22 +00:00
for (tableName in tableList){
fields <- subset(cdmSpecs, cdmTableName == tableName)
fieldNames <- fields$cdmFieldName
2021-08-18 17:02:22 +00:00
if ('person_id' %in% fieldNames){
query <- "\n\n--HINT DISTRIBUTE ON KEY (person_id)\n"
} else {
2021-08-18 17:02:22 +00:00
query <- "\n\n--HINT DISTRIBUTE ON RANDOM\n"
}
2021-08-18 17:47:13 +00:00
sql_result <- c(sql_result, query, paste0("CREATE TABLE @cdmDatabaseSchema.", tableName, " ("))
2021-08-18 17:02:22 +00:00
n_fields <- length(fieldNames)
for(fieldName in fieldNames) {
2021-08-18 17:02:22 +00:00
if (subset(fields, cdmFieldName == fieldName, isRequired) == "Yes") {
nullable_sql <- (" NOT NULL")
} else {
2021-08-18 17:02:22 +00:00
nullable_sql <- (" NULL")
}
2021-08-18 17:02:22 +00:00
if (fieldName == fieldNames[[n_fields]]) {
closing_sql <- (" );")
} else {
2021-08-18 17:02:22 +00:00
closing_sql <- (",")
}
2021-08-18 17:02:22 +00:00
if (fieldName=="offset") {
field <- paste0('"',fieldName,'"')
} else {
2021-08-18 17:02:22 +00:00
field <- fieldName
}
2021-08-18 17:02:22 +00:00
fieldSql <- paste0("\n\t\t\t",
field," ",
subset(fields, cdmFieldName == fieldName, cdmDatatype),
nullable_sql,
closing_sql)
sql_result <- c(sql_result, fieldSql)
}
2021-08-18 17:02:22 +00:00
sql_result <- c(sql_result, "")
}
2021-08-18 17:02:22 +00:00
return(paste0(sql_result, collapse = ""))
}
#' @describeIn createDdl createPrimaryKeys Returns a string containing the OHDSQL for creation of primary keys in the OMOP CDM.
#' @export
createPrimaryKeys <- function(cdmVersion){
# argument checks
stopifnot(is.character(cdmVersion), length(cdmVersion) == 1, cdmVersion %in% listSupportedVersions())
cdmFieldCsvLoc <- system.file(file.path("csv", paste0("OMOP_CDMv", cdmVersion, "_Field_Level.csv")), package = "CommonDataModel", mustWork = TRUE)
cdmSpecs <- read.csv(cdmFieldCsvLoc, stringsAsFactors = FALSE)
primaryKeys <- subset(cdmSpecs, isPrimaryKey == "Yes")
pkFields <- primaryKeys$cdmFieldName
sql_result <- c(paste0("--@targetDialect CDM Primary Key Constraints for OMOP Common Data Model ", cdmVersion, "\n"))
for (pkField in pkFields){
subquery <- subset(primaryKeys, cdmFieldName==pkField)
sql_result <- c(sql_result, paste0("\nALTER TABLE @cdmDatabaseSchema.", subquery$cdmTableName, " ADD CONSTRAINT xpk_", subquery$cdmTableName, " PRIMARY KEY NONCLUSTERED (", subquery$cdmFieldName , ");\n"))
}
return(paste0(sql_result, collapse = ""))
}
#' @describeIn createDdl createForeignKeys Returns a string containing the OHDSQL for creation of foreign keys in the OMOP CDM.
#' @export
createForeignKeys <- function(cdmVersion){
# argument checks
stopifnot(is.character(cdmVersion), length(cdmVersion) == 1, cdmVersion %in% listSupportedVersions())
cdmFieldCsvLoc <- system.file(file.path("csv", paste0("OMOP_CDMv", cdmVersion, "_Field_Level.csv")), package = "CommonDataModel", mustWork = TRUE)
cdmSpecs <- read.csv(cdmFieldCsvLoc, stringsAsFactors = FALSE)
foreignKeys <- subset(cdmSpecs, isForeignKey == "Yes")
foreignKeys$key <- paste0(foreignKeys$cdmTableName, "_", foreignKeys$cdmFieldName)
sql_result <- c(paste0("--@targetDialect CDM Foreign Key Constraints for OMOP Common Data Model ", cdmVersion, "\n"))
for (foreignKey in foreignKeys$key){
subquery <- subset(foreignKeys, foreignKeys$key==foreignKey)
sql_result <- c(sql_result, paste0("\nALTER TABLE @cdmDatabaseSchema.", subquery$cdmTableName, " ADD CONSTRAINT fpk_", subquery$cdmTableName, "_", subquery$cdmFieldName, " FOREIGN KEY (", subquery$cdmFieldName , ") REFERENCES @cdmDatabaseSchema.", subquery$fkTableName, " (", subquery$fkFieldName, ");\n"))
}
return(paste0(sql_result, collapse = ""))
}