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); }
DatasetResultSchema GetShapedSchema(DemographicExecutionContext context, SqlDataReader reader) { var shape = context.Shape; var actualSchema = GetResultSchema(shape, reader); var validationSchema = ValidationSchema.For(shape); var result = validationSchema.Validate(actualSchema); switch (result.State) { case SchemaValidationState.Warning: log.LogWarning("Demographic Schema Validation Warning. Messages:{Messages}", result.Messages); break; case SchemaValidationState.Error: log.LogError("Demographic Schema Validation Error. Messages:{Messages}", result.Messages); throw new LeafCompilerException($"Demographic query failed schema validation"); } return(validationSchema.GetShapedSchema(actualSchema)); }
public async Task <PatientDemographicContext> ExecuteDemographicsAsync(DemographicExecutionContext context, CancellationToken token) { var sql = context.CompiledQuery; var parameters = context.SqlParameters(); var pepper = context.QueryContext.Pepper; using (var cn = new SqlConnection(opts.ConnectionString)) { await cn.OpenAsync(); using (var cmd = new SqlCommand(sql, cn)) { cmd.Parameters.AddRange(parameters); using (var reader = await cmd.ExecuteReaderAsync(token)) { var resultSchema = GetShapedSchema(context, reader); var marshaller = new DemographicMarshaller(resultSchema, pepper); return(marshaller.Marshal(reader, user.Anonymize())); } } } }