public async Task <PatientDemographicContext> GetDemographicsAsync(DemographicCompilerContext context, CancellationToken token) { var exeContext = compiler.BuildDemographicSql(context, user.Anonymize()); log.LogInformation("Compiled Demographic Execution Context. Context:{@Context}", exeContext); var demographic = await ExecuteDemographicsAsync(exeContext, token); return(demographic); }
CompilerContextState GetContextState(DemographicCompilerContext context) { if (context.DemographicQuery == null || string.IsNullOrWhiteSpace(context.DemographicQuery.SqlStatement)) { log.LogError("No demographic query configured in Leaf database."); return(CompilerContextState.DatasetNotFound); } if (!context.QueryContext.Found) { log.LogWarning("Incomplete demographic compiler context. Context:{@Context}", context); return(CompilerContextState.QueryNotFound); } return(CompilerContextState.Ok); }
public DemographicExecutionContext BuildDemographicSql(DemographicCompilerContext context, bool restrictPhi) { executionContext = new DemographicExecutionContext(context.Shape, context.QueryContext); var cohort = CteCohortInternals(context.QueryContext); new SqlValidator(Dialect.ILLEGAL_COMMANDS).Validate(context.DemographicQuery); var dataset = CteDemographicInternals(context.DemographicQuery); var filter = CteFilterInternals(context, restrictPhi); var select = SelectFromCTE(); executionContext.CompiledQuery = Compose(cohort, dataset, filter, select); return(executionContext); }
string CteFilterInternals(DemographicCompilerContext context, bool restrictPhi) { var schema = ShapedDatasetContract.For(context.Shape); if (!restrictPhi) { executionContext.FieldSelectors = schema.Fields; return($"SELECT * FROM dataset"); } bool include(SchemaFieldSelector field) => field.Required || !field.Phi || field.Mask; var restricted = schema.Fields.Where(include); executionContext.FieldSelectors = restricted; var fields = string.Join(", ", restricted.Select(f => f.Name)); return($"SELECT {fields} FROM dataset"); }