Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exportToSQLite errors caused by old table name and parameter order #159

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 121 additions & 83 deletions R/exportToSQLite.r
Original file line number Diff line number Diff line change
@@ -1,83 +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_attribute",
"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"
)

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(renderedSql, targetDialect = connectionDetails$dbms)
writeLines(paste0("Fetching: ", tableName))
tableData <- DatabaseConnector::querySql(conn, translatedSql)
DatabaseConnector::insertTable(sqliteCon, toupper(tableName), 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)
}