Skip to content

Commit

Permalink
Merge pull request #275 from OHDSI/geom_dep
Browse files Browse the repository at this point in the history
Move geom_dependency_uuid from data_source to variable_source table
  • Loading branch information
rtmill authored Nov 2, 2023
2 parents 7d8ad44 + e3419c5 commit 26b58a8
Show file tree
Hide file tree
Showing 12 changed files with 510 additions and 530 deletions.
40 changes: 26 additions & 14 deletions R/createIndices.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

createIndices <- function(connectionDetails) {
dataSourceTable <- getDataSourceTable(connectionDetails = connectionDetails)
variableSourceTable <- getVariableSourceTable(connectionDetails = connectionDetails)
uuids <- dataSourceTable$data_source_uuid
geomIndexTable <- getGeomIndexTable(connectionDetails = connectionDetails)
attrIndexTable <- getAttrIndexTable(connectionDetails = connectionDetails)
Expand All @@ -29,19 +30,28 @@ createIndices <- function(connectionDetails) {
dataSourceRecord = record)
}
# IF attr AND not in aidsid check dependency
if (record$has_attributes == 1 & !id %in% attrIndexTable$data_source_id) {
# Removed "& !id %in% attrIndexTable$data_source_id", need to reincorporate at variable_source level
if (record$has_attributes == 1) {
## IF geom dependency AND dependency not in gidsid then create geom index record AND insert into db
if (!is.na(record$geom_dependency_uuid) &
!record$geom_dependency_uuid %in% geomIndexTable$data_source_id &
record$geom_dependency_uuid != record$data_source_uuid) {
geomDependencyDataSourceRecord <- dplyr::filter(dataSourceTable,
data_source_uuid == record$geom_dependency_uuid)
geomIndexTable <- appendGeomIndexRecord(geomIndexTable = geomIndexTable,
dataSourceRecord = geomDependencyDataSourceRecord)
# TODO for every variable_source$data_source_uuid == id
for (vid in variableSourceTable[variableSourceTable$data_source_uuid == id,]$variable_source_id) {
if (!vid %in% attrIndexTable$variable_source_id) {
variableRecord <- variableSourceTable[variableSourceTable$variable_source_id == vid,]
# TODO switch record$geom_dependency_uuid for varRecord$geom_dependency_uuid
if (!is.na(variableRecord$geom_dependency_uuid) &
!variableRecord$geom_dependency_uuid %in% geomIndexTable$data_source_id &
variableRecord$geom_dependency_uuid != variableRecord$data_source_uuid) {
geomDependencyDataSourceRecord <- dplyr::filter(dataSourceTable,
data_source_uuid == variableRecord$geom_dependency_uuid)
geomIndexTable <- appendGeomIndexRecord(geomIndexTable = geomIndexTable,
dataSourceRecord = geomDependencyDataSourceRecord)
}
attrIndexTable <- appendAttrIndexRecord(attrIndexTable = attrIndexTable,
geomIndexTable = geomIndexTable,
dataSourceRecord = record,
variableSourceRecord = variableRecord)
}
}
attrIndexTable <- appendAttrIndexRecord(attrIndexTable = attrIndexTable,
geomIndexTable = geomIndexTable,
dataSourceRecord = record)
}
}

Expand Down Expand Up @@ -90,16 +100,18 @@ appendGeomIndexRecord <- function(geomIndexTable, dataSourceRecord) {
#' @return (data.frame) updated attrIndexTable
#'

appendAttrIndexRecord <- function(attrIndexTable, geomIndexTable, dataSourceRecord) {
# TODO incorporate varSourceRecord
appendAttrIndexRecord <- function(attrIndexTable, geomIndexTable, dataSourceRecord, variableSourceRecord) {
if (nrow(attrIndexTable) == 0) {
attr_index_id <- 1
} else {
attr_index_id <- max(attrIndexTable$attr_index_id) + 1
}
indexRecord <- dplyr::tibble(
attr_index_id = attr_index_id,
variable_source_id = variableSourceRecord$variable_source_id,
attr_of_geom_index_id = getAttrOfGeomIndexId(geomIndexTable = geomIndexTable,
dataSourceUuid = dataSourceRecord$geom_dependency_uuid),
dataSourceUuid = variableSourceRecord$geom_dependency_uuid),
database_schema = createSchemaString(dataSourceRecord),
table_name = createNameString(name = dataSourceRecord$dataset_name),
data_source_id = dataSourceRecord$data_source_uuid)
Expand All @@ -110,7 +122,7 @@ appendAttrIndexRecord <- function(attrIndexTable, geomIndexTable, dataSourceReco
#' Get foreign key for attr_of_geom_index_id
#'
#' @param geomIndexTable (data.frame) the existing geomIndexTable
#' @param dataSourceUuid (UUID) The UUID for the data source that is registered in the backbone.data_source table
#' @param dataSourceUuid (UUID) The UUID for the geometry data source that is registered in the backbone.data_source table
#'
#' @return (integer) Identifier for the corresponding backbone.geom_index entry
#'
Expand Down
19 changes: 6 additions & 13 deletions R/dbUtils.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ getVariableSourceSummaryTable <- function(connectionDetails) {
getVariableSourceSummaryQuery <- paste0(
"select vs.variable_source_id, ds.data_source_uuid as data_source_id, ",
"vs.variable_name, vs.variable_desc, ds.org_id, ds.org_set_id, ds.documentation_url, ",
"ds.dataset_name, ds.dataset_version, ds.boundary_type, ds.geom_dependency_uuid, ",
"ds.dataset_name, ds.dataset_version, ds.boundary_type, vs.geom_dependency_uuid, ",
"ds2.dataset_name as geom_dependency_name, ds2.geom_type ",
"from backbone.variable_source vs ",
"join backbone.data_source ds ",
"on vs.data_source_uuid=ds.data_source_uuid ",
"join backbone.data_source ds2 ",
"on ds.geom_dependency_uuid = ds2.data_source_uuid")
"on vs.geom_dependency_uuid = ds2.data_source_uuid")
summaryTable <- DatabaseConnector::querySql(conn, getVariableSourceSummaryQuery)
ids <- loadedVariableSourceRecordIds$VARIABLE_SOURCE_RECORD_ID
summaryTable <- dplyr::mutate(summaryTable,
Expand Down Expand Up @@ -75,17 +75,10 @@ getGeomNameFromVariableSourceId <- function(connectionDetails, variableSourceId)
on.exit(DatabaseConnector::disconnect(conn))
DatabaseConnector::dbGetQuery(conn, paste0(
"select concat(database_schema, '.geom_', table_name)
from backbone.geom_index gi
where data_source_id in (
select geom_dependency_uuid
from backbone.data_source ds
where data_source_uuid in (
select data_source_uuid
from backbone.variable_source vs
where variable_source_id = ", variableSourceId,"
)
)"
)
from backbone.geom_index gi
inner join backbone.variable_source vs
on vs.geom_dependency_uuid = gi.data_source_id
and vs.variable_source_id = ", variableSourceId)
)[[1]]
}

Expand Down
55 changes: 0 additions & 55 deletions R/importShapefile.R

This file was deleted.

10 changes: 5 additions & 5 deletions R/loadVariable.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ loadVariable <- function(connectionDetails, variableSourceId){
return(message("Variable already exists in the database."))
}

geomIndexRecord <- getGeomIndexRecord(connectionDetails = connectionDetails,
dataSourceUuid = variableSourceRecord$geom_dependency_uuid)

# get data_source_record
dataSourceRecord <- getDataSourceRecord(connectionDetails = connectionDetails,
dataSourceUuid = variableSourceRecord$data_source_uuid)

geomIndexRecord <- getGeomIndexRecord(connectionDetails = connectionDetails,
dataSourceUuid = dataSourceRecord$geom_dependency_uuid)


# get stage data
staged <- getStaged(dataSourceRecord)

Expand All @@ -76,7 +76,7 @@ loadVariable <- function(connectionDetails, variableSourceId){
if (!geomTableExists) {
message("Loading geom table dependency")
loadGeometry(connectionDetails = connectionDetails,
dataSourceUuid = dataSourceRecord$geom_dependency_uuid)
dataSourceUuid = variableSourceRecord$geom_dependency_uuid)
}

# get mapping values from geom table
Expand Down
Loading

0 comments on commit 26b58a8

Please sign in to comment.