R/CreateCohorts.R

Defines functions .indexCohortTable .createCohorts

# Copyright 2020 Observational Health Data Sciences and Informatics
#
# This file is part of DrugUtilization
#
# 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.

.createCohorts <- function(connection,
                           cdmDatabaseSchema,
                           vocabularyDatabaseSchema = cdmDatabaseSchema,
                           cohortDatabaseSchema,
                           cohortTable,
                           addIndex = FALSE,
                           oracleTempSchema,
                           outputFolder) {
  
  # Create study cohort table structure:
  sql <- loadRenderTranslateSql(connection = connection,
                                sqlFileInPackage = "CreateCohortTable.sql",
                                oracleTempSchema = oracleTempSchema,
                                cohort_database_schema = cohortDatabaseSchema,
                                cohort_table = cohortTable)
  DatabaseConnector::executeSql(connection, sql, progressBar = FALSE, reportOverallTime = FALSE)
  
  
  
  # Instantiate cohorts:
  pathToCsv <- system.file("settings", "CohortsToCreate.csv", package = "DrugUtilization")
  cohortsToCreate <- read.csv(pathToCsv)
  for (i in 1:nrow(cohortsToCreate)) {
    writeLines(paste("Creating cohort:", cohortsToCreate$name[i]))
    sql <- loadRenderTranslateSql(connection = connection,
                                  sqlFileInPackage = paste0(cohortsToCreate$name[i], ".sql"),
                                  oracleTempSchema = oracleTempSchema,
                                  cdm_database_schema = cdmDatabaseSchema,
                                  vocabulary_database_schema = vocabularyDatabaseSchema,
                                  target_database_schema = cohortDatabaseSchema,
                                  target_cohort_table = cohortTable,
                                  target_cohort_id = cohortsToCreate$cohortId[i])
    DatabaseConnector::executeSql(connection, sql)
  }
  
  # Index cohort table if specified
  if (addIndex) {
    .indexCohortTable(connection = connection,
                      cohortDatabaseSchema = cohortDatabaseSchema,
                      cohortTable = cohortTable,
                      oracleTempSchema = oracleTempSchema)
  }
}

.indexCohortTable <- function(connection,
                              cohortDatabaseSchema,
                              cohortTable,
                              oracleTempSchema) {
  writeLines(paste("Indexing cohort table:", paste(cohortDatabaseSchema, cohortTable, sep=".")))
  sql <- loadRenderTranslateSql(connection = connection,
                                sqlFileInPackage = "CreateCohortTableIndex.sql",
                                oracleTempSchema = oracleTempSchema,
                                cohort_database_schema = cohortDatabaseSchema,
                                cohort_table = cohortTable)
  DatabaseConnector::executeSql(connection, sql)
}
mi-erasmusmc/RanitidineStudy documentation built on Jan. 23, 2021, 11:50 p.m.