2021-06-09 00:19:01 +00:00
|
|
|
# Copyright 2019 Observational Health Data Sciences and Informatics
|
|
|
|
#
|
|
|
|
# This file is part of CdmDdlBase
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
#' Create a primary key sql script from two csv files that detail the OMOP CDM Specifications.
|
|
|
|
|
2021-08-18 19:30:04 +00:00
|
|
|
#' @param cdmVersionNum The version of the CDM you are creating, e.g. 5.3
|
2021-06-09 00:19:01 +00:00
|
|
|
#' @export
|
|
|
|
|
2021-08-18 19:30:04 +00:00
|
|
|
createPrimaryKeys <- function(cdmVersion = cdmVersion){
|
2021-08-18 17:28:21 +00:00
|
|
|
cdmFieldCsvLoc <- paste0("inst/csv/OMOP_CDMv", cdmVersion, "_Field_Level.csv")
|
2021-06-09 00:19:01 +00:00
|
|
|
cdmSpecs <- read.csv(cdmFieldCsvLoc, stringsAsFactors = FALSE)
|
|
|
|
|
2021-08-18 17:28:21 +00:00
|
|
|
primaryKeys <- subset(cdmSpecs, isPrimaryKey == "Yes")
|
|
|
|
pkFields <- primaryKeys$cdmFieldName
|
2021-06-09 00:19:01 +00:00
|
|
|
|
2021-08-18 17:28:21 +00:00
|
|
|
sql_result <- c(paste0("--@targetdialect CDM Primary Key Constraints for OMOP Common Data Model ", cdmVersion, "\n"))
|
|
|
|
for (pkField in pkFields){
|
2021-06-09 00:19:01 +00:00
|
|
|
|
2021-08-18 17:28:21 +00:00
|
|
|
subquery <- subset(primaryKeys, cdmFieldName==pkField)
|
2021-06-09 00:19:01 +00:00
|
|
|
|
2021-08-18 17:28:21 +00:00
|
|
|
sql_result <- c(sql_result, paste0("\nALTER TABLE @cdmDatabaseSchema.", subquery$cdmTableName, " ADD CONSTRAINT xpk_", subquery$cdmTableName, " PRIMARY KEY NONCLUSTERED (", subquery$cdmFieldName , ");\n"))
|
2021-06-09 00:19:01 +00:00
|
|
|
|
|
|
|
}
|
2021-08-18 17:28:21 +00:00
|
|
|
return(paste0(sql_result, collapse = ""))
|
2021-06-09 00:19:01 +00:00
|
|
|
}
|