refactor create and write sql for foreign key and primary key

This commit is contained in:
Maxim Moinat 2021-08-18 19:28:21 +02:00
parent f8cd93d159
commit 5d9887a356
6 changed files with 53 additions and 126 deletions

View File

@ -16,31 +16,23 @@
#' Create a foreign key sql script from two csv files that detail the OMOP CDM Specifications.
#' @param cdmVersionNum The version of the CDM you are creating
#'
#' @param cdmFieldCsvLoc The location of the csv file with the CDM field information. This is defaulted to "inst/csv/OMOP_CDMv5.3.1_Field_Level.csv".
#' If a new version of this file was committed to the CDM repository the package automatically will grab it and place it in "inst/csv/".
#' @param outputFile The name of the output foreign key file. This is defaulted to a location in the inst/sql/sql server folder and named with today's date and CDM version.
#' @param cdmVersionNum The version of the CDM you are creating, e.g. 5.3.1
#' @export
createFkFromFile <- function(cdmVersionNum = cdmVersion,
cdmFieldCsvLoc = "inst/csv/OMOP_CDMv5.3.1_Field_Level.csv",
outputFile = paste0("inst/sql/sql_server/OMOP CDM fk ", cdmVersion, " ", Sys.Date(), ".sql")){
createFkFromFile <- function(cdmVersion = cdmVersion){
cdmFieldCsvLoc <- paste0("inst/csv/OMOP_CDMv", cdmVersion, "_Field_Level.csv")
cdmSpecs <- read.csv(cdmFieldCsvLoc, stringsAsFactors = FALSE)
fks <- subset(cdmSpecs, isForeignKey == "Yes")
fks$key <- paste0(fks$cdmTableName,"_",fks$cdmFieldName)
foreignKeys <- subset(cdmSpecs, isForeignKey == "Yes")
foreignKeys$key <- paste0(foreignKeys$cdmTableName, "_", foreignKeys$cdmFieldName)
s <- c()
s <- c(paste0("--@targetdialect CDM Foreign Key Constraints for OMOP Common Data Model ",cdmVersionNum, "\n"))
for (t in fks$key){
sql_result <- c(paste0("--@targetdialect CDM Foreign Key Constraints for OMOP Common Data Model ", cdmVersion, "\n"))
for (foreignKey in foreignKeys$key){
q <- subset(fks, fks$key==t)
subquery <- subset(foreignKeys, foreignKeys$key==foreignKey)
s <- c(s, paste0("\nALTER TABLE @cdmDatabaseSchema.", q$cdmTableName, " ADD CONSTRAINT fpk_", q$cdmTableName, "_", q$cdmFieldName, " FOREIGN KEY (", q$cdmFieldName ,") REFERENCES @cdmDatabaseSchema.", q$fkTableName, " (", q$fkFieldName, ");\n"))
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"))
}
SqlRender::writeSql(s, targetFile = outputFile)
return(s)
return(paste0(sql_result, collapse = ""))
}

View File

@ -16,32 +16,23 @@
#' Create a primary key sql script from two csv files that detail the OMOP CDM Specifications.
#' @param cdmVersionNum The version of the CDM you are creating
#'
#' @param cdmFieldCsvLoc The location of the csv file with the CDM field information. This is defaulted to "inst/csv/OMOP_CDMv5.3.1_Field_Level.csv".
#' If a new version of this file was committed to the CDM repository the package automatically will grab it and place it in "inst/csv/".
#' @param outputFile The name of the output primary key sql file. This is defaulted to a location in the inst/sql/sql server folder and named with today's date and CDM Version.
#' @param cdmVersionNum The version of the CDM you are creating, e.g. 5.3.1
#' @export
createPkFromFile <- function(cdmVersionNum = cdmVersion,
cdmFieldCsvLoc = "inst/csv/OMOP_CDMv5.3.1_Field_Level.csv",
outputFile = paste0("inst/sql/sql_server/OMOP CDM pk ", cdmVersion, " ", Sys.Date(), ".sql")){
createPkFromFile <- function(cdmVersion = cdmVersion){
cdmFieldCsvLoc <- paste0("inst/csv/OMOP_CDMv", cdmVersion, "_Field_Level.csv")
cdmSpecs <- read.csv(cdmFieldCsvLoc, stringsAsFactors = FALSE)
pks <- subset(cdmSpecs, isPrimaryKey == "Yes")
primaryKeys <- subset(cdmSpecs, isPrimaryKey == "Yes")
pkFields <- primaryKeys$cdmFieldName
pkFields <- pks$cdmFieldName
sql_result <- c(paste0("--@targetdialect CDM Primary Key Constraints for OMOP Common Data Model ", cdmVersion, "\n"))
for (pkField in pkFields){
s <- c()
s <- c(paste0("--@targetdialect CDM Primary Key Constraints for OMOP Common Data Model ",cdmVersionNum, "\n"))
for (f in pkFields){
subquery <- subset(primaryKeys, cdmFieldName==pkField)
q <- subset(pks, cdmFieldName==f)
s <- c(s, paste0("\nALTER TABLE @cdmDatabaseSchema.", q$cdmTableName, " ADD CONSTRAINT xpk_", q$cdmTableName, " PRIMARY KEY NONCLUSTERED (", q$cdmFieldName ,");\n"))
sql_result <- c(sql_result, paste0("\nALTER TABLE @cdmDatabaseSchema.", subquery$cdmTableName, " ADD CONSTRAINT xpk_", subquery$cdmTableName, " PRIMARY KEY NONCLUSTERED (", subquery$cdmFieldName , ");\n"))
}
SqlRender::writeSql(s, targetFile = outputFile)
return(s)
return(paste0(sql_result, collapse = ""))
}

View File

@ -18,27 +18,20 @@
#'
#' @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.
#' @param sqlFilename The name of the file that should be rendered and translated to a different dbms.
#' @param cdmDatabaseSchema The name of the schema where the cdm sits. Defaults to "@cdmDatabaseSchema"
#'
#' @export
writeConstraints <- function(targetdialect, cdmVersion, cdmDatabaseSchema, sqlFileName) {
if(!dir.exists("output")){
dir.create("output")
}
if(!dir.exists(paste0("output/",targetdialect))){
dir.create(paste0("output/",targetdialect))
}
sql <- SqlRender::loadRenderTranslateSql(sqlFilename = sqlFileName,
packageName = "CdmDdlBase",
dbms = targetdialect,
targetdialect = targetdialect,
cdmDatabaseSchema = cdmDatabaseSchema)
SqlRender::writeSql(sql = sql,
targetFile = paste0("output/",targetdialect,"/OMOP CDM ",targetdialect," ", cdmVersion, " constraints.sql"))
writeConstraints <- function(targetdialect, cdmVersion, cdmDatabaseSchema = "@cdmDatabaseSchema") {
outputpath <- file.path("ddl", cdmVersion, targetdialect)
dir.create(outputpath, showWarnings = FALSE, recursive = TRUE)
sql <- createFkFromFile(cdmVersion)
sql <- SqlRender::render(sql = sql, cdmDatabaseSchema = cdmDatabaseSchema)
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

@ -30,7 +30,7 @@ writeDDL <- function(targetdialect, cdmVersion, cdmDatabaseSchema = "@cdmDatabas
sql <- SqlRender::render(sql = sql, cdmDatabaseSchema = cdmDatabaseSchema)
sql <- SqlRender::translate(sql, targetDialect = targetdialect)
filename <- paste0("OMOPCDM_", targetdialect, "_", cdmVersion, "_ddl.sql")
filename <- paste("OMOPCDM", targetdialect, cdmVersion, "ddl.sql", sep = " ")
SqlRender::writeSql(sql = sql,
targetFile = file.path(outputpath, filename)
)

View File

@ -18,27 +18,20 @@
#'
#' @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.
#' @param sqlFilename The name of the file that should be rendered and translated to a different dbms.
#' @param cdmDatabaseSchema The name of the schema where the cdm sits. Defaults to "@cdmDatabaseSchema"
#'
#' @export
writePrimaryKeys <- function(targetdialect, cdmVersion, cdmDatabaseSchema, sqlFilename) {
if(!dir.exists("output")){
dir.create("output")
}
writePrimaryKeys <- function(targetdialect, cdmVersion, cdmDatabaseSchema = "@cdmDatabaseSchema") {
outputpath <- file.path("ddl", cdmVersion, targetdialect)
dir.create(outputpath, showWarnings = FALSE, recursive = TRUE)
if(!dir.exists(paste0("output/",targetdialect))){
dir.create(paste0("output/",targetdialect))
}
sql <- SqlRender::loadRenderTranslateSql(sqlFilename = sqlFilename,
packageName = "CdmDdlBase",
dbms = targetdialect,
targetdialect = targetdialect,
cdmDatabaseSchema = cdmDatabaseSchema)
sql <- createFkFromFile(cdmVersion)
sql <- SqlRender::render(sql = sql, cdmDatabaseSchema = cdmDatabaseSchema)
sql <- SqlRender::translate(sql, targetDialect = targetdialect)
filename <- paste("OMOPCDM", targetdialect, cdmVersion, "primary", "keys.sql", sep = " ")
SqlRender::writeSql(sql = sql,
targetFile = paste0("output/",targetdialect,"/OMOP CDM ",targetdialect, " ", cdmVersion, " primary keys.sql" ))
targetFile = file.path(outputpath, filename)
)
}

View File

@ -8,17 +8,15 @@
# Step 3: After creating the csv files for the new version, create the sql server DDL from the file
s <- CdmDdlBase::createDdlFromFile(cdmVersion)
ddl <- CdmDdlBase::createDdlFromFile(cdmVersion)
# Step 3.1: Create the primary key constraints for the new version
p <- CdmDdlBase::createPkFromFile(cdmVersionNum = cdmVersion,
cdmFieldCsvLoc = "inst/csv/OMOP_CDMv6.0_Field_Level.csv")
primaryKeys <- CdmDdlBase::createPkFromFile(cdmVersion)
# Step 3.2: Create the foreign key constraints for the new version
f <- CdmDdlBase::createFkFromFile(cdmVersionNum = cdmVersion,
cdmFieldCsvLoc = "inst/csv/OMOP_CDMv6.0_Field_Level.csv")
foreignKeys <- CdmDdlBase::createFkFromFile(cdmVersion)
# At this point you should rebuild the package
# Step 4: Run the following code to render the DDLs for each dialect. These will be used for testing on the ohdsi servers which is why the cdmDatabaseSchema is specified.
@ -106,55 +104,15 @@ writeIndex("sql server",
for (targetdialect in c("oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server")) {
writeDDL(targetdialect = targetdialect,
cdmVersion = cdmVersion)
writePrimaryKeys(targetdialect = targetdialect,
cdmVersion = cdmVersion)
writeConstraints(targetdialect = targetdialect,
cdmVersion = cdmVersion)
}
## Write all primary keys
writePrimaryKeys(targetdialect = "oracle",
cdmVersion = cdmVersion,
sqlFilename = paste0("OMOP CDM pk ", cdmVersion, " ", Sys.Date(), ".sql"),
cdmDatabaseSchema = "@cdmDatabaseSchema")
writePrimaryKeys(targetdialect = "postgresql",
cdmVersion = cdmVersion,
sqlFilename = paste0("OMOP CDM pk ", cdmVersion, " ", Sys.Date(), ".sql"),
cdmDatabaseSchema = "@cdmDatabaseSchema")
writePrimaryKeys(targetdialect = "sql server",
cdmVersion = cdmVersion,
sqlFilename = paste0("OMOP CDM pk ", cdmVersion, " ", Sys.Date(), ".sql"),
cdmDatabaseSchema = "@cdmDatabaseSchema")
writePrimaryKeys(targetdialect = "netezza",
cdmVersion = cdmVersion,
sqlFilename = paste0("OMOP CDM pk ", cdmVersion, " ", Sys.Date(), ".sql"),
cdmDatabaseSchema = "@cdmDatabaseSchema")
## write all foreign key constraints
writeConstraints("oracle",
cdmVersion,
sqlFileName = paste0("OMOP CDM fk ", cdmVersion, " ", Sys.Date(), ".sql"),
"@cdmDatabaseSchema")
writeConstraints("postgresql",
cdmVersion,
sqlFileName = paste0("OMOP CDM fk ", cdmVersion, " ", Sys.Date(), ".sql"),
"@cdmDatabaseSchema")
writeConstraints("sql server",
cdmVersion,
sqlFileName = paste0("OMOP CDM fk ", cdmVersion, " ", Sys.Date(), ".sql"),
"@cdmDatabaseSchema")
writeConstraints("pdw",
cdmVersion,
sqlFileName = paste0("OMOP CDM fk ", cdmVersion, " ", Sys.Date(), ".sql"),
"@cdmDatabaseSchema")
## write all indices
writeIndex("oracle", ### NOTE: ORACLE CREATES AUTO INDEXING AND NEED TO UPDATE INST/INDEX FILE TO REPRESENT IT