Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 19 additions & 29 deletions R/CohortQueries.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,28 @@ getCohortDefinitions <- function(
cgTablePrefix = 'cg_',
targetIds = NULL
){

# to make this backward compatible for older CohortGenerator
subsetTableMissing <- is.null(tryCatch({
connectionHandler$queryDb(
sql = "select * from @schema.@cg_table_prefixcohort_subset_definition;",
schema = schema,
cg_table_prefix = cgTablePrefix
)
}, error = function(e){return(NULL)}
))

if(!subsetTableMissing){
sql <- 'select cd.*, csd.json as subset_definition_json
from @schema.@cg_table_prefixcohort_definition cd
left join
sql <- "SELECT cd.*,
csd.json as subset_json,
ctd.template_name,
ctd.template_sql,
ctd.json as template_json

FROM @schema.@cg_table_prefixCOHORT_DEFINITION cd
left join
@schema.@cg_table_prefixcohort_subset_definition csd
on cd.subset_definition_id = csd.subset_definition_id
{@use_targets}?{where cd.cohort_definition_id in (@target_id)}
;'
} else{
sql <- "select *, NULL as subset_definition_json
from @schema.@cg_table_prefixcohort_definition
{@use_targets}?{where cohort_definition_id in (@target_id)}

left join @schema.@cg_table_prefixcohort_template_link ctl
ON ctl.cohort_definition_id = cd.cohort_definition_id

left join @schema.@cg_table_prefixcohort_template_definition ctd
ON ctl.template_definition_id = ctd.template_definition_id
;"
}


result <- connectionHandler$queryDb(
sql = sql,
schema = schema,
cg_table_prefix = cgTablePrefix,
use_targets = !is.null(targetIds),
target_id = paste0(targetIds, collapse = ',')
sql = sql,
schema = resultDatabaseSettings$schema,
cg_table_prefix = resultDatabaseSettings$cgTablePrefix
)

return(result)
Expand Down Expand Up @@ -503,4 +493,4 @@ getCohortCounts <- function(
)

return(result)
}
}
Loading