2021-06-09 00:19:01 +00:00
# Copyright 2019 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 DDL script
#'
2021-08-19 03:32:31 +00:00
#' Write the DDL to a SQL file. The SQL will be rendered (parameters replaced) and translated to the target SQL
#' dialect. By default the @cdmDatabaseSchema parameter is kept in the SQL file and needs to be replaced before
#' execution.
#'
2022-09-26 15:59:28 +00:00
#' @param targetDialect The dialect of the target database. Choices are "oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server",
#' "spark", "snowflake", "synapse"
2021-08-19 03:32:31 +00:00
#' @param cdmVersion The version of the CDM you are creating, e.g. 5.3, 5.4
2021-08-20 15:06:23 +00:00
#' @param outputfolder The directory or folder where the SQL file should be saved.
2021-08-18 17:02:22 +00:00
#' @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"
2021-06-09 00:19:01 +00:00
#'
#' @export
2021-08-20 15:06:23 +00:00
writeDdl <- function ( targetDialect , cdmVersion , outputfolder , cdmDatabaseSchema = " @cdmDatabaseSchema" ) {
2021-08-19 03:32:31 +00:00
# argument checks
2022-09-26 15:59:28 +00:00
stopifnot ( targetDialect %in% c ( " oracle" , " postgresql" , " pdw" , " redshift" , " impala" , " netezza" , " bigquery" , " sql server" , " spark" , " snowflake" , " synapse" ) )
2021-08-19 03:32:31 +00:00
stopifnot ( cdmVersion %in% listSupportedVersions ( ) )
stopifnot ( is.character ( cdmDatabaseSchema ) )
2021-08-20 15:06:23 +00:00
if ( missing ( outputfolder ) ) {
outputfolder <- file.path ( " ddl" , cdmVersion , gsub ( " " , " _" , targetDialect ) )
2021-08-19 03:32:31 +00:00
}
2021-08-20 15:06:23 +00:00
if ( ! dir.exists ( outputfolder ) ) dir.create ( outputfolder , showWarnings = FALSE , recursive = TRUE )
2021-06-09 00:19:01 +00:00
2021-08-18 19:30:04 +00:00
sql <- createDdl ( cdmVersion )
2021-08-19 03:32:31 +00:00
sql <- SqlRender :: render ( sql = sql , cdmDatabaseSchema = cdmDatabaseSchema , targetDialect = targetDialect )
sql <- SqlRender :: translate ( sql , targetDialect = targetDialect )
filename <- paste ( " OMOPCDM" , gsub ( " " , " _" , targetDialect ) , cdmVersion , " ddl.sql" , sep = " _" )
2021-08-20 15:06:23 +00:00
SqlRender :: writeSql ( sql = sql , targetFile = file.path ( outputfolder , filename ) )
2021-08-19 03:32:31 +00:00
invisible ( filename )
}
#' @describeIn writeDdl writePrimaryKeys Write the SQL code that creates the primary keys to a file.
#' @export
2021-08-20 15:06:23 +00:00
writePrimaryKeys <- function ( targetDialect , cdmVersion , outputfolder , cdmDatabaseSchema = " @cdmDatabaseSchema" ) {
2021-08-19 03:32:31 +00:00
# argument checks
2022-09-26 15:59:28 +00:00
stopifnot ( targetDialect %in% c ( " oracle" , " postgresql" , " pdw" , " redshift" , " impala" , " netezza" , " bigquery" , " sql server" , " spark" , " snowflake" , " synapse" ) )
2021-08-19 03:32:31 +00:00
stopifnot ( cdmVersion %in% listSupportedVersions ( ) )
stopifnot ( is.character ( cdmDatabaseSchema ) )
2021-08-20 15:06:23 +00:00
if ( missing ( outputfolder ) ) {
outputfolder <- file.path ( " ddl" , cdmVersion , gsub ( " " , " _" , targetDialect ) )
2021-08-19 03:32:31 +00:00
}
2021-08-20 15:06:23 +00:00
if ( ! dir.exists ( outputfolder ) ) dir.create ( outputfolder , showWarnings = FALSE , recursive = TRUE )
2021-08-19 03:32:31 +00:00
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 = " _" )
2021-08-20 15:06:23 +00:00
SqlRender :: writeSql ( sql = sql , targetFile = file.path ( outputfolder , filename ) )
2021-08-19 03:32:31 +00:00
invisible ( filename )
}
#' @describeIn writeDdl writeForeignKeys Write the SQL code that creates the foreign keys to a file.
#' @export
2021-08-20 15:06:23 +00:00
writeForeignKeys <- function ( targetDialect , cdmVersion , outputfolder , cdmDatabaseSchema = " @cdmDatabaseSchema" ) {
2021-08-19 03:32:31 +00:00
# argument checks
2022-09-26 15:59:28 +00:00
stopifnot ( targetDialect %in% c ( " oracle" , " postgresql" , " pdw" , " redshift" , " impala" , " netezza" , " bigquery" , " sql server" , " spark" , " snowflake" , " synapse" ) )
2021-08-19 03:32:31 +00:00
stopifnot ( cdmVersion %in% listSupportedVersions ( ) )
stopifnot ( is.character ( cdmDatabaseSchema ) )
2021-08-20 15:06:23 +00:00
if ( missing ( outputfolder ) ) {
outputfolder <- file.path ( " ddl" , cdmVersion , gsub ( " " , " _" , targetDialect ) )
2021-08-19 03:32:31 +00:00
}
2021-08-20 15:06:23 +00:00
if ( ! dir.exists ( outputfolder ) ) dir.create ( outputfolder , showWarnings = FALSE , recursive = TRUE )
2021-08-19 03:32:31 +00:00
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 = " _" )
2021-08-20 15:06:23 +00:00
SqlRender :: writeSql ( sql = sql , targetFile = file.path ( outputfolder , filename ) )
2021-08-19 03:32:31 +00:00
invisible ( filename )
}
#' @describeIn writeDdl writeIndex Write the rendered and translated sql that creates recommended indexes to a file.
#' @export
2021-08-20 15:06:23 +00:00
writeIndex <- function ( targetDialect , cdmVersion , outputfolder , cdmDatabaseSchema = " @cdmDatabaseSchema" ) {
2021-08-19 03:32:31 +00:00
# argument checks
2022-09-26 15:59:28 +00:00
stopifnot ( targetDialect %in% c ( " oracle" , " postgresql" , " pdw" , " redshift" , " impala" , " netezza" , " bigquery" , " sql server" , " spark" , " snowflake" , " synapse" ) )
2021-08-19 03:32:31 +00:00
stopifnot ( cdmVersion %in% listSupportedVersions ( ) )
stopifnot ( is.character ( cdmDatabaseSchema ) )
2021-08-20 15:06:23 +00:00
if ( missing ( outputfolder ) ) {
outputfolder <- file.path ( " ddl" , cdmVersion , gsub ( " " , " _" , targetDialect ) )
2021-08-19 03:32:31 +00:00
}
2021-08-20 15:06:23 +00:00
if ( ! dir.exists ( outputfolder ) ) dir.create ( outputfolder , showWarnings = FALSE , recursive = TRUE )
2021-08-19 03:32:31 +00:00
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 )
2021-06-09 00:19:01 +00:00
2021-08-19 03:32:31 +00:00
filename <- paste ( " OMOPCDM" , gsub ( " " , " _" , targetDialect ) , cdmVersion , " indices.sql" , sep = " _" )
2021-08-20 15:06:23 +00:00
SqlRender :: writeSql ( sql = sql , targetFile = file.path ( outputfolder , filename ) )
2021-08-19 03:32:31 +00:00
invisible ( filename )
2021-06-09 00:19:01 +00:00
}