|
| 1 | +# Copyright 2019 Observational Health Data Sciences and Informatics |
| 2 | +# |
| 3 | +# This file is part of CdmDdlBase |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +#' Create OMOP CDM SQL files |
| 18 | +#' |
| 19 | +#' Writes DDL, ForeignKey, PrimaryKey and index SQL files for given cdmVersion |
| 20 | +#' and targetDialect to the 'ddl' folder in specified output folder. |
| 21 | +#' |
| 22 | +#' @param cdmVersions The versions of the CDM you are creating, e.g. 5.3, 5.4. |
| 23 | +#' Defaults to all supported CDM versions. |
| 24 | +#' @param targetDialects A character vector of target dialects. |
| 25 | +#' Defaults to all supported dialects. |
| 26 | +#' @param outputfolder The base folder where the SQL files will be written. |
| 27 | +#' Subfolders will be created for each cdmVersion and targetDialect. |
| 28 | +#' @return Writes DDL, ForeignKey, PrimaryKey and index SQL files for given cdmVersion |
| 29 | +#' and targetDialect to the 'ddl' folder in specified output folder. |
| 30 | +#' @export |
| 31 | +buildRelease <- function(cdmVersions = listSupportedVersions(), |
| 32 | + targetDialects = listSupportedDialects(), |
| 33 | + outputfolder = file.path(tempdir(), "inst", "ddl")){ |
| 34 | + basefolder <- outputfolder |
| 35 | + for (cdmVersion in cdmVersions) { |
| 36 | + for (targetDialect in targetDialects) { |
| 37 | + outputfolder <- file.path(basefolder, cdmVersion, gsub(" ", "_", targetDialect)) |
| 38 | + |
| 39 | + writeDdl(targetDialect = targetDialect, |
| 40 | + cdmVersion = cdmVersion, |
| 41 | + outputfolder = outputfolder) |
| 42 | + |
| 43 | + writePrimaryKeys(targetDialect = targetDialect, |
| 44 | + cdmVersion = cdmVersion, |
| 45 | + outputfolder = outputfolder) |
| 46 | + |
| 47 | + writeForeignKeys(targetDialect = targetDialect, |
| 48 | + cdmVersion = cdmVersion, |
| 49 | + outputfolder = outputfolder) |
| 50 | + |
| 51 | + writeIndex(targetDialect = targetDialect, |
| 52 | + cdmVersion = cdmVersion, |
| 53 | + outputfolder = outputfolder) |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#' Create OMOP CDM release zip |
| 59 | +#' |
| 60 | +#' First calls \code{buildReleaseZips} for given cdmVersions and targetDialects. |
| 61 | +#' This writes the ddl sql files to the ddl folder. |
| 62 | +#' Then zips all written ddl files into a release zip to given output folder. |
| 63 | +#' |
| 64 | +#' If no (or multiple) targetDialect is given, |
| 65 | +#' then one zip is written with the files of all supported dialects. |
| 66 | +#' |
| 67 | +#' @param cdmVersion The version of the CDM you are creating, e.g. 5.3, 5.4. |
| 68 | +#' Defaults to all supported CDM versions. |
| 69 | +#' @param targetDialect The target dialect. Defaults to all supported dialects. |
| 70 | +#' @param outputfolder The output folder. Defaults to "output" |
| 71 | +#' @return A character string containing the OHDSQL DDL |
| 72 | +#' @export |
| 73 | +#' @examples |
| 74 | +#'\dontrun{ |
| 75 | +#' buildReleaseZip(cdmVersion='5.3', targetDialect='sql server', outputfolder='.') |
| 76 | +#'} |
| 77 | +buildReleaseZip <- function(cdmVersion, |
| 78 | + targetDialect = listSupportedDialects(), |
| 79 | + outputfolder = file.path(tempdir(), "output")){ |
| 80 | + # argument checks |
| 81 | + stopifnot(is.character(cdmVersion), length(cdmVersion) == 1, cdmVersion %in% listSupportedVersions()) |
| 82 | + |
| 83 | + if (!dir.exists(outputfolder)) { |
| 84 | + dir.create(outputfolder) |
| 85 | + } |
| 86 | + |
| 87 | + files <- c() |
| 88 | + for (dialect in targetDialect) { |
| 89 | + buildRelease(cdmVersion, dialect, outputfolder = outputfolder) |
| 90 | + files <- c(files, list.files(file.path(outputfolder, cdmVersion, gsub(" ", "_", dialect)), |
| 91 | + pattern = ".*\\.sql$", |
| 92 | + full.names = TRUE)) |
| 93 | + } |
| 94 | + |
| 95 | + if (length(targetDialect) == 1) { |
| 96 | + zipName <- file.path(outputfolder, paste0("OMOPCDM", "_", gsub(" ", "_", targetDialect), "_", cdmVersion, '.zip')) |
| 97 | + } else { |
| 98 | + zipName <- file.path(outputfolder, paste0("OMOPCDM", "_", cdmVersion, '.zip')) |
| 99 | + } |
| 100 | + |
| 101 | + DatabaseConnector::createZipFile(zipFile = zipName, files = files) |
| 102 | +} |
0 commit comments