diff --git a/R/CohortQueries.R b/R/CohortQueries.R index 915ab95..e413722 100644 --- a/R/CohortQueries.R +++ b/R/CohortQueries.R @@ -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) @@ -503,4 +493,4 @@ getCohortCounts <- function( ) return(result) -} \ No newline at end of file +}