From e4271ee6de165d8bf7fb252c8081aeb35cc78509 Mon Sep 17 00:00:00 2001 From: mccullen Date: Tue, 18 Apr 2023 10:55:22 -0400 Subject: [PATCH 1/3] Fix exportToSQLite errors caused by old table name and parameter order --- R/exportToSQLite.r | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/R/exportToSQLite.r b/R/exportToSQLite.r index 7407187..93d223b 100644 --- a/R/exportToSQLite.r +++ b/R/exportToSQLite.r @@ -29,7 +29,6 @@ exportToSQLite <- "care_site", "cdm_source", "cohort", - "cohort_attribute", "condition_era", "condition_occurrence", "cost", @@ -72,10 +71,10 @@ exportToSQLite <- sqlQuery <- paste0("select * from ", paste0(cdmSchema, ".", tableName), ";") translatedSql <- - SqlRender::translate(renderedSql, targetDialect = connectionDetails$dbms) + SqlRender::translate(sql=sqlQuery, targetDialect = connectionDetails$dbms) writeLines(paste0("Fetching: ", tableName)) - tableData <- DatabaseConnector::querySql(conn, translatedSql) - DatabaseConnector::insertTable(sqliteCon, toupper(tableName), tableData) + tableData <- DatabaseConnector::querySql(connection=conn, sql=translatedSql) + DatabaseConnector::insertTable(connection=sqliteCon, tableName=toupper(tableName), data=tableData) } DatabaseConnector::disconnect(conn) From 8cbe3ac7caf83b423902ea85590aa84a0e71a238 Mon Sep 17 00:00:00 2001 From: mccullen Date: Thu, 20 Apr 2023 18:47:09 -0400 Subject: [PATCH 2/3] Add the rest of the tables to exportToSqlite() --- R/exportToSQLite.r | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/exportToSQLite.r b/R/exportToSQLite.r index 93d223b..bfac031 100644 --- a/R/exportToSQLite.r +++ b/R/exportToSQLite.r @@ -29,6 +29,7 @@ exportToSQLite <- "care_site", "cdm_source", "cohort", + "cohort_definition", "condition_era", "condition_occurrence", "cost", @@ -37,6 +38,8 @@ exportToSQLite <- "dose_era", "drug_era", "drug_exposure", + "episode", + "episode_event", "fact_relationship", "location", "measurement", From feaf650edf7105db915cfe0138659b787f6cca9f Mon Sep 17 00:00:00 2001 From: mccullen Date: Sat, 17 Feb 2024 13:57:07 -0500 Subject: [PATCH 3/3] Add cdm version support for 5.3 and 5.4 to exportToSQLite --- R/exportToSQLite.r | 206 ++++++++++++++++++++++++++------------------- 1 file changed, 121 insertions(+), 85 deletions(-) diff --git a/R/exportToSQLite.r b/R/exportToSQLite.r index bfac031..379898d 100644 --- a/R/exportToSQLite.r +++ b/R/exportToSQLite.r @@ -1,85 +1,121 @@ -#' @title Export a CDM (Vocabulary and Event tables) into a SQLite Database. -#' -#' @description This function fetches (pruned) table data into R dataframes and writes them to a SQLite DB. (Eunomia support) -#' -#' @param connectionDetails An R object of type\cr\code{connectionDetails} created using the -#' function \code{createConnectionDetails} in the -#' \code{DatabaseConnector} package. -#' -#' @param cdmSchema The name of the database schema that contains the CDM. -#' Requires read and write permissions to this database. On SQL -#' Server, this should specifiy both the database and the schema, -#' so for example 'cdm_instance.dbo'. -#' -#' @param SQLiteDbName The name of the SQLite Database File. -#' -#'@export - -exportToSQLite <- - function(connectionDetails, - cdmSchema, - SQLiteDbName = "cdm.sqlite") - { - conn <- DatabaseConnector::connect(connectionDetails) - sqliteCD <- - DatabaseConnector::createConnectionDetails(dbms = "sqlite", server = SQLiteDbName) - sqliteCon <- DatabaseConnector::connect(sqliteCD) - - eventTable <- c( - "care_site", - "cdm_source", - "cohort", - "cohort_definition", - "condition_era", - "condition_occurrence", - "cost", - "death", - "device_exposure", - "dose_era", - "drug_era", - "drug_exposure", - "episode", - "episode_event", - "fact_relationship", - "location", - "measurement", - "metadata", - "note", - "note_nlp", - "observation", - "observation_period", - "payer_plan_period", - "person", - "procedure_occurrence", - "provider", - "specimen", - "visit_detail", - "visit_occurrence" - ) - - vocabTable <- c( - "concept", - "concept_ancestor", - "concept_class", - "concept_relationship", - "concept_synonym", - "domain", - "drug_strength", - "relationship", - "source_to_concept_map", - "vocabulary" - ) - - for (tableName in c(eventTable, vocabTable)) { - sqlQuery <- - paste0("select * from ", paste0(cdmSchema, ".", tableName), ";") - translatedSql <- - SqlRender::translate(sql=sqlQuery, targetDialect = connectionDetails$dbms) - writeLines(paste0("Fetching: ", tableName)) - tableData <- DatabaseConnector::querySql(connection=conn, sql=translatedSql) - DatabaseConnector::insertTable(connection=sqliteCon, tableName=toupper(tableName), data=tableData) - } - - DatabaseConnector::disconnect(conn) - DatabaseConnector::disconnect(sqliteCon) - } +#' @title Export a CDM (Vocabulary and Event tables) into a SQLite Database. +#' +#' @description This function fetches (pruned) table data into R dataframes and writes them to a SQLite DB. (Eunomia support) +#' +#' @param connectionDetails An R object of type\cr\code{connectionDetails} created using the +#' function \code{createConnectionDetails} in the +#' \code{DatabaseConnector} package. +#' +#' @param cdmSchema The name of the database schema that contains the CDM. +#' Requires read and write permissions to this database. On SQL +#' Server, this should specifiy both the database and the schema, +#' so for example 'cdm_instance.dbo'. +#' +#' @param SQLiteDbName The name of the SQLite Database File. +#' +#'@export + +exportToSQLite <- + function(connectionDetails, + cdmSchema, + cdmVersion, + SQLiteDbName = "cdm.sqlite") + { + conn <- DatabaseConnector::connect(connectionDetails) + sqliteCD <- + DatabaseConnector::createConnectionDetails(dbms = "sqlite", server = SQLiteDbName) + sqliteCon <- DatabaseConnector::connect(sqliteCD) + + if (cdmVersion == "5.3") + eventTable <- c( + "attribute_definition", + "care_site", + "cdm_source", + "cohort_definition", + "condition_era", + "condition_occurrence", + "cost", + "death", + "device_exposure", + "dose_era", + "drug_era", + "drug_exposure", + "fact_relationship", + "location", + "measurement", + "metadata", + "note", + "note_nlp", + "observation", + "observation_period", + "payer_plan_period", + "person", + "procedure_occurrence", + "provider", + "specimen", + "visit_detail", + "visit_occurrence" + ) + else if (cdmVersion == "5.4") + eventTable <- c( + "care_site", + "cdm_source", + "cohort", + "cohort_definition", + "condition_era", + "condition_occurrence", + "cost", + "death", + "device_exposure", + "dose_era", + "drug_era", + "drug_exposure", + "episode", + "episode_event", + "fact_relationship", + "location", + "measurement", + "metadata", + "note", + "note_nlp", + "observation", + "observation_period", + "payer_plan_period", + "person", + "procedure_occurrence", + "provider", + "specimen", + "visit_detail", + "visit_occurrence" + ) + else + stop("Unsupported CDM specified. Supported CDM versions are \"5.3\" and \"5.4\"") + + + + vocabTable <- c( + "concept", + "concept_ancestor", + "concept_class", + "concept_relationship", + "concept_synonym", + "domain", + "drug_strength", + "relationship", + "source_to_concept_map", + "vocabulary" + ) + + for (tableName in c(eventTable, vocabTable)) { + sqlQuery <- + paste0("select * from ", paste0(cdmSchema, ".", tableName), ";") + translatedSql <- + SqlRender::translate(sql=sqlQuery, targetDialect = connectionDetails$dbms) + writeLines(paste0("Fetching: ", tableName)) + tableData <- DatabaseConnector::querySql(connection=conn, sql=translatedSql) + DatabaseConnector::insertTable(connection=sqliteCon, tableName=toupper(tableName), data=tableData) + } + + DatabaseConnector::disconnect(conn) + DatabaseConnector::disconnect(sqliteCon) + }