async Task <Dataset> ExecuteDatasetAsync(DatasetExecutionContext 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 dbSchema = GetShapedSchema(context, reader); var marshaller = DatasetMarshaller.For(context.Shape, dbSchema, pepper); var data = marshaller.Marshal(reader, user.Anonymize()); var resultSchema = ShapedDatasetSchema.From(dbSchema); return(new Dataset { Schema = resultSchema, Results = data.GroupBy(d => d.PersonId).ToDictionary(g => g.Key, g => g.Select(r => r)) }); } } } }
public static DatasetMarshaller For(DatasetExecutionContext context, DatasetResultSchema schema, Guid pepper) { switch (context.Shape) { case Shape.Dynamic: return(new DynamicMarshaller(context, schema, pepper)); case Shape.Observation: return(new ObservationMarshaller(schema, pepper)); case Shape.Encounter: return(new EncounterMarshaller(schema, pepper)); case Shape.Condition: return(new ConditionMarshaller(schema, pepper)); case Shape.Procedure: return(new ProcedureMarshaller(schema, pepper)); case Shape.Immunization: return(new ImmunizationMarshaller(schema, pepper)); case Shape.Allergy: return(new AllergyMarshaller(schema, pepper)); case Shape.MedicationRequest: return(new MedicationRequestMarshaller(schema, pepper)); case Shape.MedicationAdministration: return(new MedicationAdministrationMarshaller(schema, pepper)); default: throw new ArgumentException($"{context.Shape.ToString()} switch branch not implemented"); } }
public async Task <Dataset> ExecuteDatasetAsync(DatasetExecutionContext context, CancellationToken token) { var sql = context.CompiledQuery; var parameters = context.SqlParameters(); var pepper = context.QueryContext.Pepper; var deidentify = deidentOpts.Patient.Enabled && user.Anonymize(); using (var cn = new SqlConnection(dbOpts.ConnectionString)) { await cn.OpenAsync(); using (var cmd = new SqlCommand(sql, cn)) { cmd.Parameters.AddRange(parameters); using (var reader = await cmd.ExecuteReaderAsync(token)) { var dbSchema = GetShapedSchema(context, reader); var marshaller = DatasetMarshaller.For(context, dbSchema, pepper); var data = marshaller.Marshal(reader, deidentify, deidentOpts); var resultSchema = ShapedDatasetSchema.From(dbSchema, context); return(new Dataset(resultSchema, data)); } } } }
public static ShapedDatasetSchema From(DatasetResultSchema resultSchema, DatasetExecutionContext context) { var contract = ShapedDatasetContract.For(resultSchema.Shape, context.DatasetQuery); var fields = resultSchema.Fields.Where(f => contract.Fields.Any(cf => cf.Name == f.Name)).ToArray(); return(new ShapedDatasetSchema { Shape = resultSchema.Shape, Fields = fields, Contract = contract }); }
public DatasetExecutionContext BuildDatasetSql(DatasetCompilerContext compilerContext) { executionContext = new DatasetExecutionContext(compilerContext.Shape, compilerContext.QueryContext, compilerContext.DatasetQuery.Id.Value); var cohort = CteCohortInternals(compilerContext.QueryContext); new SqlValidator(Dialect.ILLEGAL_COMMANDS).Validate(compilerContext.DatasetQuery); var dataset = CteDatasetInternals(compilerContext.DatasetQuery); var filter = CteFilterInternals(compilerContext); var select = SelectFromCTE(); executionContext.CompiledQuery = Compose(cohort, dataset, filter, select); return(executionContext); }
public DatasetExecutionContext BuildDatasetSql(DatasetCompilerContext compilerContext) { executionContext = new DatasetExecutionContext(compilerContext.Shape, compilerContext.QueryContext, compilerContext.DatasetQuery.Id.Value); var cohort = CteCohortInternals(compilerContext); new SqlValidator(Dialect.IllegalCommands).Validate(compilerContext.DatasetQuery.SqlStatement); var dataset = CteDatasetInternals(compilerContext.DatasetQuery); var filter = CteFilterInternals(compilerContext); var select = SelectFromCTE(compilerContext); var parameters = compiler.BuildContextParameterSql(); executionContext.DatasetQuery = compilerContext.DatasetQuery; executionContext.CompiledQuery = Compose(parameters, cohort, dataset, filter, select); return(executionContext); }
DatasetResultSchema GetShapedSchema(DatasetExecutionContext 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("Dataset Schema Validation Warning. Context:{@Context} Messages:{Messages}", context, result.Messages); break; case SchemaValidationState.Error: log.LogError("Dataset Schema Validation Error. Context:{@Context} Messages:{Messages}", context, result.Messages); throw new LeafCompilerException($"Dataset {context.DatasetId} failed schema validation"); } return(validationSchema.GetShapedSchema(actualSchema)); }
public DynamicMarshaller(DatasetExecutionContext context, DatasetResultSchema schema, Guid pepper) : base(pepper) { _context = context; _schema = schema; }