public AggregateConfigurationCommand(AggregateConfiguration aggregate) { Aggregate = aggregate; IsPatientIndexTable = Aggregate.IsJoinablePatientIndexTable(); //is the aggregate part of cohort identification CohortIdentificationConfigurationIfAny = Aggregate.GetCohortIdentificationConfigurationIfAny(); //assume no join users JoinableUsersIfAny = new AggregateConfiguration[0]; //unless theres a cic if (CohortIdentificationConfigurationIfAny != null) { //with this aggregate as a joinable JoinableDeclarationIfAny = CohortIdentificationConfigurationIfAny.GetAllJoinables().SingleOrDefault(j => j.AggregateConfiguration_ID == Aggregate.ID); //then get the joinable users if any and use that array if (JoinableDeclarationIfAny != null) { JoinableUsersIfAny = JoinableDeclarationIfAny.Users.Select(u => u.AggregateConfiguration).ToArray(); } } //if so we should find out all the containers in the tree (Containers are INTERSECT\EXCEPT\UNION) AllContainersInTreeIfPartOfOne = new List <CohortAggregateContainer>(); //if it is part of cohort identification if (CohortIdentificationConfigurationIfAny != null) { //find all the containers so we can prevent us being copied into one of them var root = CohortIdentificationConfigurationIfAny.RootCohortAggregateContainer; AllContainersInTreeIfPartOfOne.Add(root); AllContainersInTreeIfPartOfOne.AddRange(root.GetAllSubContainersRecursively()); } ContainerIfAny = Aggregate.GetCohortAggregateContainerIfAny(); }
protected override string GetSQL() { CohortQueryBuilder builder = new CohortQueryBuilder(AggregateConfiguration, CohortIdentificationConfigurationIfAny.GetAllParameters(), true); var sql = builder.SQL; var extractionIdentifier = AggregateConfiguration.AggregateDimensions.Single(d => d.IsExtractionIdentifier); //IMPORTANT: We are using impromptu SQL instead of a Spontaneous container / CustomLine because we want the CohortQueryBuilder to decide to use //the cached table data (if any). If it senses we are monkeying with the query it will run it verbatim which will be very slow. string whereString = AggregateConfiguration.RootFilterContainer_ID != null ? "AND " : "WHERE "; var impromptuSql = whereString + extractionIdentifier.SelectSQL + " IN (SELECT " + _extractableCohort.GetPrivateIdentifier() + " FROM " + _extractableCohort.ExternalCohortTable.TableName + " WHERE " + _extractableCohort.WhereSQL() + ")"; //if there is a group by then we must insert the AND patient in cohort bit before the group by but after any WHERE containers int insertionPoint = sql.IndexOf("group by", 0, StringComparison.CurrentCultureIgnoreCase); //if there isn't a group by if (insertionPoint == -1) { return(sql + Environment.NewLine + impromptuSql); } //there is a group by return(sql.Substring(0, insertionPoint) + Environment.NewLine + impromptuSql + Environment.NewLine + sql.Substring(insertionPoint, sql.Length - insertionPoint)); }