Skip to content

Commit

Permalink
updated truncate functions to use DatabaseConnector since object_id i…
Browse files Browse the repository at this point in the history
…sn't translated to PG by SqlRender
  • Loading branch information
AnthonyMolinaro committed May 22, 2019
1 parent eba743e commit 5c277a9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
25 changes: 12 additions & 13 deletions R/TruncateEventTables.r
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@
TruncateEventTables <- function (connectionDetails, cdmDatabaseSchema)
{

eventTables <- 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' )

pathToSql <- base::system.file("sql/sql_server", package = "ETLSyntheaBuilder")
conn <- DatabaseConnector::connect(connectionDetails)

sqlFile <- base::paste0(pathToSql, "/", "truncate_event_tables.sql")

sqlQuery <- base::readChar(sqlFile, base::file.info(sqlFile)$size)

renderedSql <- SqlRender::render(sqlQuery, cdm_schema = cdmDatabaseSchema)

translatedSql <- SqlRender::translate(renderedSql, targetDialect = connectionDetails$dbms)

writeLines("Running truncate_event_tables.sql")
writeLines("Truncating event tables...")

conn <- DatabaseConnector::connect(connectionDetails)
for (tableName in eventTables) {
if (DatabaseConnector::dbExistsTable(conn = conn, name = tableName, schema = cdmDatabaseSchema)) {
writeLines(paste0("Truncating ",tableName))
DatabaseConnector::dbExecute(conn, paste0("truncate table ",cdmDatabaseSchema,".",tableName,";"))
}
}

DatabaseConnector::dbExecute(conn, translatedSql, progressBar = TRUE, reportOverallTime = TRUE)

on.exit(DatabaseConnector::disconnect(conn))

}
23 changes: 10 additions & 13 deletions R/TruncateVocabTables.r
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,20 @@
TruncateVocabTables <- function (connectionDetails, vocabDatabaseSchema)
{

vocabTables <- c( 'attribute_definition', 'cohort_definition', 'concept', 'concept_ancestor', 'concept_class', 'concept_relationship',
'concept_synonym', 'domain', 'drug_strength', 'relationship', 'source_to_concept_map', 'vocabulary' )

pathToSql <- base::system.file("sql/sql_server", package = "ETLSyntheaBuilder")
conn <- DatabaseConnector::connect(connectionDetails)

sqlFile <- base::paste0(pathToSql, "/", "truncate_vocab_tables.sql")

sqlQuery <- base::readChar(sqlFile, base::file.info(sqlFile)$size)

renderedSql <- SqlRender::render(sqlQuery, vocab_schema = vocabDatabaseSchema)

translatedSql <- SqlRender::translate(renderedSql, targetDialect = connectionDetails$dbms)

writeLines("Running truncate_vocab_tables.sql")
writeLines("Truncating vocabulary tables...")

conn <- DatabaseConnector::connect(connectionDetails)
for (tableName in vocabTables) {
if (DatabaseConnector::dbExistsTable(conn = conn, name = tableName, schema = vocabDatabaseSchema)) {
writeLines(paste0("Truncating ",tableName))
DatabaseConnector::dbExecute(conn, paste0("truncate table ",vocabDatabaseSchema,".",tableName,";"))
}
}

DatabaseConnector::dbExecute(conn, translatedSql, progressBar = TRUE, reportOverallTime = TRUE)

on.exit(DatabaseConnector::disconnect(conn))

}

0 comments on commit 5c277a9

Please sign in to comment.