refactor create and write sql for foreign key and primary key
This commit is contained in:
parent
f8cd93d159
commit
5d9887a356
|
@ -16,31 +16,23 @@
|
||||||
|
|
||||||
#' Create a foreign key sql script from two csv files that detail the OMOP CDM Specifications.
|
#' 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 cdmVersionNum The version of the CDM you are creating, e.g. 5.3.1
|
||||||
#'
|
|
||||||
#' @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.
|
|
||||||
#' @export
|
#' @export
|
||||||
|
|
||||||
createFkFromFile <- function(cdmVersionNum = cdmVersion,
|
createFkFromFile <- function(cdmVersion = cdmVersion){
|
||||||
cdmFieldCsvLoc = "inst/csv/OMOP_CDMv5.3.1_Field_Level.csv",
|
cdmFieldCsvLoc <- paste0("inst/csv/OMOP_CDMv", cdmVersion, "_Field_Level.csv")
|
||||||
outputFile = paste0("inst/sql/sql_server/OMOP CDM fk ", cdmVersion, " ", Sys.Date(), ".sql")){
|
|
||||||
|
|
||||||
cdmSpecs <- read.csv(cdmFieldCsvLoc, stringsAsFactors = FALSE)
|
cdmSpecs <- read.csv(cdmFieldCsvLoc, stringsAsFactors = FALSE)
|
||||||
|
|
||||||
fks <- subset(cdmSpecs, isForeignKey == "Yes")
|
foreignKeys <- subset(cdmSpecs, isForeignKey == "Yes")
|
||||||
fks$key <- paste0(fks$cdmTableName,"_",fks$cdmFieldName)
|
foreignKeys$key <- paste0(foreignKeys$cdmTableName, "_", foreignKeys$cdmFieldName)
|
||||||
|
|
||||||
s <- c()
|
sql_result <- c(paste0("--@targetdialect CDM Foreign Key Constraints for OMOP Common Data Model ", cdmVersion, "\n"))
|
||||||
s <- c(paste0("--@targetdialect CDM Foreign Key Constraints for OMOP Common Data Model ",cdmVersionNum, "\n"))
|
for (foreignKey in foreignKeys$key){
|
||||||
for (t in fks$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(paste0(sql_result, collapse = ""))
|
||||||
return(s)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,32 +16,23 @@
|
||||||
|
|
||||||
#' Create a primary key sql script from two csv files that detail the OMOP CDM Specifications.
|
#' 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 cdmVersionNum The version of the CDM you are creating, e.g. 5.3.1
|
||||||
#'
|
|
||||||
#' @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.
|
|
||||||
#' @export
|
#' @export
|
||||||
|
|
||||||
createPkFromFile <- function(cdmVersionNum = cdmVersion,
|
createPkFromFile <- function(cdmVersion = cdmVersion){
|
||||||
cdmFieldCsvLoc = "inst/csv/OMOP_CDMv5.3.1_Field_Level.csv",
|
cdmFieldCsvLoc <- paste0("inst/csv/OMOP_CDMv", cdmVersion, "_Field_Level.csv")
|
||||||
outputFile = paste0("inst/sql/sql_server/OMOP CDM pk ", cdmVersion, " ", Sys.Date(), ".sql")){
|
|
||||||
|
|
||||||
cdmSpecs <- read.csv(cdmFieldCsvLoc, stringsAsFactors = FALSE)
|
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()
|
subquery <- subset(primaryKeys, cdmFieldName==pkField)
|
||||||
s <- c(paste0("--@targetdialect CDM Primary Key Constraints for OMOP Common Data Model ",cdmVersionNum, "\n"))
|
|
||||||
for (f in pkFields){
|
|
||||||
|
|
||||||
q <- subset(pks, cdmFieldName==f)
|
sql_result <- c(sql_result, paste0("\nALTER TABLE @cdmDatabaseSchema.", subquery$cdmTableName, " ADD CONSTRAINT xpk_", subquery$cdmTableName, " PRIMARY KEY NONCLUSTERED (", subquery$cdmFieldName , ");\n"))
|
||||||
|
|
||||||
s <- c(s, paste0("\nALTER TABLE @cdmDatabaseSchema.", q$cdmTableName, " ADD CONSTRAINT xpk_", q$cdmTableName, " PRIMARY KEY NONCLUSTERED (", q$cdmFieldName ,");\n"))
|
|
||||||
|
|
||||||
}
|
}
|
||||||
SqlRender::writeSql(s, targetFile = outputFile)
|
return(paste0(sql_result, collapse = ""))
|
||||||
return(s)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,27 +18,20 @@
|
||||||
#'
|
#'
|
||||||
#' @param targetdialect The dialect of the target database. Choices are "oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server"
|
#' @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 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 cdmDatabaseSchema The name of the schema where the cdm sits. Defaults to "@cdmDatabaseSchema"
|
||||||
#' @param sqlFilename The name of the file that should be rendered and translated to a different dbms.
|
|
||||||
#'
|
#'
|
||||||
#' @export
|
#' @export
|
||||||
|
|
||||||
writeConstraints <- function(targetdialect, cdmVersion, cdmDatabaseSchema, sqlFileName) {
|
writeConstraints <- function(targetdialect, cdmVersion, cdmDatabaseSchema = "@cdmDatabaseSchema") {
|
||||||
if(!dir.exists("output")){
|
outputpath <- file.path("ddl", cdmVersion, targetdialect)
|
||||||
dir.create("output")
|
dir.create(outputpath, showWarnings = FALSE, recursive = TRUE)
|
||||||
}
|
|
||||||
|
|
||||||
if(!dir.exists(paste0("output/",targetdialect))){
|
sql <- createFkFromFile(cdmVersion)
|
||||||
dir.create(paste0("output/",targetdialect))
|
sql <- SqlRender::render(sql = sql, cdmDatabaseSchema = cdmDatabaseSchema)
|
||||||
}
|
sql <- SqlRender::translate(sql, targetDialect = targetdialect)
|
||||||
|
|
||||||
sql <- SqlRender::loadRenderTranslateSql(sqlFilename = sqlFileName,
|
|
||||||
packageName = "CdmDdlBase",
|
|
||||||
dbms = targetdialect,
|
|
||||||
targetdialect = targetdialect,
|
|
||||||
cdmDatabaseSchema = cdmDatabaseSchema)
|
|
||||||
|
|
||||||
|
filename <- paste("OMOPCDM", targetdialect, cdmVersion, "constraints.sql", sep = " ")
|
||||||
SqlRender::writeSql(sql = sql,
|
SqlRender::writeSql(sql = sql,
|
||||||
targetFile = paste0("output/",targetdialect,"/OMOP CDM ",targetdialect," ", cdmVersion, " constraints.sql"))
|
targetFile = file.path(outputpath, filename)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ writeDDL <- function(targetdialect, cdmVersion, cdmDatabaseSchema = "@cdmDatabas
|
||||||
sql <- SqlRender::render(sql = sql, cdmDatabaseSchema = cdmDatabaseSchema)
|
sql <- SqlRender::render(sql = sql, cdmDatabaseSchema = cdmDatabaseSchema)
|
||||||
sql <- SqlRender::translate(sql, targetDialect = targetdialect)
|
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,
|
SqlRender::writeSql(sql = sql,
|
||||||
targetFile = file.path(outputpath, filename)
|
targetFile = file.path(outputpath, filename)
|
||||||
)
|
)
|
||||||
|
|
|
@ -18,27 +18,20 @@
|
||||||
#'
|
#'
|
||||||
#' @param targetdialect The dialect of the target database. Choices are "oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server"
|
#' @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 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 cdmDatabaseSchema The name of the schema where the cdm sits. Defaults to "@cdmDatabaseSchema"
|
||||||
#' @param sqlFilename The name of the file that should be rendered and translated to a different dbms.
|
|
||||||
#'
|
#'
|
||||||
#' @export
|
#' @export
|
||||||
|
|
||||||
writePrimaryKeys <- function(targetdialect, cdmVersion, cdmDatabaseSchema, sqlFilename) {
|
writePrimaryKeys <- function(targetdialect, cdmVersion, cdmDatabaseSchema = "@cdmDatabaseSchema") {
|
||||||
if(!dir.exists("output")){
|
outputpath <- file.path("ddl", cdmVersion, targetdialect)
|
||||||
dir.create("output")
|
dir.create(outputpath, showWarnings = FALSE, recursive = TRUE)
|
||||||
}
|
|
||||||
|
|
||||||
if(!dir.exists(paste0("output/",targetdialect))){
|
sql <- createFkFromFile(cdmVersion)
|
||||||
dir.create(paste0("output/",targetdialect))
|
sql <- SqlRender::render(sql = sql, cdmDatabaseSchema = cdmDatabaseSchema)
|
||||||
}
|
sql <- SqlRender::translate(sql, targetDialect = targetdialect)
|
||||||
|
|
||||||
sql <- SqlRender::loadRenderTranslateSql(sqlFilename = sqlFilename,
|
|
||||||
packageName = "CdmDdlBase",
|
|
||||||
dbms = targetdialect,
|
|
||||||
targetdialect = targetdialect,
|
|
||||||
cdmDatabaseSchema = cdmDatabaseSchema)
|
|
||||||
|
|
||||||
|
filename <- paste("OMOPCDM", targetdialect, cdmVersion, "primary", "keys.sql", sep = " ")
|
||||||
SqlRender::writeSql(sql = sql,
|
SqlRender::writeSql(sql = sql,
|
||||||
targetFile = paste0("output/",targetdialect,"/OMOP CDM ",targetdialect, " ", cdmVersion, " primary keys.sql" ))
|
targetFile = file.path(outputpath, filename)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,17 +8,15 @@
|
||||||
|
|
||||||
# Step 3: After creating the csv files for the new version, create the sql server DDL from the file
|
# 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
|
# Step 3.1: Create the primary key constraints for the new version
|
||||||
|
|
||||||
p <- CdmDdlBase::createPkFromFile(cdmVersionNum = cdmVersion,
|
primaryKeys <- CdmDdlBase::createPkFromFile(cdmVersion)
|
||||||
cdmFieldCsvLoc = "inst/csv/OMOP_CDMv6.0_Field_Level.csv")
|
|
||||||
|
|
||||||
# Step 3.2: Create the foreign key constraints for the new version
|
# Step 3.2: Create the foreign key constraints for the new version
|
||||||
|
|
||||||
f <- CdmDdlBase::createFkFromFile(cdmVersionNum = cdmVersion,
|
foreignKeys <- CdmDdlBase::createFkFromFile(cdmVersion)
|
||||||
cdmFieldCsvLoc = "inst/csv/OMOP_CDMv6.0_Field_Level.csv")
|
|
||||||
# At this point you should rebuild the package
|
# 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.
|
# 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")) {
|
for (targetdialect in c("oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server")) {
|
||||||
writeDDL(targetdialect = targetdialect,
|
writeDDL(targetdialect = targetdialect,
|
||||||
cdmVersion = cdmVersion)
|
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
|
## write all indices
|
||||||
|
|
||||||
writeIndex("oracle", ### NOTE: ORACLE CREATES AUTO INDEXING AND NEED TO UPDATE INST/INDEX FILE TO REPRESENT IT
|
writeIndex("oracle", ### NOTE: ORACLE CREATES AUTO INDEXING AND NEED TO UPDATE INST/INDEX FILE TO REPRESENT IT
|
||||||
|
|
Loading…
Reference in New Issue