-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathCreateCDMIndexAndConstraintScripts.r
239 lines (223 loc) · 9.58 KB
/
CreateCDMIndexAndConstraintScripts.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#' @title Create Index and Constraint DDL SQL scripts for CDM tables on rdbms that support these features.
#'
#' @description This function creates one or more SQL scripts as defined in https://github.com/OHDSI/CommonDataModel.
#'
#' @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 CDM database schema. Requires read and write permissions to this database. On SQL
#' Server, this should specify both the database and the schema,
#' so for example 'cdm_instance.dbo'.
#' @param cdmVersion Your CDM version. Currently "5.3.1" and "5.4.0" are supported.
#' @param githubTag An optional github tag from which to pull the DDL script.
#' Currently "v5.3.1" and "v5.4.0" are supported. The default is NULL.
#'
#' @details This function creates SQL scripts for the indices and constraints on tables in a CDM by referring to the
#' correct SQL DDL script in the OHDSI CommonDataModel repo. The database platform is
#' determined by \code{connectionDetails$dbms}. Currently "oracle", "postgresql", "pdw", "netezza", and "sql server" are supported.
#' The SQL DDL scripts are written to the \code{output} directory. The SQL scripts can then be run manually in a SQL session
#' or by using \code{DatabaseConnector::executeSql(connection,DDLscriptName)}
#'
#'@export
CreateCDMIndexAndConstraintScripts <-
function(connectionDetails,
cdmSchema,
cdmVersion,
githubTag = NULL)
{
supportedDbs <-
c("oracle", "postgresql", "pdw", "netezza", "sql server")
supportedVersions <- c("5.3.1", "5.4.0")
supportedTags <-
c("v5.3.1", "v5.4.0")
rdbms <- tolower(connectionDetails$dbms)
if (!(cdmVersion %in% supportedVersions)) {
stop("Unsupported CDM specified. Supported CDM versions are \"5.3.1\" and \"5.4.0\"")
}
if (!(rdbms %in% supportedDbs)) {
stop(
"Unsupported RDBMS specified. Supported rdbms are: \"oracle\", \"postgresql\", \"pdw\", \"netezza\", and \"sql server\""
)
}
if (!is.null(githubTag) && !(githubTag %in% supportedTags)) {
stop(
"Unrecognized github tag. Supported values are \"v5.3.1\" and \"v5.4.0\""
)
}
if (cdmVersion == "5.3.1" &&
!(githubTag %in% c("v5.3.1"))) {
stop("cdmVersion and githubTag mismatch.")
}
if (cdmVersion == "5.4.0" &&
!(githubTag %in% c("v5.4.0"))) {
stop("cdmVersion and githubTag mismatch.")
}
# Determine which DDL script to use (from https://github.com/OHDSI/CommonDataModel)
# based on the given db platform, cdm version, and (optionally) github tag.
if (rdbms == "oracle") {
if (cdmVersion == "5.3.1") {
if (is.null(githubTag) || githubTag == "v5.3.1") {
webResponseInd <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.3.1/Oracle/OMOP%20CDM%20oracle%20indexes.txt"
)
webResponseCon <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.3.1/Oracle/OMOP%20CDM%20oracle%20constraints.txt"
)
}
} else if (cdmVersion == "5.4.0") {
if (is.null(githubTag) || githubTag == "v5.4.0") {
webResponseInd <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.4.0/inst/ddl/5.4/oracle/OMOPCDM_oracle_5.4_indices.sql"
)
webResponseCon <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.4.0/inst/ddl/5.4/oracle/OMOPCDM_oracle_5.4_constraints.sql"
)
}
}
} else if (rdbms == "postgresql") {
if (cdmVersion == "5.3.1") {
if (is.null(githubTag) || githubTag == "v5.3.1") {
webResponseInd <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.3.1/PostgreSQL/OMOP%20CDM%20postgresql%20indexes.txt"
)
webResponseCon <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.3.1/PostgreSQL/OMOP%20CDM%20postgresql%20constraints.txt"
)
}
} else if (cdmVersion == "5.4.0") {
if (is.null(githubTag) || githubTag == "v5.4.0") {
webResponseInd <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.4.0/inst/ddl/5.4/postgresql/OMOPCDM_postgresql_5.4_indices.sql"
)
webResponseCon <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.4.0/inst/ddl/5.4/postgresql/OMOPCDM_postgresql_5.4_constraints.sql"
)
}
}
} else if (rdbms == "pdw") {
if (cdmVersion == "5.3.1") {
if (is.null(githubTag) || githubTag == "v5.3.1") {
webResponseInd <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.3.1/ParallelDataWarehouse/OMOP%20CDM%20pdw%20indexes.txt"
)
webResponseCon <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.3.1/ParallelDataWarehouse/OMOP%20CDM%20pdw%20constraints.txt"
)
}
} else if (cdmVersion == "5.4.0") {
if (is.null(githubTag) || githubTag == "v5.4.0") {
webResponseInd <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.4.0/inst/ddl/5.4/pdw/OMOPCDM_pdw_5.4_indices.sql"
)
webResponseCon <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.4.0/inst/ddl/5.4/pdw/OMOPCDM_pdw_5.4_constraints.sql"
)
}
}
} else if (rdbms == "netezza") {
if (cdmVersion == "5.3.1") {
if (is.null(githubTag) || githubTag == "v5.3.1") {
webResponseInd <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.3.1/Netezza/OMOP%20CDM%20netezza%20primary%20keys.txt"
)
}
} else if (cdmVersion == "5.4.0") {
if (is.null(githubTag) || githubTag == "v5.4.0") {
webResponseInd <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.4.0/inst/ddl/5.4/netezza/OMOPCDM_netezza_5.4_primary_keys.sql"
)
}
}
} else if (rdbms == "sql server") {
if (cdmVersion == "5.3.1") {
if (is.null(githubTag) || githubTag == "v5.3.1") {
webResponseInd <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.3.1/Sql%20Server/OMOP%20CDM%20sql%20server%20indexes.txt"
)
webResponseCon <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.3.1/Sql%20Server/OMOP%20CDM%20sql%20server%20constraints.txt"
)
}
} else if (cdmVersion == "5.4.0") {
if (is.null(githubTag) || githubTag == "v5.4.0") {
webResponseInd <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.4.0/inst/ddl/5.4/sql_server/OMOPCDM_sql_server_5.4_indices.sql"
)
webResponseCon <-
httr::GET(
"https://raw.githubusercontent.com/OHDSI/CommonDataModel/v5.4.0/inst/ddl/5.4/sql_server/OMOPCDM_sql_server_5.4_primary_keys.sql"
)
}
}
}
# Read the SQL DDL from the correct github repo and apply some string formatting to enable
# use of parameterized sql to write to the correct schema.
indexDDL <- httr::content(webResponseInd)
constraintDDL <- httr::content(webResponseCon)
indexDDL <- toupper(indexDDL)
constraintDDL <- toupper(constraintDDL)
indexDDL <- gsub(" ON ", " ON @CDM_SCHEMA.", indexDDL)
indexDDL <-
gsub("ALTER TABLE", "ALTER TABLE @CDM_SCHEMA.", indexDDL)
indexDDL <- gsub("@CDM_SCHEMA. ", "@CDM_SCHEMA.", indexDDL)
indexDDL <-
SqlRender::render(indexDDL, CDM_SCHEMA = cdmSchema)
indexDDL <-
SqlRender::translate(indexDDL, targetDialect = rdbms)
constraintDDL <-
gsub("ALTER TABLE", "ALTER TABLE @CDM_SCHEMA.", constraintDDL)
constraintDDL <-
gsub("@CDM_SCHEMA. ", "@CDM_SCHEMA.", constraintDDL)
constraintDDL <-
SqlRender::render(constraintDDL, CDM_SCHEMA = cdmSchema)
constraintDDL <-
SqlRender::translate(constraintDDL, targetDialect = rdbms)
# Save the translated sql ddl to be run at a later time
if (!dir.exists("output")) {
dir.create("output")
}
# Constraint DDL
writeLines(paste0("Pulling constraint DDL from: ", webResponseCon$url))
writeLines(
paste0(
"Saving constraint DDL in: output/",
rdbms,
"_",
cdmVersion,
"_constraint_ddl.sql"
)
)
SqlRender::writeSql(constraintDDL,
paste0("output/", rdbms, "_", cdmVersion, "_constraint_ddl.sql"))
# Index DDL
# Netezza only has a PK script file
if (rdbms != "netezza") {
writeLines(paste0("Pulling index DDL from: ", webResponseInd$url))
writeLines(paste0(
"Saving index DDL in: output/",
rdbms,
"_",
cdmVersion,
"_index_ddl.sql"
))
SqlRender::writeSql(indexDDL,
paste0("output/", rdbms, "_", cdmVersion, "_index_ddl.sql"))
}
}