-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathTruncateEventTables.r
64 lines (61 loc) · 1.81 KB
/
TruncateEventTables.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#' @title Truncate Event Tables.
#'
#' @description This function truncates CDM tables, excluding Vocabulary tables.
#'
#' @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 OMOP CDM
#' instance. 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'.
#'
#'@export
TruncateEventTables <- function(connectionDetails, cdmSchema)
{
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"
)
conn <- DatabaseConnector::connect(connectionDetails)
allTables <- DatabaseConnector::getTableNames(conn, cdmSchema)
tablesToTruncate <- allTables[which(allTables %in% eventTables)]
sql <-
paste(
"truncate table @cdm_schema.",
tablesToTruncate,
";",
collapse = "\n",
sep = ""
)
sql <- SqlRender::render(sql, cdm_schema = cdmSchema)
sql <-
SqlRender::translate(sql, targetDialect = connectionDetails$dbms)
DatabaseConnector::executeSql(conn, sql)
on.exit(DatabaseConnector::disconnect(conn))
}